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/extensions_browser_client.h"
13 #include "extensions/browser/granted_file_entry.h"
14 #include "extensions/common/api/app_runtime.h"
15 #include "extensions/common/constants.h"
16 #include "extensions/common/feature_switch.h"
19 using content::BrowserContext
;
21 namespace extensions
{
23 namespace app_runtime
= api::app_runtime
;
27 void DispatchOnEmbedRequestedEventImpl(
28 const std::string
& extension_id
,
29 scoped_ptr
<base::DictionaryValue
> app_embedding_request_data
,
30 content::BrowserContext
* context
) {
31 scoped_ptr
<base::ListValue
> args(new base::ListValue());
32 args
->Append(app_embedding_request_data
.release());
33 scoped_ptr
<Event
> event(new Event(events::APP_RUNTIME_ON_EMBED_REQUESTED
,
34 app_runtime::OnEmbedRequested::kEventName
,
36 event
->restrict_to_browser_context
= context
;
37 EventRouter::Get(context
)
38 ->DispatchEventWithLazyListener(extension_id
, event
.Pass());
40 ExtensionPrefs::Get(context
)
41 ->SetLastLaunchTime(extension_id
, base::Time::Now());
44 void DispatchOnLaunchedEventImpl(const std::string
& extension_id
,
45 app_runtime::LaunchSource source
,
46 scoped_ptr
<base::DictionaryValue
> launch_data
,
47 BrowserContext
* context
) {
48 UMA_HISTOGRAM_ENUMERATION(
49 "Extensions.AppLaunchSource", source
, NUM_APP_LAUNCH_SOURCES
);
51 // "Forced app mode" is true for Chrome OS kiosk mode.
52 launch_data
->SetBoolean(
54 ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode());
55 scoped_ptr
<base::ListValue
> args(new base::ListValue());
56 args
->Append(launch_data
.release());
57 scoped_ptr
<Event
> event(new Event(events::APP_RUNTIME_ON_LAUNCHED
,
58 app_runtime::OnLaunched::kEventName
,
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
) {
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
;
104 case extensions::SOURCE_TEST
:
105 return app_runtime::LAUNCH_SOURCE_TEST
;
108 return app_runtime::LAUNCH_SOURCE_NONE
;
115 void AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent(
116 content::BrowserContext
* context
,
117 scoped_ptr
<base::DictionaryValue
> embed_app_data
,
118 const Extension
* extension
) {
119 DispatchOnEmbedRequestedEventImpl(
120 extension
->id(), embed_app_data
.Pass(), context
);
124 void AppRuntimeEventRouter::DispatchOnLaunchedEvent(
125 BrowserContext
* context
,
126 const Extension
* extension
,
127 extensions::AppLaunchSource source
) {
128 app_runtime::LaunchData launch_data
;
130 app_runtime::LaunchSource source_enum
= getLaunchSourceEnum(source
);
131 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) {
132 launch_data
.source
= source_enum
;
134 DispatchOnLaunchedEventImpl(
135 extension
->id(), source_enum
, launch_data
.ToValue().Pass(), context
);
139 void AppRuntimeEventRouter::DispatchOnRestartedEvent(
140 BrowserContext
* context
,
141 const Extension
* extension
) {
142 scoped_ptr
<base::ListValue
> arguments(new base::ListValue());
143 scoped_ptr
<Event
> event(new Event(events::APP_RUNTIME_ON_RESTARTED
,
144 app_runtime::OnRestarted::kEventName
,
146 event
->restrict_to_browser_context
= context
;
147 EventRouter::Get(context
)
148 ->DispatchEventToExtension(extension
->id(), event
.Pass());
152 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithFileEntries(
153 BrowserContext
* context
,
154 const Extension
* extension
,
155 const std::string
& handler_id
,
156 const std::vector
<std::string
>& mime_types
,
157 const std::vector
<GrantedFileEntry
>& file_entries
) {
158 // TODO(sergeygs): Use the same way of creating an event (using the generated
159 // boilerplate) as below in DispatchOnLaunchedEventWithUrl.
160 scoped_ptr
<base::DictionaryValue
> launch_data(new base::DictionaryValue
);
161 launch_data
->SetString("id", handler_id
);
163 app_runtime::LaunchSource source_enum
=
164 app_runtime::LAUNCH_SOURCE_FILE_HANDLER
;
165 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) {
166 launch_data
->SetString("source", app_runtime::ToString(source_enum
));
169 scoped_ptr
<base::ListValue
> items(new base::ListValue
);
170 DCHECK(file_entries
.size() == mime_types
.size());
171 for (size_t i
= 0; i
< file_entries
.size(); ++i
) {
172 scoped_ptr
<base::DictionaryValue
> launch_item(new base::DictionaryValue
);
174 launch_item
->SetString("fileSystemId", file_entries
[i
].filesystem_id
);
175 launch_item
->SetString("baseName", file_entries
[i
].registered_name
);
176 launch_item
->SetString("mimeType", mime_types
[i
]);
177 launch_item
->SetString("entryId", file_entries
[i
].id
);
178 items
->Append(launch_item
.release());
180 launch_data
->Set("items", items
.release());
181 DispatchOnLaunchedEventImpl(
182 extension
->id(), source_enum
, launch_data
.Pass(), context
);
186 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithUrl(
187 BrowserContext
* context
,
188 const Extension
* extension
,
189 const std::string
& handler_id
,
191 const GURL
& referrer_url
) {
192 app_runtime::LaunchData launch_data
;
193 app_runtime::LaunchSource source_enum
=
194 app_runtime::LAUNCH_SOURCE_URL_HANDLER
;
195 launch_data
.id
.reset(new std::string(handler_id
));
196 launch_data
.url
.reset(new std::string(url
.spec()));
197 launch_data
.referrer_url
.reset(new std::string(referrer_url
.spec()));
198 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) {
199 launch_data
.source
= source_enum
;
201 DispatchOnLaunchedEventImpl(
202 extension
->id(), source_enum
, launch_data
.ToValue().Pass(), context
);
205 } // namespace extensions