Add testing/scripts/OWNERS
[chromium-blink-merge.git] / extensions / browser / api / app_runtime / app_runtime_api.cc
blob0704478dbeac084aa2b03cad4219f9a95a06c2a7
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/app_runtime/app_runtime_api.h"
7 #include "base/time/time.h"
8 #include "base/values.h"
9 #include "extensions/browser/event_router.h"
10 #include "extensions/browser/extension_prefs.h"
11 #include "extensions/browser/extension_system.h"
12 #include "extensions/browser/extensions_browser_client.h"
13 #include "extensions/browser/granted_file_entry.h"
14 #include "extensions/common/api/app_runtime.h"
15 #include "url/gurl.h"
17 using content::BrowserContext;
19 namespace extensions {
21 namespace app_runtime = core_api::app_runtime;
23 namespace {
25 void DispatchOnEmbedRequestedEventImpl(
26 const std::string& extension_id,
27 scoped_ptr<base::DictionaryValue> app_embedding_request_data,
28 content::BrowserContext* context) {
29 scoped_ptr<base::ListValue> args(new base::ListValue());
30 args->Append(app_embedding_request_data.release());
31 ExtensionSystem* system = ExtensionSystem::Get(context);
32 scoped_ptr<Event> event(
33 new Event(app_runtime::OnEmbedRequested::kEventName, args.Pass()));
34 event->restrict_to_browser_context = context;
35 system->event_router()->DispatchEventWithLazyListener(extension_id,
36 event.Pass());
38 ExtensionPrefs::Get(context)
39 ->SetLastLaunchTime(extension_id, base::Time::Now());
42 void DispatchOnLaunchedEventImpl(const std::string& extension_id,
43 scoped_ptr<base::DictionaryValue> launch_data,
44 BrowserContext* context) {
45 // "Forced app mode" is true for Chrome OS kiosk mode.
46 launch_data->SetBoolean(
47 "isKioskSession",
48 ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode());
49 scoped_ptr<base::ListValue> args(new base::ListValue());
50 args->Append(launch_data.release());
51 scoped_ptr<Event> event(
52 new Event(app_runtime::OnLaunched::kEventName, args.Pass()));
53 event->restrict_to_browser_context = context;
54 EventRouter::Get(context)
55 ->DispatchEventWithLazyListener(extension_id, event.Pass());
56 ExtensionPrefs::Get(context)
57 ->SetLastLaunchTime(extension_id, base::Time::Now());
60 } // namespace
62 // static
63 void AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent(
64 content::BrowserContext* context,
65 scoped_ptr<base::DictionaryValue> embed_app_data,
66 const Extension* extension) {
67 DispatchOnEmbedRequestedEventImpl(
68 extension->id(), embed_app_data.Pass(), context);
71 // static
72 void AppRuntimeEventRouter::DispatchOnLaunchedEvent(
73 BrowserContext* context,
74 const Extension* extension) {
75 scoped_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue());
76 DispatchOnLaunchedEventImpl(extension->id(), launch_data.Pass(), context);
79 // static
80 void AppRuntimeEventRouter::DispatchOnRestartedEvent(
81 BrowserContext* context,
82 const Extension* extension) {
83 scoped_ptr<base::ListValue> arguments(new base::ListValue());
84 scoped_ptr<Event> event(
85 new Event(app_runtime::OnRestarted::kEventName, arguments.Pass()));
86 event->restrict_to_browser_context = context;
87 EventRouter::Get(context)
88 ->DispatchEventToExtension(extension->id(), event.Pass());
91 // static
92 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithFileEntries(
93 BrowserContext* context,
94 const Extension* extension,
95 const std::string& handler_id,
96 const std::vector<std::string>& mime_types,
97 const std::vector<GrantedFileEntry>& file_entries) {
98 // TODO(sergeygs): Use the same way of creating an event (using the generated
99 // boilerplate) as below in DispatchOnLaunchedEventWithUrl.
100 scoped_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue);
101 launch_data->SetString("id", handler_id);
102 scoped_ptr<base::ListValue> items(new base::ListValue);
103 DCHECK(file_entries.size() == mime_types.size());
104 for (size_t i = 0; i < file_entries.size(); ++i) {
105 scoped_ptr<base::DictionaryValue> launch_item(new base::DictionaryValue);
106 launch_item->SetString("fileSystemId", file_entries[i].filesystem_id);
107 launch_item->SetString("baseName", file_entries[i].registered_name);
108 launch_item->SetString("mimeType", mime_types[i]);
109 launch_item->SetString("entryId", file_entries[i].id);
110 items->Append(launch_item.release());
112 launch_data->Set("items", items.release());
113 DispatchOnLaunchedEventImpl(extension->id(), launch_data.Pass(), context);
116 // static
117 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithUrl(
118 BrowserContext* context,
119 const Extension* extension,
120 const std::string& handler_id,
121 const GURL& url,
122 const GURL& referrer_url) {
123 app_runtime::LaunchData launch_data;
124 launch_data.id.reset(new std::string(handler_id));
125 launch_data.url.reset(new std::string(url.spec()));
126 launch_data.referrer_url.reset(new std::string(referrer_url.spec()));
127 DispatchOnLaunchedEventImpl(
128 extension->id(), launch_data.ToValue().Pass(), context);
131 } // namespace extensions