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