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/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"
34
35using content::BrowserThread;
36
37namespace extensions {
38
39TestExtensionSystem::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
46TestExtensionSystem::~TestExtensionSystem() {
47}
48
49void TestExtensionSystem::Shutdown() {
50  if (extension_service_)
51    extension_service_->Shutdown();
52  process_manager_.reset();
53}
54
55void TestExtensionSystem::CreateProcessManager() {
56  process_manager_.reset(ProcessManager::Create(profile_));
57}
58
59void TestExtensionSystem::SetProcessManager(ProcessManager* manager) {
60  process_manager_.reset(manager);
61}
62
63ExtensionPrefs* 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);
68
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).
73
74  ExtensionPrefs* extension_prefs = ExtensionPrefs::Create(
75      profile_->GetPrefs(),
76      install_directory,
77      ExtensionPrefValueMapFactory::GetForBrowserContext(profile_),
78      ExtensionsBrowserClient::Get()->CreateAppSorting().Pass(),
79      extensions_disabled,
80      std::vector<ExtensionPrefsObserver*>());
81    ExtensionPrefsFactory::GetInstance()->SetInstanceForTesting(
82        profile_,
83        extension_prefs);
84    return extension_prefs;
85}
86
87ExtensionService* 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(
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_)
105          ->GetProvider());
106  runtime_data_.reset(new RuntimeData(ExtensionRegistry::Get(profile_)));
107  extension_service_.reset(new ExtensionService(profile_,
108                                                command_line,
109                                                install_directory,
110                                                ExtensionPrefs::Get(profile_),
111                                                blacklist_.get(),
112                                                autoupdate_enabled,
113                                                true,
114                                                &ready_));
115  extension_service_->ClearProvidersForTesting();
116  return extension_service_.get();
117}
118
119ExtensionService* TestExtensionSystem::extension_service() {
120  return extension_service_.get();
121}
122
123RuntimeData* TestExtensionSystem::runtime_data() {
124  return runtime_data_.get();
125}
126
127ManagementPolicy* TestExtensionSystem::management_policy() {
128  return management_policy_.get();
129}
130
131void TestExtensionSystem::SetExtensionService(ExtensionService* service) {
132  extension_service_.reset(service);
133}
134
135SharedUserScriptMaster* TestExtensionSystem::shared_user_script_master() {
136  return NULL;
137}
138
139ProcessManager* TestExtensionSystem::process_manager() {
140  return process_manager_.get();
141}
142
143StateStore* TestExtensionSystem::state_store() {
144  return state_store_.get();
145}
146
147StateStore* TestExtensionSystem::rules_store() {
148  return state_store_.get();
149}
150
151InfoMap* TestExtensionSystem::info_map() { return info_map_.get(); }
152
153LazyBackgroundTaskQueue*
154TestExtensionSystem::lazy_background_task_queue() {
155  return NULL;
156}
157
158void TestExtensionSystem::SetEventRouter(scoped_ptr<EventRouter> event_router) {
159  event_router_.reset(event_router.release());
160}
161
162EventRouter* TestExtensionSystem::event_router() { return event_router_.get(); }
163
164WarningService* TestExtensionSystem::warning_service() {
165  return NULL;
166}
167
168Blacklist* TestExtensionSystem::blacklist() {
169  return blacklist_.get();
170}
171
172ErrorConsole* TestExtensionSystem::error_console() {
173  return error_console_.get();
174}
175
176InstallVerifier* TestExtensionSystem::install_verifier() {
177  return install_verifier_.get();
178}
179
180QuotaService* TestExtensionSystem::quota_service() {
181  return quota_service_.get();
182}
183
184const OneShotEvent& TestExtensionSystem::ready() const {
185  return ready_;
186}
187
188ContentVerifier* TestExtensionSystem::content_verifier() {
189  return NULL;
190}
191
192scoped_ptr<ExtensionSet> TestExtensionSystem::GetDependentExtensions(
193    const Extension* extension) {
194  return extension_service()->shared_module_service()->GetDependentExtensions(
195      extension);
196}
197
198DeclarativeUserScriptMaster*
199TestExtensionSystem::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();
206       ++it) {
207    if ((*it)->extension_id() == extension_id) {
208      master = *it;
209      break;
210    }
211  }
212  if (!master) {
213    master = new DeclarativeUserScriptMaster(profile_, extension_id);
214    declarative_user_script_masters_.push_back(master);
215  }
216  return master;
217}
218
219// static
220KeyedService* TestExtensionSystem::Build(content::BrowserContext* profile) {
221  return new TestExtensionSystem(static_cast<Profile*>(profile));
222}
223
224}  // namespace extensions
225