Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / extensions / shell / browser / shell_extension_system.cc
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.
4
5 #include "extensions/shell/browser/shell_extension_system.h"
6
7 #include <string>
8
9 #include "base/file_util.h"
10 #include "base/files/file_path.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/event_router.h"
18 #include "extensions/browser/extension_prefs.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/browser/info_map.h"
21 #include "extensions/browser/lazy_background_task_queue.h"
22 #include "extensions/browser/notification_types.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"
27 #include "extensions/shell/browser/api/shell/shell_api.h"
28
29 using content::BrowserContext;
30 using content::BrowserThread;
31
32 namespace extensions {
33
34 ShellExtensionSystem::ShellExtensionSystem(BrowserContext* browser_context)
35     : browser_context_(browser_context) {
36 }
37
38 ShellExtensionSystem::~ShellExtensionSystem() {
39 }
40
41 bool ShellExtensionSystem::LoadApp(const base::FilePath& app_dir) {
42   // app_shell only supports unpacked extensions.
43   // NOTE: If you add packed extension support consider removing the flag
44   // FOLLOW_SYMLINKS_ANYWHERE below. Packed extensions should not have symlinks.
45   CHECK(base::DirectoryExists(app_dir)) << app_dir.AsUTF8Unsafe();
46   int load_flags = Extension::FOLLOW_SYMLINKS_ANYWHERE;
47   std::string load_error;
48   extension_ = file_util::LoadExtension(
49       app_dir, Manifest::COMMAND_LINE, load_flags, &load_error);
50   if (!extension_) {
51     LOG(ERROR) << "Loading extension at " << app_dir.value()
52                << " failed with: " << load_error;
53     return false;
54   }
55   app_id_ = extension_->id();
56
57   // TODO(jamescook): We may want to do some of these things here:
58   // * Create a PermissionsUpdater.
59   // * Call PermissionsUpdater::GrantActivePermissions().
60   // * Call ExtensionService::SatisfyImports().
61   // * Call ExtensionPrefs::OnExtensionInstalled().
62   // * Send NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED.
63
64   ExtensionRegistry::Get(browser_context_)->AddEnabled(extension_);
65
66   RegisterExtensionWithRequestContexts(extension_);
67
68   content::NotificationService::current()->Notify(
69       extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
70       content::Source<BrowserContext>(browser_context_),
71       content::Details<const Extension>(extension_));
72
73   // Inform the rest of the extensions system to start.
74   ready_.Signal();
75   content::NotificationService::current()->Notify(
76       extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
77       content::Source<BrowserContext>(browser_context_),
78       content::NotificationService::NoDetails());
79   return true;
80 }
81
82 void ShellExtensionSystem::LaunchApp() {
83   // Send the onLaunched event.
84   DCHECK(extension_.get());
85   AppRuntimeEventRouter::DispatchOnLaunchedEvent(browser_context_,
86                                                  extension_.get());
87 }
88
89 void ShellExtensionSystem::Shutdown() {
90 }
91
92 void ShellExtensionSystem::InitForRegularProfile(bool extensions_enabled) {
93   runtime_data_.reset(
94       new RuntimeData(ExtensionRegistry::Get(browser_context_)));
95   lazy_background_task_queue_.reset(
96       new LazyBackgroundTaskQueue(browser_context_));
97   event_router_.reset(
98       new EventRouter(browser_context_, ExtensionPrefs::Get(browser_context_)));
99   process_manager_.reset(ProcessManager::Create(browser_context_));
100   quota_service_.reset(new QuotaService);
101 }
102
103 ExtensionService* ShellExtensionSystem::extension_service() {
104   return NULL;
105 }
106
107 RuntimeData* ShellExtensionSystem::runtime_data() {
108   return runtime_data_.get();
109 }
110
111 ManagementPolicy* ShellExtensionSystem::management_policy() {
112   return NULL;
113 }
114
115 SharedUserScriptMaster* ShellExtensionSystem::shared_user_script_master() {
116   return NULL;
117 }
118
119 ProcessManager* ShellExtensionSystem::process_manager() {
120   return process_manager_.get();
121 }
122
123 StateStore* ShellExtensionSystem::state_store() {
124   return NULL;
125 }
126
127 StateStore* ShellExtensionSystem::rules_store() {
128   return NULL;
129 }
130
131 InfoMap* ShellExtensionSystem::info_map() {
132   if (!info_map_.get())
133     info_map_ = new InfoMap;
134   return info_map_;
135 }
136
137 LazyBackgroundTaskQueue* ShellExtensionSystem::lazy_background_task_queue() {
138   return lazy_background_task_queue_.get();
139 }
140
141 EventRouter* ShellExtensionSystem::event_router() {
142   return event_router_.get();
143 }
144
145 ExtensionWarningService* ShellExtensionSystem::warning_service() {
146   return NULL;
147 }
148
149 Blacklist* ShellExtensionSystem::blacklist() {
150   return NULL;
151 }
152
153 ErrorConsole* ShellExtensionSystem::error_console() {
154   return NULL;
155 }
156
157 InstallVerifier* ShellExtensionSystem::install_verifier() {
158   return NULL;
159 }
160
161 QuotaService* ShellExtensionSystem::quota_service() {
162   return quota_service_.get();
163 }
164
165 void ShellExtensionSystem::RegisterExtensionWithRequestContexts(
166     const Extension* extension) {
167   BrowserThread::PostTask(BrowserThread::IO,
168                           FROM_HERE,
169                           base::Bind(&InfoMap::AddExtension,
170                                      info_map(),
171                                      make_scoped_refptr(extension),
172                                      base::Time::Now(),
173                                      false,
174                                      false));
175 }
176
177 void ShellExtensionSystem::UnregisterExtensionWithRequestContexts(
178     const std::string& extension_id,
179     const UnloadedExtensionInfo::Reason reason) {
180 }
181
182 const OneShotEvent& ShellExtensionSystem::ready() const {
183   return ready_;
184 }
185
186 ContentVerifier* ShellExtensionSystem::content_verifier() {
187   return NULL;
188 }
189
190 scoped_ptr<ExtensionSet> ShellExtensionSystem::GetDependentExtensions(
191     const Extension* extension) {
192   scoped_ptr<ExtensionSet> empty(new ExtensionSet());
193   return empty.PassAs<ExtensionSet>();
194 }
195
196 DeclarativeUserScriptMaster*
197 ShellExtensionSystem::GetDeclarativeUserScriptMasterByExtension(
198     const ExtensionId& extension_id) {
199   return NULL;
200 }
201
202 }  // namespace extensions