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