Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / apps / shell / browser / shell_extension_system.cc
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.
4
5 #include "apps/shell/browser/shell_extension_system.h"
6
7 #include <string>
8
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"
27
28 using content::BrowserContext;
29 using content::BrowserThread;
30
31 namespace extensions {
32
33 ShellExtensionSystem::ShellExtensionSystem(BrowserContext* browser_context)
34     : browser_context_(browser_context) {
35 }
36
37 ShellExtensionSystem::~ShellExtensionSystem() {
38 }
39
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);
49   if (!extension_) {
50     LOG(ERROR) << "Loading extension at " << app_dir.value()
51         << " failed with: " << load_error;
52     return false;
53   }
54   app_id_ = extension_->id();
55
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.
62
63   ExtensionRegistry::Get(browser_context_)->AddEnabled(extension_);
64
65   RegisterExtensionWithRequestContexts(extension_);
66
67   content::NotificationService::current()->Notify(
68       chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
69       content::Source<BrowserContext>(browser_context_),
70       content::Details<const Extension>(extension_));
71
72   // Inform the rest of the extensions system to start.
73   ready_.Signal();
74   content::NotificationService::current()->Notify(
75       chrome::NOTIFICATION_EXTENSIONS_READY,
76       content::Source<BrowserContext>(browser_context_),
77       content::NotificationService::NoDetails());
78   return true;
79 }
80
81 void ShellExtensionSystem::LaunchApp() {
82   // Send the onLaunched event.
83   DCHECK(extension_.get());
84   apps::ShellAPI::DispatchOnLaunchedEvent(event_router_.get(),
85                                           extension_.get());
86 }
87
88 void ShellExtensionSystem::Shutdown() {
89 }
90
91 void ShellExtensionSystem::InitForRegularProfile(bool extensions_enabled) {
92   runtime_data_.reset(
93       new RuntimeData(ExtensionRegistry::Get(browser_context_)));
94   lazy_background_task_queue_.reset(
95       new LazyBackgroundTaskQueue(browser_context_));
96   event_router_.reset(
97       new EventRouter(browser_context_, ExtensionPrefs::Get(browser_context_)));
98   process_manager_.reset(ProcessManager::Create(browser_context_));
99   quota_service_.reset(new QuotaService);
100 }
101
102 ExtensionService* ShellExtensionSystem::extension_service() {
103   return NULL;
104 }
105
106 RuntimeData* ShellExtensionSystem::runtime_data() {
107   return runtime_data_.get();
108 }
109
110 ManagementPolicy* ShellExtensionSystem::management_policy() {
111   return NULL;
112 }
113
114 UserScriptMaster* ShellExtensionSystem::user_script_master() {
115   return NULL;
116 }
117
118 ProcessManager* ShellExtensionSystem::process_manager() {
119   return process_manager_.get();
120 }
121
122 StateStore* ShellExtensionSystem::state_store() {
123   return NULL;
124 }
125
126 StateStore* ShellExtensionSystem::rules_store() {
127   return NULL;
128 }
129
130 InfoMap* ShellExtensionSystem::info_map() {
131   if (!info_map_.get())
132     info_map_ = new InfoMap;
133   return info_map_;
134 }
135
136 LazyBackgroundTaskQueue* ShellExtensionSystem::lazy_background_task_queue() {
137   return lazy_background_task_queue_.get();
138 }
139
140 EventRouter* ShellExtensionSystem::event_router() {
141   return event_router_.get();
142 }
143
144 ExtensionWarningService* ShellExtensionSystem::warning_service() {
145   return NULL;
146 }
147
148 Blacklist* ShellExtensionSystem::blacklist() {
149   return NULL;
150 }
151
152 ErrorConsole* ShellExtensionSystem::error_console() {
153   return NULL;
154 }
155
156 InstallVerifier* ShellExtensionSystem::install_verifier() {
157   return NULL;
158 }
159
160 QuotaService* ShellExtensionSystem::quota_service() {
161   return quota_service_.get();
162 }
163
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(),
170                  false, false));
171 }
172
173 void ShellExtensionSystem::UnregisterExtensionWithRequestContexts(
174     const std::string& extension_id,
175     const UnloadedExtensionInfo::Reason reason) {
176 }
177
178 const OneShotEvent& ShellExtensionSystem::ready() const {
179   return ready_;
180 }
181
182 ContentVerifier* ShellExtensionSystem::content_verifier() {
183   return NULL;
184 }
185
186 scoped_ptr<ExtensionSet> ShellExtensionSystem::GetDependentExtensions(
187     const Extension* extension) {
188   scoped_ptr<ExtensionSet> empty(new ExtensionSet());
189   return empty.PassAs<ExtensionSet>();
190 }
191
192 }  // namespace extensions