diff options
Diffstat (limited to 'src/core/profile_adapter.cpp')
-rw-r--r-- | src/core/profile_adapter.cpp | 255 |
1 files changed, 184 insertions, 71 deletions
diff --git a/src/core/profile_adapter.cpp b/src/core/profile_adapter.cpp index 7f7b7181e..b26f9b1de 100644 --- a/src/core/profile_adapter.cpp +++ b/src/core/profile_adapter.cpp @@ -1,45 +1,13 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWebEngine module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "profile_adapter.h" +#include "base/files/file_util.h" #include "base/task/cancelable_task_tracker.h" +#include "base/threading/thread_restrictions.h" +#include "base/time/time_to_iso8601.h" +#include "components/embedder_support/user_agent_utils.h" #include "components/favicon/core/favicon_service.h" #include "components/history/content/browser/history_database_helper.h" #include "components/history/core/browser/history_database_params.h" @@ -48,9 +16,8 @@ #include "content/public/browser/browser_thread.h" #include "content/public/browser/browsing_data_remover.h" #include "content/public/browser/download_manager.h" -#include "content/public/browser/shared_cors_origin_access_list.h" #include "content/public/browser/storage_partition.h" -#include "services/network/public/cpp/cors/origin_access_list.h" +#include "services/network/public/mojom/network_context.mojom.h" #include "url/url_util.h" #include "api/qwebengineurlscheme.h" @@ -64,12 +31,8 @@ #include "renderer_host/user_resource_controller_host.h" #include "type_conversion.h" #include "visited_links_manager_qt.h" -#include "web_engine_context.h" #include "web_contents_adapter_client.h" - -#include "base/files/file_util.h" -#include "base/time/time_to_iso8601.h" -#include "components/keyed_service/content/browser_context_dependency_manager.h" +#include "web_engine_context.h" #if BUILDFLAG(ENABLE_EXTENSIONS) #include "extensions/browser/extension_system.h" @@ -77,6 +40,8 @@ #include <QCoreApplication> #include <QDir> +#include <QJsonObject> +#include <QSet> #include <QString> #include <QStandardPaths> @@ -100,6 +65,8 @@ ProfileAdapter::ProfileAdapter(const QString &storageName): , m_httpCacheType(DiskHttpCache) , m_persistentCookiesPolicy(AllowPersistentCookies) , m_visitedLinksPolicy(TrackVisitedLinksOnDisk) + , m_clientHintsEnabled(true) + , m_pushServiceEnabled(false) , m_httpCacheMaxSize(0) { WebEngineContext::current()->addProfileAdapter(this); @@ -113,18 +80,19 @@ ProfileAdapter::ProfileAdapter(const QString &storageName): extensions::ExtensionSystem::Get(m_profile.data())->InitForRegularProfile(true); #endif m_cancelableTaskTracker.reset(new base::CancelableTaskTracker()); + + m_profile->DoFinalInit(); } ProfileAdapter::~ProfileAdapter() { m_cancelableTaskTracker->TryCancelAll(); - content::BrowserContext::NotifyWillBeDestroyed(m_profile.data()); - while (!m_webContentsAdapterClients.isEmpty()) { - m_webContentsAdapterClients.first()->releaseProfile(); - } + m_profile->NotifyWillBeDestroyed(); + releaseAllWebContentsAdapterClients(); + WebEngineContext::current()->removeProfileAdapter(this); if (m_downloadManagerDelegate) { - m_profile->GetDownloadManager(m_profile.data())->Shutdown(); + m_profile->GetDownloadManager()->Shutdown(); m_downloadManagerDelegate.reset(); } #if QT_CONFIG(ssl) @@ -175,6 +143,26 @@ ProfileQt *ProfileAdapter::profile() return m_profile.data(); } +bool ProfileAdapter::ensureDataPathExists() +{ + Q_ASSERT(!m_offTheRecord); + base::ScopedAllowBlocking allowBlock; + const base::FilePath &path = toFilePath(dataPath()); + if (path.empty()) + return false; + if (base::DirectoryExists(path)) + return true; + + base::File::Error error; + if (base::CreateDirectoryAndGetError(path, &error)) + return true; + + std::string errorstr = base::File::ErrorToString(error); + qWarning("Cannot create directory %s. Error: %s.", path.AsUTF8Unsafe().c_str(), + errorstr.c_str()); + return false; +} + VisitedLinksManagerQt *ProfileAdapter::visitedLinksManager() { if (!m_visitedLinksManager) @@ -216,9 +204,9 @@ void ProfileAdapter::removeClient(ProfileAdapterClient *adapterClient) m_clients.removeOne(adapterClient); } -void ProfileAdapter::cancelDownload(quint32 downloadId) +bool ProfileAdapter::cancelDownload(quint32 downloadId) { - downloadManagerDelegate()->cancelDownload(downloadId); + return downloadManagerDelegate()->cancelDownload(downloadId); } void ProfileAdapter::pauseDownload(quint32 downloadId) @@ -334,13 +322,16 @@ void ProfileAdapter::setHttpUserAgent(const QString &userAgent) std::vector<content::WebContentsImpl *> list = content::WebContentsImpl::GetAllWebContents(); for (content::WebContentsImpl *web_contents : list) - if (web_contents->GetBrowserContext() == m_profile.data()) - web_contents->SetUserAgentOverride(blink::UserAgentOverride::UserAgentOnly(stdUserAgent), true); + if (web_contents->GetBrowserContext() == m_profile.data()) { + auto userAgentOverride = blink::UserAgentOverride::UserAgentOnly(stdUserAgent); + userAgentOverride.ua_metadata_override = m_profile->m_userAgentMetadata; + web_contents->SetUserAgentOverride(userAgentOverride, true); + } - content::BrowserContext::ForEachStoragePartition( - m_profile.get(), base::BindRepeating([](const std::string &user_agent, content::StoragePartition *storage_partition) { - storage_partition->GetNetworkContext()->SetUserAgent(user_agent); - }, stdUserAgent)); + m_profile->ForEachLoadedStoragePartition( + base::BindRepeating([](const std::string &user_agent, content::StoragePartition *storage_partition) { + storage_partition->GetNetworkContext()->SetUserAgent(user_agent); + }, stdUserAgent)); } ProfileAdapter::HttpCacheType ProfileAdapter::httpCacheType() const @@ -360,8 +351,6 @@ void ProfileAdapter::setHttpCacheType(ProfileAdapter::HttpCacheType newhttpCache return; if (!m_offTheRecord && !m_profile->m_profileIOData->isClearHttpCacheInProgress()) { m_profile->m_profileIOData->resetNetworkContext(); - if (m_httpCacheType == NoCache) - clearHttpCache(); } } @@ -479,10 +468,10 @@ const QList<QByteArray> ProfileAdapter::customUrlSchemes() const void ProfileAdapter::updateCustomUrlSchemeHandlers() { - content::BrowserContext::ForEachStoragePartition( - m_profile.get(), base::BindRepeating([](content::StoragePartition *storage_partition) { - storage_partition->ResetURLLoaderFactories(); - })); + m_profile->ForEachLoadedStoragePartition( + base::BindRepeating([](content::StoragePartition *storage_partition) { + storage_partition->ResetURLLoaderFactories(); + })); } void ProfileAdapter::removeUrlSchemeHandler(QWebEngineUrlSchemeHandler *handler) @@ -601,10 +590,114 @@ void ProfileAdapter::setHttpAcceptLanguage(const QString &httpAcceptLanguage) } } - content::BrowserContext::ForEachStoragePartition( - m_profile.get(), base::BindRepeating([](std::string accept_language, content::StoragePartition *storage_partition) { - storage_partition->GetNetworkContext()->SetAcceptLanguage(accept_language); - }, http_accept_language)); + m_profile->ForEachLoadedStoragePartition( + base::BindRepeating([](std::string accept_language, content::StoragePartition *storage_partition) { + storage_partition->GetNetworkContext()->SetAcceptLanguage(accept_language); + }, http_accept_language)); +} + +QVariant ProfileAdapter::clientHint(ClientHint clientHint) const +{ + blink::UserAgentMetadata &userAgentMetadata = m_profile->m_userAgentMetadata; + switch (clientHint) { + case ProfileAdapter::UAArchitecture: + return QVariant(toQString(userAgentMetadata.architecture)); + case ProfileAdapter::UAPlatform: + return QVariant(toQString(userAgentMetadata.platform)); + case ProfileAdapter::UAModel: + return QVariant(toQString(userAgentMetadata.model)); + case ProfileAdapter::UAMobile: + return QVariant(userAgentMetadata.mobile); + case ProfileAdapter::UAFullVersion: + return QVariant(toQString(userAgentMetadata.full_version)); + case ProfileAdapter::UAPlatformVersion: + return QVariant(toQString(userAgentMetadata.platform_version)); + case ProfileAdapter::UABitness: + return QVariant(toQString(userAgentMetadata.bitness)); + case ProfileAdapter::UAFullVersionList: { + QJsonObject ret; + for (const auto &value : userAgentMetadata.brand_full_version_list) + ret.insert(toQString(value.brand), QJsonValue(toQString(value.version))); + return QVariant(ret); + } + case ProfileAdapter::UAWOW64: + return QVariant(userAgentMetadata.wow64); + default: + return QVariant(); + } +} + +void ProfileAdapter::setClientHint(ClientHint clientHint, const QVariant &value) +{ + blink::UserAgentMetadata &userAgentMetadata = m_profile->m_userAgentMetadata; + switch (clientHint) { + case ProfileAdapter::UAArchitecture: + userAgentMetadata.architecture = value.toString().toStdString(); + break; + case ProfileAdapter::UAPlatform: + userAgentMetadata.platform = value.toString().toStdString(); + break; + case ProfileAdapter::UAModel: + userAgentMetadata.model = value.toString().toStdString(); + break; + case ProfileAdapter::UAMobile: + userAgentMetadata.mobile = value.toBool(); + break; + case ProfileAdapter::UAFullVersion: + userAgentMetadata.full_version = value.toString().toStdString(); + break; + case ProfileAdapter::UAPlatformVersion: + userAgentMetadata.platform_version = value.toString().toStdString(); + break; + case ProfileAdapter::UABitness: + userAgentMetadata.bitness = value.toString().toStdString(); + break; + case ProfileAdapter::UAFullVersionList: { + userAgentMetadata.brand_full_version_list.clear(); + QJsonObject fullVersionList = value.toJsonObject(); + for (const QString &key : fullVersionList.keys()) + userAgentMetadata.brand_full_version_list.push_back({ + key.toStdString(), + fullVersionList.value(key).toString().toStdString() + }); + break; + } + case ProfileAdapter::UAWOW64: + userAgentMetadata.wow64 = value.toBool(); + break; + default: + break; + } + + std::vector<content::WebContentsImpl *> list = content::WebContentsImpl::GetAllWebContents(); + for (content::WebContentsImpl *web_contents : list) { + if (web_contents->GetBrowserContext() == m_profile.data()) { + web_contents->GetMutableRendererPrefs()->user_agent_override.ua_metadata_override = userAgentMetadata; + web_contents->SyncRendererPrefs(); + } + } +} + +bool ProfileAdapter::clientHintsEnabled() +{ + return m_clientHintsEnabled; +} + +void ProfileAdapter::setClientHintsEnabled(bool enabled) +{ + m_clientHintsEnabled = enabled; +} + +void ProfileAdapter::resetClientHints() +{ + m_profile->m_userAgentMetadata = embedder_support::GetUserAgentMetadata(); + std::vector<content::WebContentsImpl *> list = content::WebContentsImpl::GetAllWebContents(); + for (content::WebContentsImpl *web_contents : list) { + if (web_contents->GetBrowserContext() == m_profile.data()) { + web_contents->GetMutableRendererPrefs()->user_agent_override.ua_metadata_override = m_profile->m_userAgentMetadata; + web_contents->SyncRendererPrefs(); + } + } } void ProfileAdapter::clearHttpCache() @@ -644,6 +737,16 @@ bool ProfileAdapter::isSpellCheckEnabled() const #endif } +bool ProfileAdapter::pushServiceEnabled() const +{ + return m_pushServiceEnabled; +} + +void ProfileAdapter::setPushServiceEnabled(bool enabled) +{ + m_pushServiceEnabled = enabled; +} + void ProfileAdapter::addWebContentsAdapterClient(WebContentsAdapterClient *client) { m_webContentsAdapterClients.append(client); @@ -654,6 +757,12 @@ void ProfileAdapter::removeWebContentsAdapterClient(WebContentsAdapterClient *cl m_webContentsAdapterClients.removeAll(client); } +void ProfileAdapter::releaseAllWebContentsAdapterClients() +{ + while (!m_webContentsAdapterClients.isEmpty()) + m_webContentsAdapterClients.first()->releaseProfile(); +} + void ProfileAdapter::resetVisitedLinksManager() { m_visitedLinksManager.reset(new VisitedLinksManagerQt(m_profile.data(), persistVisitedLinks())); @@ -661,8 +770,8 @@ void ProfileAdapter::resetVisitedLinksManager() void ProfileAdapter::reinitializeHistoryService() { - Q_ASSERT(!m_profile->IsOffTheRecord()); - if (m_profile->ensureDirectoryExists()) { + Q_ASSERT(!m_offTheRecord); + if (ensureDataPathExists()) { favicon::FaviconService *faviconService = FaviconServiceFactoryQt::GetForBrowserContext(m_profile.data()); history::HistoryService *historyService = static_cast<history::HistoryService *>( @@ -680,7 +789,11 @@ QString ProfileAdapter::determineDownloadPath(const QString &downloadDirectory, QString suggestedFilePath = suggestedFile.absoluteFilePath(); base::FilePath tmpFilePath(toFilePath(suggestedFilePath).NormalizePathSeparatorsTo('/')); - int uniquifier = base::GetUniquePathNumber(tmpFilePath); + int uniquifier = 0; + { + base::ScopedAllowBlocking allowBlock; + uniquifier = base::GetUniquePathNumber(tmpFilePath); + } if (uniquifier > 0) suggestedFilePath = toQt(tmpFilePath.InsertBeforeExtensionASCII(base::StringPrintf(" (%d)", uniquifier)).AsUTF8Unsafe()); else if (uniquifier == -1) { |