Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / extensions / browser / api / app_runtime / app_runtime_api.cc
blob6cb7c716e9aa935192803b4fc048f1ecfd577fb8
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/metrics/histogram.h"
8 #include "base/time/time.h"
9 #include "base/values.h"
10 #include "extensions/browser/event_router.h"
11 #include "extensions/browser/extension_prefs.h"
12 #include "extensions/browser/extension_system.h"
13 #include "extensions/browser/extensions_browser_client.h"
14 #include "extensions/browser/granted_file_entry.h"
15 #include "extensions/common/api/app_runtime.h"
16 #include "extensions/common/constants.h"
17 #include "extensions/common/feature_switch.h"
18 #include "url/gurl.h"
20 using content::BrowserContext;
22 namespace extensions {
24 namespace app_runtime = core_api::app_runtime;
26 namespace {
28 void DispatchOnEmbedRequestedEventImpl(
29 const std::string& extension_id,
30 scoped_ptr<base::DictionaryValue> app_embedding_request_data,
31 content::BrowserContext* context) {
32 scoped_ptr<base::ListValue> args(new base::ListValue());
33 args->Append(app_embedding_request_data.release());
34 ExtensionSystem* system = ExtensionSystem::Get(context);
35 scoped_ptr<Event> event(
36 new Event(app_runtime::OnEmbedRequested::kEventName, args.Pass()));
37 event->restrict_to_browser_context = context;
38 system->event_router()->DispatchEventWithLazyListener(extension_id,
39 event.Pass());
41 ExtensionPrefs::Get(context)
42 ->SetLastLaunchTime(extension_id, base::Time::Now());
45 void DispatchOnLaunchedEventImpl(const std::string& extension_id,
46 app_runtime::LaunchSource source,
47 scoped_ptr<base::DictionaryValue> launch_data,
48 BrowserContext* context) {
49 UMA_HISTOGRAM_ENUMERATION(
50 "Extensions.AppLaunchSource", source, NUM_APP_LAUNCH_SOURCES);
52 // "Forced app mode" is true for Chrome OS kiosk mode.
53 launch_data->SetBoolean(
54 "isKioskSession",
55 ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode());
56 scoped_ptr<base::ListValue> args(new base::ListValue());
57 args->Append(launch_data.release());
58 scoped_ptr<Event> event(
59 new Event(app_runtime::OnLaunched::kEventName, args.Pass()));
60 event->restrict_to_browser_context = context;
61 EventRouter::Get(context)
62 ->DispatchEventWithLazyListener(extension_id, event.Pass());
63 ExtensionPrefs::Get(context)
64 ->SetLastLaunchTime(extension_id, base::Time::Now());
67 app_runtime::LaunchSource getLaunchSourceEnum(
68 extensions::AppLaunchSource source) {
69 switch (source) {
70 case extensions::SOURCE_APP_LAUNCHER:
71 return app_runtime::LAUNCH_SOURCE_APP_LAUNCHER;
72 case extensions::SOURCE_NEW_TAB_PAGE:
73 return app_runtime::LAUNCH_SOURCE_NEW_TAB_PAGE;
74 case extensions::SOURCE_RELOAD:
75 return app_runtime::LAUNCH_SOURCE_RELOAD;
76 case extensions::SOURCE_RESTART:
77 return app_runtime::LAUNCH_SOURCE_RESTART;
78 case extensions::SOURCE_LOAD_AND_LAUNCH:
79 return app_runtime::LAUNCH_SOURCE_LOAD_AND_LAUNCH;
80 case extensions::SOURCE_COMMAND_LINE:
81 return app_runtime::LAUNCH_SOURCE_COMMAND_LINE;
82 case extensions::SOURCE_FILE_HANDLER:
83 return app_runtime::LAUNCH_SOURCE_FILE_HANDLER;
84 case extensions::SOURCE_URL_HANDLER:
85 return app_runtime::LAUNCH_SOURCE_URL_HANDLER;
86 case extensions::SOURCE_SYSTEM_TRAY:
87 return app_runtime::LAUNCH_SOURCE_SYSTEM_TRAY;
88 case extensions::SOURCE_ABOUT_PAGE:
89 return app_runtime::LAUNCH_SOURCE_ABOUT_PAGE;
90 case extensions::SOURCE_KEYBOARD:
91 return app_runtime::LAUNCH_SOURCE_KEYBOARD;
92 case extensions::SOURCE_EXTENSIONS_PAGE:
93 return app_runtime::LAUNCH_SOURCE_EXTENSIONS_PAGE;
94 case extensions::SOURCE_MANAGEMENT_API:
95 return app_runtime::LAUNCH_SOURCE_MANAGEMENT_API;
96 case extensions::SOURCE_EPHEMERAL_APP:
97 return app_runtime::LAUNCH_SOURCE_EPHEMERAL_APP;
98 case extensions::SOURCE_BACKGROUND:
99 return app_runtime::LAUNCH_SOURCE_BACKGROUND;
100 case extensions::SOURCE_KIOSK:
101 return app_runtime::LAUNCH_SOURCE_KIOSK;
102 case extensions::SOURCE_CHROME_INTERNAL:
103 return app_runtime::LAUNCH_SOURCE_CHROME_INTERNAL;
105 default:
106 return app_runtime::LAUNCH_SOURCE_NONE;
110 } // namespace
112 // static
113 void AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent(
114 content::BrowserContext* context,
115 scoped_ptr<base::DictionaryValue> embed_app_data,
116 const Extension* extension) {
117 DispatchOnEmbedRequestedEventImpl(
118 extension->id(), embed_app_data.Pass(), context);
121 // static
122 void AppRuntimeEventRouter::DispatchOnLaunchedEvent(
123 BrowserContext* context,
124 const Extension* extension,
125 extensions::AppLaunchSource source) {
126 app_runtime::LaunchData launch_data;
128 app_runtime::LaunchSource source_enum = getLaunchSourceEnum(source);
129 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) {
130 launch_data.source = source_enum;
132 DispatchOnLaunchedEventImpl(
133 extension->id(), source_enum, launch_data.ToValue().Pass(), context);
136 // static
137 void AppRuntimeEventRouter::DispatchOnRestartedEvent(
138 BrowserContext* context,
139 const Extension* extension) {
140 scoped_ptr<base::ListValue> arguments(new base::ListValue());
141 scoped_ptr<Event> event(
142 new Event(app_runtime::OnRestarted::kEventName, arguments.Pass()));
143 event->restrict_to_browser_context = context;
144 EventRouter::Get(context)
145 ->DispatchEventToExtension(extension->id(), event.Pass());
148 // static
149 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithFileEntries(
150 BrowserContext* context,
151 const Extension* extension,
152 const std::string& handler_id,
153 const std::vector<std::string>& mime_types,
154 const std::vector<GrantedFileEntry>& file_entries) {
155 // TODO(sergeygs): Use the same way of creating an event (using the generated
156 // boilerplate) as below in DispatchOnLaunchedEventWithUrl.
157 scoped_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue);
158 launch_data->SetString("id", handler_id);
160 app_runtime::LaunchSource source_enum =
161 app_runtime::LAUNCH_SOURCE_FILE_HANDLER;
162 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) {
163 launch_data->SetString("source", app_runtime::ToString(source_enum));
166 scoped_ptr<base::ListValue> items(new base::ListValue);
167 DCHECK(file_entries.size() == mime_types.size());
168 for (size_t i = 0; i < file_entries.size(); ++i) {
169 scoped_ptr<base::DictionaryValue> launch_item(new base::DictionaryValue);
171 launch_item->SetString("fileSystemId", file_entries[i].filesystem_id);
172 launch_item->SetString("baseName", file_entries[i].registered_name);
173 launch_item->SetString("mimeType", mime_types[i]);
174 launch_item->SetString("entryId", file_entries[i].id);
175 items->Append(launch_item.release());
177 launch_data->Set("items", items.release());
178 DispatchOnLaunchedEventImpl(
179 extension->id(), source_enum, launch_data.Pass(), context);
182 // static
183 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithUrl(
184 BrowserContext* context,
185 const Extension* extension,
186 const std::string& handler_id,
187 const GURL& url,
188 const GURL& referrer_url) {
189 app_runtime::LaunchData launch_data;
190 app_runtime::LaunchSource source_enum =
191 app_runtime::LAUNCH_SOURCE_URL_HANDLER;
192 launch_data.id.reset(new std::string(handler_id));
193 launch_data.url.reset(new std::string(url.spec()));
194 launch_data.referrer_url.reset(new std::string(referrer_url.spec()));
195 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) {
196 launch_data.source = source_enum;
198 DispatchOnLaunchedEventImpl(
199 extension->id(), source_enum, launch_data.ToValue().Pass(), context);
202 } // namespace extensions