Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / extension_system_impl.cc
1 // Copyright 2014 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_impl.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/memory/weak_ptr.h"
12 #include "base/strings/string_tokenizer.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/content_settings/cookie_settings.h"
15 #include "chrome/browser/extensions/blacklist.h"
16 #include "chrome/browser/extensions/component_loader.h"
17 #include "chrome/browser/extensions/error_console/error_console.h"
18 #include "chrome/browser/extensions/extension_error_reporter.h"
19 #include "chrome/browser/extensions/extension_service.h"
20 #include "chrome/browser/extensions/extension_system_factory.h"
21 #include "chrome/browser/extensions/extension_util.h"
22 #include "chrome/browser/extensions/extension_warning_badge_service.h"
23 #include "chrome/browser/extensions/extension_warning_set.h"
24 #include "chrome/browser/extensions/install_verifier.h"
25 #include "chrome/browser/extensions/navigation_observer.h"
26 #include "chrome/browser/extensions/standard_management_policy_provider.h"
27 #include "chrome/browser/extensions/state_store.h"
28 #include "chrome/browser/extensions/unpacked_installer.h"
29 #include "chrome/browser/extensions/updater/manifest_fetch_data.h"
30 #include "chrome/browser/extensions/user_script_master.h"
31 #include "chrome/browser/profiles/profile.h"
32 #include "chrome/browser/profiles/profile_manager.h"
33 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
34 #include "chrome/common/chrome_switches.h"
35 #include "chrome/common/chrome_version_info.h"
36 #include "chrome/common/extensions/extension_file_util.h"
37 #include "chrome/common/extensions/features/feature_channel.h"
38 #include "chrome/common/extensions/manifest_url_handler.h"
39 #include "content/public/browser/browser_thread.h"
40 #include "content/public/browser/url_data_source.h"
41 #include "extensions/browser/content_verifier.h"
42 #include "extensions/browser/content_verifier_delegate.h"
43 #include "extensions/browser/event_router.h"
44 #include "extensions/browser/extension_pref_store.h"
45 #include "extensions/browser/extension_pref_value_map.h"
46 #include "extensions/browser/extension_pref_value_map_factory.h"
47 #include "extensions/browser/extension_prefs.h"
48 #include "extensions/browser/extension_registry.h"
49 #include "extensions/browser/info_map.h"
50 #include "extensions/browser/lazy_background_task_queue.h"
51 #include "extensions/browser/management_policy.h"
52 #include "extensions/browser/process_manager.h"
53 #include "extensions/browser/quota_service.h"
54 #include "extensions/browser/runtime_data.h"
55 #include "extensions/common/constants.h"
56 #include "extensions/common/extension.h"
57 #include "extensions/common/manifest.h"
58 #include "net/base/escape.h"
59
60 #if defined(ENABLE_NOTIFICATIONS)
61 #include "chrome/browser/notifications/desktop_notification_service.h"
62 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
63 #include "ui/message_center/notifier_settings.h"
64 #endif
65
66 #if defined(OS_CHROMEOS)
67 #include "chrome/browser/app_mode/app_mode_utils.h"
68 #include "chrome/browser/chromeos/extensions/device_local_account_management_policy_provider.h"
69 #include "chrome/browser/chromeos/login/user.h"
70 #include "chrome/browser/chromeos/login/user_manager.h"
71 #include "chrome/browser/chromeos/policy/device_local_account.h"
72 #include "chromeos/chromeos_switches.h"
73 #include "chromeos/login/login_state.h"
74 #endif
75
76 using content::BrowserThread;
77
78 namespace extensions {
79
80 //
81 // ExtensionSystemImpl::Shared
82 //
83
84 ExtensionSystemImpl::Shared::Shared(Profile* profile)
85     : profile_(profile) {
86 }
87
88 ExtensionSystemImpl::Shared::~Shared() {
89 }
90
91 void ExtensionSystemImpl::Shared::InitPrefs() {
92   lazy_background_task_queue_.reset(new LazyBackgroundTaskQueue(profile_));
93   event_router_.reset(new EventRouter(profile_, ExtensionPrefs::Get(profile_)));
94 // TODO(yoz): Remove once crbug.com/159265 is fixed.
95 #if defined(ENABLE_EXTENSIONS)
96   // Two state stores. The latter, which contains declarative rules, must be
97   // loaded immediately so that the rules are ready before we issue network
98   // requests.
99   state_store_.reset(new StateStore(
100       profile_,
101       profile_->GetPath().AppendASCII(extensions::kStateStoreName),
102       true));
103
104   rules_store_.reset(new StateStore(
105       profile_,
106       profile_->GetPath().AppendASCII(extensions::kRulesStoreName),
107       false));
108
109   blacklist_.reset(new Blacklist(ExtensionPrefs::Get(profile_)));
110
111   standard_management_policy_provider_.reset(
112       new StandardManagementPolicyProvider(ExtensionPrefs::Get(profile_)));
113
114 #if defined (OS_CHROMEOS)
115   const chromeos::User* user = chromeos::UserManager::Get()->GetActiveUser();
116   policy::DeviceLocalAccount::Type device_local_account_type;
117   if (user && policy::IsDeviceLocalAccountUser(user->email(),
118                                                &device_local_account_type)) {
119     device_local_account_management_policy_provider_.reset(
120         new chromeos::DeviceLocalAccountManagementPolicyProvider(
121             device_local_account_type));
122   }
123 #endif  // defined (OS_CHROMEOS)
124
125 #endif  // defined(ENABLE_EXTENSIONS)
126 }
127
128 void ExtensionSystemImpl::Shared::RegisterManagementPolicyProviders() {
129 // TODO(yoz): Remove once crbug.com/159265 is fixed.
130 #if defined(ENABLE_EXTENSIONS)
131   DCHECK(standard_management_policy_provider_.get());
132   management_policy_->RegisterProvider(
133       standard_management_policy_provider_.get());
134
135 #if defined (OS_CHROMEOS)
136   if (device_local_account_management_policy_provider_) {
137     management_policy_->RegisterProvider(
138         device_local_account_management_policy_provider_.get());
139   }
140 #endif  // defined (OS_CHROMEOS)
141
142   management_policy_->RegisterProvider(install_verifier_.get());
143
144 #endif  // defined(ENABLE_EXTENSIONS)
145 }
146
147 namespace {
148
149 class ContentVerifierDelegateImpl : public ContentVerifierDelegate {
150  public:
151   explicit ContentVerifierDelegateImpl(ExtensionService* service)
152       : service_(service->AsWeakPtr()) {}
153
154   virtual ~ContentVerifierDelegateImpl() {}
155
156   virtual bool ShouldBeVerified(const Extension& extension) OVERRIDE {
157     if (!extension.is_extension() && !extension.is_legacy_packaged_app())
158       return false;
159     if (!Manifest::IsAutoUpdateableLocation(extension.location()))
160       return false;
161
162     if (!ManifestURL::UpdatesFromGallery(&extension)) {
163       // It's possible that the webstore update url was overridden for testing
164       // so also consider extensions with the default (production) update url
165       // to be from the store as well.
166       GURL default_webstore_url = extension_urls::GetDefaultWebstoreUpdateUrl();
167       if (ManifestURL::GetUpdateURL(&extension) != default_webstore_url)
168         return false;
169     }
170
171     return true;
172   }
173
174   virtual const ContentVerifierKey& PublicKey() OVERRIDE {
175     static ContentVerifierKey key(
176         extension_misc::kWebstoreSignaturesPublicKey,
177         extension_misc::kWebstoreSignaturesPublicKeySize);
178     return key;
179   }
180
181   virtual GURL GetSignatureFetchUrl(const std::string& extension_id,
182                                     const base::Version& version) OVERRIDE {
183     // TODO(asargent) Factor out common code from the extension updater's
184     // ManifestFetchData class that can be shared for use here.
185     std::vector<std::string> parts;
186     parts.push_back("uc");
187     parts.push_back("installsource=signature");
188     parts.push_back("id=" + extension_id);
189     parts.push_back("v=" + version.GetString());
190     std::string x_value =
191         net::EscapeQueryParamValue(JoinString(parts, "&"), true);
192     std::string query = "response=redirect&x=" + x_value;
193
194     GURL base_url = extension_urls::GetWebstoreUpdateUrl();
195     GURL::Replacements replacements;
196     replacements.SetQuery(query.c_str(), url::Component(0, query.length()));
197     return base_url.ReplaceComponents(replacements);
198   }
199
200   virtual std::set<base::FilePath> GetBrowserImagePaths(
201       const extensions::Extension* extension) OVERRIDE {
202     return extension_file_util::GetBrowserImagePaths(extension);
203   }
204
205   virtual void VerifyFailed(const std::string& extension_id) OVERRIDE {
206     if (service_)
207       service_->DisableExtension(extension_id, Extension::DISABLE_CORRUPTED);
208   }
209
210  private:
211   base::WeakPtr<ExtensionService> service_;
212   DISALLOW_COPY_AND_ASSIGN(ContentVerifierDelegateImpl);
213 };
214
215 }  // namespace
216
217 void ExtensionSystemImpl::Shared::Init(bool extensions_enabled) {
218   const CommandLine* command_line = CommandLine::ForCurrentProcess();
219
220   navigation_observer_.reset(new NavigationObserver(profile_));
221
222   bool allow_noisy_errors = !command_line->HasSwitch(switches::kNoErrorDialogs);
223   ExtensionErrorReporter::Init(allow_noisy_errors);
224
225   user_script_master_ = new UserScriptMaster(profile_);
226
227   // ExtensionService depends on RuntimeData.
228   runtime_data_.reset(new RuntimeData(ExtensionRegistry::Get(profile_)));
229
230   bool autoupdate_enabled = !profile_->IsGuestSession();
231 #if defined(OS_CHROMEOS)
232   if (!extensions_enabled)
233     autoupdate_enabled = false;
234 #endif
235   extension_service_.reset(new ExtensionService(
236       profile_,
237       CommandLine::ForCurrentProcess(),
238       profile_->GetPath().AppendASCII(extensions::kInstallDirectoryName),
239       ExtensionPrefs::Get(profile_),
240       blacklist_.get(),
241       autoupdate_enabled,
242       extensions_enabled,
243       &ready_));
244
245   // These services must be registered before the ExtensionService tries to
246   // load any extensions.
247   {
248     install_verifier_.reset(
249         new InstallVerifier(ExtensionPrefs::Get(profile_), profile_));
250     install_verifier_->Init();
251     content_verifier_ = new ContentVerifier(
252         profile_, new ContentVerifierDelegateImpl(extension_service_.get()));
253     content_verifier_->Start();
254     info_map()->SetContentVerifier(content_verifier_.get());
255
256     management_policy_.reset(new ManagementPolicy);
257     RegisterManagementPolicyProviders();
258   }
259
260   bool skip_session_extensions = false;
261 #if defined(OS_CHROMEOS)
262   // Skip loading session extensions if we are not in a user session.
263   skip_session_extensions = !chromeos::LoginState::Get()->IsUserLoggedIn();
264   if (chrome::IsRunningInForcedAppMode()) {
265     extension_service_->component_loader()->
266         AddDefaultComponentExtensionsForKioskMode(skip_session_extensions);
267   } else {
268     extension_service_->component_loader()->AddDefaultComponentExtensions(
269         skip_session_extensions);
270   }
271 #else
272   extension_service_->component_loader()->AddDefaultComponentExtensions(
273       skip_session_extensions);
274 #endif
275   if (command_line->HasSwitch(switches::kLoadComponentExtension)) {
276     CommandLine::StringType path_list = command_line->GetSwitchValueNative(
277         switches::kLoadComponentExtension);
278     base::StringTokenizerT<CommandLine::StringType,
279         CommandLine::StringType::const_iterator> t(path_list,
280                                                    FILE_PATH_LITERAL(","));
281     while (t.GetNext()) {
282       // Load the component extension manifest synchronously.
283       // Blocking the UI thread is acceptable here since
284       // this flag designated for developers.
285       base::ThreadRestrictions::ScopedAllowIO allow_io;
286       extension_service_->component_loader()->AddOrReplace(
287           base::FilePath(t.token()));
288     }
289   }
290   extension_service_->Init();
291
292   // Make the chrome://extension-icon/ resource available.
293   content::URLDataSource::Add(profile_, new ExtensionIconSource(profile_));
294
295   extension_warning_service_.reset(new ExtensionWarningService(profile_));
296   extension_warning_badge_service_.reset(
297       new ExtensionWarningBadgeService(profile_));
298   extension_warning_service_->AddObserver(
299       extension_warning_badge_service_.get());
300   error_console_.reset(new ErrorConsole(profile_));
301   quota_service_.reset(new QuotaService);
302
303   if (extensions_enabled) {
304     // Load any extensions specified with --load-extension.
305     // TODO(yoz): Seems like this should move into ExtensionService::Init.
306     // But maybe it's no longer important.
307     if (command_line->HasSwitch(switches::kLoadExtension)) {
308       CommandLine::StringType path_list = command_line->GetSwitchValueNative(
309           switches::kLoadExtension);
310       base::StringTokenizerT<CommandLine::StringType,
311           CommandLine::StringType::const_iterator> t(path_list,
312                                                      FILE_PATH_LITERAL(","));
313       while (t.GetNext()) {
314         std::string extension_id;
315         UnpackedInstaller::Create(extension_service_.get())->
316             LoadFromCommandLine(base::FilePath(t.token()), &extension_id);
317       }
318     }
319   }
320 }
321
322 void ExtensionSystemImpl::Shared::Shutdown() {
323   if (extension_warning_service_) {
324     extension_warning_service_->RemoveObserver(
325         extension_warning_badge_service_.get());
326   }
327   if (content_verifier_)
328     content_verifier_->Shutdown();
329   if (extension_service_)
330     extension_service_->Shutdown();
331 }
332
333 StateStore* ExtensionSystemImpl::Shared::state_store() {
334   return state_store_.get();
335 }
336
337 StateStore* ExtensionSystemImpl::Shared::rules_store() {
338   return rules_store_.get();
339 }
340
341 ExtensionService* ExtensionSystemImpl::Shared::extension_service() {
342   return extension_service_.get();
343 }
344
345 RuntimeData* ExtensionSystemImpl::Shared::runtime_data() {
346   return runtime_data_.get();
347 }
348
349 ManagementPolicy* ExtensionSystemImpl::Shared::management_policy() {
350   return management_policy_.get();
351 }
352
353 UserScriptMaster* ExtensionSystemImpl::Shared::user_script_master() {
354   return user_script_master_.get();
355 }
356
357 InfoMap* ExtensionSystemImpl::Shared::info_map() {
358   if (!extension_info_map_.get())
359     extension_info_map_ = new InfoMap();
360   return extension_info_map_.get();
361 }
362
363 LazyBackgroundTaskQueue*
364     ExtensionSystemImpl::Shared::lazy_background_task_queue() {
365   return lazy_background_task_queue_.get();
366 }
367
368 EventRouter* ExtensionSystemImpl::Shared::event_router() {
369   return event_router_.get();
370 }
371
372 ExtensionWarningService* ExtensionSystemImpl::Shared::warning_service() {
373   return extension_warning_service_.get();
374 }
375
376 Blacklist* ExtensionSystemImpl::Shared::blacklist() {
377   return blacklist_.get();
378 }
379
380 ErrorConsole* ExtensionSystemImpl::Shared::error_console() {
381   return error_console_.get();
382 }
383
384 InstallVerifier* ExtensionSystemImpl::Shared::install_verifier() {
385   return install_verifier_.get();
386 }
387
388 QuotaService* ExtensionSystemImpl::Shared::quota_service() {
389   return quota_service_.get();
390 }
391
392 ContentVerifier* ExtensionSystemImpl::Shared::content_verifier() {
393   return content_verifier_.get();
394 }
395
396 //
397 // ExtensionSystemImpl
398 //
399
400 ExtensionSystemImpl::ExtensionSystemImpl(Profile* profile)
401     : profile_(profile) {
402   shared_ = ExtensionSystemSharedFactory::GetForBrowserContext(profile);
403
404   if (profile->IsOffTheRecord()) {
405     process_manager_.reset(ProcessManager::Create(profile));
406   } else {
407     shared_->InitPrefs();
408   }
409 }
410
411 ExtensionSystemImpl::~ExtensionSystemImpl() {
412 }
413
414 void ExtensionSystemImpl::Shutdown() {
415   process_manager_.reset();
416 }
417
418 void ExtensionSystemImpl::InitForRegularProfile(bool extensions_enabled) {
419   DCHECK(!profile_->IsOffTheRecord());
420   if (user_script_master() || extension_service())
421     return;  // Already initialized.
422
423   // The InfoMap needs to be created before the ProcessManager.
424   shared_->info_map();
425
426   process_manager_.reset(ProcessManager::Create(profile_));
427
428   shared_->Init(extensions_enabled);
429 }
430
431 ExtensionService* ExtensionSystemImpl::extension_service() {
432   return shared_->extension_service();
433 }
434
435 RuntimeData* ExtensionSystemImpl::runtime_data() {
436   return shared_->runtime_data();
437 }
438
439 ManagementPolicy* ExtensionSystemImpl::management_policy() {
440   return shared_->management_policy();
441 }
442
443 UserScriptMaster* ExtensionSystemImpl::user_script_master() {
444   return shared_->user_script_master();
445 }
446
447 ProcessManager* ExtensionSystemImpl::process_manager() {
448   return process_manager_.get();
449 }
450
451 StateStore* ExtensionSystemImpl::state_store() {
452   return shared_->state_store();
453 }
454
455 StateStore* ExtensionSystemImpl::rules_store() {
456   return shared_->rules_store();
457 }
458
459 InfoMap* ExtensionSystemImpl::info_map() { return shared_->info_map(); }
460
461 LazyBackgroundTaskQueue* ExtensionSystemImpl::lazy_background_task_queue() {
462   return shared_->lazy_background_task_queue();
463 }
464
465 EventRouter* ExtensionSystemImpl::event_router() {
466   return shared_->event_router();
467 }
468
469 ExtensionWarningService* ExtensionSystemImpl::warning_service() {
470   return shared_->warning_service();
471 }
472
473 Blacklist* ExtensionSystemImpl::blacklist() {
474   return shared_->blacklist();
475 }
476
477 const OneShotEvent& ExtensionSystemImpl::ready() const {
478   return shared_->ready();
479 }
480
481 ErrorConsole* ExtensionSystemImpl::error_console() {
482   return shared_->error_console();
483 }
484
485 InstallVerifier* ExtensionSystemImpl::install_verifier() {
486   return shared_->install_verifier();
487 }
488
489 QuotaService* ExtensionSystemImpl::quota_service() {
490   return shared_->quota_service();
491 }
492
493 ContentVerifier* ExtensionSystemImpl::content_verifier() {
494   return shared_->content_verifier();
495 }
496
497 void ExtensionSystemImpl::RegisterExtensionWithRequestContexts(
498     const Extension* extension) {
499   base::Time install_time;
500   if (extension->location() != Manifest::COMPONENT) {
501     install_time = ExtensionPrefs::Get(profile_)->
502         GetInstallTime(extension->id());
503   }
504   bool incognito_enabled = util::IsIncognitoEnabled(extension->id(), profile_);
505
506   bool notifications_disabled = false;
507 #if defined(ENABLE_NOTIFICATIONS)
508   message_center::NotifierId notifier_id(
509       message_center::NotifierId::APPLICATION,
510       extension->id());
511
512   DesktopNotificationService* notification_service =
513       DesktopNotificationServiceFactory::GetForProfile(profile_);
514   notifications_disabled =
515       !notification_service->IsNotifierEnabled(notifier_id);
516 #endif
517
518   BrowserThread::PostTask(
519       BrowserThread::IO, FROM_HERE,
520       base::Bind(&InfoMap::AddExtension, info_map(),
521                  make_scoped_refptr(extension), install_time,
522                  incognito_enabled, notifications_disabled));
523 }
524
525 void ExtensionSystemImpl::UnregisterExtensionWithRequestContexts(
526     const std::string& extension_id,
527     const UnloadedExtensionInfo::Reason reason) {
528   BrowserThread::PostTask(
529       BrowserThread::IO,
530       FROM_HERE,
531       base::Bind(&InfoMap::RemoveExtension, info_map(), extension_id, reason));
532 }
533
534 }  // namespace extensions