[Android] Add tests for toolbar of Chrome Custom Tabs
[chromium-blink-merge.git] / extensions / browser / api / runtime / runtime_api.cc
blob72dacf7b3abe27cb226d07cde6abaf897f7005ce
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.
5 #include "extensions/browser/api/runtime/runtime_api.h"
7 #include <utility>
9 #include "base/lazy_instance.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/metrics/histogram.h"
13 #include "base/values.h"
14 #include "base/version.h"
15 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/child_process_security_policy.h"
17 #include "content/public/browser/notification_service.h"
18 #include "content/public/browser/render_process_host.h"
19 #include "content/public/browser/render_view_host.h"
20 #include "extensions/browser/api/runtime/runtime_api_delegate.h"
21 #include "extensions/browser/event_router.h"
22 #include "extensions/browser/extension_host.h"
23 #include "extensions/browser/extension_prefs.h"
24 #include "extensions/browser/extension_registry.h"
25 #include "extensions/browser/extension_system.h"
26 #include "extensions/browser/extension_util.h"
27 #include "extensions/browser/extensions_browser_client.h"
28 #include "extensions/browser/lazy_background_task_queue.h"
29 #include "extensions/browser/notification_types.h"
30 #include "extensions/browser/process_manager_factory.h"
31 #include "extensions/common/api/runtime.h"
32 #include "extensions/common/error_utils.h"
33 #include "extensions/common/extension.h"
34 #include "extensions/common/manifest_handlers/background_info.h"
35 #include "extensions/common/manifest_handlers/shared_module_info.h"
36 #include "storage/browser/fileapi/isolated_context.h"
37 #include "url/gurl.h"
39 using content::BrowserContext;
41 namespace extensions {
43 namespace runtime = core_api::runtime;
45 namespace {
47 const char kNoBackgroundPageError[] = "You do not have a background page.";
48 const char kPageLoadError[] = "Background page failed to load.";
49 const char kFailedToCreateOptionsPage[] = "Could not create an options page.";
50 const char kInstallId[] = "id";
51 const char kInstallReason[] = "reason";
52 const char kInstallReasonChromeUpdate[] = "chrome_update";
53 const char kInstallReasonUpdate[] = "update";
54 const char kInstallReasonInstall[] = "install";
55 const char kInstallReasonSharedModuleUpdate[] = "shared_module_update";
56 const char kInstallPreviousVersion[] = "previousVersion";
57 const char kInvalidUrlError[] = "Invalid URL.";
58 const char kPlatformInfoUnavailable[] = "Platform information unavailable.";
60 const char kUpdatesDisabledError[] = "Autoupdate is not enabled.";
62 // A preference key storing the url loaded when an extension is uninstalled.
63 const char kUninstallUrl[] = "uninstall_url";
65 // The name of the directory to be returned by getPackageDirectoryEntry. This
66 // particular value does not matter to user code, but is chosen for consistency
67 // with the equivalent Pepper API.
68 const char kPackageDirectoryPath[] = "crxfs";
70 void DispatchOnStartupEventImpl(BrowserContext* browser_context,
71 const std::string& extension_id,
72 bool first_call,
73 ExtensionHost* host) {
74 // A NULL host from the LazyBackgroundTaskQueue means the page failed to
75 // load. Give up.
76 if (!host && !first_call)
77 return;
79 // Don't send onStartup events to incognito browser contexts.
80 if (browser_context->IsOffTheRecord())
81 return;
83 if (ExtensionsBrowserClient::Get()->IsShuttingDown() ||
84 !ExtensionsBrowserClient::Get()->IsValidContext(browser_context))
85 return;
86 ExtensionSystem* system = ExtensionSystem::Get(browser_context);
87 if (!system)
88 return;
90 // If this is a persistent background page, we want to wait for it to load
91 // (it might not be ready, since this is startup). But only enqueue once.
92 // If it fails to load the first time, don't bother trying again.
93 const Extension* extension =
94 ExtensionRegistry::Get(browser_context)->enabled_extensions().GetByID(
95 extension_id);
96 if (extension && BackgroundInfo::HasPersistentBackgroundPage(extension) &&
97 first_call &&
98 LazyBackgroundTaskQueue::Get(browser_context)
99 ->ShouldEnqueueTask(browser_context, extension)) {
100 LazyBackgroundTaskQueue::Get(browser_context)
101 ->AddPendingTask(browser_context, extension_id,
102 base::Bind(&DispatchOnStartupEventImpl,
103 browser_context, extension_id, false));
104 return;
107 scoped_ptr<base::ListValue> event_args(new base::ListValue());
108 scoped_ptr<Event> event(
109 new Event(runtime::OnStartup::kEventName, event_args.Pass()));
110 EventRouter::Get(browser_context)
111 ->DispatchEventToExtension(extension_id, event.Pass());
114 void SetUninstallURL(ExtensionPrefs* prefs,
115 const std::string& extension_id,
116 const std::string& url_string) {
117 prefs->UpdateExtensionPref(
118 extension_id, kUninstallUrl, new base::StringValue(url_string));
121 std::string GetUninstallURL(ExtensionPrefs* prefs,
122 const std::string& extension_id) {
123 std::string url_string;
124 prefs->ReadPrefAsString(extension_id, kUninstallUrl, &url_string);
125 return url_string;
128 } // namespace
130 ///////////////////////////////////////////////////////////////////////////////
132 static base::LazyInstance<BrowserContextKeyedAPIFactory<RuntimeAPI> >
133 g_factory = LAZY_INSTANCE_INITIALIZER;
135 // static
136 BrowserContextKeyedAPIFactory<RuntimeAPI>* RuntimeAPI::GetFactoryInstance() {
137 return g_factory.Pointer();
140 template <>
141 void BrowserContextKeyedAPIFactory<RuntimeAPI>::DeclareFactoryDependencies() {
142 DependsOn(ProcessManagerFactory::GetInstance());
145 RuntimeAPI::RuntimeAPI(content::BrowserContext* context)
146 : browser_context_(context),
147 dispatch_chrome_updated_event_(false),
148 extension_registry_observer_(this),
149 process_manager_observer_(this) {
150 // RuntimeAPI is redirected in incognito, so |browser_context_| is never
151 // incognito.
152 DCHECK(!browser_context_->IsOffTheRecord());
154 registrar_.Add(this,
155 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
156 content::Source<BrowserContext>(context));
157 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_));
158 process_manager_observer_.Add(ProcessManager::Get(browser_context_));
160 delegate_ = ExtensionsBrowserClient::Get()->CreateRuntimeAPIDelegate(
161 browser_context_);
163 // Check if registered events are up-to-date. We can only do this once
164 // per browser context, since it updates internal state when called.
165 dispatch_chrome_updated_event_ =
166 ExtensionsBrowserClient::Get()->DidVersionUpdate(browser_context_);
169 RuntimeAPI::~RuntimeAPI() {
172 void RuntimeAPI::Observe(int type,
173 const content::NotificationSource& source,
174 const content::NotificationDetails& details) {
175 DCHECK_EQ(extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED, type);
176 // We're done restarting Chrome after an update.
177 dispatch_chrome_updated_event_ = false;
179 delegate_->AddUpdateObserver(this);
182 void RuntimeAPI::OnExtensionLoaded(content::BrowserContext* browser_context,
183 const Extension* extension) {
184 if (!dispatch_chrome_updated_event_)
185 return;
187 // Dispatch the onInstalled event with reason "chrome_update".
188 base::MessageLoop::current()->PostTask(
189 FROM_HERE,
190 base::Bind(&RuntimeEventRouter::DispatchOnInstalledEvent,
191 browser_context_,
192 extension->id(),
193 Version(),
194 true));
197 void RuntimeAPI::OnExtensionWillBeInstalled(
198 content::BrowserContext* browser_context,
199 const Extension* extension,
200 bool is_update,
201 bool from_ephemeral,
202 const std::string& old_name) {
203 // Ephemeral apps are not considered to be installed and do not receive
204 // the onInstalled() event.
205 if (util::IsEphemeralApp(extension->id(), browser_context_))
206 return;
208 Version old_version = delegate_->GetPreviousExtensionVersion(extension);
210 // Dispatch the onInstalled event.
211 base::MessageLoop::current()->PostTask(
212 FROM_HERE,
213 base::Bind(&RuntimeEventRouter::DispatchOnInstalledEvent,
214 browser_context_,
215 extension->id(),
216 old_version,
217 false));
220 void RuntimeAPI::OnExtensionUninstalled(
221 content::BrowserContext* browser_context,
222 const Extension* extension,
223 UninstallReason reason) {
224 // Ephemeral apps are not considered to be installed, so the uninstall URL
225 // is not invoked when they are removed.
226 if (util::IsEphemeralApp(extension->id(), browser_context_))
227 return;
229 RuntimeEventRouter::OnExtensionUninstalled(
230 browser_context_, extension->id(), reason);
233 void RuntimeAPI::Shutdown() {
234 delegate_->RemoveUpdateObserver(this);
237 void RuntimeAPI::OnAppUpdateAvailable(const Extension* extension) {
238 RuntimeEventRouter::DispatchOnUpdateAvailableEvent(
239 browser_context_, extension->id(), extension->manifest()->value());
242 void RuntimeAPI::OnChromeUpdateAvailable() {
243 RuntimeEventRouter::DispatchOnBrowserUpdateAvailableEvent(browser_context_);
246 void RuntimeAPI::OnBackgroundHostStartup(const Extension* extension) {
247 RuntimeEventRouter::DispatchOnStartupEvent(browser_context_, extension->id());
250 void RuntimeAPI::ReloadExtension(const std::string& extension_id) {
251 delegate_->ReloadExtension(extension_id);
254 bool RuntimeAPI::CheckForUpdates(
255 const std::string& extension_id,
256 const RuntimeAPIDelegate::UpdateCheckCallback& callback) {
257 return delegate_->CheckForUpdates(extension_id, callback);
260 void RuntimeAPI::OpenURL(const GURL& update_url) {
261 delegate_->OpenURL(update_url);
264 bool RuntimeAPI::GetPlatformInfo(runtime::PlatformInfo* info) {
265 return delegate_->GetPlatformInfo(info);
268 bool RuntimeAPI::RestartDevice(std::string* error_message) {
269 return delegate_->RestartDevice(error_message);
272 bool RuntimeAPI::OpenOptionsPage(const Extension* extension) {
273 return delegate_->OpenOptionsPage(extension);
276 ///////////////////////////////////////////////////////////////////////////////
278 // static
279 void RuntimeEventRouter::DispatchOnStartupEvent(
280 content::BrowserContext* context,
281 const std::string& extension_id) {
282 DispatchOnStartupEventImpl(context, extension_id, true, NULL);
285 // static
286 void RuntimeEventRouter::DispatchOnInstalledEvent(
287 content::BrowserContext* context,
288 const std::string& extension_id,
289 const Version& old_version,
290 bool chrome_updated) {
291 if (!ExtensionsBrowserClient::Get()->IsValidContext(context))
292 return;
293 ExtensionSystem* system = ExtensionSystem::Get(context);
294 if (!system)
295 return;
297 scoped_ptr<base::ListValue> event_args(new base::ListValue());
298 base::DictionaryValue* info = new base::DictionaryValue();
299 event_args->Append(info);
300 if (old_version.IsValid()) {
301 info->SetString(kInstallReason, kInstallReasonUpdate);
302 info->SetString(kInstallPreviousVersion, old_version.GetString());
303 } else if (chrome_updated) {
304 info->SetString(kInstallReason, kInstallReasonChromeUpdate);
305 } else {
306 info->SetString(kInstallReason, kInstallReasonInstall);
308 EventRouter* event_router = EventRouter::Get(context);
309 DCHECK(event_router);
310 scoped_ptr<Event> event(
311 new Event(runtime::OnInstalled::kEventName, event_args.Pass()));
312 event_router->DispatchEventWithLazyListener(extension_id, event.Pass());
314 if (old_version.IsValid()) {
315 const Extension* extension =
316 ExtensionRegistry::Get(context)->enabled_extensions().GetByID(
317 extension_id);
318 if (extension && SharedModuleInfo::IsSharedModule(extension)) {
319 scoped_ptr<ExtensionSet> dependents =
320 system->GetDependentExtensions(extension);
321 for (ExtensionSet::const_iterator i = dependents->begin();
322 i != dependents->end();
323 i++) {
324 scoped_ptr<base::ListValue> sm_event_args(new base::ListValue());
325 base::DictionaryValue* sm_info = new base::DictionaryValue();
326 sm_event_args->Append(sm_info);
327 sm_info->SetString(kInstallReason, kInstallReasonSharedModuleUpdate);
328 sm_info->SetString(kInstallPreviousVersion, old_version.GetString());
329 sm_info->SetString(kInstallId, extension_id);
330 scoped_ptr<Event> sm_event(
331 new Event(runtime::OnInstalled::kEventName, sm_event_args.Pass()));
332 event_router->DispatchEventWithLazyListener((*i)->id(),
333 sm_event.Pass());
339 // static
340 void RuntimeEventRouter::DispatchOnUpdateAvailableEvent(
341 content::BrowserContext* context,
342 const std::string& extension_id,
343 const base::DictionaryValue* manifest) {
344 ExtensionSystem* system = ExtensionSystem::Get(context);
345 if (!system)
346 return;
348 scoped_ptr<base::ListValue> args(new base::ListValue);
349 args->Append(manifest->DeepCopy());
350 EventRouter* event_router = EventRouter::Get(context);
351 DCHECK(event_router);
352 scoped_ptr<Event> event(
353 new Event(runtime::OnUpdateAvailable::kEventName, args.Pass()));
354 event_router->DispatchEventToExtension(extension_id, event.Pass());
357 // static
358 void RuntimeEventRouter::DispatchOnBrowserUpdateAvailableEvent(
359 content::BrowserContext* context) {
360 ExtensionSystem* system = ExtensionSystem::Get(context);
361 if (!system)
362 return;
364 scoped_ptr<base::ListValue> args(new base::ListValue);
365 EventRouter* event_router = EventRouter::Get(context);
366 DCHECK(event_router);
367 scoped_ptr<Event> event(
368 new Event(runtime::OnBrowserUpdateAvailable::kEventName, args.Pass()));
369 event_router->BroadcastEvent(event.Pass());
372 // static
373 void RuntimeEventRouter::DispatchOnRestartRequiredEvent(
374 content::BrowserContext* context,
375 const std::string& app_id,
376 core_api::runtime::OnRestartRequiredReason reason) {
377 ExtensionSystem* system = ExtensionSystem::Get(context);
378 if (!system)
379 return;
381 scoped_ptr<Event> event(
382 new Event(runtime::OnRestartRequired::kEventName,
383 core_api::runtime::OnRestartRequired::Create(reason)));
384 EventRouter* event_router = EventRouter::Get(context);
385 DCHECK(event_router);
386 event_router->DispatchEventToExtension(app_id, event.Pass());
389 // static
390 void RuntimeEventRouter::OnExtensionUninstalled(
391 content::BrowserContext* context,
392 const std::string& extension_id,
393 UninstallReason reason) {
394 if (!(reason == UNINSTALL_REASON_USER_INITIATED ||
395 reason == UNINSTALL_REASON_MANAGEMENT_API)) {
396 return;
399 GURL uninstall_url(
400 GetUninstallURL(ExtensionPrefs::Get(context), extension_id));
402 if (uninstall_url.is_empty())
403 return;
405 RuntimeAPI::GetFactoryInstance()->Get(context)->OpenURL(uninstall_url);
408 ExtensionFunction::ResponseAction RuntimeGetBackgroundPageFunction::Run() {
409 ExtensionHost* host = ProcessManager::Get(browser_context())
410 ->GetBackgroundHostForExtension(extension_id());
411 if (LazyBackgroundTaskQueue::Get(browser_context())
412 ->ShouldEnqueueTask(browser_context(), extension())) {
413 LazyBackgroundTaskQueue::Get(browser_context())
414 ->AddPendingTask(
415 browser_context(), extension_id(),
416 base::Bind(&RuntimeGetBackgroundPageFunction::OnPageLoaded, this));
417 } else if (host) {
418 OnPageLoaded(host);
419 } else {
420 return RespondNow(Error(kNoBackgroundPageError));
423 return RespondLater();
426 void RuntimeGetBackgroundPageFunction::OnPageLoaded(ExtensionHost* host) {
427 if (host) {
428 Respond(NoArguments());
429 } else {
430 Respond(Error(kPageLoadError));
434 ExtensionFunction::ResponseAction RuntimeOpenOptionsPageFunction::Run() {
435 RuntimeAPI* api = RuntimeAPI::GetFactoryInstance()->Get(browser_context());
436 return RespondNow(api->OpenOptionsPage(extension())
437 ? NoArguments()
438 : Error(kFailedToCreateOptionsPage));
441 ExtensionFunction::ResponseAction RuntimeSetUninstallURLFunction::Run() {
442 std::string url_string;
443 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url_string));
445 GURL url(url_string);
446 if (!url.is_valid()) {
447 return RespondNow(
448 Error(ErrorUtils::FormatErrorMessage(kInvalidUrlError, url_string)));
450 SetUninstallURL(
451 ExtensionPrefs::Get(browser_context()), extension_id(), url_string);
452 return RespondNow(NoArguments());
455 ExtensionFunction::ResponseAction RuntimeReloadFunction::Run() {
456 RuntimeAPI::GetFactoryInstance()->Get(browser_context())->ReloadExtension(
457 extension_id());
458 return RespondNow(NoArguments());
461 ExtensionFunction::ResponseAction RuntimeRequestUpdateCheckFunction::Run() {
462 if (!RuntimeAPI::GetFactoryInstance()
463 ->Get(browser_context())
464 ->CheckForUpdates(
465 extension_id(),
466 base::Bind(&RuntimeRequestUpdateCheckFunction::CheckComplete,
467 this))) {
468 return RespondNow(Error(kUpdatesDisabledError));
470 return RespondLater();
473 void RuntimeRequestUpdateCheckFunction::CheckComplete(
474 const RuntimeAPIDelegate::UpdateCheckResult& result) {
475 if (result.success) {
476 base::DictionaryValue* details = new base::DictionaryValue;
477 details->SetString("version", result.version);
478 Respond(TwoArguments(new base::StringValue(result.response), details));
479 } else {
480 // HMM(kalman): Why does !success not imply Error()?
481 Respond(OneArgument(new base::StringValue(result.response)));
485 ExtensionFunction::ResponseAction RuntimeRestartFunction::Run() {
486 std::string message;
487 bool result =
488 RuntimeAPI::GetFactoryInstance()->Get(browser_context())->RestartDevice(
489 &message);
490 if (!result) {
491 return RespondNow(Error(message));
493 return RespondNow(NoArguments());
496 ExtensionFunction::ResponseAction RuntimeGetPlatformInfoFunction::Run() {
497 runtime::PlatformInfo info;
498 if (!RuntimeAPI::GetFactoryInstance()
499 ->Get(browser_context())
500 ->GetPlatformInfo(&info)) {
501 return RespondNow(Error(kPlatformInfoUnavailable));
503 return RespondNow(
504 ArgumentList(runtime::GetPlatformInfo::Results::Create(info)));
507 ExtensionFunction::ResponseAction
508 RuntimeGetPackageDirectoryEntryFunction::Run() {
509 storage::IsolatedContext* isolated_context =
510 storage::IsolatedContext::GetInstance();
511 DCHECK(isolated_context);
513 std::string relative_path = kPackageDirectoryPath;
514 base::FilePath path = extension_->path();
515 std::string filesystem_id = isolated_context->RegisterFileSystemForPath(
516 storage::kFileSystemTypeNativeLocal, std::string(), path, &relative_path);
518 int renderer_id = render_view_host_->GetProcess()->GetID();
519 content::ChildProcessSecurityPolicy* policy =
520 content::ChildProcessSecurityPolicy::GetInstance();
521 policy->GrantReadFileSystem(renderer_id, filesystem_id);
522 base::DictionaryValue* dict = new base::DictionaryValue();
523 dict->SetString("fileSystemId", filesystem_id);
524 dict->SetString("baseName", relative_path);
525 return RespondNow(OneArgument(dict));
528 } // namespace extensions