Skip to content
This repository has been archived by the owner on Jan 22, 2022. It is now read-only.

replace UrlUtils.js by UrlUtils class in the backend #511

Open
wants to merge 10 commits into
base: xenial
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clickable.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"template": "cmake",
"builder": "cmake",
"kill": "morph-browser",
"build_args": "-DCLICK_MODE=ON",
"dependencies_target": [
Expand Down
3 changes: 3 additions & 0 deletions src/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ find_package(Qt5Gui REQUIRED)
find_package(Qt5Network REQUIRED)
find_package(Qt5Qml REQUIRED)
find_package(Qt5Quick REQUIRED)
find_package(Qt5Sql REQUIRED)
find_package(Qt5Widgets REQUIRED)
#find_package(Qt5WebEngine REQUIRED)

Expand Down Expand Up @@ -33,6 +34,7 @@ set(COMMONLIB_SRC
mime-database.cpp
session-storage.cpp
single-instance-manager.cpp
url-utils.cpp
)

add_library(${COMMONLIB} STATIC ${COMMONLIB_SRC})
Expand All @@ -44,6 +46,7 @@ target_link_libraries(${COMMONLIB}
Qt5::Network
Qt5::Qml
Qt5::Quick
Qt5::Sql
Qt5::Widgets
Qt5WebEngine
Qt5WebEngineCore
Expand Down
1 change: 0 additions & 1 deletion src/app/DomainPermissionsPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import Ubuntu.Components 1.3
import Ubuntu.Components.Popups 1.3
import Ubuntu.Content 1.3
import webbrowsercommon.private 0.1
import "UrlUtils.js" as UrlUtils

FocusScope {
id: domainPermissionsItem
Expand Down
1 change: 0 additions & 1 deletion src/app/DomainSettingsPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import Ubuntu.Components 1.3
import Ubuntu.Components.Popups 1.3
import Ubuntu.Content 1.3
import webbrowsercommon.private 0.1
import "UrlUtils.js" as UrlUtils

FocusScope {
id: domainSettingsItem
Expand Down
1 change: 0 additions & 1 deletion src/app/DownloadsPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import Ubuntu.Content 1.3
import webbrowsercommon.private 0.1

import "MimeTypeMapper.js" as MimeTypeMapper
import "UrlUtils.js" as UrlUtils
import "FileUtils.js" as FileUtils

BrowserPage {
Expand Down
144 changes: 0 additions & 144 deletions src/app/UrlUtils.js

This file was deleted.

1 change: 0 additions & 1 deletion src/app/WebViewImpl.qml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import QtWebEngine 1.10
import Morph.Web 0.1
import webbrowsercommon.private 0.1
import "actions" as Actions
import "UrlUtils.js" as UrlUtils

WebView {
id: webview
Expand Down
1 change: 0 additions & 1 deletion src/app/ZoomControls.qml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Ubuntu.Components 1.3 // For UbuntuShape.
import Ubuntu.Components.Popups 1.3 as Popups // For saveDialog.
import QtWebEngine 1.7
import webbrowsercommon.private 0.1 // For DomainSettingsModel singleton.
import "UrlUtils.js" as UrlUtils

// ZoomControls object to provide zoom menu, control and autofit logic for WebViewImpl.
// Scope requirements:
Expand Down
3 changes: 3 additions & 0 deletions src/app/browserapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "meminfo.h"
#include "mime-database.h"
#include "session-storage.h"
#include "url-utils.h"

BrowserApplication::BrowserApplication(int& argc, char** argv)
: QApplication(argc, argv)
Expand Down Expand Up @@ -108,6 +109,7 @@ MAKE_SINGLETON_FACTORY(DownloadsModel)
MAKE_SINGLETON_FACTORY(FileOperations)
MAKE_SINGLETON_FACTORY(MemInfo)
MAKE_SINGLETON_FACTORY(MimeDatabase)
MAKE_SINGLETON_FACTORY(UrlUtils)
MAKE_SINGLETON_FACTORY(UserAgentsModel)

bool BrowserApplication::initialize(const QString& qmlFileSubPath
Expand Down Expand Up @@ -202,6 +204,7 @@ bool BrowserApplication::initialize(const QString& qmlFileSubPath
qmlRegisterSingletonType<MimeDatabase>(uri, 0, 1, "MimeDatabase", MimeDatabase_singleton_factory);
qmlRegisterType<SessionStorage>(uri, 0, 1, "SessionStorage");
qmlRegisterSingletonType<UserAgentsModel>(uri, 0, 1, "UserAgentsModel", UserAgentsModel_singleton_factory);
qmlRegisterSingletonType<UrlUtils>(uri, 0, 1, "UrlUtils", UrlUtils_singleton_factory);

m_engine = new QQmlEngine;
connect(m_engine, SIGNAL(quit()), SLOT(quit()));
Expand Down
126 changes: 126 additions & 0 deletions src/app/url-utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright 2019 Chris Clime
*
* This file is part of morph-browser.
*
* morph-browser is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* morph-browser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "url-utils.h"

namespace
{
const QString PdfViewerChromeExtension = "mhjfbmdgcfjbbpaeojofohoefgiehjai";
}

UrlUtils::UrlUtils(QObject* parent) : QObject(parent)
{
}

QUrl UrlUtils::urlFromString(const QString& urlString) const
{
return QUrl(urlString);
}

QString UrlUtils::extractScheme(const QUrl& url) const
{
return url.scheme();
}

QString UrlUtils::removeScheme(const QUrl& url) const
{
QString urlWithRemovedScheme = url.adjusted(QUrl::RemoveScheme).toString();

return urlWithRemovedScheme.startsWith("//") ? urlWithRemovedScheme.mid(2) : urlWithRemovedScheme;
}

bool UrlUtils::schemeIs(const QUrl& url, const QString& expected) const
{
return (extractScheme(url) == expected);
}

bool UrlUtils::hasCustomScheme(const QUrl& url) const
{
QStringList knownSchemes = { "chrome-extension", "http", "https", "file", "ftp", "data", "mailto" };

return (! knownSchemes.contains(extractScheme(url)));
}

QString UrlUtils::extractHost(const QUrl& url) const
{
return url.host();
}

bool UrlUtils::hostIs(const QUrl& url, const QString& expected) const
{
return (extractHost(url) == expected);
}

QUrl UrlUtils::fixUrl(const QString& urlString) const
{
return QUrl::fromUserInput(urlString);
}

bool UrlUtils::looksLikeAUrl(const QString& urlString) const
{
QUrl url = QUrl::fromUserInput(urlString);

// containing invalid chars, e.g. spaces
if (! url.isValid())
{
return false;
}

// local file URL (scheme file)
if (url.isLocalFile())
{
return true;
}

// it can still be, that the string is rather a search string, than a URL, e.g. a single word.
if (!urlString.startsWith(url.scheme(), Qt::CaseInsensitive))
{
// check if there are dots in the host (IPv4 address or domain)
if (url.host().contains("."))
{
return true;
}

// check if it is an IPv6 address
// e.g. if host() is "::1", the host part is "[::1]"
QString hostPart = url.adjusted(QUrl::RemoveScheme | QUrl::RemoveUserInfo | QUrl::RemovePort | QUrl::RemovePath | QUrl::RemoveQuery | QUrl::RemoveFragment).toString().mid(2);

if (hostPart.startsWith("[") && hostPart.contains(":") && hostPart.endsWith("]"))
{
return true;
}

// looks like a search (without spaces)
return false;
}

// the url is valid (can have a custom scheme)
return true;
}

QString UrlUtils::getPdfViewerExtensionUrlPrefix() const
{
return QString("chrome-extension://%1/index.html?").arg(PdfViewerChromeExtension);
}

bool UrlUtils::isPdfViewerExtensionUrl(const QUrl& url) const
{
return url.toString().startsWith(getPdfViewerExtensionUrlPrefix());
}


Loading