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_registry.h"
21 #include "extensions/browser/info_map.h"
22 #include "extensions/browser/lazy_background_task_queue.h"
23 #include "extensions/browser/process_manager.h"
24 #include "extensions/browser/quota_service.h"
25 #include "extensions/browser/runtime_data.h"
27 using content::BrowserContext;
28 using content::BrowserThread;
30 namespace extensions {
32 ShellExtensionSystem::ShellExtensionSystem(BrowserContext* browser_context)
33 : browser_context_(browser_context) {
36 ShellExtensionSystem::~ShellExtensionSystem() {
39 bool ShellExtensionSystem::LoadAndLaunchApp(const base::FilePath& app_dir) {
40 std::string load_error;
41 scoped_refptr<Extension> extension =
42 extension_file_util::LoadExtension(app_dir,
43 extensions::Manifest::COMMAND_LINE,
47 LOG(ERROR) << "Loading extension at " << app_dir.value()
48 << " failed with: " << load_error;
52 // TODO(jamescook): We may want to do some of these things here:
53 // * Create a PermissionsUpdater.
54 // * Call PermissionsUpdater::GrantActivePermissions().
55 // * Call ExtensionService::SatisfyImports().
56 // * Call ExtensionPrefs::OnExtensionInstalled().
57 // * Send NOTIFICATION_EXTENSION_INSTALLED.
59 ExtensionRegistry::Get(browser_context_)->AddEnabled(extension);
61 RegisterExtensionWithRequestContexts(extension);
63 content::NotificationService::current()->Notify(
64 chrome::NOTIFICATION_EXTENSION_LOADED,
65 content::Source<BrowserContext>(browser_context_),
66 content::Details<const Extension>(extension));
68 // Inform the rest of the extensions system to start.
70 content::NotificationService::current()->Notify(
71 chrome::NOTIFICATION_EXTENSIONS_READY,
72 content::Source<BrowserContext>(browser_context_),
73 content::NotificationService::NoDetails());
75 // This is effectively the same behavior as
76 // extensions::AppEventRouter::DispatchOnLaunchedEvent without any dependency
77 // on ExtensionSystem or Profile.
78 scoped_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue());
79 launch_data->SetBoolean("isKioskSession", false);
80 scoped_ptr<base::ListValue> event_args(new base::ListValue());
81 event_args->Append(launch_data.release());
82 scoped_ptr<Event> event(
83 new Event("app.runtime.onLaunched", event_args.Pass()));
84 event_router_->DispatchEventWithLazyListener(extension->id(), event.Pass());
89 void ShellExtensionSystem::Shutdown() {
92 void ShellExtensionSystem::InitForRegularProfile(bool extensions_enabled) {
94 new RuntimeData(ExtensionRegistry::Get(browser_context_)));
95 lazy_background_task_queue_.reset(
96 new LazyBackgroundTaskQueue(browser_context_));
98 new EventRouter(browser_context_, ExtensionPrefs::Get(browser_context_)));
99 process_manager_.reset(ProcessManager::Create(browser_context_));
100 quota_service_.reset(new QuotaService);
103 ExtensionService* ShellExtensionSystem::extension_service() {
107 RuntimeData* ShellExtensionSystem::runtime_data() {
108 return runtime_data_.get();
111 ManagementPolicy* ShellExtensionSystem::management_policy() {
115 UserScriptMaster* ShellExtensionSystem::user_script_master() {
119 ProcessManager* ShellExtensionSystem::process_manager() {
120 return process_manager_.get();
123 StateStore* ShellExtensionSystem::state_store() {
127 StateStore* ShellExtensionSystem::rules_store() {
131 InfoMap* ShellExtensionSystem::info_map() {
132 if (!info_map_.get())
133 info_map_ = new InfoMap;
137 LazyBackgroundTaskQueue* ShellExtensionSystem::lazy_background_task_queue() {
138 return lazy_background_task_queue_.get();
141 EventRouter* ShellExtensionSystem::event_router() {
142 return event_router_.get();
145 ExtensionWarningService* ShellExtensionSystem::warning_service() {
149 Blacklist* ShellExtensionSystem::blacklist() {
153 ErrorConsole* ShellExtensionSystem::error_console() {
157 InstallVerifier* ShellExtensionSystem::install_verifier() {
161 QuotaService* ShellExtensionSystem::quota_service() {
162 return quota_service_.get();
165 void ShellExtensionSystem::RegisterExtensionWithRequestContexts(
166 const Extension* extension) {
167 BrowserThread::PostTask(
168 BrowserThread::IO, FROM_HERE,
169 base::Bind(&InfoMap::AddExtension, info_map(),
170 make_scoped_refptr(extension), base::Time::Now(),
174 void ShellExtensionSystem::UnregisterExtensionWithRequestContexts(
175 const std::string& extension_id,
176 const UnloadedExtensionInfo::Reason reason) {
179 const OneShotEvent& ShellExtensionSystem::ready() const {
183 } // namespace extensions