1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/test_extension_system.h"
7 #include "base/command_line.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/extensions/blacklist.h"
10 #include "chrome/browser/extensions/error_console/error_console.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/install_verifier.h"
13 #include "chrome/browser/extensions/standard_management_policy_provider.h"
14 #include "chrome/browser/extensions/state_store.h"
15 #include "chrome/browser/extensions/user_script_master.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "extensions/browser/event_router.h"
20 #include "extensions/browser/extension_pref_value_map.h"
21 #include "extensions/browser/extension_pref_value_map_factory.h"
22 #include "extensions/browser/extension_prefs.h"
23 #include "extensions/browser/extension_prefs_factory.h"
24 #include "extensions/browser/extension_registry.h"
25 #include "extensions/browser/extension_system.h"
26 #include "extensions/browser/extensions_browser_client.h"
27 #include "extensions/browser/info_map.h"
28 #include "extensions/browser/management_policy.h"
29 #include "extensions/browser/process_manager.h"
30 #include "extensions/browser/quota_service.h"
31 #include "extensions/browser/runtime_data.h"
32 #include "extensions/browser/value_store/testing_value_store.h"
34 using content::BrowserThread
;
36 namespace extensions
{
38 TestExtensionSystem::TestExtensionSystem(Profile
* profile
)
41 info_map_(new InfoMap()),
42 error_console_(new ErrorConsole(profile
)),
43 quota_service_(new QuotaService()) {}
45 TestExtensionSystem::~TestExtensionSystem() {
48 void TestExtensionSystem::Shutdown() {
49 process_manager_
.reset();
52 void TestExtensionSystem::CreateProcessManager() {
53 process_manager_
.reset(ProcessManager::Create(profile_
));
56 void TestExtensionSystem::SetProcessManager(ProcessManager
* manager
) {
57 process_manager_
.reset(manager
);
60 ExtensionPrefs
* TestExtensionSystem::CreateExtensionPrefs(
61 const CommandLine
* command_line
,
62 const base::FilePath
& install_directory
) {
63 bool extensions_disabled
=
64 command_line
&& command_line
->HasSwitch(switches::kDisableExtensions
);
66 // Note that the GetPrefs() creates a TestingPrefService, therefore
67 // the extension controlled pref values set in ExtensionPrefs
68 // are not reflected in the pref service. One would need to
69 // inject a new ExtensionPrefStore(extension_pref_value_map, false).
71 ExtensionPrefs
* extension_prefs
= ExtensionPrefs::Create(
74 ExtensionPrefValueMapFactory::GetForBrowserContext(profile_
),
75 ExtensionsBrowserClient::Get()->CreateAppSorting().Pass(),
77 std::vector
<ExtensionPrefsObserver
*>());
78 ExtensionPrefsFactory::GetInstance()->SetInstanceForTesting(
81 return extension_prefs
;
84 ExtensionService
* TestExtensionSystem::CreateExtensionService(
85 const CommandLine
* command_line
,
86 const base::FilePath
& install_directory
,
87 bool autoupdate_enabled
) {
88 if (!ExtensionPrefs::Get(profile_
))
89 CreateExtensionPrefs(command_line
, install_directory
);
90 install_verifier_
.reset(
91 new InstallVerifier(ExtensionPrefs::Get(profile_
), profile_
));
92 // The ownership of |value_store_| is immediately transferred to state_store_,
93 // but we keep a naked pointer to the TestingValueStore.
94 scoped_ptr
<TestingValueStore
> value_store(new TestingValueStore());
95 value_store_
= value_store
.get();
97 new StateStore(profile_
, value_store
.PassAs
<ValueStore
>()));
98 blacklist_
.reset(new Blacklist(ExtensionPrefs::Get(profile_
)));
99 standard_management_policy_provider_
.reset(
100 new StandardManagementPolicyProvider(ExtensionPrefs::Get(profile_
)));
101 management_policy_
.reset(new ManagementPolicy());
102 management_policy_
->RegisterProvider(
103 standard_management_policy_provider_
.get());
104 runtime_data_
.reset(new RuntimeData(ExtensionRegistry::Get(profile_
)));
105 extension_service_
.reset(new ExtensionService(profile_
,
108 ExtensionPrefs::Get(profile_
),
113 extension_service_
->ClearProvidersForTesting();
114 return extension_service_
.get();
117 ExtensionService
* TestExtensionSystem::extension_service() {
118 return extension_service_
.get();
121 RuntimeData
* TestExtensionSystem::runtime_data() {
122 return runtime_data_
.get();
125 ManagementPolicy
* TestExtensionSystem::management_policy() {
126 return management_policy_
.get();
129 void TestExtensionSystem::SetExtensionService(ExtensionService
* service
) {
130 extension_service_
.reset(service
);
133 UserScriptMaster
* TestExtensionSystem::user_script_master() {
137 ProcessManager
* TestExtensionSystem::process_manager() {
138 return process_manager_
.get();
141 StateStore
* TestExtensionSystem::state_store() {
142 return state_store_
.get();
145 StateStore
* TestExtensionSystem::rules_store() {
146 return state_store_
.get();
149 InfoMap
* TestExtensionSystem::info_map() { return info_map_
.get(); }
151 LazyBackgroundTaskQueue
*
152 TestExtensionSystem::lazy_background_task_queue() {
156 void TestExtensionSystem::SetEventRouter(scoped_ptr
<EventRouter
> event_router
) {
157 event_router_
.reset(event_router
.release());
160 EventRouter
* TestExtensionSystem::event_router() { return event_router_
.get(); }
162 ExtensionWarningService
* TestExtensionSystem::warning_service() {
166 Blacklist
* TestExtensionSystem::blacklist() {
167 return blacklist_
.get();
170 ErrorConsole
* TestExtensionSystem::error_console() {
171 return error_console_
.get();
174 InstallVerifier
* TestExtensionSystem::install_verifier() {
175 return install_verifier_
.get();
178 QuotaService
* TestExtensionSystem::quota_service() {
179 return quota_service_
.get();
182 const OneShotEvent
& TestExtensionSystem::ready() const {
187 KeyedService
* TestExtensionSystem::Build(content::BrowserContext
* profile
) {
188 return new TestExtensionSystem(static_cast<Profile
*>(profile
));
191 } // namespace extensions