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/extension_prefs.h"
18 #include "extensions/browser/extension_registry.h"
19 #include "extensions/browser/info_map.h"
20 #include "extensions/browser/notification_types.h"
21 #include "extensions/browser/null_app_sorting.h"
22 #include "extensions/browser/quota_service.h"
23 #include "extensions/browser/runtime_data.h"
24 #include "extensions/browser/service_worker_manager.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
), weak_factory_(this) {}
36 ShellExtensionSystem::~ShellExtensionSystem() {
39 const Extension
* ShellExtensionSystem::LoadApp(const base::FilePath
& app_dir
) {
40 // app_shell only supports unpacked extensions.
41 // NOTE: If you add packed extension support consider removing the flag
42 // FOLLOW_SYMLINKS_ANYWHERE below. Packed extensions should not have symlinks.
43 CHECK(base::DirectoryExists(app_dir
)) << app_dir
.AsUTF8Unsafe();
44 int load_flags
= Extension::FOLLOW_SYMLINKS_ANYWHERE
;
45 std::string load_error
;
46 scoped_refptr
<Extension
> extension
= file_util::LoadExtension(
47 app_dir
, Manifest::COMMAND_LINE
, load_flags
, &load_error
);
48 if (!extension
.get()) {
49 LOG(ERROR
) << "Loading extension at " << app_dir
.value()
50 << " failed with: " << load_error
;
54 // TODO(jamescook): We may want to do some of these things here:
55 // * Create a PermissionsUpdater.
56 // * Call PermissionsUpdater::GrantActivePermissions().
57 // * Call ExtensionService::SatisfyImports().
58 // * Call ExtensionPrefs::OnExtensionInstalled().
59 // * Send NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED.
61 ExtensionRegistry::Get(browser_context_
)->AddEnabled(extension
.get());
63 RegisterExtensionWithRequestContexts(
66 &ShellExtensionSystem::OnExtensionRegisteredWithRequestContexts
,
67 weak_factory_
.GetWeakPtr(), extension
));
69 content::NotificationService::current()->Notify(
70 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
,
71 content::Source
<BrowserContext
>(browser_context_
),
72 content::Details
<const Extension
>(extension
.get()));
74 return extension
.get();
77 void ShellExtensionSystem::Init() {
78 // Inform the rest of the extensions system to start.
80 content::NotificationService::current()->Notify(
81 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED
,
82 content::Source
<BrowserContext
>(browser_context_
),
83 content::NotificationService::NoDetails());
86 void ShellExtensionSystem::LaunchApp(const ExtensionId
& extension_id
) {
87 // Send the onLaunched event.
88 DCHECK(ExtensionRegistry::Get(browser_context_
)
89 ->enabled_extensions()
90 .Contains(extension_id
));
91 const Extension
* extension
= ExtensionRegistry::Get(browser_context_
)
92 ->enabled_extensions()
93 .GetByID(extension_id
);
94 AppRuntimeEventRouter::DispatchOnLaunchedEvent(
95 browser_context_
, extension
, extensions::SOURCE_UNTRACKED
);
98 void ShellExtensionSystem::Shutdown() {
101 void ShellExtensionSystem::InitForRegularProfile(bool extensions_enabled
) {
102 service_worker_manager_
.reset(new ServiceWorkerManager(browser_context_
));
104 new RuntimeData(ExtensionRegistry::Get(browser_context_
)));
105 quota_service_
.reset(new QuotaService
);
106 app_sorting_
.reset(new NullAppSorting
);
109 ExtensionService
* ShellExtensionSystem::extension_service() {
113 RuntimeData
* ShellExtensionSystem::runtime_data() {
114 return runtime_data_
.get();
117 ManagementPolicy
* ShellExtensionSystem::management_policy() {
121 ServiceWorkerManager
* ShellExtensionSystem::service_worker_manager() {
122 return service_worker_manager_
.get();
125 SharedUserScriptMaster
* ShellExtensionSystem::shared_user_script_master() {
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 QuotaService
* ShellExtensionSystem::quota_service() {
144 return quota_service_
.get();
147 AppSorting
* ShellExtensionSystem::app_sorting() {
148 return app_sorting_
.get();
151 void ShellExtensionSystem::RegisterExtensionWithRequestContexts(
152 const Extension
* extension
,
153 const base::Closure
& callback
) {
154 BrowserThread::PostTaskAndReply(BrowserThread::IO
, FROM_HERE
,
155 base::Bind(&InfoMap::AddExtension
, info_map(),
156 make_scoped_refptr(extension
),
157 base::Time::Now(), false, false),
161 void ShellExtensionSystem::UnregisterExtensionWithRequestContexts(
162 const std::string
& extension_id
,
163 const UnloadedExtensionInfo::Reason reason
) {
166 const OneShotEvent
& ShellExtensionSystem::ready() const {
170 ContentVerifier
* ShellExtensionSystem::content_verifier() {
174 scoped_ptr
<ExtensionSet
> ShellExtensionSystem::GetDependentExtensions(
175 const Extension
* extension
) {
176 return make_scoped_ptr(new ExtensionSet());
179 void ShellExtensionSystem::OnExtensionRegisteredWithRequestContexts(
180 scoped_refptr
<Extension
> extension
) {
181 ExtensionRegistry
* registry
= ExtensionRegistry::Get(browser_context_
);
182 registry
->AddReady(extension
);
183 registry
->TriggerOnReady(extension
.get());
186 } // namespace extensions