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