1 // Copyright 2013 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 "apps/shell/browser/shell_extension_system.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/common/extensions/extension_file_util.h"
13 #include "content/public/browser/browser_context.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/notification_details.h"
16 #include "content/public/browser/notification_service.h"
17 #include "content/public/browser/notification_source.h"
18 #include "extensions/browser/event_router.h"
19 #include "extensions/browser/extension_prefs.h"
20 #include "extensions/browser/extension_prefs_factory.h"
21 #include "extensions/browser/extension_registry.h"
22 #include "extensions/browser/extension_registry_factory.h"
23 #include "extensions/browser/info_map.h"
24 #include "extensions/browser/lazy_background_task_queue.h"
25 #include "extensions/browser/process_manager.h"
26 #include "extensions/browser/quota_service.h"
27 #include "extensions/browser/runtime_data.h"
29 using content::BrowserContext
;
30 using content::BrowserThread
;
32 namespace extensions
{
34 ShellExtensionSystem::ShellExtensionSystem(BrowserContext
* browser_context
)
35 : browser_context_(browser_context
) {
38 ShellExtensionSystem::~ShellExtensionSystem() {
42 std::vector
<BrowserContextKeyedServiceFactory
*>
43 ShellExtensionSystem::GetDependencies() {
44 std::vector
<BrowserContextKeyedServiceFactory
*> depends_on
;
45 depends_on
.push_back(ExtensionPrefsFactory::GetInstance());
46 depends_on
.push_back(ExtensionRegistryFactory::GetInstance());
50 bool ShellExtensionSystem::LoadAndLaunchApp(const base::FilePath
& app_dir
) {
51 std::string load_error
;
52 scoped_refptr
<Extension
> extension
=
53 extension_file_util::LoadExtension(app_dir
,
54 extensions::Manifest::COMMAND_LINE
,
58 LOG(ERROR
) << "Loading extension at " << app_dir
.value()
59 << " failed with: " << load_error
;
63 // TODO(jamescook): We may want to do some of these things here:
64 // * Create a PermissionsUpdater.
65 // * Call PermissionsUpdater::GrantActivePermissions().
66 // * Call ExtensionService::SatisfyImports().
67 // * Call ExtensionPrefs::OnExtensionInstalled().
68 // * Send NOTIFICATION_EXTENSION_INSTALLED.
70 ExtensionRegistry::Get(browser_context_
)->AddEnabled(extension
);
72 RegisterExtensionWithRequestContexts(extension
);
74 content::NotificationService::current()->Notify(
75 chrome::NOTIFICATION_EXTENSION_LOADED
,
76 content::Source
<BrowserContext
>(browser_context_
),
77 content::Details
<const Extension
>(extension
));
79 // Inform the rest of the extensions system to start.
81 content::NotificationService::current()->Notify(
82 chrome::NOTIFICATION_EXTENSIONS_READY
,
83 content::Source
<BrowserContext
>(browser_context_
),
84 content::NotificationService::NoDetails());
86 // This is effectively the same behavior as
87 // extensions::AppEventRouter::DispatchOnLaunchedEvent without any dependency
88 // on ExtensionSystem or Profile.
89 scoped_ptr
<base::DictionaryValue
> launch_data(new base::DictionaryValue());
90 launch_data
->SetBoolean("isKioskSession", false);
91 scoped_ptr
<base::ListValue
> event_args(new base::ListValue());
92 event_args
->Append(launch_data
.release());
93 scoped_ptr
<Event
> event(
94 new Event("app.runtime.onLaunched", event_args
.Pass()));
95 event_router_
->DispatchEventWithLazyListener(extension
->id(), event
.Pass());
100 void ShellExtensionSystem::Shutdown() {
103 void ShellExtensionSystem::InitForRegularProfile(bool extensions_enabled
) {
105 new RuntimeData(ExtensionRegistry::Get(browser_context_
)));
106 lazy_background_task_queue_
.reset(
107 new LazyBackgroundTaskQueue(browser_context_
));
109 new EventRouter(browser_context_
, ExtensionPrefs::Get(browser_context_
)));
110 process_manager_
.reset(ProcessManager::Create(browser_context_
));
111 quota_service_
.reset(new QuotaService
);
114 ExtensionService
* ShellExtensionSystem::extension_service() {
118 RuntimeData
* ShellExtensionSystem::runtime_data() {
119 return runtime_data_
.get();
122 ManagementPolicy
* ShellExtensionSystem::management_policy() {
126 UserScriptMaster
* ShellExtensionSystem::user_script_master() {
130 ProcessManager
* ShellExtensionSystem::process_manager() {
131 return process_manager_
.get();
134 StateStore
* ShellExtensionSystem::state_store() {
138 StateStore
* ShellExtensionSystem::rules_store() {
142 InfoMap
* ShellExtensionSystem::info_map() {
143 if (!info_map_
.get())
144 info_map_
= new InfoMap
;
148 LazyBackgroundTaskQueue
* ShellExtensionSystem::lazy_background_task_queue() {
149 return lazy_background_task_queue_
.get();
152 EventRouter
* ShellExtensionSystem::event_router() {
153 return event_router_
.get();
156 ExtensionWarningService
* ShellExtensionSystem::warning_service() {
160 Blacklist
* ShellExtensionSystem::blacklist() {
164 ErrorConsole
* ShellExtensionSystem::error_console() {
168 InstallVerifier
* ShellExtensionSystem::install_verifier() {
172 QuotaService
* ShellExtensionSystem::quota_service() {
173 return quota_service_
.get();
176 void ShellExtensionSystem::RegisterExtensionWithRequestContexts(
177 const Extension
* extension
) {
178 BrowserThread::PostTask(
179 BrowserThread::IO
, FROM_HERE
,
180 base::Bind(&InfoMap::AddExtension
, info_map(),
181 make_scoped_refptr(extension
), base::Time::Now(),
185 void ShellExtensionSystem::UnregisterExtensionWithRequestContexts(
186 const std::string
& extension_id
,
187 const UnloadedExtensionInfo::Reason reason
) {
190 const OneShotEvent
& ShellExtensionSystem::ready() const {
194 } // namespace extensions