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