Commit 69334e22 authored by Marshall Greenblatt's avatar Marshall Greenblatt
Browse files

Add StoragePartition and extension object proxy support (issue #1973)

parent 1c06d8e3
......@@ -379,6 +379,8 @@ static_library("libcef_static") {
"libcef/browser/ssl_host_state_delegate.h",
"libcef/browser/ssl_info_impl.cc",
"libcef/browser/ssl_info_impl.h",
"libcef/browser/storage_partition_proxy.cc",
"libcef/browser/storage_partition_proxy.h",
"libcef/browser/stream_impl.cc",
"libcef/browser/stream_impl.h",
"libcef/browser/trace_impl.cc",
......
......@@ -14,8 +14,6 @@
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "extensions/browser/extension_protocols.h"
#include "extensions/common/constants.h"
#ifndef NDEBUG
base::AtomicRefCount CefBrowserContext::DebugObjCt = 0;
......@@ -41,12 +39,17 @@ void CefBrowserContext::Initialize() {
content::BrowserContext::Initialize(this, GetPath());
const bool extensions_enabled = extensions::ExtensionsEnabled();
bool extensions_initialized = false;
if (extensions_enabled) {
// Create the custom ExtensionSystem first because other KeyedServices
// depend on it.
// The same CefExtensionSystem instance is shared by CefBrowserContextImpl
// and CefBrowserContextProxy objects.
extension_system_ = static_cast<extensions::CefExtensionSystem*>(
extensions::ExtensionSystem::Get(this));
extension_system_->InitForRegularProfile(true);
extensions_initialized = extension_system_->initialized();
if (!extensions_initialized)
extension_system_->InitForRegularProfile(true);
}
resource_context_.reset(new CefResourceContext(
......@@ -63,25 +66,10 @@ void CefBrowserContext::Initialize() {
DCHECK(pref_service);
user_prefs::UserPrefs::Set(this, pref_service);
if (extensions_enabled)
if (extensions_enabled && !extensions_initialized)
extension_system_->Init();
}
void CefBrowserContext::CreateProtocolHandlers(
content::ProtocolHandlerMap* protocol_handlers) {
if (extensions::ExtensionsEnabled()) {
// Handle only chrome-extension:// requests. CEF does not support
// chrome-extension-resource:// requests (it does not store shared extension
// data in its installation directory).
extensions::InfoMap* extension_info_map =
extension_system()->info_map();
(*protocol_handlers)[extensions::kExtensionScheme] =
linked_ptr<net::URLRequestJobFactory::ProtocolHandler>(
extensions::CreateExtensionProtocolHandler(
IsOffTheRecord(), extension_info_map).release());
}
}
void CefBrowserContext::Shutdown() {
CEF_REQUIRE_UIT();
......
......@@ -33,12 +33,22 @@
//
// BCI = CefBrowserContextImpl
// Entry point from WC when using an isolated RCI. Owns the RC and creates the
// URCGI. Life span controlled by RCI and (for the global context)
// SPI indirectly. Life span controlled by RCI and (for the global context)
// CefBrowserMainParts.
//
// BCP = CefBrowserContextProxy
// Entry point from WC when using a custom RCI. Owns the RC and creates the
// URCGP. Life span controlled by RCI.
// URCGP and SPP. Life span controlled by RCI.
//
// SPI = content::StoragePartitionImpl
// Owns storage-related objects like Quota, IndexedDB, Cache, etc. Created by
// StoragePartitionImplMap::Get(). Provides access to the URCGI. Life span is
// controlled indirectly by BCI.
//
// SPP = CefStoragePartitionProxy
// Forwards requests for storage-related objects to SPI. Created by
// GetStoragePartitionFromConfig() calling BCI::GetStoragePartitionProxy().
// Provides access to the URCGP. Life span is controlled by BCP.
//
// RC = CefResourceContext
// Acts as a bridge for resource loading. URLRequest life span is tied to this
......@@ -46,11 +56,14 @@
// controlled by BCI/BCP.
//
// URCGI = CefURLRequestContextGetterImpl
// Creates and owns the URCI. Life span is controlled by RC and (for the
// global context) CefBrowserMainParts.
// Creates and owns the URCI. Created by StoragePartitionImplMap::Get()
// calling BCI::CreateRequestContext(). Life span is controlled by RC and (for
// the global context) CefBrowserMainParts, and SPI.
//
// URCGP = CefURLRequestContextGetterProxy
// Creates and owns the URCP. Life span is controlled by RC.
// Creates and owns the URCP. Created by GetStoragePartitionFromConfig()
// calling BCI::GetStoragePartitionProxy(). Life span is controlled by RC and
// SPP.
//
// URCI = CefURLRequestContextImpl
// Owns various network-related objects including the isolated cookie manager.
......@@ -73,15 +86,15 @@
// own = ownership (scoped_ptr)
// ptr = raw pointer
//
// CefBrowserMainParts isolated cookie manager, etc...
// | | ^
// ref ref ref/own
// v v |
// /---> BCI -ref-> URCGI --own-> URCI <-ptr-- CSP
// / ^ ^ ^
// ptr ref ref /
// / | | /
// BHI -own-> WC -ptr-> BCP -ref-> URCGP -own-> URCP --ref-/
// CefBrowserMainParts----\ isolated cookie manager, etc.
// | \ ^
// ref ref ref/own
// v v |
// /---> BCI -own-> SPI -ref-> URCGI --own-> URCI <-ptr-- CSP
// / ^ ^ ^ ^
// ptr ref ptr ref /
// / | | | /
// BHI -own-> WC -ptr-> BCP -own-> SPP -ref-> URCGP -own-> URCP --ref-/
//
// BHI -ref-> RCI -ref-> BCI/BCP -own-> RC -ref-> URCGI/URCGP
//
......@@ -166,8 +179,6 @@ class CefBrowserContext
protected:
~CefBrowserContext() override;
void CreateProtocolHandlers(content::ProtocolHandlerMap* protocol_handlers);
// Must be called before the child object destructor has completed.
void Shutdown();
......
......@@ -10,6 +10,7 @@
#include "libcef/browser/browser_context_proxy.h"
#include "libcef/browser/context.h"
#include "libcef/browser/download_manager_delegate.h"
#include "libcef/browser/extensions/extension_system.h"
#include "libcef/browser/permissions/permission_manager.h"
#include "libcef/browser/prefs/browser_prefs.h"
#include "libcef/browser/ssl_host_state_delegate.h"
......@@ -35,6 +36,8 @@
#include "content/public/browser/download_manager.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "extensions/browser/extension_protocols.h"
#include "extensions/common/constants.h"
#include "net/proxy/proxy_config_service.h"
using content::BrowserThread;
......@@ -201,6 +204,9 @@ CefBrowserContextImpl::CefBrowserContextImpl(
}
CefBrowserContextImpl::~CefBrowserContextImpl() {
// Unregister the context first to avoid re-entrancy during shutdown.
g_manager.Get().RemoveImpl(this, cache_path_);
Shutdown();
// The FontFamilyCache references the ProxyService so delete it before the
......@@ -218,8 +224,6 @@ CefBrowserContextImpl::~CefBrowserContextImpl() {
// when it's accessed from the content::BrowserContext destructor.
if (download_manager_delegate_)
download_manager_delegate_.reset(NULL);
g_manager.Get().RemoveImpl(this, cache_path_);
}
void CefBrowserContextImpl::Initialize() {
......@@ -390,22 +394,6 @@ content::BackgroundSyncController*
return nullptr;
}
PrefService* CefBrowserContextImpl::GetPrefs() {
return pref_service_.get();
}
const PrefService* CefBrowserContextImpl::GetPrefs() const {
return pref_service_.get();
}
const CefRequestContextSettings& CefBrowserContextImpl::GetSettings() const {
return settings_;
}
CefRefPtr<CefRequestContextHandler> CefBrowserContextImpl::GetHandler() const {
return NULL;
}
net::URLRequestContextGetter* CefBrowserContextImpl::CreateRequestContext(
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) {
......@@ -417,7 +405,17 @@ net::URLRequestContextGetter* CefBrowserContextImpl::CreateRequestContext(
ProxyServiceFactory::CreateProxyConfigService(
pref_proxy_config_tracker_.get()));
CreateProtocolHandlers(protocol_handlers);
if (extensions::ExtensionsEnabled()) {
// Handle only chrome-extension:// requests. CEF does not support
// chrome-extension-resource:// requests (it does not store shared extension
// data in its installation directory).
extensions::InfoMap* extension_info_map =
extension_system()->info_map();
(*protocol_handlers)[extensions::kExtensionScheme] =
linked_ptr<net::URLRequestJobFactory::ProtocolHandler>(
extensions::CreateExtensionProtocolHandler(
IsOffTheRecord(), extension_info_map).release());
}
url_request_getter_ = new CefURLRequestContextGetterImpl(
settings_,
......@@ -437,6 +435,30 @@ net::URLRequestContextGetter*
bool in_memory,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) {
return nullptr;
}
content::StoragePartition* CefBrowserContextImpl::GetStoragePartitionProxy(
content::BrowserContext* browser_context,
content::StoragePartition* partition_impl) {
CefBrowserContextProxy* proxy =
static_cast<CefBrowserContextProxy*>(browser_context);
return proxy->GetOrCreateStoragePartitionProxy(partition_impl);
}
PrefService* CefBrowserContextImpl::GetPrefs() {
return pref_service_.get();
}
const PrefService* CefBrowserContextImpl::GetPrefs() const {
return pref_service_.get();
}
const CefRequestContextSettings& CefBrowserContextImpl::GetSettings() const {
return settings_;
}
CefRefPtr<CefRequestContextHandler> CefBrowserContextImpl::GetHandler() const {
return NULL;
}
......
......@@ -65,14 +65,6 @@ class CefBrowserContextImpl : public CefBrowserContext,
content::SSLHostStateDelegate* GetSSLHostStateDelegate() override;
content::PermissionManager* GetPermissionManager() override;
content::BackgroundSyncController* GetBackgroundSyncController() override;
// Profile methods.
PrefService* GetPrefs() override;
const PrefService* GetPrefs() const override;
// CefBrowserContext methods.
const CefRequestContextSettings& GetSettings() const override;
CefRefPtr<CefRequestContextHandler> GetHandler() const override;
net::URLRequestContextGetter* CreateRequestContext(
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors)
......@@ -83,6 +75,17 @@ class CefBrowserContextImpl : public CefBrowserContext,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors)
override;
content::StoragePartition* GetStoragePartitionProxy(
content::BrowserContext* browser_context,
content::StoragePartition* partition_impl) override;
// Profile methods.
PrefService* GetPrefs() override;
const PrefService* GetPrefs() const override;
// CefBrowserContext methods.
const CefRequestContextSettings& GetSettings() const override;
CefRefPtr<CefRequestContextHandler> GetHandler() const override;
HostContentSettingsMap* GetHostContentSettingsMap() override;
void AddVisitedURLs(const std::vector<GURL>& urls) override;
......
......@@ -7,22 +7,33 @@
#include "libcef/browser/content_browser_client.h"
#include "libcef/browser/download_manager_delegate.h"
#include "libcef/browser/net/url_request_context_getter_proxy.h"
#include "libcef/browser/storage_partition_proxy.h"
#include "libcef/browser/thread_util.h"
#include "base/logging.h"
#include "chrome/browser/font_family_cache.h"
#include "components/guest_view/common/guest_view_constants.h"
#include "components/visitedlink/browser/visitedlink_master.h"
#include "content/browser/blob_storage/chrome_blob_storage_context.h"
#include "content/browser/resource_context_impl.h"
#include "content/browser/streams/stream_context.h"
#include "content/public/browser/storage_partition.h"
namespace {
bool ShouldProxyUserData(const void* key) {
// If this value is not proxied the blob data fails to load for the PDF
// If this value is not proxied then multiple StoragePartitionImpl objects
// will be created and filesystem API access will fail, among other things.
if (key == content::BrowserContext::GetStoragePartitionMapUserDataKey())
return true;
// If these values are not proxied then blob data fails to load for the PDF
// extension.
if (key == content::StreamContext::GetUserDataKey())
// See also the call to InitializeResourceContext().
if (key == content::ChromeBlobStorageContext::GetUserDataKey() ||
key == content::StreamContext::GetUserDataKey()) {
return true;
}
// If this value is not proxied then CefBrowserContextImpl::GetGuestManager()
// returns NULL.
......@@ -129,6 +140,24 @@ content::BackgroundSyncController*
return parent_->GetBackgroundSyncController();
}
net::URLRequestContextGetter* CefBrowserContextProxy::CreateRequestContext(
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) {
// CefBrowserContextImpl::GetOrCreateStoragePartitionProxy is called instead
// of this method.
NOTREACHED();
return nullptr;
}
net::URLRequestContextGetter*
CefBrowserContextProxy::CreateRequestContextForStoragePartition(
const base::FilePath& partition_path,
bool in_memory,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) {
return nullptr;
}
PrefService* CefBrowserContextProxy::GetPrefs() {
return parent_->GetPrefs();
}
......@@ -145,29 +174,6 @@ CefRefPtr<CefRequestContextHandler> CefBrowserContextProxy::GetHandler() const {
return handler_;
}
net::URLRequestContextGetter* CefBrowserContextProxy::CreateRequestContext(
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) {
CEF_REQUIRE_UIT();
DCHECK(!url_request_getter_.get());
CreateProtocolHandlers(protocol_handlers);
url_request_getter_ =
new CefURLRequestContextGetterProxy(handler_, parent_->request_context());
resource_context()->set_url_request_context_getter(url_request_getter_.get());
return url_request_getter_.get();
}
net::URLRequestContextGetter*
CefBrowserContextProxy::CreateRequestContextForStoragePartition(
const base::FilePath& partition_path,
bool in_memory,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) {
return NULL;
}
HostContentSettingsMap* CefBrowserContextProxy::GetHostContentSettingsMap() {
return parent_->GetHostContentSettingsMap();
}
......@@ -175,3 +181,29 @@ HostContentSettingsMap* CefBrowserContextProxy::GetHostContentSettingsMap() {
void CefBrowserContextProxy::AddVisitedURLs(const std::vector<GURL>& urls) {
parent_->AddVisitedURLs(urls);
}
content::StoragePartition*
CefBrowserContextProxy::GetOrCreateStoragePartitionProxy(
content::StoragePartition* partition_impl) {
CEF_REQUIRE_UIT();
if (!storage_partition_proxy_) {
scoped_refptr<CefURLRequestContextGetterProxy> url_request_getter =
new CefURLRequestContextGetterProxy(handler_,
parent_->request_context());
resource_context()->set_url_request_context_getter(
url_request_getter.get());
storage_partition_proxy_.reset(
new CefStoragePartitionProxy(partition_impl,
url_request_getter.get()));
// Associates UserData keys with the ResourceContext.
// Called from StoragePartitionImplMap::Get() for CefBrowserContextImpl.
content::InitializeResourceContext(this);
}
// There should only be one CefStoragePartitionProxy for this
// CefBrowserContextProxy.
DCHECK_EQ(storage_partition_proxy_->parent(), partition_impl);
return storage_partition_proxy_.get();
}
......@@ -13,7 +13,7 @@
#include "base/memory/ref_counted.h"
class CefDownloadManagerDelegate;
class CefURLRequestContextGetterProxy;
class CefStoragePartitionProxy;
// BrowserContext implementation for a particular CefRequestContext. Life span
// is controlled by CefRequestContextImpl. Only accessed on the UI thread. See
......@@ -40,14 +40,6 @@ class CefBrowserContextProxy : public CefBrowserContext {
content::SSLHostStateDelegate* GetSSLHostStateDelegate() override;
content::PermissionManager* GetPermissionManager() override;
content::BackgroundSyncController* GetBackgroundSyncController() override;
// Profile methods.
PrefService* GetPrefs() override;
const PrefService* GetPrefs() const override;
// CefBrowserContext methods.
const CefRequestContextSettings& GetSettings() const override;
CefRefPtr<CefRequestContextHandler> GetHandler() const override;
net::URLRequestContextGetter* CreateRequestContext(
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors)
......@@ -58,9 +50,20 @@ class CefBrowserContextProxy : public CefBrowserContext {
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors)
override;
// Profile methods.
PrefService* GetPrefs() override;
const PrefService* GetPrefs() const override;
// CefBrowserContext methods.
const CefRequestContextSettings& GetSettings() const override;
CefRefPtr<CefRequestContextHandler> GetHandler() const override;
HostContentSettingsMap* GetHostContentSettingsMap() override;
void AddVisitedURLs(const std::vector<GURL>& urls) override;
content::StoragePartition* GetOrCreateStoragePartitionProxy(
content::StoragePartition* partition_impl);
scoped_refptr<CefBrowserContextImpl> parent() const {
return parent_;
}
......@@ -78,7 +81,7 @@ class CefBrowserContextProxy : public CefBrowserContext {
scoped_refptr<CefBrowserContextImpl> parent_;
std::unique_ptr<CefDownloadManagerDelegate> download_manager_delegate_;
scoped_refptr<CefURLRequestContextGetterProxy> url_request_getter_;
std::unique_ptr<CefStoragePartitionProxy> storage_partition_proxy_;
DISALLOW_COPY_AND_ASSIGN(CefBrowserContextProxy);
};
......
......@@ -80,6 +80,7 @@ base::DictionaryValue* ParseManifest(
CefExtensionSystem::CefExtensionSystem(BrowserContext* browser_context)
: browser_context_(browser_context),
initialized_(false),
registry_(ExtensionRegistry::Get(browser_context)),
weak_ptr_factory_(this) {
}
......@@ -88,6 +89,8 @@ CefExtensionSystem::~CefExtensionSystem() {
}
void CefExtensionSystem::Init() {
DCHECK(!initialized_);
// There's complexity here related to the ordering of message delivery. For
// an extension to load correctly both the ExtensionMsg_Loaded and
// ExtensionMsg_ActivateExtension messages must be sent. These messages are
......@@ -141,6 +144,8 @@ void CefExtensionSystem::Init() {
AddExtension(pdf_extension_util::GetManifest(),
base::FilePath(FILE_PATH_LITERAL("pdf")));
}
initialized_ = true;
}
// Implementation based on ComponentLoader::Add.
......@@ -173,6 +178,7 @@ void CefExtensionSystem::Shutdown() {
}
void CefExtensionSystem::InitForRegularProfile(bool extensions_enabled) {
DCHECK(!initialized_);
service_worker_manager_.reset(new ServiceWorkerManager(browser_context_));
runtime_data_.reset(new RuntimeData(registry_));
quota_service_.reset(new QuotaService);
......
......@@ -78,6 +78,8 @@ class CefExtensionSystem : public ExtensionSystem {
void InstallUpdate(const std::string& extension_id,
const base::FilePath& temp_dir) override;
bool initialized() const { return initialized_; }
private:
// Information about a registered component extension.
struct ComponentExtensionInfo {
......@@ -120,6 +122,8 @@ class CefExtensionSystem : public ExtensionSystem {
content::BrowserContext* browser_context_; // Not owned.
bool initialized_;
// Data to be accessed on the IO thread. Must outlive process_manager_.
scoped_refptr<InfoMap> info_map_;
......
......@@ -6,6 +6,7 @@
#include "libcef/browser/extensions/extension_system.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "extensions/browser/extension_prefs_factory.h"
#include "extensions/browser/extension_registry_factory.h"
......@@ -45,7 +46,7 @@ KeyedService* CefExtensionSystemFactory::BuildServiceInstanceFor(
BrowserContext* CefExtensionSystemFactory::GetBrowserContextToUse(
BrowserContext* context) const {
// Use a separate instance for incognito.
return context;
return chrome::GetBrowserContextOwnInstanceInIncognito(context);
}
bool CefExtensionSystemFactory::ServiceIsCreatedWithBrowserContext() const {
......
......@@ -18,6 +18,7 @@
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/chrome_url_request_util.h"
#include "chrome/browser/extensions/event_router_forwarder.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
......@@ -61,7 +62,10 @@ bool CefExtensionsBrowserClient::IsValidContext(BrowserContext* context) {
bool CefExtensionsBrowserClient::IsSameContext(BrowserContext* first,
BrowserContext* second) {
return first == second;
// Returns true if |first| and |second| share the same underlying
// CefBrowserContextImpl.
return CefBrowserContextImpl::GetForContext(first).get() ==
CefBrowserContextImpl::GetForContext(second).get();
}
bool CefExtensionsBrowserClient::HasOffTheRecordContext(
......@@ -77,7 +81,7 @@ BrowserContext* CefExtensionsBrowserClient::GetOffTheRecordContext(
BrowserContext* CefExtensionsBrowserClient::GetOriginalContext(
BrowserContext* context) {
return context;
return chrome::GetBrowserContextRedirectedInIncognito(context);
}
bool CefExtensionsBrowserClient::IsGuestSession(
......
......@@ -74,7 +74,7 @@ std::unique_ptr<net::ClientCertStore>
}
void CefResourceContext::set_url_request_context_getter(
scoped_refptr<CefURLRequestContextGetter> getter) {
CefURLRequestContextGetter* getter) {
DCHECK(!getter_.get());
getter_ = getter;
}
......@@ -35,8 +35,7 @@ class CefResourceContext : public content::ResourceContext {
std::unique_ptr<net::ClientCertStore> CreateClientCertStore();
void set_url_request_context_getter(
scoped_refptr<CefURLRequestContextGetter> getter);
void set_url_request_context_getter(CefURLRequestContextGetter* getter);
// State transferred from the BrowserContext for use on the IO thread.
bool IsOffTheRecord() const { return is_off_the_record_; }
......
// Copyright (c) 2016 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/storage_partition_proxy.h"
CefStoragePartitionProxy::CefStoragePartitionProxy(
content::StoragePartition* parent,
CefURLRequestContextGetterProxy* url_request_context)
: parent_(parent),
url_request_context_(url_request_context) {
}
base::FilePath CefStoragePartitionProxy::GetPath() {
return parent_->GetPath();
}
net::URLRequestContextGetter* CefStoragePartitionProxy::GetURLRequestContext() {
return url_request_context_.get();
}
net::URLRequestContextGetter*
CefStoragePartitionProxy::GetMediaURLRequestContext() {
return GetURLRequestContext();
}
storage::QuotaManager* CefStoragePartitionProxy::GetQuotaManager() {
return parent_->GetQuotaManager();
}
content::AppCacheService* CefStoragePartitionProxy::GetAppCacheService() {
return parent_->GetAppCacheService();
}
storage::FileSystemContext* CefStoragePartitionProxy::GetFileSystemContext() {
return parent_->GetFileSystemContext();
}
storage::DatabaseTracker* CefStoragePartitionProxy::GetDatabaseTracker() {
return parent_->GetDatabaseTracker();
}
content::DOMStorageContext* CefStoragePartitionProxy::GetDOMStorageContext() {
return parent_->GetDOMStorageContext();
}
content::IndexedDBContext* CefStoragePartitionProxy::GetIndexedDBContext() {
return parent_->GetIndexedDBContext();
}
content::ServiceWorkerContext*