6defb0edaff34d5677fd624150405aa3bc73248d
[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/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"
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::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,
45                                          Extension::NO_FLAGS,
46                                          &load_error);
47   if (!extension) {
48     LOG(ERROR) << "Loading extension at " << app_dir.value()
49         << " failed with: " << load_error;
50     return false;
51   }
52   app_id_ = extension->id();
53
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.
60
61   ExtensionRegistry::Get(browser_context_)->AddEnabled(extension);
62
63   RegisterExtensionWithRequestContexts(extension);
64
65   content::NotificationService::current()->Notify(
66       chrome::NOTIFICATION_EXTENSION_LOADED,
67       content::Source<BrowserContext>(browser_context_),
68       content::Details<const Extension>(extension));
69
70   // Inform the rest of the extensions system to start.
71   ready_.Signal();
72   content::NotificationService::current()->Notify(
73       chrome::NOTIFICATION_EXTENSIONS_READY,
74       content::Source<BrowserContext>(browser_context_),
75       content::NotificationService::NoDetails());
76
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());
87
88   return true;
89 }
90
91 void ShellExtensionSystem::CloseApp() {
92   apps::AppWindowRegistry::Get(browser_context_)
93       ->CloseAllAppWindowsForApp(app_id_);
94 }
95
96 void ShellExtensionSystem::Shutdown() {
97 }
98
99 void ShellExtensionSystem::InitForRegularProfile(bool extensions_enabled) {
100   runtime_data_.reset(
101       new RuntimeData(ExtensionRegistry::Get(browser_context_)));
102   lazy_background_task_queue_.reset(
103       new LazyBackgroundTaskQueue(browser_context_));
104   event_router_.reset(
105       new EventRouter(browser_context_, ExtensionPrefs::Get(browser_context_)));
106   process_manager_.reset(ProcessManager::Create(browser_context_));
107   quota_service_.reset(new QuotaService);
108 }
109
110 ExtensionService* ShellExtensionSystem::extension_service() {
111   return NULL;
112 }
113
114 RuntimeData* ShellExtensionSystem::runtime_data() {
115   return runtime_data_.get();
116 }
117
118 ManagementPolicy* ShellExtensionSystem::management_policy() {
119   return NULL;
120 }
121
122 UserScriptMaster* ShellExtensionSystem::user_script_master() {
123   return NULL;
124 }
125
126 ProcessManager* ShellExtensionSystem::process_manager() {
127   return process_manager_.get();
128 }
129
130 StateStore* ShellExtensionSystem::state_store() {
131   return NULL;
132 }
133
134 StateStore* ShellExtensionSystem::rules_store() {
135   return NULL;
136 }
137
138 InfoMap* ShellExtensionSystem::info_map() {
139   if (!info_map_.get())
140     info_map_ = new InfoMap;
141   return info_map_;
142 }
143
144 LazyBackgroundTaskQueue* ShellExtensionSystem::lazy_background_task_queue() {
145   return lazy_background_task_queue_.get();
146 }
147
148 EventRouter* ShellExtensionSystem::event_router() {
149   return event_router_.get();
150 }
151
152 ExtensionWarningService* ShellExtensionSystem::warning_service() {
153   return NULL;
154 }
155
156 Blacklist* ShellExtensionSystem::blacklist() {
157   return NULL;
158 }
159
160 ErrorConsole* ShellExtensionSystem::error_console() {
161   return NULL;
162 }
163
164 InstallVerifier* ShellExtensionSystem::install_verifier() {
165   return NULL;
166 }
167
168 QuotaService* ShellExtensionSystem::quota_service() {
169   return quota_service_.get();
170 }
171
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(),
178                  false, false));
179 }
180
181 void ShellExtensionSystem::UnregisterExtensionWithRequestContexts(
182     const std::string& extension_id,
183     const UnloadedExtensionInfo::Reason reason) {
184 }
185
186 const OneShotEvent& ShellExtensionSystem::ready() const {
187   return ready_;
188 }
189
190 }  // namespace extensions