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
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  process_manager_.reset();
51}
52
53void TestExtensionSystem::CreateProcessManager() {
54  process_manager_.reset(ProcessManager::Create(profile_));
55}
56
57void TestExtensionSystem::SetProcessManager(ProcessManager* manager) {
58  process_manager_.reset(manager);
59}
60
61ExtensionPrefs* 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
85ExtensionService* 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
118ExtensionService* TestExtensionSystem::extension_service() {
119  return extension_service_.get();
120}
121
122RuntimeData* TestExtensionSystem::runtime_data() {
123  return runtime_data_.get();
124}
125
126ManagementPolicy* TestExtensionSystem::management_policy() {
127  return management_policy_.get();
128}
129
130void TestExtensionSystem::SetExtensionService(ExtensionService* service) {
131  extension_service_.reset(service);
132}
133
134UserScriptMaster* TestExtensionSystem::user_script_master() {
135  return NULL;
136}
137
138ProcessManager* TestExtensionSystem::process_manager() {
139  return process_manager_.get();
140}
141
142StateStore* TestExtensionSystem::state_store() {
143  return state_store_.get();
144}
145
146StateStore* TestExtensionSystem::rules_store() {
147  return state_store_.get();
148}
149
150InfoMap* TestExtensionSystem::info_map() { return info_map_.get(); }
151
152LazyBackgroundTaskQueue*
153TestExtensionSystem::lazy_background_task_queue() {
154  return NULL;
155}
156
157void TestExtensionSystem::SetEventRouter(scoped_ptr<EventRouter> event_router) {
158  event_router_.reset(event_router.release());
159}
160
161EventRouter* TestExtensionSystem::event_router() { return event_router_.get(); }
162
163ExtensionWarningService* TestExtensionSystem::warning_service() {
164  return NULL;
165}
166
167Blacklist* TestExtensionSystem::blacklist() {
168  return blacklist_.get();
169}
170
171ErrorConsole* TestExtensionSystem::error_console() {
172  return error_console_.get();
173}
174
175InstallVerifier* TestExtensionSystem::install_verifier() {
176  return install_verifier_.get();
177}
178
179QuotaService* TestExtensionSystem::quota_service() {
180  return quota_service_.get();
181}
182
183const OneShotEvent& TestExtensionSystem::ready() const {
184  return ready_;
185}
186
187ContentVerifier* TestExtensionSystem::content_verifier() {
188  return NULL;
189}
190
191scoped_ptr<ExtensionSet> TestExtensionSystem::GetDependentExtensions(
192    const Extension* extension) {
193  return extension_service()->shared_module_service()->GetDependentExtensions(
194      extension);
195}
196
197// static
198KeyedService* TestExtensionSystem::Build(content::BrowserContext* profile) {
199  return new TestExtensionSystem(static_cast<Profile*>(profile));
200}
201
202}  // namespace extensions
203