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();
100 new StateStore(profile_, value_store.PassAs<ValueStore>()));
101 blacklist_.reset(new Blacklist(ExtensionPrefs::Get(profile_)));
102 management_policy_.reset(new ManagementPolicy());
103 management_policy_->RegisterProvider(
104 ExtensionManagementFactory::GetForBrowserContext(profile_)
106 runtime_data_.reset(new RuntimeData(ExtensionRegistry::Get(profile_)));
107 extension_service_.reset(new ExtensionService(profile_,
110 ExtensionPrefs::Get(profile_),
115 extension_service_->ClearProvidersForTesting();
116 return extension_service_.get();
119 ExtensionService* TestExtensionSystem::extension_service() {
120 return extension_service_.get();
123 RuntimeData* TestExtensionSystem::runtime_data() {
124 return runtime_data_.get();
127 ManagementPolicy* TestExtensionSystem::management_policy() {
128 return management_policy_.get();
131 void TestExtensionSystem::SetExtensionService(ExtensionService* service) {
132 extension_service_.reset(service);
135 SharedUserScriptMaster* TestExtensionSystem::shared_user_script_master() {
139 ProcessManager* TestExtensionSystem::process_manager() {
140 return process_manager_.get();
143 StateStore* TestExtensionSystem::state_store() {
144 return state_store_.get();
147 StateStore* TestExtensionSystem::rules_store() {
148 return state_store_.get();
151 InfoMap* TestExtensionSystem::info_map() { return info_map_.get(); }
153 LazyBackgroundTaskQueue*
154 TestExtensionSystem::lazy_background_task_queue() {
158 void TestExtensionSystem::SetEventRouter(scoped_ptr<EventRouter> event_router) {
159 event_router_.reset(event_router.release());
162 EventRouter* TestExtensionSystem::event_router() { return event_router_.get(); }
164 WarningService* TestExtensionSystem::warning_service() {
168 Blacklist* TestExtensionSystem::blacklist() {
169 return blacklist_.get();
172 ErrorConsole* TestExtensionSystem::error_console() {
173 return error_console_.get();
176 InstallVerifier* TestExtensionSystem::install_verifier() {
177 return install_verifier_.get();
180 QuotaService* TestExtensionSystem::quota_service() {
181 return quota_service_.get();
184 const OneShotEvent& TestExtensionSystem::ready() const {
188 ContentVerifier* TestExtensionSystem::content_verifier() {
192 scoped_ptr<ExtensionSet> TestExtensionSystem::GetDependentExtensions(
193 const Extension* extension) {
194 return extension_service()->shared_module_service()->GetDependentExtensions(
198 DeclarativeUserScriptMaster*
199 TestExtensionSystem::GetDeclarativeUserScriptMasterByExtension(
200 const ExtensionId& extension_id) {
201 DCHECK(ready().is_signaled());
202 DeclarativeUserScriptMaster* master = NULL;
203 for (ScopedVector<DeclarativeUserScriptMaster>::iterator it =
204 declarative_user_script_masters_.begin();
205 it != declarative_user_script_masters_.end();
207 if ((*it)->extension_id() == extension_id) {
213 master = new DeclarativeUserScriptMaster(profile_, extension_id);
214 declarative_user_script_masters_.push_back(master);
220 KeyedService* TestExtensionSystem::Build(content::BrowserContext* profile) {
221 return new TestExtensionSystem(static_cast<Profile*>(profile));
224 } // namespace extensions