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/shell/browser/api/shell/shell_api.h"
10 #include "base/file_util.h"
11 #include "base/files/file_path.h"
12 #include "chrome/browser/chrome_notification_types.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"
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 bool 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 extension_ = file_util::LoadExtension(
48 app_dir, Manifest::COMMAND_LINE, load_flags, &load_error);
50 LOG(ERROR) << "Loading extension at " << app_dir.value()
51 << " failed with: " << load_error;
54 app_id_ = extension_->id();
56 // TODO(jamescook): We may want to do some of these things here:
57 // * Create a PermissionsUpdater.
58 // * Call PermissionsUpdater::GrantActivePermissions().
59 // * Call ExtensionService::SatisfyImports().
60 // * Call ExtensionPrefs::OnExtensionInstalled().
61 // * Send NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED.
63 ExtensionRegistry::Get(browser_context_)->AddEnabled(extension_);
65 RegisterExtensionWithRequestContexts(extension_);
67 content::NotificationService::current()->Notify(
68 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
69 content::Source<BrowserContext>(browser_context_),
70 content::Details<const Extension>(extension_));
72 // Inform the rest of the extensions system to start.
74 content::NotificationService::current()->Notify(
75 chrome::NOTIFICATION_EXTENSIONS_READY,
76 content::Source<BrowserContext>(browser_context_),
77 content::NotificationService::NoDetails());
81 void ShellExtensionSystem::LaunchApp() {
82 // Send the onLaunched event.
83 DCHECK(extension_.get());
84 apps::ShellAPI::DispatchOnLaunchedEvent(event_router_.get(),
88 void ShellExtensionSystem::Shutdown() {
91 void ShellExtensionSystem::InitForRegularProfile(bool extensions_enabled) {
93 new RuntimeData(ExtensionRegistry::Get(browser_context_)));
94 lazy_background_task_queue_.reset(
95 new LazyBackgroundTaskQueue(browser_context_));
97 new EventRouter(browser_context_, ExtensionPrefs::Get(browser_context_)));
98 process_manager_.reset(ProcessManager::Create(browser_context_));
99 quota_service_.reset(new QuotaService);
102 ExtensionService* ShellExtensionSystem::extension_service() {
106 RuntimeData* ShellExtensionSystem::runtime_data() {
107 return runtime_data_.get();
110 ManagementPolicy* ShellExtensionSystem::management_policy() {
114 UserScriptMaster* ShellExtensionSystem::user_script_master() {
118 ProcessManager* ShellExtensionSystem::process_manager() {
119 return process_manager_.get();
122 StateStore* ShellExtensionSystem::state_store() {
126 StateStore* ShellExtensionSystem::rules_store() {
130 InfoMap* ShellExtensionSystem::info_map() {
131 if (!info_map_.get())
132 info_map_ = new InfoMap;
136 LazyBackgroundTaskQueue* ShellExtensionSystem::lazy_background_task_queue() {
137 return lazy_background_task_queue_.get();
140 EventRouter* ShellExtensionSystem::event_router() {
141 return event_router_.get();
144 ExtensionWarningService* ShellExtensionSystem::warning_service() {
148 Blacklist* ShellExtensionSystem::blacklist() {
152 ErrorConsole* ShellExtensionSystem::error_console() {
156 InstallVerifier* ShellExtensionSystem::install_verifier() {
160 QuotaService* ShellExtensionSystem::quota_service() {
161 return quota_service_.get();
164 void ShellExtensionSystem::RegisterExtensionWithRequestContexts(
165 const Extension* extension) {
166 BrowserThread::PostTask(
167 BrowserThread::IO, FROM_HERE,
168 base::Bind(&InfoMap::AddExtension, info_map(),
169 make_scoped_refptr(extension), base::Time::Now(),
173 void ShellExtensionSystem::UnregisterExtensionWithRequestContexts(
174 const std::string& extension_id,
175 const UnloadedExtensionInfo::Reason reason) {
178 const OneShotEvent& ShellExtensionSystem::ready() const {
182 ContentVerifier* ShellExtensionSystem::content_verifier() {
186 scoped_ptr<ExtensionSet> ShellExtensionSystem::GetDependentExtensions(
187 const Extension* extension) {
188 scoped_ptr<ExtensionSet> empty(new ExtensionSet());
189 return empty.PassAs<ExtensionSet>();
192 } // namespace extensions