• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/extensions/test_extension_system.h"
6 
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/shared_module_service.h"
14 #include "chrome/browser/extensions/standard_management_policy_provider.h"
15 #include "chrome/browser/extensions/state_store.h"
16 #include "chrome/browser/extensions/user_script_master.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "extensions/browser/event_router.h"
21 #include "extensions/browser/extension_pref_value_map.h"
22 #include "extensions/browser/extension_pref_value_map_factory.h"
23 #include "extensions/browser/extension_prefs.h"
24 #include "extensions/browser/extension_prefs_factory.h"
25 #include "extensions/browser/extension_registry.h"
26 #include "extensions/browser/extension_system.h"
27 #include "extensions/browser/extensions_browser_client.h"
28 #include "extensions/browser/info_map.h"
29 #include "extensions/browser/management_policy.h"
30 #include "extensions/browser/process_manager.h"
31 #include "extensions/browser/quota_service.h"
32 #include "extensions/browser/runtime_data.h"
33 #include "extensions/browser/value_store/testing_value_store.h"
34 
35 using content::BrowserThread;
36 
37 namespace extensions {
38 
TestExtensionSystem(Profile * profile)39 TestExtensionSystem::TestExtensionSystem(Profile* profile)
40     : profile_(profile),
41       value_store_(NULL),
42       info_map_(new InfoMap()),
43       error_console_(new ErrorConsole(profile)),
44       quota_service_(new QuotaService()) {}
45 
~TestExtensionSystem()46 TestExtensionSystem::~TestExtensionSystem() {
47 }
48 
Shutdown()49 void TestExtensionSystem::Shutdown() {
50   process_manager_.reset();
51 }
52 
CreateProcessManager()53 void TestExtensionSystem::CreateProcessManager() {
54   process_manager_.reset(ProcessManager::Create(profile_));
55 }
56 
SetProcessManager(ProcessManager * manager)57 void TestExtensionSystem::SetProcessManager(ProcessManager* manager) {
58   process_manager_.reset(manager);
59 }
60 
CreateExtensionPrefs(const CommandLine * command_line,const base::FilePath & install_directory)61 ExtensionPrefs* TestExtensionSystem::CreateExtensionPrefs(
62     const CommandLine* command_line,
63     const base::FilePath& install_directory) {
64   bool extensions_disabled =
65       command_line && command_line->HasSwitch(switches::kDisableExtensions);
66 
67   // Note that the GetPrefs() creates a TestingPrefService, therefore
68   // the extension controlled pref values set in ExtensionPrefs
69   // are not reflected in the pref service. One would need to
70   // inject a new ExtensionPrefStore(extension_pref_value_map, false).
71 
72   ExtensionPrefs* extension_prefs = ExtensionPrefs::Create(
73       profile_->GetPrefs(),
74       install_directory,
75       ExtensionPrefValueMapFactory::GetForBrowserContext(profile_),
76       ExtensionsBrowserClient::Get()->CreateAppSorting().Pass(),
77       extensions_disabled,
78       std::vector<ExtensionPrefsObserver*>());
79     ExtensionPrefsFactory::GetInstance()->SetInstanceForTesting(
80         profile_,
81         extension_prefs);
82     return extension_prefs;
83 }
84 
CreateExtensionService(const CommandLine * command_line,const base::FilePath & install_directory,bool autoupdate_enabled)85 ExtensionService* TestExtensionSystem::CreateExtensionService(
86     const CommandLine* command_line,
87     const base::FilePath& install_directory,
88     bool autoupdate_enabled) {
89   if (!ExtensionPrefs::Get(profile_))
90     CreateExtensionPrefs(command_line, install_directory);
91   install_verifier_.reset(
92       new InstallVerifier(ExtensionPrefs::Get(profile_), profile_));
93   // The ownership of |value_store_| is immediately transferred to state_store_,
94   // but we keep a naked pointer to the TestingValueStore.
95   scoped_ptr<TestingValueStore> value_store(new TestingValueStore());
96   value_store_ = value_store.get();
97   state_store_.reset(
98       new StateStore(profile_, value_store.PassAs<ValueStore>()));
99   blacklist_.reset(new Blacklist(ExtensionPrefs::Get(profile_)));
100   standard_management_policy_provider_.reset(
101       new StandardManagementPolicyProvider(ExtensionPrefs::Get(profile_)));
102   management_policy_.reset(new ManagementPolicy());
103   management_policy_->RegisterProvider(
104       standard_management_policy_provider_.get());
105   runtime_data_.reset(new RuntimeData(ExtensionRegistry::Get(profile_)));
106   extension_service_.reset(new ExtensionService(profile_,
107                                                 command_line,
108                                                 install_directory,
109                                                 ExtensionPrefs::Get(profile_),
110                                                 blacklist_.get(),
111                                                 autoupdate_enabled,
112                                                 true,
113                                                 &ready_));
114   extension_service_->ClearProvidersForTesting();
115   return extension_service_.get();
116 }
117 
extension_service()118 ExtensionService* TestExtensionSystem::extension_service() {
119   return extension_service_.get();
120 }
121 
runtime_data()122 RuntimeData* TestExtensionSystem::runtime_data() {
123   return runtime_data_.get();
124 }
125 
management_policy()126 ManagementPolicy* TestExtensionSystem::management_policy() {
127   return management_policy_.get();
128 }
129 
SetExtensionService(ExtensionService * service)130 void TestExtensionSystem::SetExtensionService(ExtensionService* service) {
131   extension_service_.reset(service);
132 }
133 
user_script_master()134 UserScriptMaster* TestExtensionSystem::user_script_master() {
135   return NULL;
136 }
137 
process_manager()138 ProcessManager* TestExtensionSystem::process_manager() {
139   return process_manager_.get();
140 }
141 
state_store()142 StateStore* TestExtensionSystem::state_store() {
143   return state_store_.get();
144 }
145 
rules_store()146 StateStore* TestExtensionSystem::rules_store() {
147   return state_store_.get();
148 }
149 
info_map()150 InfoMap* TestExtensionSystem::info_map() { return info_map_.get(); }
151 
152 LazyBackgroundTaskQueue*
lazy_background_task_queue()153 TestExtensionSystem::lazy_background_task_queue() {
154   return NULL;
155 }
156 
SetEventRouter(scoped_ptr<EventRouter> event_router)157 void TestExtensionSystem::SetEventRouter(scoped_ptr<EventRouter> event_router) {
158   event_router_.reset(event_router.release());
159 }
160 
event_router()161 EventRouter* TestExtensionSystem::event_router() { return event_router_.get(); }
162 
warning_service()163 ExtensionWarningService* TestExtensionSystem::warning_service() {
164   return NULL;
165 }
166 
blacklist()167 Blacklist* TestExtensionSystem::blacklist() {
168   return blacklist_.get();
169 }
170 
error_console()171 ErrorConsole* TestExtensionSystem::error_console() {
172   return error_console_.get();
173 }
174 
install_verifier()175 InstallVerifier* TestExtensionSystem::install_verifier() {
176   return install_verifier_.get();
177 }
178 
quota_service()179 QuotaService* TestExtensionSystem::quota_service() {
180   return quota_service_.get();
181 }
182 
ready() const183 const OneShotEvent& TestExtensionSystem::ready() const {
184   return ready_;
185 }
186 
content_verifier()187 ContentVerifier* TestExtensionSystem::content_verifier() {
188   return NULL;
189 }
190 
GetDependentExtensions(const Extension * extension)191 scoped_ptr<ExtensionSet> TestExtensionSystem::GetDependentExtensions(
192     const Extension* extension) {
193   return extension_service()->shared_module_service()->GetDependentExtensions(
194       extension);
195 }
196 
197 // static
Build(content::BrowserContext * profile)198 KeyedService* TestExtensionSystem::Build(content::BrowserContext* profile) {
199   return new TestExtensionSystem(static_cast<Profile*>(profile));
200 }
201 
202 }  // namespace extensions
203