- add sources.
[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/event_router.h"
12 #include "chrome/browser/extensions/extension_info_map.h"
13 #include "chrome/browser/extensions/extension_pref_value_map.h"
14 #include "chrome/browser/extensions/extension_pref_value_map_factory.h"
15 #include "chrome/browser/extensions/extension_prefs.h"
16 #include "chrome/browser/extensions/extension_prefs_factory.h"
17 #include "chrome/browser/extensions/extension_process_manager.h"
18 #include "chrome/browser/extensions/extension_service.h"
19 #include "chrome/browser/extensions/extension_system.h"
20 #include "chrome/browser/extensions/management_policy.h"
21 #include "chrome/browser/extensions/standard_management_policy_provider.h"
22 #include "chrome/browser/extensions/state_store.h"
23 #include "chrome/browser/extensions/user_script_master.h"
24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/value_store/testing_value_store.h"
26 #include "chrome/common/chrome_switches.h"
27 #include "content/public/browser/browser_thread.h"
28
29 using content::BrowserThread;
30
31 namespace extensions {
32
33 TestExtensionSystem::TestExtensionSystem(Profile* profile)
34     : profile_(profile),
35       value_store_(NULL),
36       info_map_(new ExtensionInfoMap()),
37       error_console_(new ErrorConsole(profile, NULL)) {
38 }
39
40 TestExtensionSystem::~TestExtensionSystem() {
41 }
42
43 void TestExtensionSystem::Shutdown() {
44   extension_process_manager_.reset();
45 }
46
47 void TestExtensionSystem::CreateExtensionProcessManager() {
48   extension_process_manager_.reset(ExtensionProcessManager::Create(profile_));
49 }
50
51 void TestExtensionSystem::SetExtensionProcessManager(
52     ExtensionProcessManager* manager) {
53   extension_process_manager_.reset(manager);
54 }
55
56 ExtensionPrefs* 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       extensions_disabled);
72     ExtensionPrefsFactory::GetInstance()->SetInstanceForTesting(
73         profile_,
74         extension_prefs);
75     return extension_prefs;
76 }
77
78 ExtensionService* TestExtensionSystem::CreateExtensionService(
79     const CommandLine* command_line,
80     const base::FilePath& install_directory,
81     bool autoupdate_enabled) {
82   if (!ExtensionPrefs::Get(profile_))
83     CreateExtensionPrefs(command_line, install_directory);
84   // The ownership of |value_store_| is immediately transferred to state_store_,
85   // but we keep a naked pointer to the TestingValueStore.
86   scoped_ptr<TestingValueStore> value_store(new TestingValueStore());
87   value_store_ = value_store.get();
88   state_store_.reset(
89       new StateStore(profile_, value_store.PassAs<ValueStore>()));
90   blacklist_.reset(new Blacklist(ExtensionPrefs::Get(profile_)));
91   standard_management_policy_provider_.reset(
92       new StandardManagementPolicyProvider(ExtensionPrefs::Get(profile_)));
93   management_policy_.reset(new ManagementPolicy());
94   management_policy_->RegisterProvider(
95       standard_management_policy_provider_.get());
96   extension_service_.reset(new ExtensionService(profile_,
97                                                 command_line,
98                                                 install_directory,
99                                                 ExtensionPrefs::Get(profile_),
100                                                 blacklist_.get(),
101                                                 autoupdate_enabled,
102                                                 true,
103                                                 &ready_));
104   extension_service_->ClearProvidersForTesting();
105   return extension_service_.get();
106 }
107
108 ExtensionService* TestExtensionSystem::extension_service() {
109   return extension_service_.get();
110 }
111
112 ManagementPolicy* TestExtensionSystem::management_policy() {
113   return management_policy_.get();
114 }
115
116 void TestExtensionSystem::SetExtensionService(ExtensionService* service) {
117   extension_service_.reset(service);
118 }
119
120 UserScriptMaster* TestExtensionSystem::user_script_master() {
121   return NULL;
122 }
123
124 ExtensionProcessManager* TestExtensionSystem::process_manager() {
125   return extension_process_manager_.get();
126 }
127
128 StateStore* TestExtensionSystem::state_store() {
129   return state_store_.get();
130 }
131
132 StateStore* TestExtensionSystem::rules_store() {
133   return state_store_.get();
134 }
135
136 ExtensionInfoMap* TestExtensionSystem::info_map() {
137   return info_map_.get();
138 }
139
140 LazyBackgroundTaskQueue*
141 TestExtensionSystem::lazy_background_task_queue() {
142   return NULL;
143 }
144
145 EventRouter* TestExtensionSystem::event_router() {
146   return NULL;
147 }
148
149 ExtensionWarningService* TestExtensionSystem::warning_service() {
150   return NULL;
151 }
152
153 Blacklist* TestExtensionSystem::blacklist() {
154   return blacklist_.get();
155 }
156
157 const OneShotEvent& TestExtensionSystem::ready() const {
158   return ready_;
159 }
160
161 ErrorConsole* TestExtensionSystem::error_console() {
162   return error_console_.get();
163 }
164
165 // static
166 BrowserContextKeyedService* TestExtensionSystem::Build(
167     content::BrowserContext* profile) {
168   return new TestExtensionSystem(static_cast<Profile*>(profile));
169 }
170
171 }  // namespace extensions