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/declarative_user_script_master.h"
11 #include "chrome/browser/extensions/error_console/error_console.h"
12 #include "chrome/browser/extensions/extension_management.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/extensions/install_verifier.h"
15 #include "chrome/browser/extensions/shared_module_service.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/state_store.h"
33 #include "extensions/browser/value_store/testing_value_store.h"
35 using content::BrowserThread
;
37 namespace extensions
{
39 TestExtensionSystem::TestExtensionSystem(Profile
* profile
)
42 info_map_(new InfoMap()),
43 error_console_(new ErrorConsole(profile
)),
44 quota_service_(new QuotaService()) {}
46 TestExtensionSystem::~TestExtensionSystem() {
49 void TestExtensionSystem::Shutdown() {
50 if (extension_service_
)
51 extension_service_
->Shutdown();
52 process_manager_
.reset();
55 void TestExtensionSystem::CreateProcessManager() {
56 process_manager_
.reset(ProcessManager::Create(profile_
));
59 void TestExtensionSystem::SetProcessManager(ProcessManager
* manager
) {
60 process_manager_
.reset(manager
);
63 ExtensionPrefs
* TestExtensionSystem::CreateExtensionPrefs(
64 const CommandLine
* command_line
,
65 const base::FilePath
& install_directory
) {
66 bool extensions_disabled
=
67 command_line
&& command_line
->HasSwitch(switches::kDisableExtensions
);
69 // Note that the GetPrefs() creates a TestingPrefService, therefore
70 // the extension controlled pref values set in ExtensionPrefs
71 // are not reflected in the pref service. One would need to
72 // inject a new ExtensionPrefStore(extension_pref_value_map, false).
74 ExtensionPrefs
* extension_prefs
= ExtensionPrefs::Create(
77 ExtensionPrefValueMapFactory::GetForBrowserContext(profile_
),
78 ExtensionsBrowserClient::Get()->CreateAppSorting().Pass(),
80 std::vector
<ExtensionPrefsObserver
*>());
81 ExtensionPrefsFactory::GetInstance()->SetInstanceForTesting(
84 return extension_prefs
;
87 ExtensionService
* TestExtensionSystem::CreateExtensionService(
88 const CommandLine
* command_line
,
89 const base::FilePath
& install_directory
,
90 bool autoupdate_enabled
) {
91 if (!ExtensionPrefs::Get(profile_
))
92 CreateExtensionPrefs(command_line
, install_directory
);
93 install_verifier_
.reset(
94 new InstallVerifier(ExtensionPrefs::Get(profile_
), profile_
));
95 // The ownership of |value_store_| is immediately transferred to state_store_,
96 // but we keep a naked pointer to the TestingValueStore.
97 scoped_ptr
<TestingValueStore
> value_store(new TestingValueStore());
98 value_store_
= value_store
.get();
99 state_store_
.reset(new StateStore(profile_
, value_store
.Pass()));
100 blacklist_
.reset(new Blacklist(ExtensionPrefs::Get(profile_
)));
101 management_policy_
.reset(new ManagementPolicy());
102 management_policy_
->RegisterProvider(
103 ExtensionManagementFactory::GetForBrowserContext(profile_
)
105 runtime_data_
.reset(new RuntimeData(ExtensionRegistry::Get(profile_
)));
106 extension_service_
.reset(new ExtensionService(profile_
,
109 ExtensionPrefs::Get(profile_
),
114 extension_service_
->ClearProvidersForTesting();
115 return extension_service_
.get();
118 ExtensionService
* TestExtensionSystem::extension_service() {
119 return extension_service_
.get();
122 RuntimeData
* TestExtensionSystem::runtime_data() {
123 return runtime_data_
.get();
126 ManagementPolicy
* TestExtensionSystem::management_policy() {
127 return management_policy_
.get();
130 void TestExtensionSystem::SetExtensionService(ExtensionService
* service
) {
131 extension_service_
.reset(service
);
134 SharedUserScriptMaster
* TestExtensionSystem::shared_user_script_master() {
138 ProcessManager
* TestExtensionSystem::process_manager() {
139 return process_manager_
.get();
142 StateStore
* TestExtensionSystem::state_store() {
143 return state_store_
.get();
146 StateStore
* TestExtensionSystem::rules_store() {
147 return state_store_
.get();
150 InfoMap
* TestExtensionSystem::info_map() { return info_map_
.get(); }
152 LazyBackgroundTaskQueue
*
153 TestExtensionSystem::lazy_background_task_queue() {
157 void TestExtensionSystem::SetEventRouter(scoped_ptr
<EventRouter
> event_router
) {
158 event_router_
.reset(event_router
.release());
161 EventRouter
* TestExtensionSystem::event_router() { return event_router_
.get(); }
163 WarningService
* TestExtensionSystem::warning_service() {
167 Blacklist
* TestExtensionSystem::blacklist() {
168 return blacklist_
.get();
171 ErrorConsole
* TestExtensionSystem::error_console() {
172 return error_console_
.get();
175 InstallVerifier
* TestExtensionSystem::install_verifier() {
176 return install_verifier_
.get();
179 QuotaService
* TestExtensionSystem::quota_service() {
180 return quota_service_
.get();
183 const OneShotEvent
& TestExtensionSystem::ready() const {
187 ContentVerifier
* TestExtensionSystem::content_verifier() {
191 scoped_ptr
<ExtensionSet
> TestExtensionSystem::GetDependentExtensions(
192 const Extension
* extension
) {
193 return extension_service()->shared_module_service()->GetDependentExtensions(
197 DeclarativeUserScriptMaster
*
198 TestExtensionSystem::GetDeclarativeUserScriptMasterByExtension(
199 const ExtensionId
& extension_id
) {
200 DCHECK(ready().is_signaled());
201 DeclarativeUserScriptMaster
* master
= NULL
;
202 for (ScopedVector
<DeclarativeUserScriptMaster
>::iterator it
=
203 declarative_user_script_masters_
.begin();
204 it
!= declarative_user_script_masters_
.end();
206 if ((*it
)->extension_id() == extension_id
) {
212 master
= new DeclarativeUserScriptMaster(profile_
, extension_id
);
213 declarative_user_script_masters_
.push_back(master
);
219 KeyedService
* TestExtensionSystem::Build(content::BrowserContext
* profile
) {
220 return new TestExtensionSystem(static_cast<Profile
*>(profile
));
223 } // namespace extensions