diff --git a/include/internal/cef_types.h b/include/internal/cef_types.h index 37b2e99eb..595d0fbb2 100644 --- a/include/internal/cef_types.h +++ b/include/internal/cef_types.h @@ -242,7 +242,9 @@ typedef struct _cef_settings_t { // in-memory caches are used for storage and no data is persisted to disk. // HTML5 databases such as localStorage will only persist across sessions if a // cache path is specified. Can be overridden for individual CefRequestContext - // instances via the CefRequestContextSettings.cache_path value. + // instances via the CefRequestContextSettings.cache_path value. When using + // the Chrome runtime the "default" profile will be used if |cache_path| and + // |root_cache_path| have the same value. /// cef_string_t cache_path; @@ -264,7 +266,8 @@ typedef struct _cef_settings_t { // directory on Linux, "~/Library/Application Support/CEF/User Data" directory // on Mac OS X, "Local Settings\Application Data\CEF\User Data" directory // under the user profile directory on Windows). If this value is non-empty - // then it must be an absolute path. + // then it must be an absolute path. When using the Chrome runtime this value + // will be ignored in favor of the |root_cache_path| value. /// cef_string_t user_data_path; diff --git a/libcef/browser/browser_context.cc b/libcef/browser/browser_context.cc index c585fa660..956edf3a6 100644 --- a/libcef/browser/browser_context.cc +++ b/libcef/browser/browser_context.cc @@ -12,11 +12,13 @@ #include "libcef/browser/request_context_impl.h" #include "libcef/browser/thread_util.h" #include "libcef/common/cef_switches.h" +#include "libcef/features/runtime.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/profiles/profile.h" #include "content/public/browser/browser_context.h" #include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_thread.h" @@ -229,6 +231,25 @@ CefBrowserContext* CefBrowserContext::FromBrowserContext( return g_manager.Get().GetImplFromBrowserContext(context); } +// static +CefBrowserContext* CefBrowserContext::FromProfile(const Profile* profile) { + auto* cef_context = FromBrowserContext(profile); + if (cef_context) + return cef_context; + + if (cef::IsChromeRuntimeEnabled()) { + auto* original_profile = profile->GetOriginalProfile(); + if (original_profile != profile) { + // With the Chrome runtime if the user launches an incognito window via + // the UI we might be associated with the original Profile instead of the + // (current) incognito profile. + return FromBrowserContext(original_profile); + } + } + + return nullptr; +} + // static std::vector CefBrowserContext::GetAll() { return g_manager.Get().GetAllImpl(); diff --git a/libcef/browser/browser_context.h b/libcef/browser/browser_context.h index 2e8673f55..7c8e7d0a7 100644 --- a/libcef/browser/browser_context.h +++ b/libcef/browser/browser_context.h @@ -103,6 +103,7 @@ class CefBrowserContext { // Returns the underlying CefBrowserContext if any. static CefBrowserContext* FromBrowserContext( const content::BrowserContext* context); + static CefBrowserContext* FromProfile(const Profile* profile); // Returns all existing CefBrowserContext. static std::vector GetAll(); diff --git a/libcef/browser/chrome/chrome_browser_context.cc b/libcef/browser/chrome/chrome_browser_context.cc index e8ae49d89..01fd852eb 100644 --- a/libcef/browser/chrome/chrome_browser_context.cc +++ b/libcef/browser/chrome/chrome_browser_context.cc @@ -6,11 +6,11 @@ #include "libcef/browser/prefs/browser_prefs.h" -#include "chrome/browser/profiles/profile_manager.h" +#include "chrome/browser/browser_process.h" ChromeBrowserContext::ChromeBrowserContext( const CefRequestContextSettings& settings) - : CefBrowserContext(settings) {} + : CefBrowserContext(settings), weak_ptr_factory_(this) {} ChromeBrowserContext::~ChromeBrowserContext() = default; @@ -22,19 +22,73 @@ Profile* ChromeBrowserContext::AsProfile() { return profile_; } -void ChromeBrowserContext::Initialize() { +void ChromeBrowserContext::InitializeAsync(base::OnceClosure initialized_cb) { + initialized_cb_ = std::move(initialized_cb); + CefBrowserContext::Initialize(); - // TODO(chrome-runtime): ProfileManager can create new profiles relative to - // the user-data-dir, but it should be done asynchronously. - // The global ProfileManager instance can be retrieved via - // |g_browser_process->profile_manager()|. - profile_ = ProfileManager::GetLastUsedProfileAllowedByPolicy(); + if (!cache_path_.empty()) { + auto* profile_manager = g_browser_process->profile_manager(); + const auto& user_data_dir = profile_manager->user_data_dir(); - browser_prefs::SetLanguagePrefs(profile_); + if (cache_path_ == user_data_dir) { + // Use the default disk-based profile. + ProfileCreated(profile_manager->GetActiveUserProfile(), + Profile::CreateStatus::CREATE_STATUS_INITIALIZED); + return; + } else if (cache_path_.DirName() == user_data_dir) { + // Create or load a specific disk-based profile. May continue + // synchronously or asynchronously. + profile_manager->CreateProfileAsync( + cache_path_, + base::Bind(&ChromeBrowserContext::ProfileCreated, + weak_ptr_factory_.GetWeakPtr()), + /*name=*/base::string16(), /*icon_url=*/std::string()); + return; + } else { + // All profile directories must be relative to |user_data_dir|. + LOG(ERROR) << "Cannot create profile at path " + << cache_path_.AsUTF8Unsafe(); + } + } + + // Default to creating a new/unique OffTheRecord profile. + ProfileCreated(nullptr, Profile::CreateStatus::CREATE_STATUS_CANCELED); } void ChromeBrowserContext::Shutdown() { CefBrowserContext::Shutdown(); + // |g_browser_process| may be nullptr during shutdown. + if (should_destroy_ && g_browser_process) { + g_browser_process->profile_manager() + ->GetActiveUserProfile() + ->DestroyOffTheRecordProfile(profile_); + } profile_ = nullptr; } + +void ChromeBrowserContext::ProfileCreated(Profile* profile, + Profile::CreateStatus status) { + if (status != Profile::CreateStatus::CREATE_STATUS_CREATED && + status != Profile::CreateStatus::CREATE_STATUS_INITIALIZED) { + DCHECK(!profile); + + // Creation of a disk-based profile failed for some reason. Create a + // new/unique OffTheRecord profile instead. + const auto& profile_id = Profile::OTRProfileID::CreateUniqueForCEF(); + profile = g_browser_process->profile_manager() + ->GetActiveUserProfile() + ->GetOffTheRecordProfile(profile_id); + status = Profile::CreateStatus::CREATE_STATUS_INITIALIZED; + should_destroy_ = true; + } + + if (status == Profile::CreateStatus::CREATE_STATUS_INITIALIZED) { + DCHECK(profile); + DCHECK(!profile_); + profile_ = profile; + browser_prefs::SetLanguagePrefs(profile_); + + std::move(initialized_cb_).Run(); + } +} diff --git a/libcef/browser/chrome/chrome_browser_context.h b/libcef/browser/chrome/chrome_browser_context.h index 54816a838..db8c3a726 100644 --- a/libcef/browser/chrome/chrome_browser_context.h +++ b/libcef/browser/chrome/chrome_browser_context.h @@ -8,22 +8,32 @@ #include "libcef/browser/browser_context.h" +#include "base/memory/weak_ptr.h" +#include "chrome/browser/profiles/profile_manager.h" + // See CefBrowserContext documentation for usage. Only accessed on the UI thread // unless otherwise indicated. class ChromeBrowserContext : public CefBrowserContext { public: explicit ChromeBrowserContext(const CefRequestContextSettings& settings); + void InitializeAsync(base::OnceClosure initialized_cb); + // CefBrowserContext overrides. content::BrowserContext* AsBrowserContext() override; Profile* AsProfile() override; - void Initialize() override; void Shutdown() override; private: ~ChromeBrowserContext() override; + void ProfileCreated(Profile* profile, Profile::CreateStatus status); + + base::OnceClosure initialized_cb_; Profile* profile_ = nullptr; + bool should_destroy_ = false; + + base::WeakPtrFactory weak_ptr_factory_; DISALLOW_COPY_AND_ASSIGN(ChromeBrowserContext); }; diff --git a/libcef/browser/chrome/chrome_content_browser_client_cef.cc b/libcef/browser/chrome/chrome_content_browser_client_cef.cc index 5a52fbb7c..b334fa1b8 100644 --- a/libcef/browser/chrome/chrome_content_browser_client_cef.cc +++ b/libcef/browser/chrome/chrome_content_browser_client_cef.cc @@ -175,7 +175,7 @@ bool ChromeContentBrowserClientCef::WillCreateURLLoaderFactory( // For example, the User Manager profile created via // profiles::CreateSystemProfileForUserManager. auto profile = Profile::FromBrowserContext(browser_context); - if (!CefBrowserContext::FromBrowserContext(profile->GetOriginalProfile())) + if (!CefBrowserContext::FromProfile(profile)) return false; auto request_handler = net_service::CreateInterceptedRequestHandler( diff --git a/libcef/browser/net_service/resource_request_handler_wrapper.cc b/libcef/browser/net_service/resource_request_handler_wrapper.cc index 2a7717101..2dd430e02 100644 --- a/libcef/browser/net_service/resource_request_handler_wrapper.cc +++ b/libcef/browser/net_service/resource_request_handler_wrapper.cc @@ -262,8 +262,7 @@ class InterceptedRequestHandlerWrapper : public InterceptedRequestHandler { browser_context_ = browser_context; auto profile = Profile::FromBrowserContext(browser_context); - auto cef_browser_context = - CefBrowserContext::FromBrowserContext(profile->GetOriginalProfile()); + auto cef_browser_context = CefBrowserContext::FromProfile(profile); iothread_state_ = cef_browser_context->iothread_state(); DCHECK(iothread_state_); cookieable_schemes_ = cef_browser_context->GetCookieableSchemes(); diff --git a/libcef/browser/request_context_impl.cc b/libcef/browser/request_context_impl.cc index 630b5bd25..21ca16d2f 100644 --- a/libcef/browser/request_context_impl.cc +++ b/libcef/browser/request_context_impl.cc @@ -612,7 +612,19 @@ CefRequestContextImpl::GetOrCreateRequestContext(const Config& config) { } // The new context will be initialized later by EnsureBrowserContext(). - return new CefRequestContextImpl(config); + CefRefPtr context = new CefRequestContextImpl(config); + + if (config.handler) { + // Keep the context alive until OnRequestContextInitialized is called. + if (CEF_CURRENTLY_ON_UIT()) { + context->Initialize(); + } else { + CEF_POST_TASK( + CEF_UIT, base::BindOnce(&CefRequestContextImpl::Initialize, context)); + } + } + + return context; } CefRequestContextImpl::CefRequestContextImpl( @@ -645,16 +657,20 @@ void CefRequestContextImpl::Initialize() { } } + auto initialized_cb = + base::BindOnce(&CefRequestContextImpl::BrowserContextInitialized, this); + if (!browser_context_) { // Create a new CefBrowserContext instance. If the cache path is non- // 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_ = - CefAppManager::Get()->CreateNewBrowserContext(config_.settings); + browser_context_ = CefAppManager::Get()->CreateNewBrowserContext( + config_.settings, std::move(initialized_cb)); } else { // Share the same settings as the existing context. config_.settings = browser_context_->settings(); + std::move(initialized_cb).Run(); } // We'll disassociate from |browser_context_| on destruction. @@ -666,9 +682,16 @@ void CefRequestContextImpl::Initialize() { // IsSharedWith(). config_.other = nullptr; } +} - if (config_.handler) - config_.handler->OnRequestContextInitialized(this); +void CefRequestContextImpl::BrowserContextInitialized() { + if (config_.handler) { + // Always execute asynchronously so the current call stack can unwind. + CEF_POST_TASK( + CEF_UIT, + base::BindOnce(&CefRequestContextHandler::OnRequestContextInitialized, + config_.handler, CefRefPtr(this))); + } } void CefRequestContextImpl::EnsureBrowserContext() { diff --git a/libcef/browser/request_context_impl.h b/libcef/browser/request_context_impl.h index 0144932f5..ed1f84966 100644 --- a/libcef/browser/request_context_impl.h +++ b/libcef/browser/request_context_impl.h @@ -128,6 +128,7 @@ class CefRequestContextImpl : public CefRequestContext { explicit CefRequestContextImpl(const Config& config); void Initialize(); + void BrowserContextInitialized(); // Make sure the browser context exists. Only called on the UI thread. void EnsureBrowserContext(); diff --git a/libcef/common/alloy/alloy_main_delegate.cc b/libcef/common/alloy/alloy_main_delegate.cc index 11ef91a9e..b9882fb56 100644 --- a/libcef/common/alloy/alloy_main_delegate.cc +++ b/libcef/common/alloy/alloy_main_delegate.cc @@ -443,9 +443,11 @@ CefRefPtr AlloyMainDelegate::GetGlobalRequestContext() { } CefBrowserContext* AlloyMainDelegate::CreateNewBrowserContext( - const CefRequestContextSettings& settings) { + const CefRequestContextSettings& settings, + base::OnceClosure initialized_cb) { auto context = new AlloyBrowserContext(settings); context->Initialize(); + std::move(initialized_cb).Run(); return context; } diff --git a/libcef/common/alloy/alloy_main_delegate.h b/libcef/common/alloy/alloy_main_delegate.h index c621b0e0e..424957e81 100644 --- a/libcef/common/alloy/alloy_main_delegate.h +++ b/libcef/common/alloy/alloy_main_delegate.h @@ -62,7 +62,8 @@ class AlloyMainDelegate : public content::ContentMainDelegate, } CefRefPtr GetGlobalRequestContext() override; CefBrowserContext* CreateNewBrowserContext( - const CefRequestContextSettings& settings) override; + const CefRequestContextSettings& settings, + base::OnceClosure initialized_cb) override; // CefTaskRunnerManager overrides. scoped_refptr GetBackgroundTaskRunner() diff --git a/libcef/common/app_manager.h b/libcef/common/app_manager.h index ffa8aaa13..d78c39516 100644 --- a/libcef/common/app_manager.h +++ b/libcef/common/app_manager.h @@ -11,6 +11,7 @@ #include "include/cef_app.h" #include "include/cef_request_context.h" +#include "base/callback.h" #include "base/macros.h" #include "build/build_config.h" #include "content/public/common/content_client.h" @@ -44,10 +45,12 @@ class CefAppManager { // The following methods are only available in the main (browser) process. - // Called from CefRequestContextImpl. + // Called from CefRequestContextImpl. |initialized_cb| may be executed + // synchronously or asynchronously. virtual CefRefPtr GetGlobalRequestContext() = 0; virtual CefBrowserContext* CreateNewBrowserContext( - const CefRequestContextSettings& settings) = 0; + const CefRequestContextSettings& settings, + base::OnceClosure initialized_cb) = 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 90ec92471..3a5f9039f 100644 --- a/libcef/common/chrome/chrome_main_delegate_cef.cc +++ b/libcef/common/chrome/chrome_main_delegate_cef.cc @@ -15,6 +15,7 @@ #include "base/command_line.h" #include "base/lazy_instance.h" +#include "chrome/common/chrome_switches.h" #include "content/public/common/content_switches.h" #include "sandbox/policy/switches.h" @@ -194,9 +195,10 @@ CefRefPtr ChromeMainDelegateCef::GetGlobalRequestContext() { } CefBrowserContext* ChromeMainDelegateCef::CreateNewBrowserContext( - const CefRequestContextSettings& settings) { + const CefRequestContextSettings& settings, + base::OnceClosure initialized_cb) { auto context = new ChromeBrowserContext(settings); - context->Initialize(); + context->InitializeAsync(std::move(initialized_cb)); return context; } diff --git a/libcef/common/chrome/chrome_main_delegate_cef.h b/libcef/common/chrome/chrome_main_delegate_cef.h index b8a9781e8..410d45d86 100644 --- a/libcef/common/chrome/chrome_main_delegate_cef.h +++ b/libcef/common/chrome/chrome_main_delegate_cef.h @@ -54,7 +54,8 @@ class ChromeMainDelegateCef : public ChromeMainDelegate, } CefRefPtr GetGlobalRequestContext() override; CefBrowserContext* CreateNewBrowserContext( - const CefRequestContextSettings& settings) override; + const CefRequestContextSettings& settings, + base::OnceClosure initialized_cb) override; // CefTaskRunnerManager overrides. scoped_refptr GetBackgroundTaskRunner() diff --git a/libcef/common/resource_util.cc b/libcef/common/resource_util.cc index 77936b344..7e8e49f38 100644 --- a/libcef/common/resource_util.cc +++ b/libcef/common/resource_util.cc @@ -8,6 +8,8 @@ #include #endif +#include "libcef/features/runtime.h" + #include "base/command_line.h" #include "base/files/file_path.h" #include "base/files/file_util.h" @@ -80,8 +82,21 @@ bool GetDefaultUserDataDirectory(base::FilePath* result) { base::FilePath GetUserDataPath(CefSettings* settings, const base::CommandLine* command_line) { // |settings| will be non-nullptr in the main process only. - if (settings && settings->user_data_path.length > 0) - return base::FilePath(CefString(&settings->user_data_path)); + if (settings) { + // With the Chrome runtime Profile paths must always be relative to the + // user data directory, so defaulting to |root_cache_path| first is + // appropriate. + CefString user_data_path; + if (cef::IsChromeRuntimeEnabled() && settings->root_cache_path.length > 0) { + user_data_path = CefString(&settings->root_cache_path); + } + if (user_data_path.empty() && settings->user_data_path.length > 0) { + user_data_path = CefString(&settings->user_data_path); + } + if (!user_data_path.empty()) { + return base::FilePath(user_data_path); + } + } // This may be set for sub-processes. base::FilePath result = diff --git a/patch/patch.cfg b/patch/patch.cfg index d80af1b66..cc04727d1 100644 --- a/patch/patch.cfg +++ b/patch/patch.cfg @@ -232,6 +232,9 @@ patches = [ # # Don't create IdentityManager in RendererUpdater. # https://bitbucket.org/chromiumembedded/cef/issues/1917 + # + # chrome: Support CEF incognito Profiles that allow Browser creation. + # https://bitbucket.org/chromiumembedded/cef/issues/2969 'name': 'chrome_browser_profiles', }, { diff --git a/patch/patches/chrome_browser_profiles.patch b/patch/patches/chrome_browser_profiles.patch index 555dae365..2d024cbde 100644 --- a/patch/patches/chrome_browser_profiles.patch +++ b/patch/patches/chrome_browser_profiles.patch @@ -1,3 +1,50 @@ +diff --git chrome/browser/profiles/profile.cc chrome/browser/profiles/profile.cc +index 8e8a7abdcf17..89c47d18d379 100644 +--- chrome/browser/profiles/profile.cc ++++ chrome/browser/profiles/profile.cc +@@ -77,6 +77,7 @@ base::LazyInstance>::Leaky + + namespace { + ++const char kCEFOTRProfileIDPrefix[] = "CEF::BrowserContext"; + const char kDevToolsOTRProfileIDPrefix[] = "Devtools::BrowserContext"; + const char kMediaRouterOTRProfileIDPrefix[] = "MediaRouter::Presentation"; + +@@ -90,6 +91,8 @@ bool Profile::OTRProfileID::AllowsBrowserWindows() const { + // DevTools::BrowserContext and MediaRouter::Presentation are an + // exception to this ban. + return *this == PrimaryID() || ++ base::StartsWith(profile_id_, kCEFOTRProfileIDPrefix, ++ base::CompareCase::SENSITIVE) || + base::StartsWith(profile_id_, kDevToolsOTRProfileIDPrefix, + base::CompareCase::SENSITIVE) || + base::StartsWith(profile_id_, kMediaRouterOTRProfileIDPrefix, +@@ -111,6 +114,11 @@ Profile::OTRProfileID Profile::OTRProfileID::CreateUnique( + first_unused_index_++)); + } + ++// static ++Profile::OTRProfileID Profile::OTRProfileID::CreateUniqueForCEF() { ++ return CreateUnique(kCEFOTRProfileIDPrefix); ++} ++ + // static + Profile::OTRProfileID Profile::OTRProfileID::CreateUniqueForDevTools() { + return CreateUnique(kDevToolsOTRProfileIDPrefix); +diff --git chrome/browser/profiles/profile.h chrome/browser/profiles/profile.h +index e77f4b15ce32..13569302c96b 100644 +--- chrome/browser/profiles/profile.h ++++ chrome/browser/profiles/profile.h +@@ -116,6 +116,9 @@ class Profile : public content::BrowserContext { + // Creates a unique OTR profile id with the given profile id prefix. + static OTRProfileID CreateUnique(const std::string& profile_id_prefix); + ++ // Creates a unique OTR profile id to be used for CEF browser contexts. ++ static OTRProfileID CreateUniqueForCEF(); ++ + // Creates a unique OTR profile id to be used for DevTools browser contexts. + static OTRProfileID CreateUniqueForDevTools(); + diff --git chrome/browser/profiles/profile_manager.cc chrome/browser/profiles/profile_manager.cc index 7037c1375d9c..2b1c7911e597 100644 --- chrome/browser/profiles/profile_manager.cc diff --git a/tests/ceftests/extensions/extension_test_handler.cc b/tests/ceftests/extensions/extension_test_handler.cc index b47c230de..a1234036e 100644 --- a/tests/ceftests/extensions/extension_test_handler.cc +++ b/tests/ceftests/extensions/extension_test_handler.cc @@ -26,9 +26,8 @@ ExtensionTestHandler::ExtensionTestHandler( ExtensionTestHandler::~ExtensionTestHandler() { if (!request_context_temp_dir_.IsEmpty()) { - // Delete temporary directories on shutdown. - CefTestSuite::GetInstance()->RegisterTempDirectory( - request_context_temp_dir_.Take()); + // Temporary directory will be deleted on shutdown. + request_context_temp_dir_.Take(); } } @@ -65,7 +64,8 @@ void ExtensionTestHandler::RunTest() { if (request_context_on_disk()) { // Create a new temporary directory. - EXPECT_TRUE(request_context_temp_dir_.CreateUniqueTempDir()); + EXPECT_TRUE(request_context_temp_dir_.CreateUniqueTempDirUnderPath( + CefTestSuite::GetInstance()->root_cache_path())); CefString(&settings.cache_path) = request_context_temp_dir_.GetPath(); } diff --git a/tests/ceftests/request_context_unittest.cc b/tests/ceftests/request_context_unittest.cc index 5058ea9f5..c62841d85 100644 --- a/tests/ceftests/request_context_unittest.cc +++ b/tests/ceftests/request_context_unittest.cc @@ -136,7 +136,8 @@ TEST(RequestContextTest, BasicCreateSharedGlobal) { TEST(RequestContextTest, BasicCreateSharedOnDisk) { CefScopedTempDir tempdir; - EXPECT_TRUE(tempdir.CreateUniqueTempDir()); + EXPECT_TRUE(tempdir.CreateUniqueTempDirUnderPath( + CefTestSuite::GetInstance()->root_cache_path())); CefRequestContextSettings settings; CefString(&settings.cache_path) = tempdir.GetPath(); diff --git a/tests/ceftests/test_suite.cc b/tests/ceftests/test_suite.cc index 46546ed7b..66cc63dc3 100644 --- a/tests/ceftests/test_suite.cc +++ b/tests/ceftests/test_suite.cc @@ -5,6 +5,7 @@ #include "tests/ceftests/test_suite.h" #include "include/cef_file_util.h" +#include "include/wrapper/cef_scoped_temp_dir.h" #include "tests/gtest/include/gtest/gtest.h" #include "tests/shared/common/client_switches.h" @@ -92,6 +93,18 @@ CefTestSuite::CefTestSuite(int argc, char** argv) #else command_line_->InitFromArgv(argc, argv); #endif + + if (!command_line_->HasSwitch("type")) { + // Initialize in the main process only. + root_cache_path_ = + command_line_->GetSwitchValue(client::switches::kCachePath); + if (root_cache_path_.empty()) { + CefScopedTempDir temp_dir; + CHECK(temp_dir.CreateUniqueTempDir()); + root_cache_path_ = temp_dir.Take(); + RegisterTempDirectory(root_cache_path_); + } + } } CefTestSuite::~CefTestSuite() { @@ -133,8 +146,9 @@ void CefTestSuite::GetSettings(CefSettings& settings) const { command_line_->HasSwitch(client::switches::kExternalMessagePump); } - CefString(&settings.cache_path) = - command_line_->GetSwitchValue(client::switches::kCachePath); + CefString(&settings.cache_path) = root_cache_path_; + CefString(&settings.root_cache_path) = root_cache_path_; + CefString(&settings.user_data_path) = root_cache_path_; // Always expose the V8 gc() function to give tests finer-grained control over // memory management. @@ -156,17 +170,6 @@ void CefTestSuite::GetSettings(CefSettings& settings) const { CefString(&settings.accept_language_list) = CEF_SETTINGS_ACCEPT_LANGUAGE; } -// static -bool CefTestSuite::GetCachePath(std::string& path) const { - if (command_line_->HasSwitch(client::switches::kCachePath)) { - // Set the cache_path value. - path = command_line_->GetSwitchValue(client::switches::kCachePath); - return true; - } - - return false; -} - void CefTestSuite::RegisterTempDirectory(const CefString& directory) { base::AutoLock lock_scope(temp_directories_lock_); temp_directories_.push_back(directory); diff --git a/tests/ceftests/test_suite.h b/tests/ceftests/test_suite.h index 6eaf7bd93..ccae2d90b 100644 --- a/tests/ceftests/test_suite.h +++ b/tests/ceftests/test_suite.h @@ -25,7 +25,6 @@ class CefTestSuite { int Run(); void GetSettings(CefSettings& settings) const; - bool GetCachePath(std::string& path) const; // Register a temp directory that should be deleted on shutdown. void RegisterTempDirectory(const CefString& directory); @@ -34,6 +33,7 @@ class CefTestSuite { void DeleteTempDirectories(); CefRefPtr command_line() const { return command_line_; } + CefString root_cache_path() const { return root_cache_path_; } // The return value from Run(). int retval() const { return retval_; } @@ -50,6 +50,8 @@ class CefTestSuite { std::vector temp_directories_; base::Lock temp_directories_lock_; + CefString root_cache_path_; + int retval_; }; diff --git a/tests/ceftests/test_util.h b/tests/ceftests/test_util.h index c70185961..201e1fe4a 100644 --- a/tests/ceftests/test_util.h +++ b/tests/ceftests/test_util.h @@ -99,7 +99,8 @@ CefRefPtr CreateTestRequestContext( CefScopedTempDir scoped_temp_dir; \ std::string cache_path; \ if (with_cache_path) { \ - EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDir()); \ + EXPECT_TRUE(scoped_temp_dir.CreateUniqueTempDirUnderPath( \ + CefTestSuite::GetInstance()->root_cache_path())); \ cache_path = scoped_temp_dir.GetPath(); \ } \ CefRefPtr handler = \ @@ -107,8 +108,7 @@ CefRefPtr CreateTestRequestContext( handler->ExecuteTest(); \ ReleaseAndWaitForDestructor(handler); \ if (!scoped_temp_dir.IsEmpty()) { \ - CefTestSuite::GetInstance()->RegisterTempDirectory( \ - scoped_temp_dir.Take()); \ + scoped_temp_dir.Take(); \ } \ } diff --git a/tests/ceftests/urlrequest_unittest.cc b/tests/ceftests/urlrequest_unittest.cc index 6c8ff8a9c..c06f80676 100644 --- a/tests/ceftests/urlrequest_unittest.cc +++ b/tests/ceftests/urlrequest_unittest.cc @@ -47,6 +47,11 @@ class URLRequestBrowserTest : public client::ClientAppBrowser::Delegate { CefRefPtr command_line) override { // Delegate auth callbacks to GetAuthCredentials with the chrome runtime. command_line->AppendSwitch("disable-chrome-login-prompt"); + + // Disable component extensions that require creation of a background + // WebContents because they slow down test runs. + command_line->AppendSwitch( + "disable-component-extensions-with-background-pages"); } private: @@ -2843,7 +2848,7 @@ class RequestTestHandler : public TestHandler { void RunTest() override { // Time out the test after a reasonable period of time. - SetTestTimeout(); + SetTestTimeout(IsChromeRuntimeEnabled() ? 10000 : 5000); // Start pre-setup actions. PreSetupStart(); @@ -2858,7 +2863,8 @@ class RequestTestHandler : public TestHandler { EXPECT_TRUE(CefCurrentlyOn(TID_FILE_USER_VISIBLE)); if (context_mode_ == CONTEXT_ONDISK) { - EXPECT_TRUE(context_tmpdir_.CreateUniqueTempDir()); + EXPECT_TRUE(context_tmpdir_.CreateUniqueTempDirUnderPath( + CefTestSuite::GetInstance()->root_cache_path())); context_tmpdir_path_ = context_tmpdir_.GetPath(); EXPECT_FALSE(context_tmpdir_path_.empty()); } @@ -2894,26 +2900,29 @@ class RequestTestHandler : public TestHandler { CefString(&settings.cache_path) = context_tmpdir_path_; } - // Create a new temporary request context. - CefRefPtr request_context = - CefRequestContext::CreateContext(settings, - new RequestContextHandler(this)); - EXPECT_TRUE(request_context.get()); - test_runner_->SetRequestContext(request_context); + // Create a new temporary request context. Calls OnContextInitialized. + CefRequestContext::CreateContext(settings, + new RequestContextHandler(this)); + } + } - if (!test_server_backend_) { - // Set the schemes that are allowed to store cookies. - std::vector supported_schemes; - supported_schemes.push_back(GetRequestScheme(false)); + void OnContextInitialized(CefRefPtr request_context) { + EXPECT_TRUE(CefCurrentlyOn(TID_UI)); + EXPECT_TRUE(request_context.get()); + test_runner_->SetRequestContext(request_context); - // Continue the test once supported schemes has been set. - request_context->GetCookieManager(nullptr)->SetSupportedSchemes( - supported_schemes, true, - new TestCompletionCallback( - base::Bind(&RequestTestHandler::PreSetupComplete, this))); - } else { - PreSetupComplete(); - } + if (!test_server_backend_) { + // Set the schemes that are allowed to store cookies. + std::vector supported_schemes; + supported_schemes.push_back(GetRequestScheme(false)); + + // Continue the test once supported schemes has been set. + request_context->GetCookieManager(nullptr)->SetSupportedSchemes( + supported_schemes, true, + new TestCompletionCallback( + base::Bind(&RequestTestHandler::PreSetupComplete, this))); + } else { + PreSetupComplete(); } } @@ -3182,9 +3191,8 @@ class RequestTestHandler : public TestHandler { got_on_test_complete_.yes(); if (!context_tmpdir_.IsEmpty()) { - // Delete the temp directory on application shutdown. - CefTestSuite::GetInstance()->RegisterTempDirectory( - context_tmpdir_.Take()); + // Temp directory will be deleted on application shutdown. + context_tmpdir_.Take(); } TestComplete(); @@ -3199,6 +3207,11 @@ class RequestTestHandler : public TestHandler { : test_handler_(test_handler) {} ~RequestContextHandler() override { test_handler_->OnTestComplete(); } + void OnRequestContextInitialized( + CefRefPtr request_context) override { + test_handler_->OnContextInitialized(request_context); + } + private: CefRefPtr test_handler_;