diff --git a/BUILD.gn b/BUILD.gn index af0da1377..0018db418 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -407,6 +407,8 @@ test("libcef_static_unittests") { static_library("libcef_static") { sources = includes_common + gypi_paths.autogen_cpp_includes + [ + "libcef/browser/alloy/alloy_browser_context.cc", + "libcef/browser/alloy/alloy_browser_context.h", "libcef/browser/alloy/alloy_browser_main.cc", "libcef/browser/alloy/alloy_browser_main.h", "libcef/browser/alloy/chrome_browser_process_alloy.cc", @@ -512,6 +514,8 @@ static_library("libcef_static") { "libcef/browser/frame_host_impl.h", "libcef/browser/image_impl.cc", "libcef/browser/image_impl.h", + "libcef/browser/iothread_state.cc", + "libcef/browser/iothread_state.h", "libcef/browser/javascript_dialog_runner.h", "libcef/browser/javascript_dialog_manager.cc", "libcef/browser/javascript_dialog_manager.h", @@ -607,8 +611,6 @@ static_library("libcef_static") { "libcef/browser/printing/print_view_manager.cc", "libcef/browser/printing/print_view_manager.h", "libcef/browser/process_util_impl.cc", - "libcef/browser/resource_context.cc", - "libcef/browser/resource_context.h", "libcef/browser/request_context_handler_map.cc", "libcef/browser/request_context_handler_map.h", "libcef/browser/request_context_impl.cc", diff --git a/libcef/browser/alloy/alloy_browser_context.cc b/libcef/browser/alloy/alloy_browser_context.cc new file mode 100644 index 000000000..6dedc6d86 --- /dev/null +++ b/libcef/browser/alloy/alloy_browser_context.cc @@ -0,0 +1,449 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "libcef/browser/alloy/alloy_browser_context.h" + +#include +#include + +#include "libcef/browser/download_manager_delegate.h" +#include "libcef/browser/extensions/extension_system.h" +#include "libcef/browser/prefs/browser_prefs.h" +#include "libcef/browser/ssl_host_state_delegate.h" +#include "libcef/browser/thread_util.h" +#include "libcef/common/cef_switches.h" +#include "libcef/common/extensions/extensions_util.h" + +#include "base/command_line.h" +#include "base/files/file_util.h" +#include "base/lazy_instance.h" +#include "base/logging.h" +#include "base/strings/string_util.h" +#include "chrome/browser/font_family_cache.h" +#include "chrome/browser/plugins/chrome_plugin_service_filter.h" +#include "chrome/browser/profiles/profile_key.h" +#include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h" +#include "chrome/common/pref_names.h" +#include "components/guest_view/browser/guest_view_manager.h" +#include "components/keyed_service/content/browser_context_dependency_manager.h" +#include "components/keyed_service/core/simple_dependency_manager.h" +#include "components/keyed_service/core/simple_key_map.h" +#include "components/prefs/pref_service.h" +#include "components/proxy_config/pref_proxy_config_tracker_impl.h" +#include "components/user_prefs/user_prefs.h" +#include "components/visitedlink/browser/visitedlink_event_listener.h" +#include "components/visitedlink/browser/visitedlink_writer.h" +#include "components/zoom/zoom_event_manager.h" +#include "content/public/browser/browser_task_traits.h" +#include "content/public/browser/browser_thread.h" +#include "content/public/browser/download_manager.h" +#include "content/public/browser/resource_context.h" +#include "content/public/browser/storage_partition.h" +#include "extensions/browser/extension_protocols.h" +#include "extensions/browser/process_manager.h" +#include "extensions/common/constants.h" +#include "net/proxy_resolution/proxy_config_service.h" +#include "services/network/public/mojom/cors_origin_pattern.mojom.h" + +using content::BrowserThread; + +// Creates and manages VisitedLinkEventListener objects for each +// AlloyBrowserContext sharing the same VisitedLinkWriter. +class CefVisitedLinkListener : public visitedlink::VisitedLinkWriter::Listener { + public: + CefVisitedLinkListener() { DCHECK(listener_map_.empty()); } + + void CreateListenerForContext(content::BrowserContext* context) { + CEF_REQUIRE_UIT(); + auto listener = + std::make_unique(context); + listener_map_.insert(std::make_pair(context, std::move(listener))); + } + + void RemoveListenerForContext(content::BrowserContext* context) { + CEF_REQUIRE_UIT(); + ListenerMap::iterator it = listener_map_.find(context); + DCHECK(it != listener_map_.end()); + listener_map_.erase(it); + } + + // visitedlink::VisitedLinkWriter::Listener methods. + + void NewTable(base::ReadOnlySharedMemoryRegion* table_region) override { + CEF_REQUIRE_UIT(); + ListenerMap::iterator it = listener_map_.begin(); + for (; it != listener_map_.end(); ++it) + it->second->NewTable(table_region); + } + + void Add(visitedlink::VisitedLinkCommon::Fingerprint fingerprint) override { + CEF_REQUIRE_UIT(); + ListenerMap::iterator it = listener_map_.begin(); + for (; it != listener_map_.end(); ++it) + it->second->Add(fingerprint); + } + + void Reset(bool invalidate_hashes) override { + CEF_REQUIRE_UIT(); + ListenerMap::iterator it = listener_map_.begin(); + for (; it != listener_map_.end(); ++it) + it->second->Reset(invalidate_hashes); + } + + private: + // Map of AlloyBrowserContext to the associated VisitedLinkEventListener. + typedef std::map> + ListenerMap; + ListenerMap listener_map_; + + DISALLOW_COPY_AND_ASSIGN(CefVisitedLinkListener); +}; + +AlloyBrowserContext::AlloyBrowserContext( + const CefRequestContextSettings& settings) + : CefBrowserContext(settings) {} + +AlloyBrowserContext::~AlloyBrowserContext() { + if (resource_context_) { + content::BrowserThread::DeleteSoon(content::BrowserThread::IO, FROM_HERE, + resource_context_.release()); + } +} + +void AlloyBrowserContext::Initialize() { + CefBrowserContext::Initialize(); + + key_ = std::make_unique(cache_path_); + SimpleKeyMap::GetInstance()->Associate(this, key_.get()); + + // Initialize the PrefService object. + pref_service_ = browser_prefs::CreatePrefService( + this, cache_path_, !!settings_.persist_user_preferences); + + // This must be called before creating any services to avoid hitting + // DependencyManager::AssertContextWasntDestroyed when creating/destroying + // multiple browser contexts (due to pointer address reuse). + BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices( + this); + + const bool extensions_enabled = extensions::ExtensionsEnabled(); + if (extensions_enabled) { + // Create the custom ExtensionSystem first because other KeyedServices + // depend on it. + extension_system_ = static_cast( + extensions::ExtensionSystem::Get(this)); + extension_system_->InitForRegularProfile(true); + + // Make sure the ProcessManager is created so that it receives extension + // load notifications. This is necessary for the proper initialization of + // background/event pages. + extensions::ProcessManager::Get(this); + } + + // Initialize visited links management. + base::FilePath visited_link_path; + if (!cache_path_.empty()) + visited_link_path = cache_path_.Append(FILE_PATH_LITERAL("Visited Links")); + visitedlink_listener_ = new CefVisitedLinkListener; + visitedlink_master_.reset(new visitedlink::VisitedLinkWriter( + visitedlink_listener_, this, !visited_link_path.empty(), false, + visited_link_path, 0)); + visitedlink_listener_->CreateListenerForContext(this); + visitedlink_master_->Init(); + + // Initialize proxy configuration tracker. + pref_proxy_config_tracker_.reset(new PrefProxyConfigTrackerImpl( + GetPrefs(), base::CreateSingleThreadTaskRunner({BrowserThread::IO}))); + + // Spell checking support and possibly other subsystems retrieve the + // PrefService associated with a BrowserContext via UserPrefs::Get(). + PrefService* pref_service = GetPrefs(); + DCHECK(pref_service); + user_prefs::UserPrefs::Set(this, pref_service); + key_->SetPrefs(pref_service); + + if (extensions_enabled) + extension_system_->Init(); + + ChromePluginServiceFilter::GetInstance()->RegisterProfile(this); +} + +void AlloyBrowserContext::Shutdown() { + CefBrowserContext::Shutdown(); + + // Send notifications to clean up objects associated with this Profile. + MaybeSendDestroyedNotification(); + + ChromePluginServiceFilter::GetInstance()->UnregisterProfile(this); + + // Remove any BrowserContextKeyedServiceFactory associations. This must be + // called before the ProxyService owned by AlloyBrowserContext is destroyed. + // The SimpleDependencyManager should always be passed after the + // BrowserContextDependencyManager. This is because the KeyedService instances + // in the BrowserContextDependencyManager's dependency graph can depend on the + // ones in the SimpleDependencyManager's graph. + DependencyManager::PerformInterlockedTwoPhaseShutdown( + BrowserContextDependencyManager::GetInstance(), this, + SimpleDependencyManager::GetInstance(), key_.get()); + + key_.reset(); + SimpleKeyMap::GetInstance()->Dissociate(this); + + // Shuts down the storage partitions associated with this browser context. + // This must be called before the browser context is actually destroyed + // and before a clean-up task for its corresponding IO thread residents + // (e.g. ResourceContext) is posted, so that the classes that hung on + // StoragePartition can have time to do necessary cleanups on IO thread. + ShutdownStoragePartitions(); + + visitedlink_listener_->RemoveListenerForContext(this); + + // The FontFamilyCache references the ProxyService so delete it before the + // ProxyService is deleted. + SetUserData(&kFontFamilyCacheKey, nullptr); + + pref_proxy_config_tracker_->DetachFromPrefService(); + + // Delete the download manager delegate here because otherwise we'll crash + // when it's accessed from the content::BrowserContext destructor. + if (download_manager_delegate_) + download_manager_delegate_.reset(nullptr); +} + +void AlloyBrowserContext::RemoveCefRequestContext( + CefRequestContextImpl* context) { + CEF_REQUIRE_UIT(); + + if (extensions::ExtensionsEnabled()) { + extension_system()->OnRequestContextDeleted(context); + } + + // May result in |this| being deleted. + CefBrowserContext::RemoveCefRequestContext(context); +} + +void AlloyBrowserContext::LoadExtension( + const CefString& root_directory, + CefRefPtr manifest, + CefRefPtr handler, + CefRefPtr loader_context) { + if (!extensions::ExtensionsEnabled()) { + if (handler) + handler->OnExtensionLoadFailed(ERR_ABORTED); + return; + } + + if (manifest && manifest->GetSize() > 0) { + CefDictionaryValueImpl* value_impl = + static_cast(manifest.get()); + extension_system()->LoadExtension(base::WrapUnique(value_impl->CopyValue()), + root_directory, false /* builtin */, + loader_context, handler); + } else { + extension_system()->LoadExtension(root_directory, false /* builtin */, + loader_context, handler); + } +} + +bool AlloyBrowserContext::GetExtensions(std::vector& extension_ids) { + if (!extensions::ExtensionsEnabled()) + return false; + + extensions::CefExtensionSystem::ExtensionMap extension_map = + extension_system()->GetExtensions(); + extensions::CefExtensionSystem::ExtensionMap::const_iterator it = + extension_map.begin(); + for (; it != extension_map.end(); ++it) + extension_ids.push_back(it->second->GetIdentifier()); + + return true; +} + +CefRefPtr AlloyBrowserContext::GetExtension( + const CefString& extension_id) { + if (!extensions::ExtensionsEnabled()) + return nullptr; + + return extension_system()->GetExtension(extension_id); +} + +bool AlloyBrowserContext::UnloadExtension(const CefString& extension_id) { + DCHECK(extensions::ExtensionsEnabled()); + return extension_system()->UnloadExtension(extension_id); +} + +bool AlloyBrowserContext::IsPrintPreviewSupported() const { + CEF_REQUIRE_UIT(); + if (!extensions::PrintPreviewEnabled()) + return false; + + return !GetPrefs()->GetBoolean(prefs::kPrintPreviewDisabled); +} + +void AlloyBrowserContext::AddVisitedURLs(const std::vector& urls) { + visitedlink_master_->AddURLs(urls); +} + +content::ResourceContext* AlloyBrowserContext::GetResourceContext() { + if (!resource_context_) { + resource_context_ = std::make_unique(); + } + return resource_context_.get(); +} + +content::ClientHintsControllerDelegate* +AlloyBrowserContext::GetClientHintsControllerDelegate() { + return nullptr; +} + +void AlloyBrowserContext::SetCorsOriginAccessListForOrigin( + const url::Origin& source_origin, + std::vector allow_patterns, + std::vector block_patterns, + base::OnceClosure closure) { + // This method is called for Extension support. + base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, std::move(closure)); +} + +ChromeZoomLevelPrefs* AlloyBrowserContext::GetZoomLevelPrefs() { + return static_cast( + GetStoragePartition(this, nullptr)->GetZoomLevelDelegate()); +} + +scoped_refptr +AlloyBrowserContext::GetURLLoaderFactory() { + return GetDefaultStoragePartition(this) + ->GetURLLoaderFactoryForBrowserProcess(); +} + +base::FilePath AlloyBrowserContext::GetPath() { + return cache_path_; +} + +base::FilePath AlloyBrowserContext::GetPath() const { + return cache_path_; +} + +std::unique_ptr +AlloyBrowserContext::CreateZoomLevelDelegate( + const base::FilePath& partition_path) { + if (cache_path_.empty()) + return std::unique_ptr(); + + return base::WrapUnique(new ChromeZoomLevelPrefs( + GetPrefs(), cache_path_, partition_path, + zoom::ZoomEventManager::GetForBrowserContext(this)->GetWeakPtr())); +} + +bool AlloyBrowserContext::IsOffTheRecord() const { + // Alloy contexts are never flagged as off-the-record. It causes problems + // for the extension system. + return false; +} + +content::DownloadManagerDelegate* +AlloyBrowserContext::GetDownloadManagerDelegate() { + if (!download_manager_delegate_) { + content::DownloadManager* manager = + BrowserContext::GetDownloadManager(this); + download_manager_delegate_.reset(new CefDownloadManagerDelegate(manager)); + } + return download_manager_delegate_.get(); +} + +content::BrowserPluginGuestManager* AlloyBrowserContext::GetGuestManager() { + DCHECK(extensions::ExtensionsEnabled()); + return guest_view::GuestViewManager::FromBrowserContext(this); +} + +storage::SpecialStoragePolicy* AlloyBrowserContext::GetSpecialStoragePolicy() { + return nullptr; +} + +content::PushMessagingService* AlloyBrowserContext::GetPushMessagingService() { + return nullptr; +} + +content::StorageNotificationService* +AlloyBrowserContext::GetStorageNotificationService() { + return nullptr; +} + +content::SSLHostStateDelegate* AlloyBrowserContext::GetSSLHostStateDelegate() { + if (!ssl_host_state_delegate_) + ssl_host_state_delegate_.reset(new CefSSLHostStateDelegate()); + return ssl_host_state_delegate_.get(); +} + +content::PermissionControllerDelegate* +AlloyBrowserContext::GetPermissionControllerDelegate() { + return nullptr; +} + +content::BackgroundFetchDelegate* +AlloyBrowserContext::GetBackgroundFetchDelegate() { + return nullptr; +} + +content::BackgroundSyncController* +AlloyBrowserContext::GetBackgroundSyncController() { + return nullptr; +} + +content::BrowsingDataRemoverDelegate* +AlloyBrowserContext::GetBrowsingDataRemoverDelegate() { + return nullptr; +} + +PrefService* AlloyBrowserContext::GetPrefs() { + return pref_service_.get(); +} + +const PrefService* AlloyBrowserContext::GetPrefs() const { + return pref_service_.get(); +} + +ProfileKey* AlloyBrowserContext::GetProfileKey() const { + DCHECK(key_); + return key_.get(); +} + +policy::SchemaRegistryService* +AlloyBrowserContext::GetPolicySchemaRegistryService() { + NOTREACHED(); + return nullptr; +} + +policy::UserCloudPolicyManager* +AlloyBrowserContext::GetUserCloudPolicyManager() { + NOTREACHED(); + return nullptr; +} + +policy::ProfilePolicyConnector* +AlloyBrowserContext::GetProfilePolicyConnector() { + NOTREACHED(); + return nullptr; +} + +const policy::ProfilePolicyConnector* +AlloyBrowserContext::GetProfilePolicyConnector() const { + NOTREACHED(); + return nullptr; +} + +void AlloyBrowserContext::RebuildTable( + const scoped_refptr& enumerator) { + // Called when visited links will not or cannot be loaded from disk. + enumerator->OnComplete(true); +} + +DownloadPrefs* AlloyBrowserContext::GetDownloadPrefs() { + CEF_REQUIRE_UIT(); + if (!download_prefs_) { + download_prefs_.reset(new DownloadPrefs(this)); + } + return download_prefs_.get(); +} diff --git a/libcef/browser/alloy/alloy_browser_context.h b/libcef/browser/alloy/alloy_browser_context.h new file mode 100644 index 000000000..1b25cb4ce --- /dev/null +++ b/libcef/browser/alloy/alloy_browser_context.h @@ -0,0 +1,146 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CEF_LIBCEF_BROWSER_ALLOY_ALLOY_BROWSER_CONTEXT_H_ +#define CEF_LIBCEF_BROWSER_ALLOY_ALLOY_BROWSER_CONTEXT_H_ +#pragma once + +#include "include/cef_request_context_handler.h" +#include "libcef/browser/alloy/chrome_profile_alloy.h" +#include "libcef/browser/browser_context.h" +#include "libcef/browser/request_context_handler_map.h" + +#include "chrome/browser/download/download_prefs.h" +#include "components/proxy_config/pref_proxy_config_tracker.h" +#include "components/visitedlink/browser/visitedlink_delegate.h" + +class CefDownloadManagerDelegate; +class CefSSLHostStateDelegate; +class CefVisitedLinkListener; +class PrefService; + +namespace extensions { +class CefExtensionSystem; +} + +namespace visitedlink { +class VisitedLinkWriter; +} + +// See CefBrowserContext documentation for usage. Only accessed on the UI thread +// unless otherwise indicated. ChromeProfileAlloy must be the first listed base +// class to avoid issues when casting between void* and content::BrowserContext* +// in Chromium code. +class AlloyBrowserContext : public ChromeProfileAlloy, + public CefBrowserContext, + public visitedlink::VisitedLinkDelegate { + public: + explicit AlloyBrowserContext(const CefRequestContextSettings& settings); + + // CefBrowserContext overrides. + content::BrowserContext* AsBrowserContext() override { return this; } + Profile* AsProfile() override { return this; } + void Initialize() override; + void Shutdown() override; + void RemoveCefRequestContext(CefRequestContextImpl* context) override; + void LoadExtension(const CefString& root_directory, + CefRefPtr manifest, + CefRefPtr handler, + CefRefPtr loader_context) override; + bool GetExtensions(std::vector& extension_ids) override; + CefRefPtr GetExtension(const CefString& extension_id) override; + bool UnloadExtension(const CefString& extension_id) override; + bool IsPrintPreviewSupported() const override; + void AddVisitedURLs(const std::vector& urls) override; + + // content::BrowserContext overrides. + content::ResourceContext* GetResourceContext() override; + content::ClientHintsControllerDelegate* GetClientHintsControllerDelegate() + override; + void SetCorsOriginAccessListForOrigin( + const url::Origin& source_origin, + std::vector allow_patterns, + std::vector block_patterns, + base::OnceClosure closure) override; + base::FilePath GetPath() override; + base::FilePath GetPath() const override; + std::unique_ptr CreateZoomLevelDelegate( + const base::FilePath& partition_path) override; + bool IsOffTheRecord() const override; + content::DownloadManagerDelegate* GetDownloadManagerDelegate() override; + content::BrowserPluginGuestManager* GetGuestManager() override; + storage::SpecialStoragePolicy* GetSpecialStoragePolicy() override; + content::PushMessagingService* GetPushMessagingService() override; + content::StorageNotificationService* GetStorageNotificationService() override; + content::SSLHostStateDelegate* GetSSLHostStateDelegate() override; + content::PermissionControllerDelegate* GetPermissionControllerDelegate() + override; + content::BackgroundFetchDelegate* GetBackgroundFetchDelegate() override; + content::BackgroundSyncController* GetBackgroundSyncController() override; + content::BrowsingDataRemoverDelegate* GetBrowsingDataRemoverDelegate() + override; + + // Profile overrides. + ChromeZoomLevelPrefs* GetZoomLevelPrefs() override; + scoped_refptr GetURLLoaderFactory() override; + PrefService* GetPrefs() override; + bool AllowsBrowserWindows() const override { return false; } + const PrefService* GetPrefs() const override; + ProfileKey* GetProfileKey() const override; + policy::SchemaRegistryService* GetPolicySchemaRegistryService() override; + policy::UserCloudPolicyManager* GetUserCloudPolicyManager() override; + policy::ProfilePolicyConnector* GetProfilePolicyConnector() override; + const policy::ProfilePolicyConnector* GetProfilePolicyConnector() + const override; + + // Values checked in ProfileNetworkContextService::CreateNetworkContextParams + // when creating the NetworkContext. + bool ShouldRestoreOldSessionCookies() override { + return ShouldPersistSessionCookies(); + } + bool ShouldPersistSessionCookies() override { + return !!settings_.persist_session_cookies; + } + base::Optional> GetCookieableSchemes() override { + return cookieable_schemes_; + } + + // visitedlink::VisitedLinkDelegate methods. + void RebuildTable(const scoped_refptr& enumerator) override; + + // Manages extensions. + extensions::CefExtensionSystem* extension_system() const { + return extension_system_; + } + + // Called from DownloadPrefs::FromBrowserContext. + DownloadPrefs* GetDownloadPrefs(); + + private: + ~AlloyBrowserContext() override; + + std::unique_ptr pref_service_; + std::unique_ptr pref_proxy_config_tracker_; + + std::unique_ptr download_manager_delegate_; + std::unique_ptr ssl_host_state_delegate_; + std::unique_ptr visitedlink_master_; + // |visitedlink_listener_| is owned by visitedlink_master_. + CefVisitedLinkListener* visitedlink_listener_ = nullptr; + + // Owned by the KeyedService system. + extensions::CefExtensionSystem* extension_system_ = nullptr; + + // The key to index KeyedService instances created by + // SimpleKeyedServiceFactory. + std::unique_ptr key_; + + std::unique_ptr download_prefs_; + + std::unique_ptr resource_context_; + + DISALLOW_COPY_AND_ASSIGN(AlloyBrowserContext); +}; + +#endif // CEF_LIBCEF_BROWSER_ALLOY_ALLOY_BROWSER_CONTEXT_H_ diff --git a/libcef/browser/alloy/alloy_browser_main.cc b/libcef/browser/alloy/alloy_browser_main.cc index af60641c5..0bb5a360e 100644 --- a/libcef/browser/alloy/alloy_browser_main.cc +++ b/libcef/browser/alloy/alloy_browser_main.cc @@ -196,8 +196,8 @@ void AlloyBrowserMainParts::PreMainMessageLoopRun() { // Create the global RequestContext. global_request_context_ = CefRequestContextImpl::CreateGlobalRequestContext(settings); - CefBrowserContext* browser_context = static_cast( - global_request_context_->GetBrowserContext()); + auto browser_context = + global_request_context_->GetBrowserContext()->AsBrowserContext(); CefDevToolsManagerDelegate::StartHttpHandler(browser_context); diff --git a/libcef/browser/alloy/alloy_content_browser_client.cc b/libcef/browser/alloy/alloy_content_browser_client.cc index a43949809..2a2ca9c04 100644 --- a/libcef/browser/alloy/alloy_content_browser_client.cc +++ b/libcef/browser/alloy/alloy_content_browser_client.cc @@ -8,6 +8,7 @@ #include #include "include/cef_version.h" +#include "libcef/browser/alloy/alloy_browser_context.h" #include "libcef/browser/alloy/alloy_browser_main.h" #include "libcef/browser/browser_context.h" #include "libcef/browser/browser_host_impl.h" @@ -678,8 +679,8 @@ void AlloyContentBrowserClient::SiteInstanceGotProcess( if (!extension) return; - CefBrowserContext* browser_context = - static_cast(site_instance->GetBrowserContext()); + auto browser_context = + static_cast(site_instance->GetBrowserContext()); extensions::ProcessMap::Get(browser_context) ->Insert(extension->id(), site_instance->GetProcess()->GetID(), @@ -710,8 +711,8 @@ void AlloyContentBrowserClient::SiteInstanceDeleting( if (!extension) return; - CefBrowserContext* browser_context = - static_cast(site_instance->GetBrowserContext()); + auto browser_context = + static_cast(site_instance->GetBrowserContext()); extensions::ProcessMap::Get(browser_context) ->Remove(extension->id(), site_instance->GetProcess()->GetID(), @@ -803,18 +804,19 @@ void AlloyContentBrowserClient::AppendExtraCommandLineSwitches( if (extensions::ExtensionsEnabled()) { content::RenderProcessHost* process = content::RenderProcessHost::FromID(child_process_id); - CefBrowserContext* context = - process - ? CefBrowserContext::GetForContext(process->GetBrowserContext()) - : nullptr; - if (context) { - if (context->IsPrintPreviewSupported()) { + auto browser_context = process->GetBrowserContext(); + CefBrowserContext* cef_browser_context = + process ? CefBrowserContext::FromBrowserContext(browser_context) + : nullptr; + if (cef_browser_context) { + if (cef_browser_context->IsPrintPreviewSupported()) { command_line->AppendSwitch(switches::kEnablePrintPreview); } // Based on ChromeContentBrowserClientExtensionsPart:: // AppendExtraRendererCommandLineSwitches - if (extensions::ProcessMap::Get(context)->Contains(process->GetID())) { + if (extensions::ProcessMap::Get(browser_context) + ->Contains(process->GetID())) { command_line->AppendSwitch(extensions::switches::kExtensionProcess); } } diff --git a/libcef/browser/alloy/chrome_browser_process_alloy.cc b/libcef/browser/alloy/chrome_browser_process_alloy.cc index 1e9231a4e..6b4b20ea0 100644 --- a/libcef/browser/alloy/chrome_browser_process_alloy.cc +++ b/libcef/browser/alloy/chrome_browser_process_alloy.cc @@ -84,8 +84,9 @@ void ChromeBrowserProcessAlloy::CleanupOnUIThread() { // Release any references held by objects associated with a Profile. The // Profile will be deleted later. - for (const auto& profile : CefBrowserContext::GetAll()) { + for (const auto& browser_context : CefBrowserContext::GetAll()) { // Release any references to |local_state_|. + auto profile = browser_context->AsProfile(); PrefWatcher* pref_watcher = PrefWatcher::Get(profile); if (pref_watcher) pref_watcher->Shutdown(); diff --git a/libcef/browser/alloy/chrome_profile_alloy.cc b/libcef/browser/alloy/chrome_profile_alloy.cc index 686435052..38af012ae 100644 --- a/libcef/browser/alloy/chrome_profile_alloy.cc +++ b/libcef/browser/alloy/chrome_profile_alloy.cc @@ -7,7 +7,6 @@ #include "components/variations/variations_client.h" #include "components/variations/variations_http_header_provider.h" -#include "content/public/browser/resource_context.h" #include "net/url_request/url_request_context.h" namespace { diff --git a/libcef/browser/alloy/chrome_profile_manager_alloy.cc b/libcef/browser/alloy/chrome_profile_manager_alloy.cc index 99e16a8cb..860604709 100644 --- a/libcef/browser/alloy/chrome_profile_manager_alloy.cc +++ b/libcef/browser/alloy/chrome_profile_manager_alloy.cc @@ -24,7 +24,7 @@ namespace { CefBrowserContext* GetActiveBrowserContext() { auto request_context = static_cast( CefAppManager::Get()->GetGlobalRequestContext().get()); - return static_cast(request_context->GetBrowserContext()); + return request_context->GetBrowserContext(); } } // namespace @@ -37,7 +37,7 @@ ChromeProfileManagerAlloy::~ChromeProfileManagerAlloy() {} Profile* ChromeProfileManagerAlloy::GetProfile( const base::FilePath& profile_dir) { CefBrowserContext* browser_context = - CefBrowserContext::GetForCachePath(profile_dir); + CefBrowserContext::FromCachePath(profile_dir); if (!browser_context) { // ProfileManager makes assumptions about profile directory paths that do // not match CEF usage. For example, the default Chrome profile name is @@ -47,19 +47,19 @@ Profile* ChromeProfileManagerAlloy::GetProfile( // asking for. browser_context = GetActiveBrowserContext(); } - return browser_context; + return browser_context->AsProfile(); } bool ChromeProfileManagerAlloy::IsValidProfile(const void* profile) { if (!profile) return false; - return !!CefBrowserContext::GetForContext( - reinterpret_cast(const_cast(profile))); + return !!CefBrowserContext::FromBrowserContext( + static_cast(profile)); } Profile* ChromeProfileManagerAlloy::GetLastUsedProfile( const base::FilePath& user_data_dir) { // Override this method to avoid having to register prefs::kProfileLastUsed, // usage of which doesn't make sense for CEF. - return GetActiveBrowserContext(); + return GetActiveBrowserContext()->AsProfile(); } diff --git a/libcef/browser/browser_context.cc b/libcef/browser/browser_context.cc index 248777e30..73b10064f 100644 --- a/libcef/browser/browser_context.cc +++ b/libcef/browser/browser_context.cc @@ -7,47 +7,20 @@ #include #include -#include "libcef/browser/alloy/alloy_content_browser_client.h" -#include "libcef/browser/download_manager_delegate.h" -#include "libcef/browser/extensions/extension_system.h" +#include "libcef/browser/iothread_state.h" #include "libcef/browser/media_router/media_router_manager.h" -#include "libcef/browser/prefs/browser_prefs.h" #include "libcef/browser/request_context_impl.h" -#include "libcef/browser/ssl_host_state_delegate.h" #include "libcef/browser/thread_util.h" #include "libcef/common/cef_switches.h" -#include "libcef/common/extensions/extensions_util.h" -#include "base/command_line.h" #include "base/files/file_util.h" #include "base/lazy_instance.h" #include "base/logging.h" #include "base/strings/string_util.h" -#include "chrome/browser/font_family_cache.h" -#include "chrome/browser/plugins/chrome_plugin_service_filter.h" -#include "chrome/browser/profiles/profile_key.h" -#include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h" -#include "chrome/common/pref_names.h" -#include "components/content_settings/core/browser/host_content_settings_map.h" -#include "components/guest_view/browser/guest_view_manager.h" -#include "components/keyed_service/content/browser_context_dependency_manager.h" -#include "components/keyed_service/core/simple_dependency_manager.h" -#include "components/keyed_service/core/simple_key_map.h" -#include "components/prefs/pref_service.h" -#include "components/proxy_config/pref_proxy_config_tracker_impl.h" -#include "components/user_prefs/user_prefs.h" -#include "components/visitedlink/browser/visitedlink_event_listener.h" -#include "components/visitedlink/browser/visitedlink_writer.h" -#include "components/zoom/zoom_event_manager.h" +#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_thread.h" -#include "content/public/browser/download_manager.h" #include "content/public/browser/storage_partition.h" -#include "extensions/browser/extension_protocols.h" -#include "extensions/browser/process_manager.h" -#include "extensions/common/constants.h" -#include "net/proxy_resolution/proxy_config_service.h" -#include "services/network/public/mojom/cors_origin_pattern.mojom.h" using content::BrowserThread; @@ -90,10 +63,10 @@ class ImplManager { return GetImplPos(impl) != all_.end(); } - CefBrowserContext* GetImplForIDs(int render_process_id, - int render_frame_id, - int frame_tree_node_id, - bool require_frame_match) { + CefBrowserContext* GetImplFromIDs(int render_process_id, + int render_frame_id, + int frame_tree_node_id, + bool require_frame_match) { CEF_REQUIRE_UIT(); for (const auto& context : all_) { if (context->IsAssociatedContext(render_process_id, render_frame_id, @@ -105,15 +78,15 @@ class ImplManager { return nullptr; } - CefBrowserContext* GetImplForContext(const content::BrowserContext* context) { + CefBrowserContext* GetImplFromBrowserContext( + const content::BrowserContext* context) { CEF_REQUIRE_UIT(); if (!context) return nullptr; - Vector::iterator it = all_.begin(); - for (; it != all_.end(); ++it) { - if (*it == context) - return *it; + for (const auto& bc : all_) { + if (bc->AsBrowserContext() == context) + return bc; } return nullptr; } @@ -122,11 +95,11 @@ class ImplManager { CEF_REQUIRE_UIT(); DCHECK(!path.empty()); DCHECK(IsValidImpl(impl)); - DCHECK(GetImplForPath(path) == nullptr); + DCHECK(GetImplFromPath(path) == nullptr); map_.insert(std::make_pair(path, impl)); } - CefBrowserContext* GetImplForPath(const base::FilePath& path) { + CefBrowserContext* GetImplFromPath(const base::FilePath& path) { CEF_REQUIRE_UIT(); DCHECK(!path.empty()); PathMap::const_iterator it = map_.find(path); @@ -170,59 +143,6 @@ CefBrowserContext* GetSelf(base::WeakPtr self) { } // namespace -// Creates and manages VisitedLinkEventListener objects for each -// CefBrowserContext sharing the same VisitedLinkWriter. -class CefVisitedLinkListener : public visitedlink::VisitedLinkWriter::Listener { - public: - CefVisitedLinkListener() { DCHECK(listener_map_.empty()); } - - void CreateListenerForContext(const CefBrowserContext* context) { - CEF_REQUIRE_UIT(); - auto listener = std::make_unique( - const_cast(context)); - listener_map_.insert(std::make_pair(context, std::move(listener))); - } - - void RemoveListenerForContext(const CefBrowserContext* context) { - CEF_REQUIRE_UIT(); - ListenerMap::iterator it = listener_map_.find(context); - DCHECK(it != listener_map_.end()); - listener_map_.erase(it); - } - - // visitedlink::VisitedLinkWriter::Listener methods. - - void NewTable(base::ReadOnlySharedMemoryRegion* table_region) override { - CEF_REQUIRE_UIT(); - ListenerMap::iterator it = listener_map_.begin(); - for (; it != listener_map_.end(); ++it) - it->second->NewTable(table_region); - } - - void Add(visitedlink::VisitedLinkCommon::Fingerprint fingerprint) override { - CEF_REQUIRE_UIT(); - ListenerMap::iterator it = listener_map_.begin(); - for (; it != listener_map_.end(); ++it) - it->second->Add(fingerprint); - } - - void Reset(bool invalidate_hashes) override { - CEF_REQUIRE_UIT(); - ListenerMap::iterator it = listener_map_.begin(); - for (; it != listener_map_.end(); ++it) - it->second->Reset(invalidate_hashes); - } - - private: - // Map of CefBrowserContext to the associated VisitedLinkEventListener. - typedef std::map> - ListenerMap; - ListenerMap listener_map_; - - DISALLOW_COPY_AND_ASSIGN(CefVisitedLinkListener); -}; - CefBrowserContext::CefBrowserContext(const CefRequestContextSettings& settings) : settings_(settings), weak_ptr_factory_(this) { g_manager.Get().AddImpl(this); @@ -231,6 +151,31 @@ CefBrowserContext::CefBrowserContext(const CefRequestContextSettings& settings) CefBrowserContext::~CefBrowserContext() { CEF_REQUIRE_UIT(); + DCHECK(is_shutdown_); + + if (iothread_state_) { + // Destruction of the CefIOThreadState will trigger destruction of all + // associated network requests. + content::BrowserThread::DeleteSoon(content::BrowserThread::IO, FROM_HERE, + iothread_state_.release()); + } +} + +void CefBrowserContext::Initialize() { + cache_path_ = base::FilePath(CefString(&settings_.cache_path)); + + if (!cache_path_.empty()) + g_manager.Get().SetImplPath(this, cache_path_); + + iothread_state_ = std::make_unique(); +} + +void CefBrowserContext::Shutdown() { + CEF_REQUIRE_UIT(); + +#if DCHECK_IS_ON() + is_shutdown_ = true; +#endif // No CefRequestContext should be referencing this object any longer. DCHECK(request_context_set_.empty()); @@ -240,121 +185,6 @@ CefBrowserContext::~CefBrowserContext() { // Destroy objects that may hold references to the MediaRouter. media_router_manager_.reset(); - - // Send notifications to clean up objects associated with this Profile. - MaybeSendDestroyedNotification(); - - ChromePluginServiceFilter::GetInstance()->UnregisterProfile(this); - - // Remove any BrowserContextKeyedServiceFactory associations. This must be - // called before the ProxyService owned by CefBrowserContext is destroyed. - // The SimpleDependencyManager should always be passed after the - // BrowserContextDependencyManager. This is because the KeyedService instances - // in the BrowserContextDependencyManager's dependency graph can depend on the - // ones in the SimpleDependencyManager's graph. - DependencyManager::PerformInterlockedTwoPhaseShutdown( - BrowserContextDependencyManager::GetInstance(), this, - SimpleDependencyManager::GetInstance(), key_.get()); - - key_.reset(); - SimpleKeyMap::GetInstance()->Dissociate(this); - - // Shuts down the storage partitions associated with this browser context. - // This must be called before the browser context is actually destroyed - // and before a clean-up task for its corresponding IO thread residents - // (e.g. ResourceContext) is posted, so that the classes that hung on - // StoragePartition can have time to do necessary cleanups on IO thread. - ShutdownStoragePartitions(); - - if (resource_context_.get()) { - // Destruction of the ResourceContext will trigger destruction of all - // associated network requests. - content::BrowserThread::DeleteSoon(content::BrowserThread::IO, FROM_HERE, - resource_context_.release()); - } - - visitedlink_listener_->RemoveListenerForContext(this); - - // The FontFamilyCache references the ProxyService so delete it before the - // ProxyService is deleted. - SetUserData(&kFontFamilyCacheKey, nullptr); - - pref_proxy_config_tracker_->DetachFromPrefService(); - - if (host_content_settings_map_) - host_content_settings_map_->ShutdownOnUIThread(); - - // Delete the download manager delegate here because otherwise we'll crash - // when it's accessed from the content::BrowserContext destructor. - if (download_manager_delegate_) - download_manager_delegate_.reset(nullptr); -} - -void CefBrowserContext::Initialize() { - cache_path_ = base::FilePath(CefString(&settings_.cache_path)); - - if (!cache_path_.empty()) - g_manager.Get().SetImplPath(this, cache_path_); - - if (!!settings_.persist_session_cookies) { - set_should_persist_session_cookies(true); - } - - key_ = std::make_unique(cache_path_); - SimpleKeyMap::GetInstance()->Associate(this, key_.get()); - - // Initialize the PrefService object. - pref_service_ = browser_prefs::CreatePrefService( - this, cache_path_, !!settings_.persist_user_preferences); - - resource_context_.reset(new CefResourceContext(IsOffTheRecord())); - - // This must be called before creating any services to avoid hitting - // DependencyManager::AssertContextWasntDestroyed when creating/destroying - // multiple browser contexts (due to pointer address reuse). - BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices( - this); - - const bool extensions_enabled = extensions::ExtensionsEnabled(); - if (extensions_enabled) { - // Create the custom ExtensionSystem first because other KeyedServices - // depend on it. - extension_system_ = static_cast( - extensions::ExtensionSystem::Get(this)); - extension_system_->InitForRegularProfile(true); - - // Make sure the ProcessManager is created so that it receives extension - // load notifications. This is necessary for the proper initialization of - // background/event pages. - extensions::ProcessManager::Get(this); - } - - // Initialize visited links management. - base::FilePath visited_link_path; - if (!cache_path_.empty()) - visited_link_path = cache_path_.Append(FILE_PATH_LITERAL("Visited Links")); - visitedlink_listener_ = new CefVisitedLinkListener; - visitedlink_master_.reset(new visitedlink::VisitedLinkWriter( - visitedlink_listener_, this, !visited_link_path.empty(), false, - visited_link_path, 0)); - visitedlink_listener_->CreateListenerForContext(this); - visitedlink_master_->Init(); - - // Initialize proxy configuration tracker. - pref_proxy_config_tracker_.reset(new PrefProxyConfigTrackerImpl( - GetPrefs(), base::CreateSingleThreadTaskRunner({BrowserThread::IO}))); - - // Spell checking support and possibly other subsystems retrieve the - // PrefService associated with a BrowserContext via UserPrefs::Get(). - PrefService* pref_service = GetPrefs(); - DCHECK(pref_service); - user_prefs::UserPrefs::Set(this, pref_service); - key_->SetPrefs(pref_service); - - if (extensions_enabled) - extension_system_->Init(); - - ChromePluginServiceFilter::GetInstance()->RegisterProfile(this); } void CefBrowserContext::AddCefRequestContext(CefRequestContextImpl* context) { @@ -366,36 +196,35 @@ void CefBrowserContext::RemoveCefRequestContext( CefRequestContextImpl* context) { CEF_REQUIRE_UIT(); - if (extensions::ExtensionsEnabled()) { - extension_system()->OnRequestContextDeleted(context); - } - request_context_set_.erase(context); // Delete ourselves when the reference count reaches zero. - if (request_context_set_.empty()) + if (request_context_set_.empty()) { + Shutdown(); delete this; + } } // static -CefBrowserContext* CefBrowserContext::GetForCachePath( +CefBrowserContext* CefBrowserContext::FromCachePath( const base::FilePath& cache_path) { - return g_manager.Get().GetImplForPath(cache_path); + return g_manager.Get().GetImplFromPath(cache_path); } // static -CefBrowserContext* CefBrowserContext::GetForIDs(int render_process_id, - int render_frame_id, - int frame_tree_node_id, - bool require_frame_match) { - return g_manager.Get().GetImplForIDs(render_process_id, render_frame_id, - frame_tree_node_id, require_frame_match); +CefBrowserContext* CefBrowserContext::FromIDs(int render_process_id, + int render_frame_id, + int frame_tree_node_id, + bool require_frame_match) { + return g_manager.Get().GetImplFromIDs(render_process_id, render_frame_id, + frame_tree_node_id, + require_frame_match); } // static -CefBrowserContext* CefBrowserContext::GetForContext( - content::BrowserContext* context) { - return g_manager.Get().GetImplForContext(context); +CefBrowserContext* CefBrowserContext::FromBrowserContext( + const content::BrowserContext* context) { + return g_manager.Get().GetImplFromBrowserContext(context); } // static @@ -403,194 +232,6 @@ std::vector CefBrowserContext::GetAll() { return g_manager.Get().GetAllImpl(); } -content::ResourceContext* CefBrowserContext::GetResourceContext() { - return resource_context_.get(); -} - -content::ClientHintsControllerDelegate* -CefBrowserContext::GetClientHintsControllerDelegate() { - return nullptr; -} - -void CefBrowserContext::SetCorsOriginAccessListForOrigin( - const url::Origin& source_origin, - std::vector allow_patterns, - std::vector block_patterns, - base::OnceClosure closure) { - // This method is called for Extension support. - base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, std::move(closure)); -} - -ChromeZoomLevelPrefs* CefBrowserContext::GetZoomLevelPrefs() { - return static_cast( - GetStoragePartition(this, nullptr)->GetZoomLevelDelegate()); -} - -scoped_refptr -CefBrowserContext::GetURLLoaderFactory() { - return GetDefaultStoragePartition(this) - ->GetURLLoaderFactoryForBrowserProcess(); -} - -base::FilePath CefBrowserContext::GetPath() { - return cache_path_; -} - -base::FilePath CefBrowserContext::GetPath() const { - return cache_path_; -} - -std::unique_ptr -CefBrowserContext::CreateZoomLevelDelegate( - const base::FilePath& partition_path) { - if (cache_path_.empty()) - return std::unique_ptr(); - - return base::WrapUnique(new ChromeZoomLevelPrefs( - GetPrefs(), cache_path_, partition_path, - zoom::ZoomEventManager::GetForBrowserContext(this)->GetWeakPtr())); -} - -bool CefBrowserContext::IsOffTheRecord() const { - // CEF contexts are never flagged as off-the-record. It causes problems - // for the extension system. - return false; -} - -content::DownloadManagerDelegate* -CefBrowserContext::GetDownloadManagerDelegate() { - if (!download_manager_delegate_) { - content::DownloadManager* manager = - BrowserContext::GetDownloadManager(this); - download_manager_delegate_.reset(new CefDownloadManagerDelegate(manager)); - } - return download_manager_delegate_.get(); -} - -content::BrowserPluginGuestManager* CefBrowserContext::GetGuestManager() { - DCHECK(extensions::ExtensionsEnabled()); - return guest_view::GuestViewManager::FromBrowserContext(this); -} - -storage::SpecialStoragePolicy* CefBrowserContext::GetSpecialStoragePolicy() { - return nullptr; -} - -content::PushMessagingService* CefBrowserContext::GetPushMessagingService() { - return nullptr; -} - -content::StorageNotificationService* -CefBrowserContext::GetStorageNotificationService() { - return nullptr; -} - -content::SSLHostStateDelegate* CefBrowserContext::GetSSLHostStateDelegate() { - if (!ssl_host_state_delegate_.get()) - ssl_host_state_delegate_.reset(new CefSSLHostStateDelegate()); - return ssl_host_state_delegate_.get(); -} - -content::PermissionControllerDelegate* -CefBrowserContext::GetPermissionControllerDelegate() { - return nullptr; -} - -content::BackgroundFetchDelegate* -CefBrowserContext::GetBackgroundFetchDelegate() { - return nullptr; -} - -content::BackgroundSyncController* -CefBrowserContext::GetBackgroundSyncController() { - return nullptr; -} - -content::BrowsingDataRemoverDelegate* -CefBrowserContext::GetBrowsingDataRemoverDelegate() { - return nullptr; -} - -PrefService* CefBrowserContext::GetPrefs() { - return pref_service_.get(); -} - -const PrefService* CefBrowserContext::GetPrefs() const { - return pref_service_.get(); -} - -ProfileKey* CefBrowserContext::GetProfileKey() const { - DCHECK(key_); - return key_.get(); -} - -policy::SchemaRegistryService* -CefBrowserContext::GetPolicySchemaRegistryService() { - NOTREACHED(); - return nullptr; -} - -policy::UserCloudPolicyManager* CefBrowserContext::GetUserCloudPolicyManager() { - NOTREACHED(); - return nullptr; -} - -policy::ProfilePolicyConnector* CefBrowserContext::GetProfilePolicyConnector() { - NOTREACHED(); - return nullptr; -} - -const policy::ProfilePolicyConnector* -CefBrowserContext::GetProfilePolicyConnector() const { - NOTREACHED(); - return nullptr; -} - -const CefRequestContextSettings& CefBrowserContext::GetSettings() const { - return settings_; -} - -HostContentSettingsMap* CefBrowserContext::GetHostContentSettingsMap() { - DCHECK_CURRENTLY_ON(BrowserThread::UI); - if (!host_content_settings_map_.get()) { - // The |is_incognito_profile| and |is_guest_profile| arguments are - // intentionally set to false as they otherwise limit the types of values - // that can be stored in the settings map (for example, default values set - // via DefaultProvider::SetWebsiteSetting). - host_content_settings_map_ = - new HostContentSettingsMap(GetPrefs(), false, false, false, false); - - // Change the default plugin policy. - const base::CommandLine* command_line = - base::CommandLine::ForCurrentProcess(); - const std::string& plugin_policy_str = - command_line->GetSwitchValueASCII(switches::kPluginPolicy); - if (!plugin_policy_str.empty()) { - ContentSetting plugin_policy = CONTENT_SETTING_ALLOW; - if (base::LowerCaseEqualsASCII(plugin_policy_str, - switches::kPluginPolicy_Detect)) { - plugin_policy = CONTENT_SETTING_DETECT_IMPORTANT_CONTENT; - } else if (base::LowerCaseEqualsASCII(plugin_policy_str, - switches::kPluginPolicy_Block)) { - plugin_policy = CONTENT_SETTING_BLOCK; - } - host_content_settings_map_->SetDefaultContentSetting( - ContentSettingsType::PLUGINS, plugin_policy); - } - } - return host_content_settings_map_.get(); -} - -void CefBrowserContext::AddVisitedURLs(const std::vector& urls) { - visitedlink_master_->AddURLs(urls); -} - -void CefBrowserContext::RebuildTable( - const scoped_refptr& enumerator) { - // Called when visited links will not or cannot be loaded from disk. - enumerator->OnComplete(true); -} - void CefBrowserContext::OnRenderFrameCreated( CefRequestContextImpl* request_context, int render_process_id, @@ -611,15 +252,14 @@ void CefBrowserContext::OnRenderFrameCreated( handler_map_.AddHandler(render_process_id, render_frame_id, frame_tree_node_id, handler); - if (resource_context_) { + if (iothread_state_) { // Using base::Unretained() is safe because both this callback and - // possible deletion of |resource_context_| will execute on the IO thread, + // possible deletion of |iothread_state_| will execute on the IO thread, // and this callback will be executed first. - CEF_POST_TASK(CEF_IOT, - base::Bind(&CefResourceContext::AddHandler, - base::Unretained(resource_context_.get()), - render_process_id, render_frame_id, - frame_tree_node_id, handler)); + CEF_POST_TASK(CEF_IOT, base::Bind(&CefIOThreadState::AddHandler, + base::Unretained(iothread_state_.get()), + render_process_id, render_frame_id, + frame_tree_node_id, handler)); } } } @@ -650,15 +290,14 @@ void CefBrowserContext::OnRenderFrameDeleted( handler_map_.RemoveHandler(render_process_id, render_frame_id, frame_tree_node_id); - if (resource_context_) { + if (iothread_state_) { // Using base::Unretained() is safe because both this callback and - // possible deletion of |resource_context_| will execute on the IO thread, + // possible deletion of |iothread_state_| will execute on the IO thread, // and this callback will be executed first. - CEF_POST_TASK( - CEF_IOT, - base::Bind(&CefResourceContext::RemoveHandler, - base::Unretained(resource_context_.get()), - render_process_id, render_frame_id, frame_tree_node_id)); + CEF_POST_TASK(CEF_IOT, base::Bind(&CefIOThreadState::RemoveHandler, + base::Unretained(iothread_state_.get()), + render_process_id, render_frame_id, + frame_tree_node_id)); } } @@ -760,56 +399,74 @@ void CefBrowserContext::ClearPluginLoadDecision(int render_process_id) { } void CefBrowserContext::RegisterSchemeHandlerFactory( - const std::string& scheme_name, - const std::string& domain_name, + const CefString& scheme_name, + const CefString& domain_name, CefRefPtr factory) { - if (resource_context_) { + if (iothread_state_) { // Using base::Unretained() is safe because both this callback and possible - // deletion of |resource_context_| will execute on the IO thread, and this + // deletion of |iothread_state_| will execute on the IO thread, and this // callback will be executed first. CEF_POST_TASK(CEF_IOT, - base::Bind(&CefResourceContext::RegisterSchemeHandlerFactory, - base::Unretained(resource_context_.get()), + base::Bind(&CefIOThreadState::RegisterSchemeHandlerFactory, + base::Unretained(iothread_state_.get()), scheme_name, domain_name, factory)); } } void CefBrowserContext::ClearSchemeHandlerFactories() { - if (resource_context_) { + if (iothread_state_) { // Using base::Unretained() is safe because both this callback and possible - // deletion of |resource_context_| will execute on the IO thread, and this + // deletion of |iothread_state_| will execute on the IO thread, and this // callback will be executed first. CEF_POST_TASK(CEF_IOT, - base::Bind(&CefResourceContext::ClearSchemeHandlerFactories, - base::Unretained(resource_context_.get()))); + base::Bind(&CefIOThreadState::ClearSchemeHandlerFactories, + base::Unretained(iothread_state_.get()))); } } -network::mojom::NetworkContext* CefBrowserContext::GetNetworkContext() { - CEF_REQUIRE_UIT(); - return GetDefaultStoragePartition(this)->GetNetworkContext(); +void CefBrowserContext::LoadExtension( + const CefString& root_directory, + CefRefPtr manifest, + CefRefPtr handler, + CefRefPtr loader_context) { + NOTIMPLEMENTED(); + if (handler) + handler->OnExtensionLoadFailed(ERR_ABORTED); } -DownloadPrefs* CefBrowserContext::GetDownloadPrefs() { - CEF_REQUIRE_UIT(); - if (!download_prefs_) { - download_prefs_.reset(new DownloadPrefs(this)); - } - return download_prefs_.get(); +bool CefBrowserContext::GetExtensions(std::vector& extension_ids) { + NOTIMPLEMENTED(); + return false; +} + +CefRefPtr CefBrowserContext::GetExtension( + const CefString& extension_id) { + NOTIMPLEMENTED(); + return nullptr; +} + +bool CefBrowserContext::UnloadExtension(const CefString& extension_id) { + NOTIMPLEMENTED(); + return false; } bool CefBrowserContext::IsPrintPreviewSupported() const { - CEF_REQUIRE_UIT(); - if (!extensions::PrintPreviewEnabled()) - return false; + return true; +} - return !GetPrefs()->GetBoolean(prefs::kPrintPreviewDisabled); +void CefBrowserContext::AddVisitedURLs(const std::vector& urls) {} + +network::mojom::NetworkContext* CefBrowserContext::GetNetworkContext() { + CEF_REQUIRE_UIT(); + auto browser_context = AsBrowserContext(); + return browser_context->GetDefaultStoragePartition(browser_context) + ->GetNetworkContext(); } CefMediaRouterManager* CefBrowserContext::GetMediaRouterManager() { CEF_REQUIRE_UIT(); if (!media_router_manager_) { - media_router_manager_.reset(new CefMediaRouterManager(this)); + media_router_manager_.reset(new CefMediaRouterManager(AsBrowserContext())); } return media_router_manager_.get(); } diff --git a/libcef/browser/browser_context.h b/libcef/browser/browser_context.h index 0ab08dcda..7a55d7774 100644 --- a/libcef/browser/browser_context.h +++ b/libcef/browser/browser_context.h @@ -7,23 +7,17 @@ #pragma once #include +#include #include "include/cef_request_context_handler.h" -#include "libcef/browser/alloy/chrome_profile_alloy.h" #include "libcef/browser/request_context_handler_map.h" -#include "libcef/browser/resource_context.h" #include "base/callback.h" #include "base/files/file_path.h" -#include "base/memory/ref_counted.h" #include "base/memory/weak_ptr.h" -#include "chrome/browser/download/download_prefs.h" +#include "base/optional.h" #include "chrome/common/plugin.mojom.h" -#include "components/proxy_config/pref_proxy_config_tracker.h" -#include "components/visitedlink/browser/visitedlink_delegate.h" -#include "content/public/browser/browser_context.h" -#include "content/public/browser/browser_thread.h" -#include "content/public/browser/content_browser_client.h" +#include "services/network/public/mojom/network_context.mojom.h" #include "url/origin.h" /* @@ -31,32 +25,30 @@ // // WC = WebContents // Content API representation of a browser. Created by BHI or the system (for -// popups) and owned by BHI. Keeps a pointer to BC. +// popups) and owned by BHI. Keeps a pointer to the content::BrowserContext. // // BHI = CefBrowserHostImpl // Implements the CefBrowser and CefBrowserHost interfaces which are exposed -// to clients. References an RCI instance. Owns a WC. Life span is controlled -// by client references and AlloyContentBrowserClient. +// to clients. References an RCI instance. Owns a WC. Lifespan is controlled +// by client references and CefBrowserInfoManager (until the browser has +// closed). // // RCI = CefRequestContextImpl // Implements the CefRequestContext interface which is exposed to clients. -// References the isolated BC. +// Creates or references a BC. Lifespan is controlled by client references and +// BrowserMainParts (for the global RCI). // // BC = CefBrowserContext -// Entry point from WC when using an isolated RCI. Owns the RC and creates the -// SPI indirectly. Owned by AlloyBrowserMainParts for the global context or -RCI -// for non-global contexts. +// Is/owns the content::BrowserContext which is the entry point from WC. +// Owns the IOTS and creates the SPI indirectly. Potentially shared by +// multiple RCI. Deletes itself when no longer needed by RCI. // // SPI = content::StoragePartitionImpl // Owns storage-related objects like Quota, IndexedDB, Cache, etc. Created by -// StoragePartitionImplMap::Get(). Provides access to the URCG. Life span is -// controlled indirectly by BC. +// StoragePartitionImplMap::Get(). Life span is controlled indirectly by BC. // -// RC = CefResourceContext -// Acts as a bridge for resource loading. Network request life span is tied to -// this object. Must be destroyed before the associated URCG. Life span is -// controlled by BC. +// IOTS = CefIOThreadState +// Stores state for access on the IO thread. Life span is controlled by BC. // // // Relationship diagram: @@ -64,142 +56,65 @@ RCI // own = ownership (std::unique_ptr) // ptr = raw pointer // -// AlloyBrowserMainParts -// | -// own -// v -// BHI -own-> WC -ptr-> BC -own-> SPI -// -// BHI -ref-> RCI -own-> BC -own-> RC +// BHI -ref-> RCI -ptr-> BC -own-> SPI, IOTS +// ^ +// BHI -own-> WC -ptr--/ // // // How shutdown works: -// 1. CefBrowserHostImpl is destroyed on any thread due to browser close, -// ref release, etc. +// 1. CefBrowserHostImpl::DestroyBrowser is called on the UI thread after the +// browser is closed and deletes the WebContents. +// 1. CefBrowserHostImpl is destroyed on any thread when the last reference +// is released. // 2. CefRequestContextImpl is destroyed (possibly asynchronously) on the UI -// thread due to CefBrowserHostImpl destruction, ref release, etc. -// 3. CefBrowserContext is destroyed on the UI thread due to -// CefRequestContextImpl destruction or deletion in -// AlloyBrowserMainParts::PostMainMessageLoopRun(). -// 4. CefResourceContext is destroyed asynchronously on the IO thread due to -// CefBrowserContext destruction. This cancels/destroys any pending -// network requests. +// thread when the last reference is released. +// 3. CefBrowserContext is destroyed on the UI thread when no longer needed +// by any CefRequestContextImpl (via RemoveCefRequestContext). +// 4. CefIOThreadState is destroyed asynchronously on the IO thread after +// the owning CefBrowserContext is destroyed. */ -class CefDownloadManagerDelegate; +namespace content { +class BrowserContext; +} + class CefMediaRouterManager; class CefRequestContextImpl; -class CefSSLHostStateDelegate; -class CefVisitedLinkListener; -class HostContentSettingsMap; -class PrefService; +class CefIOThreadState; +class Profile; -namespace extensions { -class CefExtensionSystem; -} - -namespace visitedlink { -class VisitedLinkWriter; -} - -// Main entry point for configuring behavior on a per-browser basis. An instance -// of this class is passed to WebContents::Create in CefBrowserHostImpl:: -// CreateInternal. Only accessed on the UI thread unless otherwise indicated. -class CefBrowserContext : public ChromeProfileAlloy, - public visitedlink::VisitedLinkDelegate { +// Main entry point for configuring behavior on a per-RequestContext basis. The +// content::BrowserContext represented by this class is passed to +// WebContents::Create in CefBrowserHostImpl::CreateInternal. Only accessed on +// the UI thread unless otherwise indicated. +class CefBrowserContext { public: - explicit CefBrowserContext(const CefRequestContextSettings& settings); - // Returns the existing instance, if any, associated with the specified // |cache_path|. - static CefBrowserContext* GetForCachePath(const base::FilePath& cache_path); + static CefBrowserContext* FromCachePath(const base::FilePath& cache_path); // Returns the existing instance, if any, associated with the specified IDs. // See comments on IsAssociatedContext() for usage. - static CefBrowserContext* GetForIDs(int render_process_id, - int render_frame_id, - int frame_tree_node_id, - bool require_frame_match); + static CefBrowserContext* FromIDs(int render_process_id, + int render_frame_id, + int frame_tree_node_id, + bool require_frame_match); // Returns the underlying CefBrowserContext if any. - static CefBrowserContext* GetForContext(content::BrowserContext* context); + static CefBrowserContext* FromBrowserContext( + const content::BrowserContext* context); // Returns all existing CefBrowserContext. static std::vector GetAll(); - // Must be called immediately after this object is created. - void Initialize(); + // Returns the content and chrome layer representations of the context. + virtual content::BrowserContext* AsBrowserContext() = 0; + virtual Profile* AsProfile() = 0; - // Track associated CefRequestContextImpl objects. This object will delete - // itself when the count reaches zero. + // Called from CefRequestContextImpl to track associated objects. This + // object will delete itself when the count reaches zero. void AddCefRequestContext(CefRequestContextImpl* context); - void RemoveCefRequestContext(CefRequestContextImpl* context); - - // BrowserContext methods. - content::ResourceContext* GetResourceContext() override; - content::ClientHintsControllerDelegate* GetClientHintsControllerDelegate() - override; - void SetCorsOriginAccessListForOrigin( - const url::Origin& source_origin, - std::vector allow_patterns, - std::vector block_patterns, - base::OnceClosure closure) override; - base::FilePath GetPath() override; - base::FilePath GetPath() const override; - std::unique_ptr CreateZoomLevelDelegate( - const base::FilePath& partition_path) override; - bool IsOffTheRecord() const override; - content::DownloadManagerDelegate* GetDownloadManagerDelegate() override; - content::BrowserPluginGuestManager* GetGuestManager() override; - storage::SpecialStoragePolicy* GetSpecialStoragePolicy() override; - content::PushMessagingService* GetPushMessagingService() override; - content::StorageNotificationService* GetStorageNotificationService() override; - content::SSLHostStateDelegate* GetSSLHostStateDelegate() override; - content::PermissionControllerDelegate* GetPermissionControllerDelegate() - override; - content::BackgroundFetchDelegate* GetBackgroundFetchDelegate() override; - content::BackgroundSyncController* GetBackgroundSyncController() override; - content::BrowsingDataRemoverDelegate* GetBrowsingDataRemoverDelegate() - override; - - // Profile methods. - ChromeZoomLevelPrefs* GetZoomLevelPrefs() override; - scoped_refptr GetURLLoaderFactory() override; - PrefService* GetPrefs() override; - bool AllowsBrowserWindows() const override { return false; } - const PrefService* GetPrefs() const override; - ProfileKey* GetProfileKey() const override; - policy::SchemaRegistryService* GetPolicySchemaRegistryService() override; - policy::UserCloudPolicyManager* GetUserCloudPolicyManager() override; - policy::ProfilePolicyConnector* GetProfilePolicyConnector() override; - const policy::ProfilePolicyConnector* GetProfilePolicyConnector() - const override; - - // Values checked in ProfileNetworkContextService::CreateNetworkContextParams - // when creating the NetworkContext. - bool ShouldRestoreOldSessionCookies() override { - return should_persist_session_cookies_; - } - bool ShouldPersistSessionCookies() override { - return should_persist_session_cookies_; - } - base::Optional> GetCookieableSchemes() override { - return cookieable_schemes_; - } - - // visitedlink::VisitedLinkDelegate methods. - void RebuildTable(const scoped_refptr& enumerator) override; - - // Returns the settings associated with this object. Safe to call from any - // thread. - const CefRequestContextSettings& GetSettings() const; - - // Settings for plugins and extensions. - HostContentSettingsMap* GetHostContentSettingsMap(); - - // Called from CefBrowserHostImpl::DidNavigateAnyFrame to update the table of - // visited links. - void AddVisitedURLs(const std::vector& urls); + virtual void RemoveCefRequestContext(CefRequestContextImpl* context); // Called from CefRequestContextImpl::OnRenderFrameCreated. void OnRenderFrameCreated(CefRequestContextImpl* request_context, @@ -255,79 +170,72 @@ class CefBrowserContext : public ChromeProfileAlloy, void ClearPluginLoadDecision(int render_process_id); // Called from CefRequestContextImpl methods of the same name. - void RegisterSchemeHandlerFactory(const std::string& scheme_name, - const std::string& domain_name, + void RegisterSchemeHandlerFactory(const CefString& scheme_name, + const CefString& domain_name, CefRefPtr factory); void ClearSchemeHandlerFactories(); + // TODO(chrome-runtime): Make these extension methods pure virtual. + virtual void LoadExtension(const CefString& root_directory, + CefRefPtr manifest, + CefRefPtr handler, + CefRefPtr loader_context); + virtual bool GetExtensions(std::vector& extension_ids); + virtual CefRefPtr GetExtension(const CefString& extension_id); + + // Called from CefExtensionImpl::Unload(). + virtual bool UnloadExtension(const CefString& extension_id); + + // Returns true if this context supports print preview. + virtual bool IsPrintPreviewSupported() const; + + // Called from CefBrowserHostImpl::DidNavigateAnyFrame to update the table of + // visited links. + virtual void AddVisitedURLs(const std::vector& urls); network::mojom::NetworkContext* GetNetworkContext(); - void set_should_persist_session_cookies(bool value) { - should_persist_session_cookies_ = value; - } + CefMediaRouterManager* GetMediaRouterManager(); + void set_cookieable_schemes( base::Optional> schemes) { cookieable_schemes_ = schemes; } - CefResourceContext* resource_context() const { - return resource_context_.get(); - } - extensions::CefExtensionSystem* extension_system() const { - return extension_system_; - } + // These accessors are safe to call from any thread because the values don't + // change during this object's lifespan. + const CefRequestContextSettings& settings() const { return settings_; } + base::FilePath cache_path() const { return cache_path_; } + CefIOThreadState* iothread_state() const { return iothread_state_.get(); } - // Called from DownloadPrefs::FromBrowserContext. - DownloadPrefs* GetDownloadPrefs(); - - // Returns true if this context supports print preview. - bool IsPrintPreviewSupported() const; - - CefMediaRouterManager* GetMediaRouterManager(); - - // Returns the BrowserContext, or nullptr if the BrowserContext has already - // been destroyed. + // Used to hold a WeakPtr reference to this this object. The Getter returns + // nullptr if this object has already been destroyed. using Getter = base::RepeatingCallback; Getter getter() const { return getter_; } - private: - // Allow deletion via std::unique_ptr(). - friend std::default_delete; + protected: + explicit CefBrowserContext(const CefRequestContextSettings& settings); + virtual ~CefBrowserContext(); - ~CefBrowserContext() override; + // Will be called immediately after this object is created. + virtual void Initialize(); - // Members initialized during construction are safe to access from any thread. + // Will be called immediately before this object is deleted. + virtual void Shutdown(); + + // Members initialized during construction or Initialize() are safe to access + // from any thread. const CefRequestContextSettings settings_; base::FilePath cache_path_; - // CefRequestContextImpl objects referencing this object. - std::set request_context_set_; - - std::unique_ptr pref_service_; - std::unique_ptr pref_proxy_config_tracker_; - - std::unique_ptr download_manager_delegate_; - std::unique_ptr ssl_host_state_delegate_; - scoped_refptr host_content_settings_map_; - std::unique_ptr visitedlink_master_; - // |visitedlink_listener_| is owned by visitedlink_master_. - CefVisitedLinkListener* visitedlink_listener_; - bool should_persist_session_cookies_ = false; base::Optional> cookieable_schemes_; - std::unique_ptr resource_context_; - - // Owned by the KeyedService system. - extensions::CefExtensionSystem* extension_system_ = nullptr; - - // The key to index KeyedService instances created by - // SimpleKeyedServiceFactory. - std::unique_ptr key_; - - std::unique_ptr download_prefs_; - + private: + std::unique_ptr iothread_state_; std::unique_ptr media_router_manager_; + // CefRequestContextImpl objects referencing this object. + std::set request_context_set_; + // Map IDs to CefRequestContextHandler objects. CefRequestContextHandlerMap handler_map_; @@ -351,6 +259,10 @@ class CefBrowserContext : public ChromeProfileAlloy, typedef std::set NodeIdSet; NodeIdSet node_id_set_; +#if DCHECK_IS_ON() + bool is_shutdown_ = false; +#endif + Getter getter_; base::WeakPtrFactory weak_ptr_factory_; diff --git a/libcef/browser/browser_host_impl.cc b/libcef/browser/browser_host_impl.cc index 73b55ac1b..2199bd0d9 100644 --- a/libcef/browser/browser_host_impl.cc +++ b/libcef/browser/browser_host_impl.cc @@ -8,7 +8,6 @@ #include #include -#include "libcef/browser/alloy/alloy_content_browser_client.h" #include "libcef/browser/audio_capturer.h" #include "libcef/browser/browser_context.h" #include "libcef/browser/browser_info.h" @@ -365,9 +364,10 @@ CefRefPtr CefBrowserHostImpl::Create( CefRequestContextImpl::GetOrCreateForRequestContext( create_params.request_context); DCHECK(request_context_impl); - CefBrowserContext* browser_context = + CefBrowserContext* cef_browser_context = request_context_impl->GetBrowserContext(); - DCHECK(browser_context); + DCHECK(cef_browser_context); + auto browser_context = cef_browser_context->AsBrowserContext(); if (!create_params.request_context) { // Using the global request context. @@ -384,8 +384,8 @@ CefRefPtr CefBrowserHostImpl::Create( extensions::GetExtensionForUrl(browser_context, create_params.url); } if (create_params.extension) { - cef_extension = browser_context->extension_system()->GetExtension( - create_params.extension->id()); + cef_extension = + cef_browser_context->GetExtension(create_params.extension->id()); DCHECK(cef_extension); if (create_params.extension_host_type == extensions::VIEW_TYPE_INVALID) { @@ -755,13 +755,12 @@ void CefBrowserHostImpl::StartDownload(const CefString& url) { if (!web_contents()) return; - CefBrowserContext* context = - static_cast(web_contents()->GetBrowserContext()); - if (!context) + auto browser_context = web_contents()->GetBrowserContext(); + if (!browser_context) return; content::DownloadManager* manager = - content::BrowserContext::GetDownloadManager(context); + content::BrowserContext::GetDownloadManager(browser_context); if (!manager) return; @@ -1588,9 +1587,9 @@ bool CefBrowserHostImpl::IsPrintPreviewSupported() const { if (!actionable_contents) return false; - if (!CefBrowserContext::GetForContext( - actionable_contents->GetBrowserContext()) - ->IsPrintPreviewSupported()) { + auto cef_browser_context = CefBrowserContext::FromBrowserContext( + actionable_contents->GetBrowserContext()); + if (!cef_browser_context->IsPrintPreviewSupported()) { return false; } @@ -2736,10 +2735,11 @@ void CefBrowserHostImpl::DidFinishNavigation( } if (web_contents()) { - CefBrowserContext* context = - static_cast(web_contents()->GetBrowserContext()); - if (context) { - context->AddVisitedURLs(navigation_handle->GetRedirectChain()); + auto cef_browser_context = CefBrowserContext::FromBrowserContext( + web_contents()->GetBrowserContext()); + if (cef_browser_context) { + cef_browser_context->AddVisitedURLs( + navigation_handle->GetRedirectChain()); } } } diff --git a/libcef/browser/browser_host_impl.h b/libcef/browser/browser_host_impl.h index 4523aae0a..e57075a32 100644 --- a/libcef/browser/browser_host_impl.h +++ b/libcef/browser/browser_host_impl.h @@ -42,6 +42,10 @@ class Extension; class ExtensionHost; } // namespace extensions +namespace gfx { +class ImageSkia; +} + #if defined(USE_AURA) namespace views { class Widget; diff --git a/libcef/browser/devtools/devtools_frontend.cc b/libcef/browser/devtools/devtools_frontend.cc index 81fb90801..ad00c60b8 100644 --- a/libcef/browser/devtools/devtools_frontend.cc +++ b/libcef/browser/devtools/devtools_frontend.cc @@ -29,6 +29,7 @@ #include "base/strings/utf_string_conversions.h" #include "base/task/post_task.h" #include "base/values.h" +#include "chrome/browser/profiles/profile.h" #include "chrome/common/pref_names.h" #include "components/prefs/scoped_user_pref_update.h" #include "content/public/browser/browser_context.h" @@ -620,7 +621,8 @@ void CefDevToolsFrontend::AgentHostClosed( } PrefService* CefDevToolsFrontend::GetPrefs() const { - return static_cast( + return CefBrowserContext::FromBrowserContext( frontend_browser_->web_contents()->GetBrowserContext()) + ->AsProfile() ->GetPrefs(); } diff --git a/libcef/browser/extension_impl.cc b/libcef/browser/extension_impl.cc index 5b7f677a7..dca7ee1c2 100644 --- a/libcef/browser/extension_impl.cc +++ b/libcef/browser/extension_impl.cc @@ -4,7 +4,6 @@ #include "libcef/browser/extension_impl.h" -#include "libcef/browser/extensions/extension_system.h" #include "libcef/browser/request_context_impl.h" #include "libcef/browser/thread_util.h" #include "libcef/common/values_impl.h" @@ -89,7 +88,6 @@ void CefExtensionImpl::Unload() { const bool result = static_cast(loader_context_) ->GetBrowserContext() - ->extension_system() ->UnloadExtension(id_); ALLOW_UNUSED_LOCAL(result); DCHECK(result); diff --git a/libcef/browser/extensions/browser_extensions_util.cc b/libcef/browser/extensions/browser_extensions_util.cc index c7ea3e1a3..4c797668d 100644 --- a/libcef/browser/extensions/browser_extensions_util.cc +++ b/libcef/browser/extensions/browser_extensions_util.cc @@ -149,16 +149,16 @@ CefRefPtr GetBrowserForTabId( if (tab_id < 0 || !browser_context) return nullptr; - CefBrowserContext* browser_context_impl = - CefBrowserContext::GetForContext(browser_context); + auto cef_browser_context = + CefBrowserContext::FromBrowserContext(browser_context); for (const auto& browser_info : CefBrowserInfoManager::GetInstance()->GetBrowserInfoList()) { CefRefPtr current_browser = browser_info->browser(); if (current_browser && current_browser->GetIdentifier() == tab_id) { - // Make sure we're operating in the same BrowserContextImpl. - if (CefBrowserContext::GetForContext( - current_browser->GetBrowserContext()) == browser_context_impl) { + // Make sure we're operating in the same CefBrowserContext. + if (CefBrowserContext::FromBrowserContext( + current_browser->GetBrowserContext()) == cef_browser_context) { return current_browser; } else { LOG(WARNING) << "Browser with tabId " << tab_id diff --git a/libcef/browser/extensions/extension_function_details.cc b/libcef/browser/extensions/extension_function_details.cc index 9ae9fe4b6..e199ac493 100644 --- a/libcef/browser/extensions/extension_function_details.cc +++ b/libcef/browser/extensions/extension_function_details.cc @@ -166,9 +166,10 @@ CefRefPtr CefExtensionFunctionDetails::GetCurrentBrowser() CefRefPtr active_browser_impl = static_cast(active_browser.get()); - // Make sure we're operating in the same BrowserContextImpl. - if (CefBrowserContext::GetForContext(browser->GetBrowserContext()) == - CefBrowserContext::GetForContext( + // Make sure we're operating in the same CefBrowserContext. + if (CefBrowserContext::FromBrowserContext( + browser->GetBrowserContext()) == + CefBrowserContext::FromBrowserContext( active_browser_impl->GetBrowserContext())) { browser = active_browser_impl; } else { @@ -347,13 +348,12 @@ base::DictionaryValue* CefExtensionFunctionDetails::OpenTab( if (params.index.get()) index = *params.index; - CefBrowserContext* browser_context_impl = - CefBrowserContext::GetForContext(active_browser->GetBrowserContext()); + auto cef_browser_context = CefBrowserContext::FromBrowserContext( + active_browser->GetBrowserContext()); // A CEF representation should always exist. CefRefPtr cef_extension = - browser_context_impl->extension_system()->GetExtension( - function()->extension()->id()); + cef_browser_context->GetExtension(function()->extension()->id()); DCHECK(cef_extension); if (!cef_extension) return nullptr; @@ -466,8 +466,7 @@ CefExtensionFunctionDetails::CreateMutedInfo(content::WebContents* contents) { CefRefPtr CefExtensionFunctionDetails::GetCefExtension() const { if (!cef_extension_) { cef_extension_ = - static_cast(function_->browser_context()) - ->extension_system() + CefBrowserContext::FromBrowserContext(function_->browser_context()) ->GetExtension(function_->extension_id()); DCHECK(cef_extension_); } diff --git a/libcef/browser/extensions/extensions_api_client.cc b/libcef/browser/extensions/extensions_api_client.cc index a41bfc3c0..3abc7d98c 100644 --- a/libcef/browser/extensions/extensions_api_client.cc +++ b/libcef/browser/extensions/extensions_api_client.cc @@ -36,8 +36,8 @@ CefExtensionsAPIClient::CreateGuestViewManagerDelegate( // The GuestViewManager instance associated with the returned Delegate, which // will be retrieved in the future via GuestViewManager::FromBrowserContext, // will be associated with the CefBrowserContext. - return base::WrapUnique(new extensions::ExtensionsGuestViewManagerDelegate( - CefBrowserContext::GetForContext(context))); + return base::WrapUnique( + new extensions::ExtensionsGuestViewManagerDelegate(context)); } std::unique_ptr diff --git a/libcef/browser/extensions/extensions_browser_client.cc b/libcef/browser/extensions/extensions_browser_client.cc index c5073cbe7..95cc8c274 100644 --- a/libcef/browser/extensions/extensions_browser_client.cc +++ b/libcef/browser/extensions/extensions_browser_client.cc @@ -138,7 +138,10 @@ BrowserContext* CefExtensionsBrowserClient::GetOffTheRecordContext( BrowserContext* CefExtensionsBrowserClient::GetOriginalContext( BrowserContext* context) { - return CefBrowserContext::GetForContext(context); + auto cef_browser_context = CefBrowserContext::FromBrowserContext(context); + if (cef_browser_context) + return cef_browser_context->AsBrowserContext(); + return nullptr; } bool CefExtensionsBrowserClient::IsGuestSession(BrowserContext* context) const { @@ -200,7 +203,9 @@ bool CefExtensionsBrowserClient::AllowCrossRendererResourceLoad( PrefService* CefExtensionsBrowserClient::GetPrefServiceForContext( BrowserContext* context) { - return static_cast(context)->GetPrefs(); + return CefBrowserContext::FromBrowserContext(context) + ->AsProfile() + ->GetPrefs(); } void CefExtensionsBrowserClient::GetEarlyExtensionPrefsObservers( @@ -224,12 +229,12 @@ bool CefExtensionsBrowserClient::CreateBackgroundExtensionHost( content::BrowserContext* browser_context, const GURL& url, ExtensionHost** host) { - CefBrowserContext* browser_context_impl = - CefBrowserContext::GetForContext(browser_context); + auto cef_browser_context = + CefBrowserContext::FromBrowserContext(browser_context); // A CEF representation should always exist. CefRefPtr cef_extension = - browser_context_impl->extension_system()->GetExtension(extension->id()); + cef_browser_context->GetExtension(extension->id()); DCHECK(cef_extension); if (!cef_extension) { // Cancel the background host creation. diff --git a/libcef/browser/resource_context.cc b/libcef/browser/iothread_state.cc similarity index 78% rename from libcef/browser/resource_context.cc rename to libcef/browser/iothread_state.cc index 8d1b7c6ea..6cea106f8 100644 --- a/libcef/browser/resource_context.cc +++ b/libcef/browser/iothread_state.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "libcef/browser/resource_context.h" +#include "libcef/browser/iothread_state.h" #include "libcef/browser/net/scheme_handler.h" #include "libcef/browser/thread_util.h" @@ -15,36 +15,34 @@ #include "content/browser/resource_context_impl.h" #include "content/public/browser/browser_thread.h" -CefResourceContext::CefResourceContext(bool is_off_the_record) - : is_off_the_record_(is_off_the_record) { +CefIOThreadState::CefIOThreadState() { // Using base::Unretained() is safe because both this callback and possible // deletion of |this| will execute on the IO thread, and this callback will // be executed first. - CEF_POST_TASK(CEF_IOT, base::Bind(&CefResourceContext::InitOnIOThread, + CEF_POST_TASK(CEF_IOT, base::Bind(&CefIOThreadState::InitOnIOThread, base::Unretained(this))); } -CefResourceContext::~CefResourceContext() {} +CefIOThreadState::~CefIOThreadState() {} -void CefResourceContext::AddHandler( - int render_process_id, - int render_frame_id, - int frame_tree_node_id, - CefRefPtr handler) { +void CefIOThreadState::AddHandler(int render_process_id, + int render_frame_id, + int frame_tree_node_id, + CefRefPtr handler) { CEF_REQUIRE_IOT(); handler_map_.AddHandler(render_process_id, render_frame_id, frame_tree_node_id, handler); } -void CefResourceContext::RemoveHandler(int render_process_id, - int render_frame_id, - int frame_tree_node_id) { +void CefIOThreadState::RemoveHandler(int render_process_id, + int render_frame_id, + int frame_tree_node_id) { CEF_REQUIRE_IOT(); handler_map_.RemoveHandler(render_process_id, render_frame_id, frame_tree_node_id); } -CefRefPtr CefResourceContext::GetHandler( +CefRefPtr CefIOThreadState::GetHandler( int render_process_id, int render_frame_id, int frame_tree_node_id, @@ -54,7 +52,7 @@ CefRefPtr CefResourceContext::GetHandler( frame_tree_node_id, require_frame_match); } -void CefResourceContext::RegisterSchemeHandlerFactory( +void CefIOThreadState::RegisterSchemeHandlerFactory( const std::string& scheme_name, const std::string& domain_name, CefRefPtr factory) { @@ -83,7 +81,7 @@ void CefResourceContext::RegisterSchemeHandlerFactory( } } -void CefResourceContext::ClearSchemeHandlerFactories() { +void CefIOThreadState::ClearSchemeHandlerFactories() { CEF_REQUIRE_IOT(); scheme_handler_factory_map_.clear(); @@ -92,7 +90,7 @@ void CefResourceContext::ClearSchemeHandlerFactories() { scheme::RegisterInternalHandlers(this); } -CefRefPtr CefResourceContext::GetSchemeHandlerFactory( +CefRefPtr CefIOThreadState::GetSchemeHandlerFactory( const GURL& url) { CEF_REQUIRE_IOT(); @@ -123,7 +121,7 @@ CefRefPtr CefResourceContext::GetSchemeHandlerFactory( return nullptr; } -void CefResourceContext::InitOnIOThread() { +void CefIOThreadState::InitOnIOThread() { CEF_REQUIRE_IOT(); // Add the default internal handlers. diff --git a/libcef/browser/resource_context.h b/libcef/browser/iothread_state.h similarity index 62% rename from libcef/browser/resource_context.h rename to libcef/browser/iothread_state.h index e00e3b2b6..7c738697a 100644 --- a/libcef/browser/resource_context.h +++ b/libcef/browser/iothread_state.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef CEF_LIBCEF_BROWSER_RESOURCE_CONTEXT_H_ -#define CEF_LIBCEF_BROWSER_RESOURCE_CONTEXT_H_ +#ifndef CEF_LIBCEF_BROWSER_IOTHREAD_STATE_H_ +#define CEF_LIBCEF_BROWSER_IOTHREAD_STATE_H_ #pragma once #include "include/cef_request_context.h" @@ -12,20 +12,15 @@ #include "libcef/browser/request_context_handler_map.h" -#include "content/public/browser/resource_context.h" - class GURL; -// Acts as a bridge for resource loading. Life span is controlled by -// CefBrowserContext. Created on the UI thread but accessed and destroyed on the -// IO thread. Network request objects are associated with the ResourceContext -// via ProxyURLLoaderFactory. When the ResourceContext is destroyed all -// outstanding network request objects will be canceled. -// See browser_context.h for an object relationship diagram. -class CefResourceContext : public content::ResourceContext { +// Stores state that will be accessed on the IO thread. Life span is controlled +// by CefBrowserContext. Created on the UI thread but accessed and destroyed on +// the IO thread. See browser_context.h for an object relationship diagram. +class CefIOThreadState { public: - explicit CefResourceContext(bool is_off_the_record); - ~CefResourceContext() override; + CefIOThreadState(); + virtual ~CefIOThreadState(); // See comments in CefRequestContextHandlerMap. void AddHandler(int render_process_id, @@ -48,15 +43,9 @@ class CefResourceContext : public content::ResourceContext { void ClearSchemeHandlerFactories(); CefRefPtr GetSchemeHandlerFactory(const GURL& url); - // State transferred from the BrowserContext for use on the IO thread. - bool IsOffTheRecord() const { return is_off_the_record_; } - private: void InitOnIOThread(); - // Only accessed on the IO thread. - const bool is_off_the_record_; - // Map IDs to CefRequestContextHandler objects. CefRequestContextHandlerMap handler_map_; @@ -66,7 +55,7 @@ class CefResourceContext : public content::ResourceContext { SchemeHandlerFactoryMap; SchemeHandlerFactoryMap scheme_handler_factory_map_; - DISALLOW_COPY_AND_ASSIGN(CefResourceContext); + DISALLOW_COPY_AND_ASSIGN(CefIOThreadState); }; -#endif // CEF_LIBCEF_BROWSER_RESOURCE_CONTEXT_H_ +#endif // CEF_LIBCEF_BROWSER_IOTHREAD_STATE_H_ diff --git a/libcef/browser/media_router/media_router_manager.cc b/libcef/browser/media_router/media_router_manager.cc index d364751c5..c1b703f50 100644 --- a/libcef/browser/media_router/media_router_manager.cc +++ b/libcef/browser/media_router/media_router_manager.cc @@ -121,7 +121,8 @@ class CefPresentationConnection : public blink::mojom::PresentationConnection { DISALLOW_COPY_AND_ASSIGN(CefPresentationConnection); }; -CefMediaRouterManager::CefMediaRouterManager(CefBrowserContext* browser_context) +CefMediaRouterManager::CefMediaRouterManager( + content::BrowserContext* browser_context) : browser_context_(browser_context), query_result_manager_(GetMediaRouter()), weak_ptr_factory_(this) { diff --git a/libcef/browser/media_router/media_router_manager.h b/libcef/browser/media_router/media_router_manager.h index 28b8f864f..854db44ab 100644 --- a/libcef/browser/media_router/media_router_manager.h +++ b/libcef/browser/media_router/media_router_manager.h @@ -7,7 +7,6 @@ #pragma once #include "include/cef_media_router.h" -#include "libcef/browser/browser_context.h" #include "base/memory/weak_ptr.h" #include "base/observer_list.h" @@ -15,7 +14,10 @@ #include "chrome/browser/ui/media_router/query_result_manager.h" #include "chrome/common/media_router/mojom/media_router.mojom.h" -class CefBrowserContext; +namespace content { +class BrowserContext; +} + class CefMediaRoutesObserver; class CefPresentationConnection; class CefRouteMessageObserver; @@ -46,7 +48,7 @@ class CefMediaRouterManager ~Observer() override {} }; - explicit CefMediaRouterManager(CefBrowserContext* browser_context); + explicit CefMediaRouterManager(content::BrowserContext* browser_context); ~CefMediaRouterManager() override; // |observer| must outlive this object or be removed. @@ -103,7 +105,7 @@ class CefMediaRouterManager RouteState* GetRouteState(const media_router::MediaRoute::Id& route_id); void RemoveRouteState(const media_router::MediaRoute::Id& route_id); - CefBrowserContext* const browser_context_; + content::BrowserContext* const browser_context_; base::ObserverList observers_; diff --git a/libcef/browser/net/devtools_scheme_handler.cc b/libcef/browser/net/devtools_scheme_handler.cc index e16faaa28..62fa22566 100644 --- a/libcef/browser/net/devtools_scheme_handler.cc +++ b/libcef/browser/net/devtools_scheme_handler.cc @@ -6,8 +6,8 @@ #include +#include "libcef/browser/iothread_state.h" #include "libcef/browser/net/internal_scheme_handler.h" -#include "libcef/browser/resource_context.h" #include "base/memory/ptr_util.h" #include "base/strings/string_util.h" @@ -40,8 +40,8 @@ class Delegate : public InternalHandlerDelegate { } // namespace -void RegisterChromeDevToolsHandler(CefResourceContext* resource_context) { - resource_context->RegisterSchemeHandlerFactory( +void RegisterChromeDevToolsHandler(CefIOThreadState* iothread_state) { + iothread_state->RegisterSchemeHandlerFactory( content::kChromeDevToolsScheme, kChromeDevToolsHost, CreateInternalHandlerFactory(base::WrapUnique(new Delegate()))); } diff --git a/libcef/browser/net/devtools_scheme_handler.h b/libcef/browser/net/devtools_scheme_handler.h index 667724c3f..bcde8d62f 100644 --- a/libcef/browser/net/devtools_scheme_handler.h +++ b/libcef/browser/net/devtools_scheme_handler.h @@ -6,14 +6,14 @@ #define CEF_LIBCEF_BROWSER_NET_DEVTOOLS_SCHEME_HANDLER_H_ #pragma once -class CefResourceContext; +class CefIOThreadState; namespace scheme { extern const char kChromeDevToolsHost[]; // Register the chrome-devtools scheme handler. -void RegisterChromeDevToolsHandler(CefResourceContext* resource_context); +void RegisterChromeDevToolsHandler(CefIOThreadState* iothread_state); } // namespace scheme diff --git a/libcef/browser/net/scheme_handler.cc b/libcef/browser/net/scheme_handler.cc index d832a8cd9..ae742d17d 100644 --- a/libcef/browser/net/scheme_handler.cc +++ b/libcef/browser/net/scheme_handler.cc @@ -9,13 +9,17 @@ #include "libcef/browser/net/chrome_scheme_handler.h" #include "libcef/browser/net/devtools_scheme_handler.h" #include "libcef/common/net/scheme_registration.h" +#include "libcef/features/runtime.h" #include "content/public/common/url_constants.h" namespace scheme { -void RegisterInternalHandlers(CefResourceContext* resource_context) { - scheme::RegisterChromeDevToolsHandler(resource_context); +void RegisterInternalHandlers(CefIOThreadState* iothread_state) { + if (!cef::IsAlloyRuntimeEnabled()) + return; + + scheme::RegisterChromeDevToolsHandler(iothread_state); } void DidFinishLoad(CefRefPtr frame, const GURL& validated_url) { diff --git a/libcef/browser/net/scheme_handler.h b/libcef/browser/net/scheme_handler.h index 737c18750..a912ab38f 100644 --- a/libcef/browser/net/scheme_handler.h +++ b/libcef/browser/net/scheme_handler.h @@ -11,12 +11,12 @@ #include "content/public/browser/browser_context.h" #include "url/gurl.h" -class CefResourceContext; +class CefIOThreadState; namespace scheme { // Register the internal scheme handlers that can be overridden. -void RegisterInternalHandlers(CefResourceContext* resource_context); +void RegisterInternalHandlers(CefIOThreadState* iothread_state); // Used to fire any asynchronous content updates. void DidFinishLoad(CefRefPtr frame, const GURL& validated_url); diff --git a/libcef/browser/net_service/browser_urlrequest_impl.cc b/libcef/browser/net_service/browser_urlrequest_impl.cc index e3cdcb5ed..b429bbdf2 100644 --- a/libcef/browser/net_service/browser_urlrequest_impl.cc +++ b/libcef/browser/net_service/browser_urlrequest_impl.cc @@ -188,9 +188,9 @@ class CefBrowserURLRequest::Context CefRefPtr request_context_impl = CefRequestContextImpl::GetOrCreateForRequestContext(request_context); DCHECK(request_context_impl); - CefBrowserContext* browser_context = + CefBrowserContext* cef_browser_context = request_context_impl->GetBrowserContext(); - DCHECK(browser_context); + DCHECK(cef_browser_context); int render_frame_id = MSG_ROUTING_NONE; scoped_refptr loader_factory_getter; @@ -208,12 +208,12 @@ class CefBrowserURLRequest::Context // network::mojom::kBrowserProcessId (value 0) for these requests. render_frame_id = rfh->GetFrameTreeNodeId(); - loader_factory_getter = - net_service::URLLoaderFactoryGetter::Create(rfh, browser_context); + loader_factory_getter = net_service::URLLoaderFactoryGetter::Create( + rfh, cef_browser_context->AsBrowserContext()); } } else { - loader_factory_getter = - net_service::URLLoaderFactoryGetter::Create(nullptr, browser_context); + loader_factory_getter = net_service::URLLoaderFactoryGetter::Create( + nullptr, cef_browser_context->AsBrowserContext()); } task_runner->PostTask( diff --git a/libcef/browser/net_service/cookie_manager_impl.cc b/libcef/browser/net_service/cookie_manager_impl.cc index 3db265d98..5d9453737 100644 --- a/libcef/browser/net_service/cookie_manager_impl.cc +++ b/libcef/browser/net_service/cookie_manager_impl.cc @@ -30,7 +30,8 @@ CefBrowserContext* GetBrowserContext(const CefBrowserContext::Getter& getter) { // Do not keep a reference to the object returned by this method. CookieManager* GetCookieManager(CefBrowserContext* browser_context) { CEF_REQUIRE_UIT(); - return content::BrowserContext::GetDefaultStoragePartition(browser_context) + return content::BrowserContext::GetDefaultStoragePartition( + browser_context->AsBrowserContext()) ->GetCookieManagerForBrowserProcess(); } diff --git a/libcef/browser/net_service/resource_request_handler_wrapper.cc b/libcef/browser/net_service/resource_request_handler_wrapper.cc index f002662e5..e4428c892 100644 --- a/libcef/browser/net_service/resource_request_handler_wrapper.cc +++ b/libcef/browser/net_service/resource_request_handler_wrapper.cc @@ -7,11 +7,11 @@ #include "libcef/browser/browser_host_impl.h" #include "libcef/browser/browser_platform_delegate.h" #include "libcef/browser/context.h" +#include "libcef/browser/iothread_state.h" #include "libcef/browser/net_service/cookie_helper.h" #include "libcef/browser/net_service/proxy_url_loader_factory.h" #include "libcef/browser/net_service/resource_handler_wrapper.h" #include "libcef/browser/net_service/response_filter_wrapper.h" -#include "libcef/browser/resource_context.h" #include "libcef/browser/thread_util.h" #include "libcef/common/app_manager.h" #include "libcef/common/net/scheme_registration.h" @@ -19,6 +19,7 @@ #include "libcef/common/request_impl.h" #include "libcef/common/response_impl.h" +#include "chrome/browser/profiles/profile.h" #include "chrome/common/chrome_features.h" #include "components/language/core/browser/pref_names.h" #include "components/prefs/pref_service.h" @@ -262,9 +263,10 @@ class InterceptedRequestHandlerWrapper : public InterceptedRequestHandler { CEF_REQUIRE_UIT(); browser_context_ = browser_context; - resource_context_ = static_cast( - browser_context->GetResourceContext()); - DCHECK(resource_context_); + auto cef_browser_context = + CefBrowserContext::FromBrowserContext(browser_context); + iothread_state_ = cef_browser_context->iothread_state(); + DCHECK(iothread_state_); // We register to be notified of CEF context or browser destruction so // that we can stop accepting new requests and cancel pending/in-progress @@ -312,7 +314,7 @@ class InterceptedRequestHandlerWrapper : public InterceptedRequestHandler { CefRefPtr browser_; CefRefPtr frame_; - CefResourceContext* resource_context_ = nullptr; + CefIOThreadState* iothread_state_ = nullptr; int render_process_id_ = 0; int render_frame_id_ = -1; int frame_tree_node_id_ = -1; @@ -450,7 +452,7 @@ class InterceptedRequestHandlerWrapper : public InterceptedRequestHandler { GetHandler(id, request, &intercept_only, requestPtr); CefRefPtr scheme_factory = - init_state_->resource_context_->GetSchemeHandlerFactory(request->url); + init_state_->iothread_state_->GetSchemeHandlerFactory(request->url); if (scheme_factory && !requestPtr) { requestPtr = MakeRequest(request, id.hash(), true); } @@ -1055,7 +1057,7 @@ class InterceptedRequestHandlerWrapper : public InterceptedRequestHandler { if (!handler) { // Maybe the request context wants to handle it? CefRefPtr context_handler = - init_state_->resource_context_->GetHandler( + init_state_->iothread_state_->GetHandler( init_state_->render_process_id_, request->render_frame_id, init_state_->frame_tree_node_id_, false); if (context_handler) { diff --git a/libcef/browser/plugins/plugin_service_filter.cc b/libcef/browser/plugins/plugin_service_filter.cc index f4155a276..f4d380ce9 100644 --- a/libcef/browser/plugins/plugin_service_filter.cc +++ b/libcef/browser/plugins/plugin_service_filter.cc @@ -88,8 +88,8 @@ bool CefPluginServiceFilter::IsPluginAvailable( return true; } - auto browser_context = CefBrowserContext::GetForIDs( - render_process_id, render_frame_id, -1, false); + auto browser_context = + CefBrowserContext::FromIDs(render_process_id, render_frame_id, -1, false); CefRefPtr handler; if (browser_context) { handler = browser_context->GetHandler(render_process_id, render_frame_id, diff --git a/libcef/browser/prefs/browser_prefs.cc b/libcef/browser/prefs/browser_prefs.cc index 5abda531c..a21c63af7 100644 --- a/libcef/browser/prefs/browser_prefs.cc +++ b/libcef/browser/prefs/browser_prefs.cc @@ -25,6 +25,7 @@ #include "chrome/browser/plugins/plugin_info_host_impl.h" #include "chrome/browser/prefs/chrome_command_line_pref_store.h" #include "chrome/browser/printing/print_preview_sticky_settings.h" +#include "chrome/browser/profiles/profile.h" #include "chrome/browser/renderer_host/pepper/device_id_fetcher.h" #include "chrome/browser/ssl/ssl_config_service_manager.h" #include "chrome/browser/themes/theme_service.h" @@ -73,7 +74,7 @@ namespace { std::string GetAcceptLanguageList(Profile* profile) { const CefRequestContextSettings& context_settings = - static_cast(profile)->GetSettings(); + CefBrowserContext::FromBrowserContext(profile)->settings(); if (context_settings.accept_language_list.length > 0) { return CefString(&context_settings.accept_language_list); } diff --git a/libcef/browser/prefs/renderer_prefs.cc b/libcef/browser/prefs/renderer_prefs.cc index 394183736..1ea63d756 100644 --- a/libcef/browser/prefs/renderer_prefs.cc +++ b/libcef/browser/prefs/renderer_prefs.cc @@ -6,7 +6,6 @@ #include -#include "libcef/browser/browser_context.h" #include "libcef/browser/context.h" #include "libcef/browser/extensions/browser_extensions_util.h" #include "libcef/common/cef_switches.h" @@ -20,6 +19,7 @@ #include "chrome/browser/defaults.h" #include "chrome/browser/extensions/extension_webkit_preferences.h" #include "chrome/browser/font_family_cache.h" +#include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/prefs/prefs_tab_helper.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/pref_names.h" @@ -65,7 +65,7 @@ void SetDefaultPrefs(content::WebPreferences& web) { // Chrome preferences. // Should match ChromeContentBrowserClient::OverrideWebkitPrefs. -void SetChromePrefs(CefBrowserContext* profile, content::WebPreferences& web) { +void SetChromePrefs(Profile* profile, content::WebPreferences& web) { PrefService* prefs = profile->GetPrefs(); // Fill per-script font preferences. @@ -334,7 +334,7 @@ void PopulateWebPreferences(content::RenderViewHost* rvh, // Set preferences based on the context's PrefService. if (browser) { - CefBrowserContext* profile = static_cast( + auto profile = Profile::FromBrowserContext( browser->web_contents()->GetBrowserContext()); SetChromePrefs(profile, web); } diff --git a/libcef/browser/request_context_impl.cc b/libcef/browser/request_context_impl.cc index 029a41c82..03078b38b 100644 --- a/libcef/browser/request_context_impl.cc +++ b/libcef/browser/request_context_impl.cc @@ -5,10 +5,8 @@ #include "libcef/browser/request_context_impl.h" #include "libcef/browser/browser_context.h" #include "libcef/browser/context.h" -#include "libcef/browser/extensions/extension_system.h" #include "libcef/browser/thread_util.h" #include "libcef/common/app_manager.h" -#include "libcef/common/extensions/extensions_util.h" #include "libcef/common/task_runner_impl.h" #include "libcef/common/values_impl.h" #include "libcef/features/runtime_checks.h" @@ -16,6 +14,7 @@ #include "base/atomic_sequence_num.h" #include "base/logging.h" #include "base/strings/stringprintf.h" +#include "chrome/browser/profiles/profile.h" #include "components/prefs/pref_service.h" #include "content/public/browser/browser_task_traits.h" #include "content/public/browser/plugin_service.h" @@ -374,7 +373,7 @@ bool CefRequestContextImpl::HasPreference(const CefString& name) { // Make sure the browser context exists. EnsureBrowserContext(); - PrefService* pref_service = browser_context()->GetPrefs(); + PrefService* pref_service = browser_context()->AsProfile()->GetPrefs(); return (pref_service->FindPreference(name) != nullptr); } @@ -389,7 +388,7 @@ CefRefPtr CefRequestContextImpl::GetPreference( // Make sure the browser context exists. EnsureBrowserContext(); - PrefService* pref_service = browser_context()->GetPrefs(); + PrefService* pref_service = browser_context()->AsProfile()->GetPrefs(); const PrefService::Preference* pref = pref_service->FindPreference(name); if (!pref) return nullptr; @@ -407,7 +406,7 @@ CefRefPtr CefRequestContextImpl::GetAllPreferences( // Make sure the browser context exists. EnsureBrowserContext(); - PrefService* pref_service = browser_context()->GetPrefs(); + PrefService* pref_service = browser_context()->AsProfile()->GetPrefs(); std::unique_ptr values = pref_service->GetPreferenceValues(include_defaults @@ -428,7 +427,7 @@ bool CefRequestContextImpl::CanSetPreference(const CefString& name) { // Make sure the browser context exists. EnsureBrowserContext(); - PrefService* pref_service = browser_context()->GetPrefs(); + PrefService* pref_service = browser_context()->AsProfile()->GetPrefs(); const PrefService::Preference* pref = pref_service->FindPreference(name); return (pref && pref->IsUserModifiable()); } @@ -445,7 +444,7 @@ bool CefRequestContextImpl::SetPreference(const CefString& name, // Make sure the browser context exists. EnsureBrowserContext(); - PrefService* pref_service = browser_context()->GetPrefs(); + PrefService* pref_service = browser_context()->AsProfile()->GetPrefs(); // The below validation logic should match PrefService::SetUserPrefValue. @@ -531,22 +530,10 @@ void CefRequestContextImpl::LoadExtension( return; } - if (!extensions::ExtensionsEnabled()) { - if (handler) - handler->OnExtensionLoadFailed(ERR_ABORTED); - return; - } + // Make sure the browser context exists. + EnsureBrowserContext(); - if (manifest && manifest->GetSize() > 0) { - CefDictionaryValueImpl* value_impl = - static_cast(manifest.get()); - GetBrowserContext()->extension_system()->LoadExtension( - base::WrapUnique(value_impl->CopyValue()), root_directory, - false /* builtin */, this, handler); - } else { - GetBrowserContext()->extension_system()->LoadExtension( - root_directory, false /* builtin */, this, handler); - } + browser_context()->LoadExtension(root_directory, manifest, handler, this); } bool CefRequestContextImpl::DidLoadExtension(const CefString& extension_id) { @@ -568,17 +555,10 @@ bool CefRequestContextImpl::GetExtensions( return false; } - if (!extensions::ExtensionsEnabled()) - return false; + // Make sure the browser context exists. + EnsureBrowserContext(); - extensions::CefExtensionSystem::ExtensionMap extension_map = - GetBrowserContext()->extension_system()->GetExtensions(); - extensions::CefExtensionSystem::ExtensionMap::const_iterator it = - extension_map.begin(); - for (; it != extension_map.end(); ++it) - extension_ids.push_back(it->second->GetIdentifier()); - - return true; + return browser_context()->GetExtensions(extension_ids); } CefRefPtr CefRequestContextImpl::GetExtension( @@ -588,10 +568,10 @@ CefRefPtr CefRequestContextImpl::GetExtension( return nullptr; } - if (!extensions::ExtensionsEnabled()) - return nullptr; + // Make sure the browser context exists. + EnsureBrowserContext(); - return GetBrowserContext()->extension_system()->GetExtension(extension_id); + return browser_context()->GetExtension(extension_id); } CefRefPtr CefRequestContextImpl::GetMediaRouter() { @@ -648,8 +628,7 @@ void CefRequestContextImpl::Initialize() { if (config_.other) { // Share storage with |config_.other|. - browser_context_ = - CefBrowserContext::GetForContext(config_.other->GetBrowserContext()); + browser_context_ = config_.other->GetBrowserContext(); DCHECK(browser_context_); } @@ -664,7 +643,7 @@ void CefRequestContextImpl::Initialize() { if (!cache_path.empty()) { // Check if a CefBrowserContext is already globally registered for // the specified cache path. If so then use it. - browser_context_ = CefBrowserContext::GetForCachePath(cache_path); + browser_context_ = CefBrowserContext::FromCachePath(cache_path); } } @@ -673,11 +652,11 @@ void CefRequestContextImpl::Initialize() { // empty then this new instance will become the globally registered // CefBrowserContext for that path. Otherwise, this new instance will // be a completely isolated "incognito mode" context. - browser_context_ = new CefBrowserContext(config_.settings); - browser_context_->Initialize(); + browser_context_ = + CefAppManager::Get()->CreateNewBrowserContext(config_.settings); } else { // Share the same settings as the existing context. - config_.settings = browser_context_->GetSettings(); + config_.settings = browser_context_->settings(); } // We'll disassociate from |browser_context_| on destruction. @@ -728,8 +707,8 @@ void CefRequestContextImpl::PurgePluginListCacheInternal( CefBrowserContext* browser_context) { CEF_REQUIRE_UIT(); browser_context->ClearPluginLoadDecision(-1); - content::PluginService::GetInstance()->PurgePluginListCache(browser_context, - false); + content::PluginService::GetInstance()->PurgePluginListCache( + browser_context->AsBrowserContext(), false); } void CefRequestContextImpl::ClearCertificateExceptionsInternal( @@ -738,7 +717,7 @@ void CefRequestContextImpl::ClearCertificateExceptionsInternal( CEF_REQUIRE_UIT(); content::SSLHostStateDelegate* ssl_delegate = - browser_context->GetSSLHostStateDelegate(); + browser_context->AsBrowserContext()->GetSSLHostStateDelegate(); if (ssl_delegate) ssl_delegate->Clear(base::Callback()); diff --git a/libcef/common/alloy/alloy_main_delegate.cc b/libcef/common/alloy/alloy_main_delegate.cc index 4a97185e5..aaabcabaa 100644 --- a/libcef/common/alloy/alloy_main_delegate.cc +++ b/libcef/common/alloy/alloy_main_delegate.cc @@ -8,6 +8,7 @@ #include #endif +#include "libcef/browser/alloy/alloy_browser_context.h" #include "libcef/browser/alloy/alloy_content_browser_client.h" #include "libcef/common/cef_switches.h" #include "libcef/common/command_line_impl.h" @@ -723,6 +724,13 @@ CefRefPtr AlloyMainDelegate::GetGlobalRequestContext() { return browser_client_->request_context(); } +CefBrowserContext* AlloyMainDelegate::CreateNewBrowserContext( + const CefRequestContextSettings& settings) { + auto context = new AlloyBrowserContext(settings); + context->Initialize(); + return context; +} + scoped_refptr AlloyMainDelegate::GetBackgroundTaskRunner() { if (browser_client_) diff --git a/libcef/common/alloy/alloy_main_delegate.h b/libcef/common/alloy/alloy_main_delegate.h index 8cf8e1972..c621b0e0e 100644 --- a/libcef/common/alloy/alloy_main_delegate.h +++ b/libcef/common/alloy/alloy_main_delegate.h @@ -61,6 +61,8 @@ class AlloyMainDelegate : public content::ContentMainDelegate, return &content_client_; } CefRefPtr GetGlobalRequestContext() override; + CefBrowserContext* CreateNewBrowserContext( + const CefRequestContextSettings& settings) override; // CefTaskRunnerManager overrides. scoped_refptr GetBackgroundTaskRunner() diff --git a/libcef/common/app_manager.h b/libcef/common/app_manager.h index 033c4eb9d..cf005b65f 100644 --- a/libcef/common/app_manager.h +++ b/libcef/common/app_manager.h @@ -19,6 +19,7 @@ namespace blink { class WebURLLoaderFactory; } +class CefBrowserContext; struct CefSchemeInfo; // Exposes global application state in the main and render processes. @@ -47,7 +48,10 @@ class CefAppManager { // The following methods are only available in the main (browser) process. + // Called from CefRequestContextImpl. virtual CefRefPtr GetGlobalRequestContext() = 0; + virtual CefBrowserContext* CreateNewBrowserContext( + const CefRequestContextSettings& settings) = 0; #if defined(OS_WIN) // Returns the module name (usually libcef.dll). diff --git a/libcef/common/chrome/chrome_main_delegate_cef.cc b/libcef/common/chrome/chrome_main_delegate_cef.cc index ac71cc5cd..cac45b53a 100644 --- a/libcef/common/chrome/chrome_main_delegate_cef.cc +++ b/libcef/common/chrome/chrome_main_delegate_cef.cc @@ -54,6 +54,13 @@ CefRefPtr ChromeMainDelegateCef::GetGlobalRequestContext() { return nullptr; } +CefBrowserContext* ChromeMainDelegateCef::CreateNewBrowserContext( + const CefRequestContextSettings& settings) { + // TODO(chrome-runtime): Implement this method. + NOTIMPLEMENTED(); + return nullptr; +} + scoped_refptr ChromeMainDelegateCef::GetBackgroundTaskRunner() { auto browser_client = content_browser_client(); diff --git a/libcef/common/chrome/chrome_main_delegate_cef.h b/libcef/common/chrome/chrome_main_delegate_cef.h index 933669b39..8e4224adf 100644 --- a/libcef/common/chrome/chrome_main_delegate_cef.h +++ b/libcef/common/chrome/chrome_main_delegate_cef.h @@ -45,6 +45,8 @@ class ChromeMainDelegateCef : public ChromeMainDelegate, return &chrome_content_client_cef_; } CefRefPtr GetGlobalRequestContext() override; + CefBrowserContext* CreateNewBrowserContext( + const CefRequestContextSettings& settings) override; // CefTaskRunnerManager overrides. scoped_refptr GetBackgroundTaskRunner() diff --git a/patch/patches/chrome_plugins.patch b/patch/patches/chrome_plugins.patch index 75bb12741..409b5fd1c 100644 --- a/patch/patches/chrome_plugins.patch +++ b/patch/patches/chrome_plugins.patch @@ -128,36 +128,35 @@ index d49a1df73622..9a389e4383f0 100644 // If we broke out of the loop, we have found an enabled plugin. bool enabled = i < matching_plugins.size(); diff --git chrome/browser/plugins/plugin_utils.cc chrome/browser/plugins/plugin_utils.cc -index 4e64db143b8a..71322b0261a1 100644 +index 4e64db143b8a..3dc127b7b992 100644 --- chrome/browser/plugins/plugin_utils.cc +++ chrome/browser/plugins/plugin_utils.cc @@ -5,6 +5,7 @@ #include "chrome/browser/plugins/plugin_utils.h" #include "base/values.h" -+#include "cef/libcef/features/features.h" ++#include "cef/libcef/features/runtime.h" #include "chrome/browser/profiles/profile_io_data.h" #include "chrome/common/plugin_utils.h" #include "components/content_settings/core/browser/host_content_settings_map.h" -@@ -14,6 +15,11 @@ +@@ -14,6 +15,10 @@ #include "url/gurl.h" #include "url/origin.h" +#if BUILDFLAG(ENABLE_CEF) -+#include "cef/libcef/browser/resource_context.h" +#include "cef/libcef/common/extensions/extensions_util.h" +#endif + #if BUILDFLAG(ENABLE_EXTENSIONS) #include "chrome/common/pref_names.h" #include "components/prefs/pref_service.h" -@@ -182,6 +188,12 @@ base::flat_map +@@ -182,6 +187,12 @@ base::flat_map PluginUtils::GetMimeTypeToExtensionIdMap( content::BrowserContext* browser_context) { base::flat_map mime_type_to_extension_id_map; + +#if BUILDFLAG(ENABLE_CEF) -+ if (!extensions::ExtensionsEnabled()) ++ if (cef::IsAlloyRuntimeEnabled() && !extensions::ExtensionsEnabled()) + return mime_type_to_extension_id_map; +#endif + diff --git a/patch/patches/print_preview_123.patch b/patch/patches/print_preview_123.patch index d3bdfccf4..ac7c1546c 100644 --- a/patch/patches/print_preview_123.patch +++ b/patch/patches/print_preview_123.patch @@ -1,12 +1,12 @@ diff --git chrome/browser/download/download_prefs.cc chrome/browser/download/download_prefs.cc -index a996e15eeba1..76191e5ffa14 100644 +index a996e15eeba1..cbc0ad1f9019 100644 --- chrome/browser/download/download_prefs.cc +++ chrome/browser/download/download_prefs.cc @@ -23,6 +23,7 @@ #include "base/strings/sys_string_conversions.h" #include "base/strings/utf_string_conversions.h" #include "build/build_config.h" -+#include "cef/libcef/features/features.h" ++#include "cef/libcef/features/runtime.h" #include "chrome/browser/download/chrome_download_manager_delegate.h" #include "chrome/browser/download/download_core_service_factory.h" #include "chrome/browser/download/download_core_service_impl.h" @@ -15,24 +15,24 @@ index a996e15eeba1..76191e5ffa14 100644 #endif +#if BUILDFLAG(ENABLE_CEF) -+#include "cef/libcef/browser/browser_context.h" ++#include "cef/libcef/browser/alloy/alloy_browser_context.h" +#endif + using content::BrowserContext; using content::BrowserThread; using content::DownloadManager; -@@ -340,7 +345,11 @@ DownloadPrefs* DownloadPrefs::FromDownloadManager( +@@ -340,6 +345,11 @@ DownloadPrefs* DownloadPrefs::FromDownloadManager( // static DownloadPrefs* DownloadPrefs::FromBrowserContext( content::BrowserContext* context) { -+#if !BUILDFLAG(ENABLE_CEF) - return FromDownloadManager(BrowserContext::GetDownloadManager(context)); -+#else -+ return CefBrowserContext::GetForContext(context)->GetDownloadPrefs(); ++#if BUILDFLAG(ENABLE_CEF) ++ if (cef::IsAlloyRuntimeEnabled()) { ++ return static_cast(context)->GetDownloadPrefs(); ++ } +#endif + return FromDownloadManager(BrowserContext::GetDownloadManager(context)); } - bool DownloadPrefs::IsFromTrustedSource(const download::DownloadItem& item) { diff --git chrome/browser/printing/print_preview_dialog_controller.cc chrome/browser/printing/print_preview_dialog_controller.cc index 84818167bce1..cdbd3f7f6f27 100644 --- chrome/browser/printing/print_preview_dialog_controller.cc