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.
5 #include "extensions/shell/browser/shell_extension_system.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/notification_details.h"
14 #include "content/public/browser/notification_service.h"
15 #include "content/public/browser/notification_source.h"
16 #include "extensions/browser/api/app_runtime/app_runtime_api.h"
17 #include "extensions/browser/event_router.h"
18 #include "extensions/browser/extension_prefs.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/browser/info_map.h"
21 #include "extensions/browser/lazy_background_task_queue.h"
22 #include "extensions/browser/notification_types.h"
23 #include "extensions/browser/quota_service.h"
24 #include "extensions/browser/runtime_data.h"
25 #include "extensions/common/constants.h"
26 #include "extensions/common/file_util.h"
28 using content::BrowserContext
;
29 using content::BrowserThread
;
31 namespace extensions
{
33 ShellExtensionSystem::ShellExtensionSystem(BrowserContext
* browser_context
)
34 : browser_context_(browser_context
) {
37 ShellExtensionSystem::~ShellExtensionSystem() {
40 const Extension
* ShellExtensionSystem::LoadApp(const base::FilePath
& app_dir
) {
41 // app_shell only supports unpacked extensions.
42 // NOTE: If you add packed extension support consider removing the flag
43 // FOLLOW_SYMLINKS_ANYWHERE below. Packed extensions should not have symlinks.
44 CHECK(base::DirectoryExists(app_dir
)) << app_dir
.AsUTF8Unsafe();
45 int load_flags
= Extension::FOLLOW_SYMLINKS_ANYWHERE
;
46 std::string load_error
;
47 scoped_refptr
<Extension
> extension
= file_util::LoadExtension(
48 app_dir
, Manifest::COMMAND_LINE
, load_flags
, &load_error
);
49 if (!extension
.get()) {
50 LOG(ERROR
) << "Loading extension at " << app_dir
.value()
51 << " failed with: " << load_error
;
55 // TODO(jamescook): We may want to do some of these things here:
56 // * Create a PermissionsUpdater.
57 // * Call PermissionsUpdater::GrantActivePermissions().
58 // * Call ExtensionService::SatisfyImports().
59 // * Call ExtensionPrefs::OnExtensionInstalled().
60 // * Send NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED.
62 ExtensionRegistry::Get(browser_context_
)->AddEnabled(extension
.get());
64 RegisterExtensionWithRequestContexts(extension
.get());
66 content::NotificationService::current()->Notify(
67 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
,
68 content::Source
<BrowserContext
>(browser_context_
),
69 content::Details
<const Extension
>(extension
.get()));
71 return extension
.get();
74 void ShellExtensionSystem::Init() {
75 // Inform the rest of the extensions system to start.
77 content::NotificationService::current()->Notify(
78 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED
,
79 content::Source
<BrowserContext
>(browser_context_
),
80 content::NotificationService::NoDetails());
83 void ShellExtensionSystem::LaunchApp(const ExtensionId
& extension_id
) {
84 // Send the onLaunched event.
85 DCHECK(ExtensionRegistry::Get(browser_context_
)
86 ->enabled_extensions()
87 .Contains(extension_id
));
88 const Extension
* extension
= ExtensionRegistry::Get(browser_context_
)
89 ->enabled_extensions()
90 .GetByID(extension_id
);
91 AppRuntimeEventRouter::DispatchOnLaunchedEvent(
92 browser_context_
, extension
, extensions::SOURCE_UNTRACKED
);
95 void ShellExtensionSystem::Shutdown() {
98 void ShellExtensionSystem::InitForRegularProfile(bool extensions_enabled
) {
100 new RuntimeData(ExtensionRegistry::Get(browser_context_
)));
101 lazy_background_task_queue_
.reset(
102 new LazyBackgroundTaskQueue(browser_context_
));
104 new EventRouter(browser_context_
, ExtensionPrefs::Get(browser_context_
)));
105 quota_service_
.reset(new QuotaService
);
108 ExtensionService
* ShellExtensionSystem::extension_service() {
112 RuntimeData
* ShellExtensionSystem::runtime_data() {
113 return runtime_data_
.get();
116 ManagementPolicy
* ShellExtensionSystem::management_policy() {
120 SharedUserScriptMaster
* ShellExtensionSystem::shared_user_script_master() {
124 DeclarativeUserScriptManager
*
125 ShellExtensionSystem::declarative_user_script_manager() {
129 StateStore
* ShellExtensionSystem::state_store() {
133 StateStore
* ShellExtensionSystem::rules_store() {
137 InfoMap
* ShellExtensionSystem::info_map() {
138 if (!info_map_
.get())
139 info_map_
= new InfoMap
;
140 return info_map_
.get();
143 LazyBackgroundTaskQueue
* ShellExtensionSystem::lazy_background_task_queue() {
144 return lazy_background_task_queue_
.get();
147 EventRouter
* ShellExtensionSystem::event_router() {
148 return event_router_
.get();
151 ErrorConsole
* ShellExtensionSystem::error_console() {
155 InstallVerifier
* ShellExtensionSystem::install_verifier() {
159 QuotaService
* ShellExtensionSystem::quota_service() {
160 return quota_service_
.get();
163 void ShellExtensionSystem::RegisterExtensionWithRequestContexts(
164 const Extension
* extension
) {
165 BrowserThread::PostTask(BrowserThread::IO
,
167 base::Bind(&InfoMap::AddExtension
,
169 make_scoped_refptr(extension
),
175 void ShellExtensionSystem::UnregisterExtensionWithRequestContexts(
176 const std::string
& extension_id
,
177 const UnloadedExtensionInfo::Reason reason
) {
180 const OneShotEvent
& ShellExtensionSystem::ready() const {
184 ContentVerifier
* ShellExtensionSystem::content_verifier() {
188 scoped_ptr
<ExtensionSet
> ShellExtensionSystem::GetDependentExtensions(
189 const Extension
* extension
) {
190 return make_scoped_ptr(new ExtensionSet());
193 } // namespace extensions