diff options
-rw-r--r-- | extensions/shell/BUILD.gn | 10 | ||||
-rw-r--r-- | extensions/shell/app_shell.gyp | 6 | ||||
-rw-r--r-- | extensions/shell/browser/shell_browser_context.h | 1 | ||||
-rw-r--r-- | extensions/shell/browser/shell_browser_main_parts.cc | 20 | ||||
-rw-r--r-- | extensions/shell/browser/shell_browser_main_parts.h | 6 | ||||
-rw-r--r-- | extensions/shell/browser/shell_extensions_browser_client.cc | 33 | ||||
-rw-r--r-- | extensions/shell/browser/shell_extensions_browser_client.h | 10 | ||||
-rw-r--r-- | extensions/shell/browser/shell_prefs.cc | 51 | ||||
-rw-r--r-- | extensions/shell/browser/shell_prefs.h | 32 | ||||
-rw-r--r-- | extensions/shell/browser/shell_prefs_unittest.cc | 59 | ||||
-rw-r--r-- | extensions/test/data/shell_prefs/user_prefs.json | 7 |
11 files changed, 191 insertions, 44 deletions
diff --git a/extensions/shell/BUILD.gn b/extensions/shell/BUILD.gn index 068d95a..db2979e 100644 --- a/extensions/shell/BUILD.gn +++ b/extensions/shell/BUILD.gn @@ -19,15 +19,15 @@ grit("resources") { } source_set("app_shell_lib") { - # This library is testonly because it depends on testonly libarries, - # namely //base:prefs_test_support and //content/shell:content_shell_lib - # TODO(jamescook): investigate and get rid of test dependencies. + # TODO(jamescook): investigate and get rid of test dependencies. This library + # is testonly because it depends on testonly libraries, namely + # //content/shell:content_shell_lib. See http://crbug.com/438283 testonly = true deps = [ ":resources", ":version_header", "//base", - "//base:prefs_test_support", + "//base:prefs", "//components/omaha_client", "//components/pref_registry", "//components/user_prefs", @@ -108,6 +108,8 @@ source_set("app_shell_lib") { "browser/shell_oauth2_token_service.h", "browser/shell_omaha_query_params_delegate.cc", "browser/shell_omaha_query_params_delegate.h", + "browser/shell_prefs.cc", + "browser/shell_prefs.h", "browser/shell_runtime_api_delegate.cc", "browser/shell_runtime_api_delegate.h", "browser/shell_special_storage_policy.cc", diff --git a/extensions/shell/app_shell.gyp b/extensions/shell/app_shell.gyp index 4383832..f2c7565 100644 --- a/extensions/shell/app_shell.gyp +++ b/extensions/shell/app_shell.gyp @@ -13,7 +13,7 @@ 'dependencies': [ 'app_shell_version_header', '<(DEPTH)/base/base.gyp:base', - '<(DEPTH)/base/base.gyp:base_prefs_test_support', + '<(DEPTH)/base/base.gyp:base_prefs', '<(DEPTH)/components/components.gyp:omaha_client', '<(DEPTH)/components/components.gyp:pref_registry', '<(DEPTH)/components/components.gyp:user_prefs', @@ -95,6 +95,8 @@ 'browser/shell_oauth2_token_service.h', 'browser/shell_omaha_query_params_delegate.cc', 'browser/shell_omaha_query_params_delegate.h', + 'browser/shell_prefs.cc', + 'browser/shell_prefs.h', 'browser/shell_runtime_api_delegate.cc', 'browser/shell_runtime_api_delegate.h', 'browser/shell_special_storage_policy.cc', @@ -251,6 +253,7 @@ 'dependencies': [ 'app_shell_lib', '<(DEPTH)/base/base.gyp:base', + '<(DEPTH)/base/base.gyp:base_prefs_test_support', '<(DEPTH)/base/base.gyp:test_support_base', '<(DEPTH)/content/content.gyp:content_app_both', '<(DEPTH)/content/content_shell_and_tests.gyp:test_support_content', @@ -265,6 +268,7 @@ 'browser/shell_audio_controller_chromeos_unittest.cc', 'browser/shell_native_app_window_aura_unittest.cc', 'browser/shell_oauth2_token_service_unittest.cc', + 'browser/shell_prefs_unittest.cc', 'common/shell_content_client_unittest.cc' ], 'conditions': [ diff --git a/extensions/shell/browser/shell_browser_context.h b/extensions/shell/browser/shell_browser_context.h index 1bc4db6..08c72be 100644 --- a/extensions/shell/browser/shell_browser_context.h +++ b/extensions/shell/browser/shell_browser_context.h @@ -37,6 +37,7 @@ class ShellBrowserContext : public content::ShellBrowserContext { private: void InitURLRequestContextOnIOThread(); + net::NetLog* net_log_; scoped_refptr<storage::SpecialStoragePolicy> storage_policy_; diff --git a/extensions/shell/browser/shell_browser_main_parts.cc b/extensions/shell/browser/shell_browser_main_parts.cc index 7af36498..fe60355 100644 --- a/extensions/shell/browser/shell_browser_main_parts.cc +++ b/extensions/shell/browser/shell_browser_main_parts.cc @@ -4,7 +4,10 @@ #include "extensions/shell/browser/shell_browser_main_parts.h" +#include <string> + #include "base/command_line.h" +#include "base/prefs/pref_service.h" #include "base/run_loop.h" #include "components/keyed_service/content/browser_context_dependency_manager.h" #include "components/omaha_client/omaha_query_params.h" @@ -31,6 +34,7 @@ #include "extensions/shell/browser/shell_extensions_browser_client.h" #include "extensions/shell/browser/shell_oauth2_token_service.h" #include "extensions/shell/browser/shell_omaha_query_params_delegate.h" +#include "extensions/shell/browser/shell_prefs.h" #include "extensions/shell/common/shell_extensions_client.h" #include "extensions/shell/common/switches.h" #include "ui/base/ime/input_method_initializer.h" @@ -128,6 +132,7 @@ int ShellBrowserMainParts::PreCreateThreads() { void ShellBrowserMainParts::PreMainMessageLoopRun() { // Initialize our "profile" equivalent. browser_context_.reset(new ShellBrowserContext(net_log_.get())); + pref_service_ = ShellPrefs::CreatePrefService(browser_context_.get()); #if defined(USE_AURA) aura::Env::GetInstance()->set_context_factory(content::GetContextFactory()); @@ -146,8 +151,8 @@ void ShellBrowserMainParts::PreMainMessageLoopRun() { extensions_client_.reset(CreateExtensionsClient()); ExtensionsClient::Set(extensions_client_.get()); - extensions_browser_client_.reset( - CreateExtensionsBrowserClient(browser_context_.get())); + extensions_browser_client_.reset(CreateExtensionsBrowserClient( + browser_context_.get(), pref_service_.get())); ExtensionsBrowserClient::Set(extensions_browser_client_.get()); omaha_query_params_delegate_.reset(new ShellOmahaQueryParamsDelegate); @@ -221,6 +226,7 @@ bool ShellBrowserMainParts::MainMessageLoopRun(int* result_code) { } void ShellBrowserMainParts::PostMainMessageLoopRun() { + // NOTE: Please destroy objects in the reverse order of their creation. browser_main_delegate_->Shutdown(); devtools_http_handler_.reset(); @@ -235,11 +241,14 @@ void ShellBrowserMainParts::PostMainMessageLoopRun() { extension_system_ = NULL; ExtensionsBrowserClient::Set(NULL); extensions_browser_client_.reset(); - browser_context_.reset(); desktop_controller_.reset(); storage_monitor::StorageMonitor::Destroy(); + + pref_service_->CommitPendingWrite(); + pref_service_.reset(); + browser_context_.reset(); } void ShellBrowserMainParts::PostDestroyThreads() { @@ -257,8 +266,9 @@ ExtensionsClient* ShellBrowserMainParts::CreateExtensionsClient() { } ExtensionsBrowserClient* ShellBrowserMainParts::CreateExtensionsBrowserClient( - content::BrowserContext* context) { - return new ShellExtensionsBrowserClient(context); + content::BrowserContext* context, + PrefService* service) { + return new ShellExtensionsBrowserClient(context, service); } void ShellBrowserMainParts::CreateExtensionSystem() { diff --git a/extensions/shell/browser/shell_browser_main_parts.h b/extensions/shell/browser/shell_browser_main_parts.h index 29032b1..edc8810 100644 --- a/extensions/shell/browser/shell_browser_main_parts.h +++ b/extensions/shell/browser/shell_browser_main_parts.h @@ -13,6 +13,8 @@ #include "content/public/common/main_function_params.h" #include "ui/aura/window_tree_host_observer.h" +class PrefService; + namespace content { class BrowserContext; class DevToolsHttpHandler; @@ -71,7 +73,8 @@ class ShellBrowserMainParts : public content::BrowserMainParts { // This class takes ownership of the returned objects. virtual ExtensionsClient* CreateExtensionsClient(); virtual ExtensionsBrowserClient* CreateExtensionsBrowserClient( - content::BrowserContext* context); + content::BrowserContext* context, + PrefService* service); private: // Creates and initializes the ExtensionSystem. @@ -83,6 +86,7 @@ class ShellBrowserMainParts : public content::BrowserMainParts { #endif scoped_ptr<DesktopController> desktop_controller_; scoped_ptr<ShellBrowserContext> browser_context_; + scoped_ptr<PrefService> pref_service_; scoped_ptr<ShellDeviceClient> device_client_; scoped_ptr<AppWindowClient> app_window_client_; scoped_ptr<ExtensionsClient> extensions_client_; diff --git a/extensions/shell/browser/shell_extensions_browser_client.cc b/extensions/shell/browser/shell_extensions_browser_client.cc index 6cbaa14..e4006f1 100644 --- a/extensions/shell/browser/shell_extensions_browser_client.cc +++ b/extensions/shell/browser/shell_extensions_browser_client.cc @@ -4,18 +4,12 @@ #include "extensions/shell/browser/shell_extensions_browser_client.h" -#include "base/prefs/pref_service.h" -#include "base/prefs/pref_service_factory.h" -#include "base/prefs/testing_pref_store.h" -#include "components/pref_registry/pref_registry_syncable.h" -#include "components/user_prefs/user_prefs.h" #include "content/public/browser/browser_thread.h" #include "extensions/browser/api/extensions_api_client.h" #include "extensions/browser/api/generated_api_registration.h" #include "extensions/browser/app_sorting.h" #include "extensions/browser/event_router.h" #include "extensions/browser/extension_function_registry.h" -#include "extensions/browser/extension_prefs.h" #include "extensions/browser/null_app_sorting.h" #include "extensions/browser/updater/null_extension_cache.h" #include "extensions/browser/url_request_util.h" @@ -28,33 +22,14 @@ using content::BrowserContext; using content::BrowserThread; namespace extensions { -namespace { - -// See chrome::RegisterProfilePrefs() in chrome/browser/prefs/browser_prefs.cc -void RegisterPrefs(user_prefs::PrefRegistrySyncable* registry) { - ExtensionPrefs::RegisterProfilePrefs(registry); -} - -} // namespace ShellExtensionsBrowserClient::ShellExtensionsBrowserClient( - BrowserContext* context) + BrowserContext* context, + PrefService* pref_service) : browser_context_(context), + pref_service_(pref_service), api_client_(new ExtensionsAPIClient), extension_cache_(new NullExtensionCache()) { - // Set up the preferences service. - base::PrefServiceFactory factory; - factory.set_user_prefs(new TestingPrefStore); - factory.set_extension_prefs(new TestingPrefStore); - // app_shell should not require syncable preferences, but for now we need to - // recycle some of the RegisterProfilePrefs() code in Chrome. - // TODO(jamescook): Convert this to PrefRegistrySimple. - user_prefs::PrefRegistrySyncable* pref_registry = - new user_prefs::PrefRegistrySyncable; - // Prefs should be registered before the PrefService is created. - RegisterPrefs(pref_registry); - prefs_ = factory.Create(pref_registry).Pass(); - user_prefs::UserPrefs::Set(browser_context_, prefs_.get()); } ShellExtensionsBrowserClient::~ShellExtensionsBrowserClient() { @@ -139,7 +114,7 @@ bool ShellExtensionsBrowserClient::AllowCrossRendererResourceLoad( PrefService* ShellExtensionsBrowserClient::GetPrefServiceForContext( BrowserContext* context) { - return prefs_.get(); + return pref_service_; } void ShellExtensionsBrowserClient::GetEarlyExtensionPrefsObservers( diff --git a/extensions/shell/browser/shell_extensions_browser_client.h b/extensions/shell/browser/shell_extensions_browser_client.h index 208e350..b5f95eb 100644 --- a/extensions/shell/browser/shell_extensions_browser_client.h +++ b/extensions/shell/browser/shell_extensions_browser_client.h @@ -19,7 +19,9 @@ class ExtensionsAPIClient; class ShellExtensionsBrowserClient : public ExtensionsBrowserClient { public: // |context| is the single BrowserContext used for IsValidContext() below. - explicit ShellExtensionsBrowserClient(content::BrowserContext* context); + // |pref_service| is used for GetPrefServiceForContext() below. + ShellExtensionsBrowserClient(content::BrowserContext* context, + PrefService* pref_service); ~ShellExtensionsBrowserClient() override; // ExtensionsBrowserClient overrides: @@ -82,12 +84,12 @@ class ShellExtensionsBrowserClient : public ExtensionsBrowserClient { // The single BrowserContext for app_shell. Not owned. content::BrowserContext* browser_context_; + // The PrefService for |browser_context_|. Not owned. + PrefService* pref_service_; + // Support for extension APIs. scoped_ptr<ExtensionsAPIClient> api_client_; - // The PrefService for |browser_context_|. - scoped_ptr<PrefService> prefs_; - // The extension cache used for download and installation. scoped_ptr<ExtensionCache> extension_cache_; diff --git a/extensions/shell/browser/shell_prefs.cc b/extensions/shell/browser/shell_prefs.cc new file mode 100644 index 0000000..68d89ad --- /dev/null +++ b/extensions/shell/browser/shell_prefs.cc @@ -0,0 +1,51 @@ +// Copyright 2014 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 "extensions/shell/browser/shell_prefs.h" + +#include "base/prefs/json_pref_store.h" +#include "base/prefs/pref_filter.h" +#include "base/prefs/pref_service.h" +#include "base/prefs/pref_service_factory.h" +#include "components/pref_registry/pref_registry_syncable.h" +#include "components/user_prefs/user_prefs.h" +#include "content/public/browser/browser_context.h" +#include "content/public/browser/browser_thread.h" +#include "extensions/browser/extension_prefs.h" + +using user_prefs::PrefRegistrySyncable; + +namespace extensions { + +// static +scoped_ptr<PrefService> ShellPrefs::CreatePrefService( + content::BrowserContext* browser_context) { + base::PrefServiceFactory factory; + + base::FilePath filename = + browser_context->GetPath().AppendASCII("user_prefs.json"); + scoped_refptr<base::SequencedTaskRunner> task_runner = + JsonPrefStore::GetTaskRunnerForFile( + filename, content::BrowserThread::GetBlockingPool()); + scoped_refptr<JsonPrefStore> user_prefs = + new JsonPrefStore(filename, task_runner, scoped_ptr<PrefFilter>()); + user_prefs->ReadPrefs(); // Synchronous. + factory.set_user_prefs(user_prefs); + + // TODO(jamescook): If we want to support prefs that are set by extensions + // via ChromeSettings properties (e.g. chrome.accessibilityFeatures or + // chrome.proxy) then this should create an ExtensionPrefStore and attach it + // with PrefServiceFactory::set_extension_prefs(). + // See https://developer.chrome.com/extensions/types#ChromeSetting + + // Prefs should be registered before the PrefService is created. + PrefRegistrySyncable* pref_registry = new PrefRegistrySyncable; + ExtensionPrefs::RegisterProfilePrefs(pref_registry); + + scoped_ptr<PrefService> pref_service = factory.Create(pref_registry); + user_prefs::UserPrefs::Set(browser_context, pref_service.get()); + return pref_service; +} + +} // namespace extensions diff --git a/extensions/shell/browser/shell_prefs.h b/extensions/shell/browser/shell_prefs.h new file mode 100644 index 0000000..c81a017 --- /dev/null +++ b/extensions/shell/browser/shell_prefs.h @@ -0,0 +1,32 @@ +// Copyright 2014 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 EXTENSIONS_SHELL_BROWSER_SHELL_PREFS_H_ +#define EXTENSIONS_SHELL_BROWSER_SHELL_PREFS_H_ + +#include "base/macros.h" +#include "base/memory/scoped_ptr.h" + +class PrefService; + +namespace content { +class BrowserContext; +} + +namespace extensions { + +// Support for PrefService initialization and management. +class ShellPrefs { + public: + // Creates a pref service that loads user prefs. + static scoped_ptr<PrefService> CreatePrefService( + content::BrowserContext* browser_context); + + private: + DISALLOW_IMPLICIT_CONSTRUCTORS(ShellPrefs); +}; + +} // namespace extensions + +#endif // EXTENSIONS_SHELL_BROWSER_SHELL_PREFS_H_ diff --git a/extensions/shell/browser/shell_prefs_unittest.cc b/extensions/shell/browser/shell_prefs_unittest.cc new file mode 100644 index 0000000..6e5da97 --- /dev/null +++ b/extensions/shell/browser/shell_prefs_unittest.cc @@ -0,0 +1,59 @@ +// Copyright 2014 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 "extensions/shell/browser/shell_prefs.h" + +#include "base/path_service.h" +#include "base/prefs/pref_service.h" +#include "components/user_prefs/user_prefs.h" +#include "content/public/test/test_browser_context.h" +#include "content/public/test/test_browser_thread_bundle.h" +#include "extensions/common/extension_paths.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +// A BrowserContext that uses a test data directory as its data path. +class PrefsTestBrowserContext : public content::TestBrowserContext { + public: + PrefsTestBrowserContext() {} + ~PrefsTestBrowserContext() override {} + + // content::BrowserContext: + base::FilePath GetPath() const override { + base::FilePath path; + PathService::Get(extensions::DIR_TEST_DATA, &path); + return path.AppendASCII("shell_prefs"); + } + + private: + DISALLOW_COPY_AND_ASSIGN(PrefsTestBrowserContext); +}; + +} // namespace + +namespace extensions { + +TEST(ShellPrefsTest, CreatePrefService) { + content::TestBrowserThreadBundle thread_bundle; + PrefsTestBrowserContext browser_context; + + // Create the pref service. This loads the test pref file. + scoped_ptr<PrefService> service = + ShellPrefs::CreatePrefService(&browser_context); + + // Some basic extension preferences are registered. + EXPECT_TRUE(service->FindPreference("extensions.settings")); + EXPECT_TRUE(service->FindPreference("extensions.toolbarsize")); + EXPECT_FALSE(service->FindPreference("should.not.exist")); + + // User prefs from the file have been read correctly. + EXPECT_EQ("1.2.3.4", service->GetString("extensions.last_chrome_version")); + EXPECT_EQ(123, service->GetInteger("extensions.toolbarsize")); + + // The user prefs system has been initialized. + EXPECT_EQ(service.get(), user_prefs::UserPrefs::Get(&browser_context)); +} + +} // namespace extensions diff --git a/extensions/test/data/shell_prefs/user_prefs.json b/extensions/test/data/shell_prefs/user_prefs.json new file mode 100644 index 0000000..ad17776 --- /dev/null +++ b/extensions/test/data/shell_prefs/user_prefs.json @@ -0,0 +1,7 @@ +{ + "homepage": "http://www.example.com/", + "extensions": { + "last_chrome_version": "1.2.3.4", + "toolbarsize": 123 + } +} |