- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / 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/extension_system.h"
6
7 #include "base/base_switches.h"
8 #include "base/bind.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/strings/string_tokenizer.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/content_settings/cookie_settings.h"
14 #include "chrome/browser/extensions/blacklist.h"
15 #include "chrome/browser/extensions/component_loader.h"
16 #include "chrome/browser/extensions/error_console/error_console.h"
17 #include "chrome/browser/extensions/event_router.h"
18 #include "chrome/browser/extensions/extension_error_reporter.h"
19 #include "chrome/browser/extensions/extension_info_map.h"
20 #include "chrome/browser/extensions/extension_pref_store.h"
21 #include "chrome/browser/extensions/extension_pref_value_map.h"
22 #include "chrome/browser/extensions/extension_pref_value_map_factory.h"
23 #include "chrome/browser/extensions/extension_prefs.h"
24 #include "chrome/browser/extensions/extension_process_manager.h"
25 #include "chrome/browser/extensions/extension_service.h"
26 #include "chrome/browser/extensions/extension_system_factory.h"
27 #include "chrome/browser/extensions/extension_util.h"
28 #include "chrome/browser/extensions/extension_warning_badge_service.h"
29 #include "chrome/browser/extensions/extension_warning_set.h"
30 #include "chrome/browser/extensions/management_policy.h"
31 #include "chrome/browser/extensions/navigation_observer.h"
32 #include "chrome/browser/extensions/standard_management_policy_provider.h"
33 #include "chrome/browser/extensions/state_store.h"
34 #include "chrome/browser/extensions/unpacked_installer.h"
35 #include "chrome/browser/extensions/user_script_master.h"
36 #include "chrome/browser/profiles/profile.h"
37 #include "chrome/browser/profiles/profile_manager.h"
38 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
39 #include "chrome/common/chrome_switches.h"
40 #include "chrome/common/chrome_version_info.h"
41 #include "chrome/common/extensions/extension.h"
42 #include "chrome/common/extensions/features/feature_channel.h"
43 #include "content/public/browser/browser_thread.h"
44 #include "content/public/browser/url_data_source.h"
45 #include "extensions/browser/lazy_background_task_queue.h"
46 #include "extensions/common/constants.h"
47 #include "extensions/common/manifest.h"
48
49 #if defined(OS_CHROMEOS)
50 #include "chrome/browser/app_mode/app_mode_utils.h"
51 #include "chrome/browser/chromeos/extensions/device_local_account_management_policy_provider.h"
52 #include "chrome/browser/chromeos/login/user.h"
53 #include "chrome/browser/chromeos/login/user_manager.h"
54 #include "chrome/browser/chromeos/policy/device_local_account.h"
55 #include "chromeos/chromeos_switches.h"
56 #include "chromeos/login/login_state.h"
57 #endif
58
59 using content::BrowserThread;
60
61 namespace extensions {
62
63 //
64 // ExtensionSystem
65 //
66
67 ExtensionSystem::ExtensionSystem() {
68   // Only set if it hasn't already been set (e.g. by a test).
69   if (GetCurrentChannel() == GetDefaultChannel())
70     SetCurrentChannel(chrome::VersionInfo::GetChannel());
71 }
72
73 ExtensionSystem::~ExtensionSystem() {
74 }
75
76 // static
77 ExtensionSystem* ExtensionSystem::Get(Profile* profile) {
78   return ExtensionSystemFactory::GetForProfile(profile);
79 }
80
81 // static
82 ExtensionSystem* ExtensionSystem::GetForBrowserContext(
83     content::BrowserContext* profile) {
84   return ExtensionSystemFactory::GetForProfile(static_cast<Profile*>(profile));
85 }
86
87 //
88 // ExtensionSystemImpl::Shared
89 //
90
91 ExtensionSystemImpl::Shared::Shared(Profile* profile)
92     : profile_(profile) {
93 }
94
95 ExtensionSystemImpl::Shared::~Shared() {
96 }
97
98 void ExtensionSystemImpl::Shared::InitPrefs() {
99   lazy_background_task_queue_.reset(new LazyBackgroundTaskQueue(profile_));
100   event_router_.reset(new EventRouter(profile_, ExtensionPrefs::Get(profile_)));
101 // TODO(yoz): Remove once crbug.com/159265 is fixed.
102 #if defined(ENABLE_EXTENSIONS)
103   // Two state stores. The latter, which contains declarative rules, must be
104   // loaded immediately so that the rules are ready before we issue network
105   // requests.
106   state_store_.reset(new StateStore(
107       profile_,
108       profile_->GetPath().AppendASCII(extensions::kStateStoreName),
109       true));
110
111   rules_store_.reset(new StateStore(
112       profile_,
113       profile_->GetPath().AppendASCII(extensions::kRulesStoreName),
114       false));
115
116   blacklist_.reset(new Blacklist(ExtensionPrefs::Get(profile_)));
117
118   standard_management_policy_provider_.reset(
119       new StandardManagementPolicyProvider(ExtensionPrefs::Get(profile_)));
120
121 #if defined (OS_CHROMEOS)
122   const chromeos::User* user = chromeos::UserManager::Get()->GetActiveUser();
123   policy::DeviceLocalAccount::Type device_local_account_type;
124   if (user && policy::IsDeviceLocalAccountUser(user->email(),
125                                                &device_local_account_type)) {
126     device_local_account_management_policy_provider_.reset(
127         new chromeos::DeviceLocalAccountManagementPolicyProvider(
128             device_local_account_type));
129   }
130 #endif  // defined (OS_CHROMEOS)
131
132 #endif  // defined(ENABLE_EXTENSIONS)
133 }
134
135 void ExtensionSystemImpl::Shared::RegisterManagementPolicyProviders() {
136 // TODO(yoz): Remove once crbug.com/159265 is fixed.
137 #if defined(ENABLE_EXTENSIONS)
138   DCHECK(standard_management_policy_provider_.get());
139   management_policy_->RegisterProvider(
140       standard_management_policy_provider_.get());
141
142 #if defined (OS_CHROMEOS)
143   if (device_local_account_management_policy_provider_) {
144     management_policy_->RegisterProvider(
145         device_local_account_management_policy_provider_.get());
146   }
147 #endif  // defined (OS_CHROMEOS)
148
149 #endif  // defined(ENABLE_EXTENSIONS)
150 }
151
152 void ExtensionSystemImpl::Shared::Init(bool extensions_enabled) {
153   const CommandLine* command_line = CommandLine::ForCurrentProcess();
154
155   navigation_observer_.reset(new NavigationObserver(profile_));
156
157   bool allow_noisy_errors = !command_line->HasSwitch(switches::kNoErrorDialogs);
158   ExtensionErrorReporter::Init(allow_noisy_errors);
159
160   user_script_master_ = new UserScriptMaster(profile_);
161
162   bool autoupdate_enabled = true;
163 #if defined(OS_CHROMEOS)
164   if (!extensions_enabled)
165     autoupdate_enabled = false;
166   else
167     autoupdate_enabled =
168         !command_line->HasSwitch(chromeos::switches::kGuestSession);
169 #endif
170   extension_service_.reset(new ExtensionService(
171       profile_,
172       CommandLine::ForCurrentProcess(),
173       profile_->GetPath().AppendASCII(extensions::kInstallDirectoryName),
174       ExtensionPrefs::Get(profile_),
175       blacklist_.get(),
176       autoupdate_enabled,
177       extensions_enabled,
178       &ready_));
179
180   // These services must be registered before the ExtensionService tries to
181   // load any extensions.
182   {
183     management_policy_.reset(new ManagementPolicy);
184     RegisterManagementPolicyProviders();
185   }
186
187   bool skip_session_extensions = false;
188 #if defined(OS_CHROMEOS)
189   // Skip loading session extensions if we are not in a user session.
190   skip_session_extensions = !chromeos::LoginState::Get()->IsUserLoggedIn();
191   if (!chrome::IsRunningInForcedAppMode()) {
192     extension_service_->component_loader()->AddDefaultComponentExtensions(
193         skip_session_extensions);
194   }
195 #else
196   extension_service_->component_loader()->AddDefaultComponentExtensions(
197       skip_session_extensions);
198 #endif
199   if (command_line->HasSwitch(switches::kLoadComponentExtension)) {
200     CommandLine::StringType path_list = command_line->GetSwitchValueNative(
201         switches::kLoadComponentExtension);
202     base::StringTokenizerT<CommandLine::StringType,
203         CommandLine::StringType::const_iterator> t(path_list,
204                                                    FILE_PATH_LITERAL(","));
205     while (t.GetNext()) {
206       // Load the component extension manifest synchronously.
207       // Blocking the UI thread is acceptable here since
208       // this flag designated for developers.
209       base::ThreadRestrictions::ScopedAllowIO allow_io;
210       extension_service_->component_loader()->AddOrReplace(
211           base::FilePath(t.token()));
212     }
213   }
214   extension_service_->Init();
215
216   if (extensions_enabled) {
217     // Load any extensions specified with --load-extension.
218     // TODO(yoz): Seems like this should move into ExtensionService::Init.
219     // But maybe it's no longer important.
220     if (command_line->HasSwitch(switches::kLoadExtension)) {
221       CommandLine::StringType path_list = command_line->GetSwitchValueNative(
222           switches::kLoadExtension);
223       base::StringTokenizerT<CommandLine::StringType,
224           CommandLine::StringType::const_iterator> t(path_list,
225                                                      FILE_PATH_LITERAL(","));
226       while (t.GetNext()) {
227         std::string extension_id;
228         UnpackedInstaller::Create(extension_service_.get())->
229             LoadFromCommandLine(base::FilePath(t.token()), &extension_id);
230       }
231     }
232   }
233
234   // Make the chrome://extension-icon/ resource available.
235   content::URLDataSource::Add(profile_, new ExtensionIconSource(profile_));
236
237   extension_warning_service_.reset(new ExtensionWarningService(profile_));
238   extension_warning_badge_service_.reset(
239       new ExtensionWarningBadgeService(profile_));
240   extension_warning_service_->AddObserver(
241       extension_warning_badge_service_.get());
242   error_console_.reset(new ErrorConsole(profile_, extension_service_.get()));
243 }
244
245 void ExtensionSystemImpl::Shared::Shutdown() {
246   if (extension_warning_service_) {
247     extension_warning_service_->RemoveObserver(
248         extension_warning_badge_service_.get());
249   }
250   if (extension_service_)
251     extension_service_->Shutdown();
252 }
253
254 StateStore* ExtensionSystemImpl::Shared::state_store() {
255   return state_store_.get();
256 }
257
258 StateStore* ExtensionSystemImpl::Shared::rules_store() {
259   return rules_store_.get();
260 }
261
262 ExtensionService* ExtensionSystemImpl::Shared::extension_service() {
263   return extension_service_.get();
264 }
265
266 ManagementPolicy* ExtensionSystemImpl::Shared::management_policy() {
267   return management_policy_.get();
268 }
269
270 UserScriptMaster* ExtensionSystemImpl::Shared::user_script_master() {
271   return user_script_master_.get();
272 }
273
274 ExtensionInfoMap* ExtensionSystemImpl::Shared::info_map() {
275   if (!extension_info_map_.get())
276     extension_info_map_ = new ExtensionInfoMap();
277   return extension_info_map_.get();
278 }
279
280 LazyBackgroundTaskQueue*
281     ExtensionSystemImpl::Shared::lazy_background_task_queue() {
282   return lazy_background_task_queue_.get();
283 }
284
285 EventRouter* ExtensionSystemImpl::Shared::event_router() {
286   return event_router_.get();
287 }
288
289 ExtensionWarningService* ExtensionSystemImpl::Shared::warning_service() {
290   return extension_warning_service_.get();
291 }
292
293 Blacklist* ExtensionSystemImpl::Shared::blacklist() {
294   return blacklist_.get();
295 }
296
297 ErrorConsole* ExtensionSystemImpl::Shared::error_console() {
298   return error_console_.get();
299 }
300
301 //
302 // ExtensionSystemImpl
303 //
304
305 ExtensionSystemImpl::ExtensionSystemImpl(Profile* profile)
306     : profile_(profile) {
307   shared_ = ExtensionSystemSharedFactory::GetForProfile(profile);
308
309   if (profile->IsOffTheRecord()) {
310     extension_process_manager_.reset(ExtensionProcessManager::Create(profile));
311   } else {
312     shared_->InitPrefs();
313   }
314 }
315
316 ExtensionSystemImpl::~ExtensionSystemImpl() {
317 }
318
319 void ExtensionSystemImpl::Shutdown() {
320   extension_process_manager_.reset();
321 }
322
323 void ExtensionSystemImpl::InitForRegularProfile(
324     bool extensions_enabled,
325     bool defer_background_creation) {
326   DCHECK(!profile_->IsOffTheRecord());
327   if (user_script_master() || extension_service())
328     return;  // Already initialized.
329
330   // The ExtensionInfoMap needs to be created before the
331   // ExtensionProcessManager.
332   shared_->info_map();
333
334   extension_process_manager_.reset(ExtensionProcessManager::Create(profile_));
335
336   extension_process_manager_->DeferBackgroundHostCreation(
337       defer_background_creation);
338
339   shared_->Init(extensions_enabled);
340 }
341
342 ExtensionService* ExtensionSystemImpl::extension_service() {
343   return shared_->extension_service();
344 }
345
346 ManagementPolicy* ExtensionSystemImpl::management_policy() {
347   return shared_->management_policy();
348 }
349
350 UserScriptMaster* ExtensionSystemImpl::user_script_master() {
351   return shared_->user_script_master();
352 }
353
354 ExtensionProcessManager* ExtensionSystemImpl::process_manager() {
355   return extension_process_manager_.get();
356 }
357
358 StateStore* ExtensionSystemImpl::state_store() {
359   return shared_->state_store();
360 }
361
362 StateStore* ExtensionSystemImpl::rules_store() {
363   return shared_->rules_store();
364 }
365
366 ExtensionInfoMap* ExtensionSystemImpl::info_map() {
367   return shared_->info_map();
368 }
369
370 LazyBackgroundTaskQueue* ExtensionSystemImpl::lazy_background_task_queue() {
371   return shared_->lazy_background_task_queue();
372 }
373
374 EventRouter* ExtensionSystemImpl::event_router() {
375   return shared_->event_router();
376 }
377
378 ExtensionWarningService* ExtensionSystemImpl::warning_service() {
379   return shared_->warning_service();
380 }
381
382 Blacklist* ExtensionSystemImpl::blacklist() {
383   return shared_->blacklist();
384 }
385
386 const OneShotEvent& ExtensionSystemImpl::ready() const {
387   return shared_->ready();
388 }
389
390 ErrorConsole* ExtensionSystemImpl::error_console() {
391   return shared_->error_console();
392 }
393
394 void ExtensionSystemImpl::RegisterExtensionWithRequestContexts(
395     const Extension* extension) {
396   base::Time install_time;
397   if (extension->location() != Manifest::COMPONENT) {
398     install_time = ExtensionPrefs::Get(profile_)->
399         GetInstallTime(extension->id());
400   }
401   bool incognito_enabled =
402       extension_util::IsIncognitoEnabled(extension->id(), extension_service());
403   BrowserThread::PostTask(
404       BrowserThread::IO, FROM_HERE,
405       base::Bind(&ExtensionInfoMap::AddExtension, info_map(),
406                  make_scoped_refptr(extension), install_time,
407                  incognito_enabled));
408 }
409
410 void ExtensionSystemImpl::UnregisterExtensionWithRequestContexts(
411     const std::string& extension_id,
412     const UnloadedExtensionInfo::Reason reason) {
413   BrowserThread::PostTask(
414       BrowserThread::IO, FROM_HERE,
415       base::Bind(&ExtensionInfoMap::RemoveExtension, info_map(),
416                  extension_id, reason));
417 }
418
419 }  // namespace extensions