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 "apps/app_window_registry.h"
10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/common/extensions/extension_file_util.h"
14 #include "content/public/browser/browser_context.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/notification_details.h"
17 #include "content/public/browser/notification_service.h"
18 #include "content/public/browser/notification_source.h"
19 #include "extensions/browser/event_router.h"
20 #include "extensions/browser/extension_prefs.h"
21 #include "extensions/browser/extension_registry.h"
22 #include "extensions/browser/info_map.h"
23 #include "extensions/browser/lazy_background_task_queue.h"
24 #include "extensions/browser/process_manager.h"
25 #include "extensions/browser/quota_service.h"
26 #include "extensions/browser/runtime_data.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 bool ShellExtensionSystem::LoadAndLaunchApp(const base::FilePath& app_dir) {
41 std::string load_error;
42 scoped_refptr<Extension> extension =
43 extension_file_util::LoadExtension(app_dir,
44 extensions::Manifest::COMMAND_LINE,
48 LOG(ERROR) << "Loading extension at " << app_dir.value()
49 << " failed with: " << load_error;
52 app_id_ = extension->id();
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_INSTALLED.
61 ExtensionRegistry::Get(browser_context_)->AddEnabled(extension);
63 RegisterExtensionWithRequestContexts(extension);
65 content::NotificationService::current()->Notify(
66 chrome::NOTIFICATION_EXTENSION_LOADED,
67 content::Source<BrowserContext>(browser_context_),
68 content::Details<const Extension>(extension));
70 // Inform the rest of the extensions system to start.
72 content::NotificationService::current()->Notify(
73 chrome::NOTIFICATION_EXTENSIONS_READY,
74 content::Source<BrowserContext>(browser_context_),
75 content::NotificationService::NoDetails());
77 // This is effectively the same behavior as
78 // extensions::AppEventRouter::DispatchOnLaunchedEvent without any dependency
79 // on ExtensionSystem or Profile.
80 scoped_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue());
81 launch_data->SetBoolean("isKioskSession", false);
82 scoped_ptr<base::ListValue> event_args(new base::ListValue());
83 event_args->Append(launch_data.release());
84 scoped_ptr<Event> event(
85 new Event("app.runtime.onLaunched", event_args.Pass()));
86 event_router_->DispatchEventWithLazyListener(extension->id(), event.Pass());
91 void ShellExtensionSystem::CloseApp() {
92 apps::AppWindowRegistry::Get(browser_context_)
93 ->CloseAllAppWindowsForApp(app_id_);
96 void ShellExtensionSystem::Shutdown() {
99 void ShellExtensionSystem::InitForRegularProfile(bool extensions_enabled) {
101 new RuntimeData(ExtensionRegistry::Get(browser_context_)));
102 lazy_background_task_queue_.reset(
103 new LazyBackgroundTaskQueue(browser_context_));
105 new EventRouter(browser_context_, ExtensionPrefs::Get(browser_context_)));
106 process_manager_.reset(ProcessManager::Create(browser_context_));
107 quota_service_.reset(new QuotaService);
110 ExtensionService* ShellExtensionSystem::extension_service() {
114 RuntimeData* ShellExtensionSystem::runtime_data() {
115 return runtime_data_.get();
118 ManagementPolicy* ShellExtensionSystem::management_policy() {
122 UserScriptMaster* ShellExtensionSystem::user_script_master() {
126 ProcessManager* ShellExtensionSystem::process_manager() {
127 return process_manager_.get();
130 StateStore* ShellExtensionSystem::state_store() {
134 StateStore* ShellExtensionSystem::rules_store() {
138 InfoMap* ShellExtensionSystem::info_map() {
139 if (!info_map_.get())
140 info_map_ = new InfoMap;
144 LazyBackgroundTaskQueue* ShellExtensionSystem::lazy_background_task_queue() {
145 return lazy_background_task_queue_.get();
148 EventRouter* ShellExtensionSystem::event_router() {
149 return event_router_.get();
152 ExtensionWarningService* ShellExtensionSystem::warning_service() {
156 Blacklist* ShellExtensionSystem::blacklist() {
160 ErrorConsole* ShellExtensionSystem::error_console() {
164 InstallVerifier* ShellExtensionSystem::install_verifier() {
168 QuotaService* ShellExtensionSystem::quota_service() {
169 return quota_service_.get();
172 void ShellExtensionSystem::RegisterExtensionWithRequestContexts(
173 const Extension* extension) {
174 BrowserThread::PostTask(
175 BrowserThread::IO, FROM_HERE,
176 base::Bind(&InfoMap::AddExtension, info_map(),
177 make_scoped_refptr(extension), base::Time::Now(),
181 void ShellExtensionSystem::UnregisterExtensionWithRequestContexts(
182 const std::string& extension_id,
183 const UnloadedExtensionInfo::Reason reason) {
186 const OneShotEvent& ShellExtensionSystem::ready() const {
190 } // namespace extensions