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"
20 using content::BrowserContext
;
22 namespace extensions
{
24 namespace app_runtime
= core_api::app_runtime
;
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
,
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(
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
) {
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(
144 new Event(app_runtime::OnRestarted::kEventName
, arguments
.Pass()));
145 event
->restrict_to_browser_context
= context
;
146 EventRouter::Get(context
)
147 ->DispatchEventToExtension(extension
->id(), event
.Pass());
151 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithFileEntries(
152 BrowserContext
* context
,
153 const Extension
* extension
,
154 const std::string
& handler_id
,
155 const std::vector
<std::string
>& mime_types
,
156 const std::vector
<GrantedFileEntry
>& file_entries
) {
157 // TODO(sergeygs): Use the same way of creating an event (using the generated
158 // boilerplate) as below in DispatchOnLaunchedEventWithUrl.
159 scoped_ptr
<base::DictionaryValue
> launch_data(new base::DictionaryValue
);
160 launch_data
->SetString("id", handler_id
);
162 app_runtime::LaunchSource source_enum
=
163 app_runtime::LAUNCH_SOURCE_FILE_HANDLER
;
164 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) {
165 launch_data
->SetString("source", app_runtime::ToString(source_enum
));
168 scoped_ptr
<base::ListValue
> items(new base::ListValue
);
169 DCHECK(file_entries
.size() == mime_types
.size());
170 for (size_t i
= 0; i
< file_entries
.size(); ++i
) {
171 scoped_ptr
<base::DictionaryValue
> launch_item(new base::DictionaryValue
);
173 launch_item
->SetString("fileSystemId", file_entries
[i
].filesystem_id
);
174 launch_item
->SetString("baseName", file_entries
[i
].registered_name
);
175 launch_item
->SetString("mimeType", mime_types
[i
]);
176 launch_item
->SetString("entryId", file_entries
[i
].id
);
177 items
->Append(launch_item
.release());
179 launch_data
->Set("items", items
.release());
180 DispatchOnLaunchedEventImpl(
181 extension
->id(), source_enum
, launch_data
.Pass(), context
);
185 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithUrl(
186 BrowserContext
* context
,
187 const Extension
* extension
,
188 const std::string
& handler_id
,
190 const GURL
& referrer_url
) {
191 app_runtime::LaunchData launch_data
;
192 app_runtime::LaunchSource source_enum
=
193 app_runtime::LAUNCH_SOURCE_URL_HANDLER
;
194 launch_data
.id
.reset(new std::string(handler_id
));
195 launch_data
.url
.reset(new std::string(url
.spec()));
196 launch_data
.referrer_url
.reset(new std::string(referrer_url
.spec()));
197 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) {
198 launch_data
.source
= source_enum
;
200 DispatchOnLaunchedEventImpl(
201 extension
->id(), source_enum
, launch_data
.ToValue().Pass(), context
);
204 } // namespace extensions