Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(295)

Side by Side Diff: extensions/browser/api/runtime/runtime_api.cc

Issue 264743014: Move chrome.runtime to //extensions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: make clang, cros happy Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/runtime/runtime_api.h" 5 #include "extensions/browser/api/runtime/runtime_api.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/lazy_instance.h" 9 #include "base/lazy_instance.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "chrome/browser/browser_process.h" 14 #include "base/version.h"
15 #include "chrome/browser/chrome_notification_types.h" 15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/extensions/extension_service.h" 16 #include "content/public/browser/browser_context.h"
17 #include "chrome/browser/extensions/extension_warning_service.h"
18 #include "chrome/browser/extensions/updater/extension_updater.h"
19 #include "chrome/browser/omaha_query_params/omaha_query_params.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/profiles/profile_manager.h"
22 #include "chrome/browser/ui/browser_finder.h"
23 #include "chrome/browser/ui/browser_navigator.h"
24 #include "chrome/browser/ui/browser_window.h"
25 #include "chrome/common/extensions/api/runtime.h"
26 #include "content/public/browser/child_process_security_policy.h" 17 #include "content/public/browser/child_process_security_policy.h"
27 #include "content/public/browser/notification_service.h" 18 #include "content/public/browser/notification_service.h"
28 #include "content/public/browser/render_process_host.h" 19 #include "content/public/browser/render_process_host.h"
29 #include "content/public/browser/render_view_host.h" 20 #include "content/public/browser/render_view_host.h"
21 #include "extensions/browser/api/runtime/runtime_api_delegate.h"
30 #include "extensions/browser/event_router.h" 22 #include "extensions/browser/event_router.h"
31 #include "extensions/browser/extension_host.h" 23 #include "extensions/browser/extension_host.h"
24 #include "extensions/browser/extension_prefs.h"
32 #include "extensions/browser/extension_registry.h" 25 #include "extensions/browser/extension_registry.h"
33 #include "extensions/browser/extension_system.h" 26 #include "extensions/browser/extension_system.h"
34 #include "extensions/browser/extensions_browser_client.h" 27 #include "extensions/browser/extensions_browser_client.h"
35 #include "extensions/browser/lazy_background_task_queue.h" 28 #include "extensions/browser/lazy_background_task_queue.h"
36 #include "extensions/browser/process_manager.h" 29 #include "extensions/browser/process_manager.h"
30 #include "extensions/common/api/runtime.h"
37 #include "extensions/common/error_utils.h" 31 #include "extensions/common/error_utils.h"
38 #include "extensions/common/extension.h" 32 #include "extensions/common/extension.h"
39 #include "extensions/common/manifest_handlers/background_info.h" 33 #include "extensions/common/manifest_handlers/background_info.h"
40 #include "url/gurl.h" 34 #include "url/gurl.h"
41 #include "webkit/browser/fileapi/isolated_context.h" 35 #include "webkit/browser/fileapi/isolated_context.h"
42 36
43 #if defined(OS_CHROMEOS)
44 #include "chrome/browser/chromeos/login/user_manager.h"
45 #include "chromeos/dbus/dbus_thread_manager.h"
46 #include "chromeos/dbus/power_manager_client.h"
47 #endif
48
49 using content::BrowserContext; 37 using content::BrowserContext;
50 38
51 namespace GetPlatformInfo = extensions::api::runtime::GetPlatformInfo; 39 namespace GetPlatformInfo = extensions::core_api::runtime::GetPlatformInfo;
52 40
53 namespace extensions { 41 namespace extensions {
54 42
55 namespace runtime = api::runtime; 43 namespace runtime = core_api::runtime;
56 44
57 namespace { 45 namespace {
58 46
59 const char kNoBackgroundPageError[] = "You do not have a background page."; 47 const char kNoBackgroundPageError[] = "You do not have a background page.";
60 const char kPageLoadError[] = "Background page failed to load."; 48 const char kPageLoadError[] = "Background page failed to load.";
61 const char kInstallReason[] = "reason"; 49 const char kInstallReason[] = "reason";
62 const char kInstallReasonChromeUpdate[] = "chrome_update"; 50 const char kInstallReasonChromeUpdate[] = "chrome_update";
63 const char kInstallReasonUpdate[] = "update"; 51 const char kInstallReasonUpdate[] = "update";
64 const char kInstallReasonInstall[] = "install"; 52 const char kInstallReasonInstall[] = "install";
65 const char kInstallPreviousVersion[] = "previousVersion"; 53 const char kInstallPreviousVersion[] = "previousVersion";
66 const char kInvalidUrlError[] = "Invalid URL."; 54 const char kInvalidUrlError[] = "Invalid URL.";
55 const char kPlatformInfoUnavailable[] = "Platform information unavailable.";
56
67 const char kUpdatesDisabledError[] = "Autoupdate is not enabled."; 57 const char kUpdatesDisabledError[] = "Autoupdate is not enabled.";
68 const char kUpdateFound[] = "update_available";
69 const char kUpdateNotFound[] = "no_update";
70 const char kUpdateThrottled[] = "throttled";
71 58
72 // A preference key storing the url loaded when an extension is uninstalled. 59 // A preference key storing the url loaded when an extension is uninstalled.
73 const char kUninstallUrl[] = "uninstall_url"; 60 const char kUninstallUrl[] = "uninstall_url";
74 61
75 // The name of the directory to be returned by getPackageDirectoryEntry. This 62 // The name of the directory to be returned by getPackageDirectoryEntry. This
76 // particular value does not matter to user code, but is chosen for consistency 63 // particular value does not matter to user code, but is chosen for consistency
77 // with the equivalent Pepper API. 64 // with the equivalent Pepper API.
78 const char kPackageDirectoryPath[] = "crxfs"; 65 const char kPackageDirectoryPath[] = "crxfs";
79 66
80 // If an extension reloads itself within this many miliseconds of reloading
81 // itself, the reload is considered suspiciously fast.
82 const int kFastReloadTime = 10000;
83
84 // After this many suspiciously fast consecutive reloads, an extension will get
85 // disabled.
86 const int kFastReloadCount = 5;
87
88 void DispatchOnStartupEventImpl(BrowserContext* browser_context, 67 void DispatchOnStartupEventImpl(BrowserContext* browser_context,
89 const std::string& extension_id, 68 const std::string& extension_id,
90 bool first_call, 69 bool first_call,
91 ExtensionHost* host) { 70 ExtensionHost* host) {
92 // A NULL host from the LazyBackgroundTaskQueue means the page failed to 71 // A NULL host from the LazyBackgroundTaskQueue means the page failed to
93 // load. Give up. 72 // load. Give up.
94 if (!host && !first_call) 73 if (!host && !first_call)
95 return; 74 return;
96 75
97 // Don't send onStartup events to incognito browser contexts. 76 // Don't send onStartup events to incognito browser contexts.
98 if (browser_context->IsOffTheRecord()) 77 if (browser_context->IsOffTheRecord())
99 return; 78 return;
100 79
101 if (ExtensionsBrowserClient::Get()->IsShuttingDown() || 80 if (ExtensionsBrowserClient::Get()->IsShuttingDown() ||
102 !ExtensionsBrowserClient::Get()->IsValidContext(browser_context)) 81 !ExtensionsBrowserClient::Get()->IsValidContext(browser_context))
103 return; 82 return;
104 ExtensionSystem* system = ExtensionSystem::Get(browser_context); 83 ExtensionSystem* system = ExtensionSystem::Get(browser_context);
105 if (!system) 84 if (!system)
106 return; 85 return;
107 86
108 // If this is a persistent background page, we want to wait for it to load 87 // If this is a persistent background page, we want to wait for it to load
109 // (it might not be ready, since this is startup). But only enqueue once. 88 // (it might not be ready, since this is startup). But only enqueue once.
110 // If it fails to load the first time, don't bother trying again. 89 // If it fails to load the first time, don't bother trying again.
111 const Extension* extension = 90 const Extension* extension =
112 ExtensionRegistry::Get(browser_context)->enabled_extensions().GetByID( 91 ExtensionRegistry::Get(browser_context)->enabled_extensions().GetByID(
113 extension_id); 92 extension_id);
114 if (extension && BackgroundInfo::HasPersistentBackgroundPage(extension) && 93 if (extension && BackgroundInfo::HasPersistentBackgroundPage(extension) &&
115 first_call && 94 first_call &&
116 system->lazy_background_task_queue()-> 95 system->lazy_background_task_queue()->ShouldEnqueueTask(browser_context,
117 ShouldEnqueueTask(browser_context, extension)) { 96 extension)) {
118 system->lazy_background_task_queue()->AddPendingTask( 97 system->lazy_background_task_queue()->AddPendingTask(
119 browser_context, extension_id, 98 browser_context,
120 base::Bind(&DispatchOnStartupEventImpl, 99 extension_id,
121 browser_context, extension_id, false)); 100 base::Bind(
101 &DispatchOnStartupEventImpl, browser_context, extension_id, false));
122 return; 102 return;
123 } 103 }
124 104
125 scoped_ptr<base::ListValue> event_args(new base::ListValue()); 105 scoped_ptr<base::ListValue> event_args(new base::ListValue());
126 scoped_ptr<Event> event(new Event(runtime::OnStartup::kEventName, 106 scoped_ptr<Event> event(
127 event_args.Pass())); 107 new Event(runtime::OnStartup::kEventName, event_args.Pass()));
128 system->event_router()->DispatchEventToExtension(extension_id, event.Pass()); 108 system->event_router()->DispatchEventToExtension(extension_id, event.Pass());
129 } 109 }
130 110
131 void SetUninstallURL(ExtensionPrefs* prefs, 111 void SetUninstallURL(ExtensionPrefs* prefs,
132 const std::string& extension_id, 112 const std::string& extension_id,
133 const std::string& url_string) { 113 const std::string& url_string) {
134 prefs->UpdateExtensionPref(extension_id, 114 prefs->UpdateExtensionPref(
135 kUninstallUrl, 115 extension_id, kUninstallUrl, new base::StringValue(url_string));
136 new base::StringValue(url_string));
137 } 116 }
138 117
139 #if defined(ENABLE_EXTENSIONS) 118 #if defined(ENABLE_EXTENSIONS)
140 std::string GetUninstallURL(ExtensionPrefs* prefs, 119 std::string GetUninstallURL(ExtensionPrefs* prefs,
141 const std::string& extension_id) { 120 const std::string& extension_id) {
142 std::string url_string; 121 std::string url_string;
143 prefs->ReadPrefAsString(extension_id, kUninstallUrl, &url_string); 122 prefs->ReadPrefAsString(extension_id, kUninstallUrl, &url_string);
144 return url_string; 123 return url_string;
145 } 124 }
146 #endif // defined(ENABLE_EXTENSIONS) 125 #endif // defined(ENABLE_EXTENSIONS)
147 126
148 } // namespace 127 } // namespace
149 128
150 /////////////////////////////////////////////////////////////////////////////// 129 ///////////////////////////////////////////////////////////////////////////////
151 130
152 static base::LazyInstance<BrowserContextKeyedAPIFactory<RuntimeAPI> > 131 static base::LazyInstance<BrowserContextKeyedAPIFactory<RuntimeAPI> >
153 g_factory = LAZY_INSTANCE_INITIALIZER; 132 g_factory = LAZY_INSTANCE_INITIALIZER;
154 133
155 // static 134 // static
156 BrowserContextKeyedAPIFactory<RuntimeAPI>* RuntimeAPI::GetFactoryInstance() { 135 BrowserContextKeyedAPIFactory<RuntimeAPI>* RuntimeAPI::GetFactoryInstance() {
157 return g_factory.Pointer(); 136 return g_factory.Pointer();
158 } 137 }
159 138
160 RuntimeAPI::RuntimeAPI(content::BrowserContext* context) 139 RuntimeAPI::RuntimeAPI(content::BrowserContext* context)
161 : browser_context_(context), 140 : browser_context_(context), dispatch_chrome_updated_event_(false) {
162 dispatch_chrome_updated_event_(false), 141 registrar_.Add(this,
163 registered_for_updates_(false) { 142 chrome::NOTIFICATION_EXTENSIONS_READY,
164 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY,
165 content::Source<BrowserContext>(context)); 143 content::Source<BrowserContext>(context));
166 registrar_.Add(this, 144 registrar_.Add(this,
167 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, 145 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
168 content::Source<BrowserContext>(context)); 146 content::Source<BrowserContext>(context));
169 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, 147 registrar_.Add(this,
148 chrome::NOTIFICATION_EXTENSION_INSTALLED,
170 content::Source<BrowserContext>(context)); 149 content::Source<BrowserContext>(context));
171 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, 150 registrar_.Add(this,
151 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
172 content::Source<BrowserContext>(context)); 152 content::Source<BrowserContext>(context));
173 153
154 delegate_ = ExtensionsBrowserClient::Get()->CreateRuntimeAPIDelegate(
155 browser_context_);
156
174 // Check if registered events are up-to-date. We can only do this once 157 // Check if registered events are up-to-date. We can only do this once
175 // per browser context, since it updates internal state when called. 158 // per browser context, since it updates internal state when called.
176 dispatch_chrome_updated_event_ = 159 dispatch_chrome_updated_event_ =
177 ExtensionsBrowserClient::Get()->DidVersionUpdate(browser_context_); 160 ExtensionsBrowserClient::Get()->DidVersionUpdate(browser_context_);
178 } 161 }
179 162
180 RuntimeAPI::~RuntimeAPI() { 163 RuntimeAPI::~RuntimeAPI() {
181 if (registered_for_updates_) { 164 delegate_->RemoveUpdateObserver(this);
182 ExtensionSystem::Get(browser_context_)->
183 extension_service()->RemoveUpdateObserver(this);
184 }
185 } 165 }
186 166
187 void RuntimeAPI::Observe(int type, 167 void RuntimeAPI::Observe(int type,
188 const content::NotificationSource& source, 168 const content::NotificationSource& source,
189 const content::NotificationDetails& details) { 169 const content::NotificationDetails& details) {
190 switch (type) { 170 switch (type) {
191 case chrome::NOTIFICATION_EXTENSIONS_READY: { 171 case chrome::NOTIFICATION_EXTENSIONS_READY: {
192 OnExtensionsReady(); 172 OnExtensionsReady();
193 break; 173 break;
194 } 174 }
(...skipping 18 matching lines...) Expand all
213 default: 193 default:
214 NOTREACHED(); 194 NOTREACHED();
215 break; 195 break;
216 } 196 }
217 } 197 }
218 198
219 void RuntimeAPI::OnExtensionsReady() { 199 void RuntimeAPI::OnExtensionsReady() {
220 // We're done restarting Chrome after an update. 200 // We're done restarting Chrome after an update.
221 dispatch_chrome_updated_event_ = false; 201 dispatch_chrome_updated_event_ = false;
222 202
223 registered_for_updates_ = true; 203 delegate_->AddUpdateObserver(this);
224
225 ExtensionSystem* extension_system = ExtensionSystem::Get(browser_context_);
226 extension_system->extension_service()->AddUpdateObserver(this);
227 204
228 // RuntimeAPI is redirected in incognito, so |browser_context_| is never 205 // RuntimeAPI is redirected in incognito, so |browser_context_| is never
229 // incognito. We don't observe incognito ProcessManagers but that is OK 206 // incognito. We don't observe incognito ProcessManagers but that is OK
230 // because we don't send onStartup events to incognito browser contexts. 207 // because we don't send onStartup events to incognito browser contexts.
231 DCHECK(!browser_context_->IsOffTheRecord()); 208 DCHECK(!browser_context_->IsOffTheRecord());
232 // Some tests use partially constructed Profiles without a process manager. 209 // Some tests use partially constructed Profiles without a process manager.
210 ExtensionSystem* extension_system = ExtensionSystem::Get(browser_context_);
233 if (extension_system->process_manager()) 211 if (extension_system->process_manager())
234 extension_system->process_manager()->AddObserver(this); 212 extension_system->process_manager()->AddObserver(this);
235 } 213 }
236 214
237 void RuntimeAPI::OnExtensionLoaded(const Extension* extension) { 215 void RuntimeAPI::OnExtensionLoaded(const Extension* extension) {
238 if (!dispatch_chrome_updated_event_) 216 if (!dispatch_chrome_updated_event_)
239 return; 217 return;
240 218
241 // Dispatch the onInstalled event with reason "chrome_update". 219 // Dispatch the onInstalled event with reason "chrome_update".
242 base::MessageLoop::current()->PostTask( 220 base::MessageLoop::current()->PostTask(
243 FROM_HERE, 221 FROM_HERE,
244 base::Bind(&RuntimeEventRouter::DispatchOnInstalledEvent, 222 base::Bind(&RuntimeEventRouter::DispatchOnInstalledEvent,
245 browser_context_, 223 browser_context_,
246 extension->id(), 224 extension->id(),
247 Version(), 225 Version(),
248 true)); 226 true));
249 } 227 }
250 228
251 void RuntimeAPI::OnExtensionInstalled(const Extension* extension) { 229 void RuntimeAPI::OnExtensionInstalled(const Extension* extension) {
252 // Ephemeral apps are not considered to be installed and do not receive 230 // Ephemeral apps are not considered to be installed and do not receive
253 // the onInstalled() event. 231 // the onInstalled() event.
254 if (extension->is_ephemeral()) 232 if (extension->is_ephemeral())
255 return; 233 return;
256 234
257 // Get the previous version to check if this is an upgrade. 235 Version old_version = delegate_->GetPreviousExtensionVersion(extension);
258 ExtensionService* service = ExtensionSystem::Get(
259 browser_context_)->extension_service();
260 const Extension* old = service->GetExtensionById(extension->id(), true);
261 Version old_version;
262 if (old)
263 old_version = *old->version();
264 236
265 // Dispatch the onInstalled event. 237 // Dispatch the onInstalled event.
266 base::MessageLoop::current()->PostTask( 238 base::MessageLoop::current()->PostTask(
267 FROM_HERE, 239 FROM_HERE,
268 base::Bind(&RuntimeEventRouter::DispatchOnInstalledEvent, 240 base::Bind(&RuntimeEventRouter::DispatchOnInstalledEvent,
269 browser_context_, 241 browser_context_,
270 extension->id(), 242 extension->id(),
271 old_version, 243 old_version,
272 false)); 244 false));
273
274 } 245 }
275 246
276 void RuntimeAPI::OnExtensionUninstalled(const Extension* extension) { 247 void RuntimeAPI::OnExtensionUninstalled(const Extension* extension) {
277 // Ephemeral apps are not considered to be installed, so the uninstall URL 248 // Ephemeral apps are not considered to be installed, so the uninstall URL
278 // is not invoked when they are removed. 249 // is not invoked when they are removed.
279 if (extension->is_ephemeral()) 250 if (extension->is_ephemeral())
280 return; 251 return;
281 252
282 Profile* profile = Profile::FromBrowserContext(browser_context_); 253 RuntimeEventRouter::OnExtensionUninstalled(browser_context_, extension->id());
283 RuntimeEventRouter::OnExtensionUninstalled(profile, extension->id());
284 } 254 }
285 255
286 void RuntimeAPI::Shutdown() { 256 void RuntimeAPI::Shutdown() {
287 // ExtensionSystem deletes its ProcessManager during the Shutdown() phase, so 257 // ExtensionSystem deletes its ProcessManager during the Shutdown() phase, so
288 // the observer must be removed here and not in the RuntimeAPI destructor. 258 // the observer must be removed here and not in the RuntimeAPI destructor.
289 ProcessManager* process_manager = 259 ProcessManager* process_manager =
290 ExtensionSystem::Get(browser_context_)->process_manager(); 260 ExtensionSystem::Get(browser_context_)->process_manager();
291 // Some tests use partially constructed Profiles without a process manager. 261 // Some tests use partially constructed Profiles without a process manager.
292 if (process_manager) 262 if (process_manager)
293 process_manager->RemoveObserver(this); 263 process_manager->RemoveObserver(this);
294 } 264 }
295 265
296 void RuntimeAPI::OnAppUpdateAvailable(const Extension* extension) { 266 void RuntimeAPI::OnAppUpdateAvailable(const Extension* extension) {
297 Profile* profile = Profile::FromBrowserContext(browser_context_);
298 RuntimeEventRouter::DispatchOnUpdateAvailableEvent( 267 RuntimeEventRouter::DispatchOnUpdateAvailableEvent(
299 profile, extension->id(), extension->manifest()->value()); 268 browser_context_, extension->id(), extension->manifest()->value());
300 } 269 }
301 270
302 void RuntimeAPI::OnChromeUpdateAvailable() { 271 void RuntimeAPI::OnChromeUpdateAvailable() {
303 Profile* profile = Profile::FromBrowserContext(browser_context_); 272 RuntimeEventRouter::DispatchOnBrowserUpdateAvailableEvent(browser_context_);
304 RuntimeEventRouter::DispatchOnBrowserUpdateAvailableEvent(profile);
305 } 273 }
306 274
307 void RuntimeAPI::OnBackgroundHostStartup(const Extension* extension) { 275 void RuntimeAPI::OnBackgroundHostStartup(const Extension* extension) {
308 RuntimeEventRouter::DispatchOnStartupEvent(browser_context_, extension->id()); 276 RuntimeEventRouter::DispatchOnStartupEvent(browser_context_, extension->id());
309 } 277 }
310 278
311 void RuntimeAPI::MaybeReloadExtension(const std::string& extension_id) { 279 void RuntimeAPI::ReloadExtension(const std::string& extension_id) {
312 std::pair<base::TimeTicks, int>& reload_info = 280 delegate_->ReloadExtension(extension_id);
313 last_reload_time_[extension_id]; 281 }
314 base::TimeTicks now = base::TimeTicks::Now();
315 if (reload_info.first.is_null() ||
316 (now - reload_info.first).InMilliseconds() > kFastReloadTime) {
317 reload_info.second = 0;
318 } else {
319 reload_info.second++;
320 }
321 if (!reload_info.first.is_null()) {
322 UMA_HISTOGRAM_LONG_TIMES("Extensions.RuntimeReloadTime",
323 now - reload_info.first);
324 }
325 UMA_HISTOGRAM_COUNTS_100("Extensions.RuntimeReloadFastCount",
326 reload_info.second);
327 reload_info.first = now;
328 282
329 ExtensionService* service = 283 bool RuntimeAPI::CheckForUpdates(
330 ExtensionSystem::Get(browser_context_)->extension_service(); 284 const std::string& extension_id,
331 if (reload_info.second >= kFastReloadCount) { 285 const RuntimeAPIDelegate::UpdateCheckCallback& callback) {
332 // Unloading an extension clears all warnings, so first terminate the 286 return delegate_->CheckForUpdates(extension_id, callback);
333 // extension, and then add the warning. Since this is called from an 287 }
334 // extension function unloading the extension has to be done 288
335 // asynchronously. Fortunately PostTask guarentees FIFO order so just 289 void RuntimeAPI::OpenURL(const GURL& update_url) {
336 // post both tasks. 290 delegate_->OpenURL(update_url);
337 base::MessageLoop::current()->PostTask( 291 }
338 FROM_HERE, 292
339 base::Bind(&ExtensionService::TerminateExtension, 293 bool RuntimeAPI::GetPlatformInfo(GetPlatformInfo::Results::PlatformInfo* info) {
340 service->AsWeakPtr(), 294 return delegate_->GetPlatformInfo(info);
341 extension_id)); 295 }
342 ExtensionWarningSet warnings; 296
343 warnings.insert( 297 bool RuntimeAPI::RestartDevice(std::string* error_message) {
344 ExtensionWarning::CreateReloadTooFrequentWarning(extension_id)); 298 return delegate_->RestartDevice(error_message);
345 base::MessageLoop::current()->PostTask(
346 FROM_HERE,
347 base::Bind(&ExtensionWarningService::NotifyWarningsOnUI,
348 browser_context_,
349 warnings));
350 } else {
351 // We can't call ReloadExtension directly, since when this method finishes
352 // it tries to decrease the reference count for the extension, which fails
353 // if the extension has already been reloaded; so instead we post a task.
354 base::MessageLoop::current()->PostTask(
355 FROM_HERE,
356 base::Bind(&ExtensionService::ReloadExtension,
357 service->AsWeakPtr(),
358 extension_id));
359 }
360 } 299 }
361 300
362 /////////////////////////////////////////////////////////////////////////////// 301 ///////////////////////////////////////////////////////////////////////////////
363 302
364 // static 303 // static
365 void RuntimeEventRouter::DispatchOnStartupEvent( 304 void RuntimeEventRouter::DispatchOnStartupEvent(
366 content::BrowserContext* context, const std::string& extension_id) { 305 content::BrowserContext* context,
306 const std::string& extension_id) {
367 DispatchOnStartupEventImpl(context, extension_id, true, NULL); 307 DispatchOnStartupEventImpl(context, extension_id, true, NULL);
368 } 308 }
369 309
370 // static 310 // static
371 void RuntimeEventRouter::DispatchOnInstalledEvent( 311 void RuntimeEventRouter::DispatchOnInstalledEvent(
372 content::BrowserContext* context, 312 content::BrowserContext* context,
373 const std::string& extension_id, 313 const std::string& extension_id,
374 const Version& old_version, 314 const Version& old_version,
375 bool chrome_updated) { 315 bool chrome_updated) {
376 if (!ExtensionsBrowserClient::Get()->IsValidContext(context)) 316 if (!ExtensionsBrowserClient::Get()->IsValidContext(context))
377 return; 317 return;
378 ExtensionSystem* system = ExtensionSystem::Get(context); 318 ExtensionSystem* system = ExtensionSystem::Get(context);
379 if (!system) 319 if (!system)
380 return; 320 return;
381 321
382 scoped_ptr<base::ListValue> event_args(new base::ListValue()); 322 scoped_ptr<base::ListValue> event_args(new base::ListValue());
383 base::DictionaryValue* info = new base::DictionaryValue(); 323 base::DictionaryValue* info = new base::DictionaryValue();
384 event_args->Append(info); 324 event_args->Append(info);
385 if (old_version.IsValid()) { 325 if (old_version.IsValid()) {
386 info->SetString(kInstallReason, kInstallReasonUpdate); 326 info->SetString(kInstallReason, kInstallReasonUpdate);
387 info->SetString(kInstallPreviousVersion, old_version.GetString()); 327 info->SetString(kInstallPreviousVersion, old_version.GetString());
388 } else if (chrome_updated) { 328 } else if (chrome_updated) {
389 info->SetString(kInstallReason, kInstallReasonChromeUpdate); 329 info->SetString(kInstallReason, kInstallReasonChromeUpdate);
390 } else { 330 } else {
391 info->SetString(kInstallReason, kInstallReasonInstall); 331 info->SetString(kInstallReason, kInstallReasonInstall);
392 } 332 }
393 DCHECK(system->event_router()); 333 DCHECK(system->event_router());
394 scoped_ptr<Event> event(new Event(runtime::OnInstalled::kEventName, 334 scoped_ptr<Event> event(
395 event_args.Pass())); 335 new Event(runtime::OnInstalled::kEventName, event_args.Pass()));
396 system->event_router()->DispatchEventWithLazyListener(extension_id, 336 system->event_router()->DispatchEventWithLazyListener(extension_id,
397 event.Pass()); 337 event.Pass());
398 } 338 }
399 339
400 // static 340 // static
401 void RuntimeEventRouter::DispatchOnUpdateAvailableEvent( 341 void RuntimeEventRouter::DispatchOnUpdateAvailableEvent(
402 Profile* profile, 342 content::BrowserContext* context,
403 const std::string& extension_id, 343 const std::string& extension_id,
404 const base::DictionaryValue* manifest) { 344 const base::DictionaryValue* manifest) {
405 ExtensionSystem* system = ExtensionSystem::Get(profile); 345 ExtensionSystem* system = ExtensionSystem::Get(context);
406 if (!system) 346 if (!system)
407 return; 347 return;
408 348
409 scoped_ptr<base::ListValue> args(new base::ListValue); 349 scoped_ptr<base::ListValue> args(new base::ListValue);
410 args->Append(manifest->DeepCopy()); 350 args->Append(manifest->DeepCopy());
411 DCHECK(system->event_router()); 351 DCHECK(system->event_router());
412 scoped_ptr<Event> event(new Event(runtime::OnUpdateAvailable::kEventName, 352 scoped_ptr<Event> event(
413 args.Pass())); 353 new Event(runtime::OnUpdateAvailable::kEventName, args.Pass()));
414 system->event_router()->DispatchEventToExtension(extension_id, event.Pass()); 354 system->event_router()->DispatchEventToExtension(extension_id, event.Pass());
415 } 355 }
416 356
417 // static 357 // static
418 void RuntimeEventRouter::DispatchOnBrowserUpdateAvailableEvent( 358 void RuntimeEventRouter::DispatchOnBrowserUpdateAvailableEvent(
419 Profile* profile) { 359 content::BrowserContext* context) {
420 ExtensionSystem* system = ExtensionSystem::Get(profile); 360 ExtensionSystem* system = ExtensionSystem::Get(context);
421 if (!system) 361 if (!system)
422 return; 362 return;
423 363
424 scoped_ptr<base::ListValue> args(new base::ListValue); 364 scoped_ptr<base::ListValue> args(new base::ListValue);
425 DCHECK(system->event_router()); 365 DCHECK(system->event_router());
426 scoped_ptr<Event> event(new Event( 366 scoped_ptr<Event> event(
427 runtime::OnBrowserUpdateAvailable::kEventName, args.Pass())); 367 new Event(runtime::OnBrowserUpdateAvailable::kEventName, args.Pass()));
428 system->event_router()->BroadcastEvent(event.Pass()); 368 system->event_router()->BroadcastEvent(event.Pass());
429 } 369 }
430 370
431 // static 371 // static
432 void RuntimeEventRouter::DispatchOnRestartRequiredEvent( 372 void RuntimeEventRouter::DispatchOnRestartRequiredEvent(
433 Profile* profile, 373 content::BrowserContext* context,
434 const std::string& app_id, 374 const std::string& app_id,
435 api::runtime::OnRestartRequired::Reason reason) { 375 core_api::runtime::OnRestartRequired::Reason reason) {
436 ExtensionSystem* system = ExtensionSystem::Get(profile); 376 ExtensionSystem* system = ExtensionSystem::Get(context);
437 if (!system) 377 if (!system)
438 return; 378 return;
439 379
440 scoped_ptr<Event> event( 380 scoped_ptr<Event> event(
441 new Event(runtime::OnRestartRequired::kEventName, 381 new Event(runtime::OnRestartRequired::kEventName,
442 api::runtime::OnRestartRequired::Create(reason))); 382 core_api::runtime::OnRestartRequired::Create(reason)));
443 383
444 DCHECK(system->event_router()); 384 DCHECK(system->event_router());
445 system->event_router()->DispatchEventToExtension(app_id, event.Pass()); 385 system->event_router()->DispatchEventToExtension(app_id, event.Pass());
446 } 386 }
447 387
448 // static 388 // static
449 void RuntimeEventRouter::OnExtensionUninstalled( 389 void RuntimeEventRouter::OnExtensionUninstalled(
450 Profile* profile, 390 content::BrowserContext* context,
451 const std::string& extension_id) { 391 const std::string& extension_id) {
452 #if defined(ENABLE_EXTENSIONS) 392 #if defined(ENABLE_EXTENSIONS)
453 GURL uninstall_url(GetUninstallURL(ExtensionPrefs::Get(profile), 393 GURL uninstall_url(
454 extension_id)); 394 GetUninstallURL(ExtensionPrefs::Get(context), extension_id));
455 395
456 if (uninstall_url.is_empty()) 396 if (uninstall_url.is_empty())
457 return; 397 return;
458 398
459 Browser* browser = chrome::FindLastActiveWithProfile(profile, 399 RuntimeAPI::GetFactoryInstance()->Get(context)->OpenURL(uninstall_url);
460 chrome::GetActiveDesktop());
461 if (!browser)
462 browser = new Browser(Browser::CreateParams(profile,
463 chrome::GetActiveDesktop()));
464
465 chrome::NavigateParams params(browser, uninstall_url,
466 content::PAGE_TRANSITION_CLIENT_REDIRECT);
467 params.disposition = NEW_FOREGROUND_TAB;
468 params.user_gesture = false;
469 chrome::Navigate(&params);
470 #endif // defined(ENABLE_EXTENSIONS) 400 #endif // defined(ENABLE_EXTENSIONS)
471 } 401 }
472 402
473 bool RuntimeGetBackgroundPageFunction::RunAsync() { 403 ExtensionFunction::ResponseAction RuntimeGetBackgroundPageFunction::Run() {
474 ExtensionSystem* system = ExtensionSystem::Get(GetProfile()); 404 ExtensionSystem* system = ExtensionSystem::Get(browser_context());
475 ExtensionHost* host = system->process_manager()-> 405 ExtensionHost* host =
476 GetBackgroundHostForExtension(extension_id()); 406 system->process_manager()->GetBackgroundHostForExtension(extension_id());
477 if (system->lazy_background_task_queue()->ShouldEnqueueTask(GetProfile(), 407 if (system->lazy_background_task_queue()->ShouldEnqueueTask(browser_context(),
478 GetExtension())) { 408 GetExtension())) {
479 system->lazy_background_task_queue()->AddPendingTask( 409 system->lazy_background_task_queue()->AddPendingTask(
480 GetProfile(), 410 browser_context(),
481 extension_id(), 411 extension_id(),
482 base::Bind(&RuntimeGetBackgroundPageFunction::OnPageLoaded, this)); 412 base::Bind(&RuntimeGetBackgroundPageFunction::OnPageLoaded, this));
483 } else if (host) { 413 } else if (host) {
484 OnPageLoaded(host); 414 OnPageLoaded(host);
485 } else { 415 } else {
486 error_ = kNoBackgroundPageError; 416 return RespondNow(Error(kNoBackgroundPageError));
487 return false;
488 } 417 }
489 418
490 return true; 419 return RespondLater();
491 } 420 }
492 421
493 void RuntimeGetBackgroundPageFunction::OnPageLoaded(ExtensionHost* host) { 422 void RuntimeGetBackgroundPageFunction::OnPageLoaded(ExtensionHost* host) {
494 if (host) { 423 if (host) {
495 SendResponse(true); 424 Respond(NoArguments());
496 } else { 425 } else {
497 error_ = kPageLoadError; 426 Respond(Error(kPageLoadError));
498 SendResponse(false);
499 } 427 }
500 } 428 }
501 429
502 bool RuntimeSetUninstallURLFunction::RunSync() { 430 ExtensionFunction::ResponseAction RuntimeSetUninstallURLFunction::Run() {
503 std::string url_string; 431 std::string url_string;
504 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url_string)); 432 EXTENSION_FUNCTION_VALIDATE_TYPESAFE(args_->GetString(0, &url_string));
505 433
506 GURL url(url_string); 434 GURL url(url_string);
507 if (!url.is_valid()) { 435 if (!url.is_valid()) {
508 error_ = ErrorUtils::FormatErrorMessage(kInvalidUrlError, url_string); 436 return RespondNow(
509 return false; 437 Error(ErrorUtils::FormatErrorMessage(kInvalidUrlError, url_string)));
510 } 438 }
511
512 SetUninstallURL( 439 SetUninstallURL(
513 ExtensionPrefs::Get(GetProfile()), extension_id(), url_string); 440 ExtensionPrefs::Get(browser_context()), extension_id(), url_string);
514 return true; 441 return RespondNow(NoArguments());
515 } 442 }
516 443
517 bool RuntimeReloadFunction::RunSync() { 444 ExtensionFunction::ResponseAction RuntimeReloadFunction::Run() {
518 RuntimeAPI::GetFactoryInstance()->Get(GetProfile())->MaybeReloadExtension( 445 RuntimeAPI::GetFactoryInstance()->Get(browser_context())->ReloadExtension(
519 extension_id()); 446 extension_id());
520 return true; 447 return RespondNow(NoArguments());
521 } 448 }
522 449
523 RuntimeRequestUpdateCheckFunction::RuntimeRequestUpdateCheckFunction() { 450 ExtensionFunction::ResponseAction RuntimeRequestUpdateCheckFunction::Run() {
524 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UPDATE_FOUND, 451 if (!RuntimeAPI::GetFactoryInstance()->Get(browser_context())
525 content::NotificationService::AllSources()); 452 ->CheckForUpdates(extension_id(),
453 base::Bind(
454 &RuntimeRequestUpdateCheckFunction::CheckComplete,
455 this))) {
456 return RespondNow(Error(kUpdatesDisabledError));
457 }
458 return RespondLater();
526 } 459 }
527 460
528 bool RuntimeRequestUpdateCheckFunction::RunAsync() { 461 void RuntimeRequestUpdateCheckFunction::CheckComplete(
529 ExtensionSystem* system = ExtensionSystem::Get(GetProfile()); 462 const RuntimeAPIDelegate::UpdateCheckResult& result) {
530 ExtensionService* service = system->extension_service(); 463 if (result.success) {
531 ExtensionUpdater* updater = service->updater(); 464 base::ListValue* results = new base::ListValue;
532 if (!updater) { 465 results->AppendString(result.response);
533 error_ = kUpdatesDisabledError; 466 base::DictionaryValue* details = new base::DictionaryValue;
534 return false; 467 results->Append(details);
535 } 468 details->SetString("version", result.version);
536 469 Respond(MultipleArguments(results));
537 did_reply_ = false;
538 if (!updater->CheckExtensionSoon(extension_id(), base::Bind(
539 &RuntimeRequestUpdateCheckFunction::CheckComplete, this))) {
540 did_reply_ = true;
541 SetResult(new base::StringValue(kUpdateThrottled));
542 SendResponse(true);
543 }
544 return true;
545 }
546
547 void RuntimeRequestUpdateCheckFunction::CheckComplete() {
548 if (did_reply_)
549 return;
550
551 did_reply_ = true;
552
553 // Since no UPDATE_FOUND notification was seen, this generally would mean
554 // that no update is found, but a previous update check might have already
555 // queued up an update, so check for that here to make sure we return the
556 // right value.
557 ExtensionSystem* system = ExtensionSystem::Get(GetProfile());
558 ExtensionService* service = system->extension_service();
559 const Extension* update = service->GetPendingExtensionUpdate(extension_id());
560 if (update) {
561 ReplyUpdateFound(update->VersionString());
562 } else { 470 } else {
563 SetResult(new base::StringValue(kUpdateNotFound)); 471 Respond(SingleArgument(new base::StringValue(result.response)));
564 }
565 SendResponse(true);
566 }
567
568 void RuntimeRequestUpdateCheckFunction::Observe(
569 int type,
570 const content::NotificationSource& source,
571 const content::NotificationDetails& details) {
572 if (did_reply_)
573 return;
574
575 DCHECK(type == chrome::NOTIFICATION_EXTENSION_UPDATE_FOUND);
576 typedef const std::pair<std::string, Version> UpdateDetails;
577 const std::string& id = content::Details<UpdateDetails>(details)->first;
578 const Version& version = content::Details<UpdateDetails>(details)->second;
579 if (id == extension_id()) {
580 ReplyUpdateFound(version.GetString());
581 } 472 }
582 } 473 }
583 474
584 void RuntimeRequestUpdateCheckFunction::ReplyUpdateFound( 475 ExtensionFunction::ResponseAction RuntimeRestartFunction::Run() {
585 const std::string& version) { 476 std::string message;
586 did_reply_ = true; 477 bool result =
587 results_.reset(new base::ListValue); 478 RuntimeAPI::GetFactoryInstance()->Get(browser_context())->RestartDevice(
588 results_->AppendString(kUpdateFound); 479 &message);
589 base::DictionaryValue* details = new base::DictionaryValue; 480 if (!result) {
590 results_->Append(details); 481 return RespondNow(Error(message));
591 details->SetString("version", version); 482 }
592 SendResponse(true); 483 return RespondNow(NoArguments());
593 } 484 }
594 485
595 bool RuntimeRestartFunction::RunSync() { 486 ExtensionFunction::ResponseAction RuntimeGetPlatformInfoFunction::Run() {
596 #if defined(OS_CHROMEOS) 487 GetPlatformInfo::Results::PlatformInfo info;
597 if (chromeos::UserManager::Get()->IsLoggedInAsKioskApp()) { 488 if (!RuntimeAPI::GetFactoryInstance()
598 chromeos::DBusThreadManager::Get() 489 ->Get(browser_context())
599 ->GetPowerManagerClient() 490 ->GetPlatformInfo(&info)) {
600 ->RequestRestart(); 491 return RespondNow(Error(kPlatformInfoUnavailable));
601 return true;
602 } 492 }
603 #endif 493 return RespondNow(
604 SetError("Function available only for ChromeOS kiosk mode."); 494 MultipleArguments(GetPlatformInfo::Results::Create(info).release()));
605 return false;
606 } 495 }
607 496
608 bool RuntimeGetPlatformInfoFunction::RunSync() { 497 ExtensionFunction::ResponseAction
609 GetPlatformInfo::Results::PlatformInfo info; 498 RuntimeGetPackageDirectoryEntryFunction::Run() {
610
611 const char* os = chrome::OmahaQueryParams::GetOS();
612 if (strcmp(os, "mac") == 0) {
613 info.os = GetPlatformInfo::Results::PlatformInfo::OS_MAC_;
614 } else if (strcmp(os, "win") == 0) {
615 info.os = GetPlatformInfo::Results::PlatformInfo::OS_WIN_;
616 } else if (strcmp(os, "android") == 0) {
617 info.os = GetPlatformInfo::Results::PlatformInfo::OS_ANDROID_;
618 } else if (strcmp(os, "cros") == 0) {
619 info.os = GetPlatformInfo::Results::PlatformInfo::OS_CROS_;
620 } else if (strcmp(os, "linux") == 0) {
621 info.os = GetPlatformInfo::Results::PlatformInfo::OS_LINUX_;
622 } else if (strcmp(os, "openbsd") == 0) {
623 info.os = GetPlatformInfo::Results::PlatformInfo::OS_OPENBSD_;
624 } else {
625 NOTREACHED();
626 return false;
627 }
628
629 const char* arch = chrome::OmahaQueryParams::GetArch();
630 if (strcmp(arch, "arm") == 0) {
631 info.arch = GetPlatformInfo::Results::PlatformInfo::ARCH_ARM;
632 } else if (strcmp(arch, "x86") == 0) {
633 info.arch = GetPlatformInfo::Results::PlatformInfo::ARCH_X86_32;
634 } else if (strcmp(arch, "x64") == 0) {
635 info.arch = GetPlatformInfo::Results::PlatformInfo::ARCH_X86_64;
636 } else {
637 NOTREACHED();
638 return false;
639 }
640
641 const char* nacl_arch = chrome::OmahaQueryParams::GetNaclArch();
642 if (strcmp(nacl_arch, "arm") == 0) {
643 info.nacl_arch = GetPlatformInfo::Results::PlatformInfo::NACL_ARCH_ARM;
644 } else if (strcmp(nacl_arch, "x86-32") == 0) {
645 info.nacl_arch = GetPlatformInfo::Results::PlatformInfo::NACL_ARCH_X86_32;
646 } else if (strcmp(nacl_arch, "x86-64") == 0) {
647 info.nacl_arch = GetPlatformInfo::Results::PlatformInfo::NACL_ARCH_X86_64;
648 } else {
649 NOTREACHED();
650 return false;
651 }
652
653 results_ = GetPlatformInfo::Results::Create(info);
654 return true;
655 }
656
657 bool RuntimeGetPackageDirectoryEntryFunction::RunSync() {
658 fileapi::IsolatedContext* isolated_context = 499 fileapi::IsolatedContext* isolated_context =
659 fileapi::IsolatedContext::GetInstance(); 500 fileapi::IsolatedContext::GetInstance();
660 DCHECK(isolated_context); 501 DCHECK(isolated_context);
661 502
662 std::string relative_path = kPackageDirectoryPath; 503 std::string relative_path = kPackageDirectoryPath;
663 base::FilePath path = extension_->path(); 504 base::FilePath path = extension_->path();
664 std::string filesystem_id = isolated_context->RegisterFileSystemForPath( 505 std::string filesystem_id = isolated_context->RegisterFileSystemForPath(
665 fileapi::kFileSystemTypeNativeLocal, path, &relative_path); 506 fileapi::kFileSystemTypeNativeLocal, path, &relative_path);
666 507
667 int renderer_id = render_view_host_->GetProcess()->GetID(); 508 int renderer_id = render_view_host_->GetProcess()->GetID();
668 content::ChildProcessSecurityPolicy* policy = 509 content::ChildProcessSecurityPolicy* policy =
669 content::ChildProcessSecurityPolicy::GetInstance(); 510 content::ChildProcessSecurityPolicy::GetInstance();
670 policy->GrantReadFileSystem(renderer_id, filesystem_id); 511 policy->GrantReadFileSystem(renderer_id, filesystem_id);
671 base::DictionaryValue* dict = new base::DictionaryValue(); 512 base::DictionaryValue* dict = new base::DictionaryValue();
672 SetResult(dict);
673 dict->SetString("fileSystemId", filesystem_id); 513 dict->SetString("fileSystemId", filesystem_id);
674 dict->SetString("baseName", relative_path); 514 dict->SetString("baseName", relative_path);
675 return true; 515 return RespondNow(SingleArgument(dict));
676 } 516 }
677 517
678 } // namespace extensions 518 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698