OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/browser/api/app_runtime/app_runtime_api.h" | 5 #include "extensions/browser/api/app_runtime/app_runtime_api.h" |
6 | 6 |
7 #include "base/metrics/histogram.h" | |
7 #include "base/time/time.h" | 8 #include "base/time/time.h" |
8 #include "base/values.h" | 9 #include "base/values.h" |
9 #include "extensions/browser/event_router.h" | 10 #include "extensions/browser/event_router.h" |
10 #include "extensions/browser/extension_prefs.h" | 11 #include "extensions/browser/extension_prefs.h" |
11 #include "extensions/browser/extension_system.h" | 12 #include "extensions/browser/extension_system.h" |
12 #include "extensions/browser/extensions_browser_client.h" | 13 #include "extensions/browser/extensions_browser_client.h" |
13 #include "extensions/browser/granted_file_entry.h" | 14 #include "extensions/browser/granted_file_entry.h" |
14 #include "extensions/common/api/app_runtime.h" | 15 #include "extensions/common/api/app_runtime.h" |
16 #include "extensions/common/constants.h" | |
17 #include "extensions/common/feature_switch.h" | |
15 #include "url/gurl.h" | 18 #include "url/gurl.h" |
16 | 19 |
17 using content::BrowserContext; | 20 using content::BrowserContext; |
18 | 21 |
19 namespace extensions { | 22 namespace extensions { |
20 | 23 |
21 namespace app_runtime = core_api::app_runtime; | 24 namespace app_runtime = core_api::app_runtime; |
22 | 25 |
23 namespace { | 26 namespace { |
24 | 27 |
25 void DispatchOnEmbedRequestedEventImpl( | 28 void DispatchOnEmbedRequestedEventImpl( |
26 const std::string& extension_id, | 29 const std::string& extension_id, |
27 scoped_ptr<base::DictionaryValue> app_embedding_request_data, | 30 scoped_ptr<base::DictionaryValue> app_embedding_request_data, |
28 content::BrowserContext* context) { | 31 content::BrowserContext* context) { |
29 scoped_ptr<base::ListValue> args(new base::ListValue()); | 32 scoped_ptr<base::ListValue> args(new base::ListValue()); |
30 args->Append(app_embedding_request_data.release()); | 33 args->Append(app_embedding_request_data.release()); |
31 ExtensionSystem* system = ExtensionSystem::Get(context); | 34 ExtensionSystem* system = ExtensionSystem::Get(context); |
32 scoped_ptr<Event> event( | 35 scoped_ptr<Event> event( |
33 new Event(app_runtime::OnEmbedRequested::kEventName, args.Pass())); | 36 new Event(app_runtime::OnEmbedRequested::kEventName, args.Pass())); |
34 event->restrict_to_browser_context = context; | 37 event->restrict_to_browser_context = context; |
35 system->event_router()->DispatchEventWithLazyListener(extension_id, | 38 system->event_router()->DispatchEventWithLazyListener(extension_id, |
36 event.Pass()); | 39 event.Pass()); |
37 | 40 |
38 ExtensionPrefs::Get(context) | 41 ExtensionPrefs::Get(context) |
39 ->SetLastLaunchTime(extension_id, base::Time::Now()); | 42 ->SetLastLaunchTime(extension_id, base::Time::Now()); |
40 } | 43 } |
41 | 44 |
42 void DispatchOnLaunchedEventImpl(const std::string& extension_id, | 45 void DispatchOnLaunchedEventImpl(const std::string& extension_id, |
43 scoped_ptr<base::DictionaryValue> launch_data, | 46 scoped_ptr<base::DictionaryValue> launch_data, |
44 BrowserContext* context) { | 47 BrowserContext* context) { |
benwells
2014/10/30 06:04:28
Please add source in here and do the metrics repor
cylee1
2014/10/30 13:03:43
Tried, but since source is already translated into
benwells
2014/10/30 22:30:55
I meant to pass in the source as an app_runtime::L
cylee1
2014/10/31 06:37:53
Done.
| |
45 // "Forced app mode" is true for Chrome OS kiosk mode. | 48 // "Forced app mode" is true for Chrome OS kiosk mode. |
46 launch_data->SetBoolean( | 49 launch_data->SetBoolean( |
47 "isKioskSession", | 50 "isKioskSession", |
48 ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode()); | 51 ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode()); |
49 scoped_ptr<base::ListValue> args(new base::ListValue()); | 52 scoped_ptr<base::ListValue> args(new base::ListValue()); |
50 args->Append(launch_data.release()); | 53 args->Append(launch_data.release()); |
51 scoped_ptr<Event> event( | 54 scoped_ptr<Event> event( |
52 new Event(app_runtime::OnLaunched::kEventName, args.Pass())); | 55 new Event(app_runtime::OnLaunched::kEventName, args.Pass())); |
53 event->restrict_to_browser_context = context; | 56 event->restrict_to_browser_context = context; |
54 EventRouter::Get(context) | 57 EventRouter::Get(context) |
55 ->DispatchEventWithLazyListener(extension_id, event.Pass()); | 58 ->DispatchEventWithLazyListener(extension_id, event.Pass()); |
56 ExtensionPrefs::Get(context) | 59 ExtensionPrefs::Get(context) |
57 ->SetLastLaunchTime(extension_id, base::Time::Now()); | 60 ->SetLastLaunchTime(extension_id, base::Time::Now()); |
58 } | 61 } |
59 | 62 |
63 app_runtime::LaunchSource getLaunchSourceEnum( | |
64 extensions::AppLaunchSource source) { | |
65 switch (source) { | |
66 case extensions::SOURCE_APP_LAUNCHER: | |
67 return app_runtime::LAUNCH_SOURCE_APP_LAUNCHER; | |
68 case extensions::SOURCE_NEW_TAB_PAGE: | |
69 return app_runtime::LAUNCH_SOURCE_NEW_TAB_PAGE; | |
70 case extensions::SOURCE_RELOAD: | |
71 return app_runtime::LAUNCH_SOURCE_RELOAD; | |
72 case extensions::SOURCE_RESTART: | |
73 return app_runtime::LAUNCH_SOURCE_RESTART; | |
74 case extensions::SOURCE_LOAD_AND_LAUNCH: | |
75 return app_runtime::LAUNCH_SOURCE_LOAD_AND_LAUNCH; | |
76 case extensions::SOURCE_COMMAND_LINE: | |
77 return app_runtime::LAUNCH_SOURCE_COMMAND_LINE; | |
78 case extensions::SOURCE_FILE_HANDLER: | |
79 return app_runtime::LAUNCH_SOURCE_FILE_HANDLER; | |
80 case extensions::SOURCE_URL_HANDLER: | |
81 return app_runtime::LAUNCH_SOURCE_URL_HANDLER; | |
82 | |
83 case extensions::SOURCE_SYSTEM_TRAY: | |
84 return app_runtime::LAUNCH_SOURCE_SYSTEM_TRAY; | |
85 case extensions::SOURCE_ABOUT_PAGE: | |
86 return app_runtime::LAUNCH_SOURCE_ABOUT_PAGE; | |
87 case extensions::SOURCE_KEYBOARD: | |
88 return app_runtime::LAUNCH_SOURCE_KEYBOARD; | |
89 | |
90 default: | |
91 return app_runtime::LAUNCH_SOURCE_NONE; | |
92 } | |
93 } | |
94 | |
95 void ReportMetrics(app_runtime::LaunchSource source) { | |
96 UMA_HISTOGRAM_ENUMERATION( | |
97 "Extensions.AppLaunchSource", source, NUM_APP_LAUNCH_SOURCES); | |
98 } | |
99 | |
60 } // namespace | 100 } // namespace |
61 | 101 |
62 // static | 102 // static |
63 void AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent( | 103 void AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent( |
64 content::BrowserContext* context, | 104 content::BrowserContext* context, |
65 scoped_ptr<base::DictionaryValue> embed_app_data, | 105 scoped_ptr<base::DictionaryValue> embed_app_data, |
66 const Extension* extension) { | 106 const Extension* extension) { |
67 DispatchOnEmbedRequestedEventImpl( | 107 DispatchOnEmbedRequestedEventImpl( |
68 extension->id(), embed_app_data.Pass(), context); | 108 extension->id(), embed_app_data.Pass(), context); |
69 } | 109 } |
70 | 110 |
71 // static | 111 // static |
72 void AppRuntimeEventRouter::DispatchOnLaunchedEvent( | 112 void AppRuntimeEventRouter::DispatchOnLaunchedEvent( |
73 BrowserContext* context, | 113 BrowserContext* context, |
74 const Extension* extension) { | 114 const Extension* extension, |
75 scoped_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue()); | 115 extensions::AppLaunchSource source) { |
76 DispatchOnLaunchedEventImpl(extension->id(), launch_data.Pass(), context); | 116 app_runtime::LaunchData launch_data; |
117 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) { | |
118 launch_data.source = getLaunchSourceEnum(source); | |
119 ReportMetrics(launch_data.source); | |
120 } | |
121 DispatchOnLaunchedEventImpl( | |
122 extension->id(), launch_data.ToValue().Pass(), context); | |
77 } | 123 } |
78 | 124 |
79 // static | 125 // static |
80 void AppRuntimeEventRouter::DispatchOnRestartedEvent( | 126 void AppRuntimeEventRouter::DispatchOnRestartedEvent( |
81 BrowserContext* context, | 127 BrowserContext* context, |
82 const Extension* extension) { | 128 const Extension* extension) { |
83 scoped_ptr<base::ListValue> arguments(new base::ListValue()); | 129 scoped_ptr<base::ListValue> arguments(new base::ListValue()); |
84 scoped_ptr<Event> event( | 130 scoped_ptr<Event> event( |
85 new Event(app_runtime::OnRestarted::kEventName, arguments.Pass())); | 131 new Event(app_runtime::OnRestarted::kEventName, arguments.Pass())); |
86 event->restrict_to_browser_context = context; | 132 event->restrict_to_browser_context = context; |
87 EventRouter::Get(context) | 133 EventRouter::Get(context) |
88 ->DispatchEventToExtension(extension->id(), event.Pass()); | 134 ->DispatchEventToExtension(extension->id(), event.Pass()); |
89 } | 135 } |
90 | 136 |
91 // static | 137 // static |
92 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithFileEntries( | 138 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithFileEntries( |
93 BrowserContext* context, | 139 BrowserContext* context, |
94 const Extension* extension, | 140 const Extension* extension, |
95 const std::string& handler_id, | 141 const std::string& handler_id, |
96 const std::vector<std::string>& mime_types, | 142 const std::vector<std::string>& mime_types, |
97 const std::vector<GrantedFileEntry>& file_entries) { | 143 const std::vector<GrantedFileEntry>& file_entries) { |
98 // TODO(sergeygs): Use the same way of creating an event (using the generated | 144 // TODO(sergeygs): Use the same way of creating an event (using the generated |
99 // boilerplate) as below in DispatchOnLaunchedEventWithUrl. | 145 // boilerplate) as below in DispatchOnLaunchedEventWithUrl. |
100 scoped_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue); | 146 base::DictionaryValue* launch_data = new base::DictionaryValue(); |
101 launch_data->SetString("id", handler_id); | 147 launch_data->SetString("id", handler_id); |
102 scoped_ptr<base::ListValue> items(new base::ListValue); | 148 |
149 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) { | |
150 launch_data->SetString( | |
151 "source", | |
152 app_runtime::ToString(app_runtime::LAUNCH_SOURCE_FILE_HANDLER)); | |
153 ReportMetrics(app_runtime::LAUNCH_SOURCE_FILE_HANDLER); | |
154 } | |
155 | |
156 base::ListValue* items = new base::ListValue(); | |
103 DCHECK(file_entries.size() == mime_types.size()); | 157 DCHECK(file_entries.size() == mime_types.size()); |
104 for (size_t i = 0; i < file_entries.size(); ++i) { | 158 for (size_t i = 0; i < file_entries.size(); ++i) { |
105 scoped_ptr<base::DictionaryValue> launch_item(new base::DictionaryValue); | 159 base::DictionaryValue* launch_item = new base::DictionaryValue(); |
106 launch_item->SetString("fileSystemId", file_entries[i].filesystem_id); | 160 |
107 launch_item->SetString("baseName", file_entries[i].registered_name); | 161 base::DictionaryValue* raw_entry = new base::DictionaryValue(); |
108 launch_item->SetString("mimeType", mime_types[i]); | 162 raw_entry->SetString("fileSystemId", file_entries[i].filesystem_id); |
109 launch_item->SetString("entryId", file_entries[i].id); | 163 raw_entry->SetString("baseName", file_entries[i].registered_name); |
110 items->Append(launch_item.release()); | 164 raw_entry->SetString("entryId", file_entries[i].id); |
165 | |
166 launch_item->Set("rawEntry", raw_entry); | |
167 launch_item->SetString("type", mime_types[i]); | |
168 | |
169 items->Append(launch_item); | |
111 } | 170 } |
112 launch_data->Set("items", items.release()); | 171 launch_data->Set("items", items); |
113 DispatchOnLaunchedEventImpl(extension->id(), launch_data.Pass(), context); | 172 DispatchOnLaunchedEventImpl( |
173 extension->id(), scoped_ptr<base::DictionaryValue>(launch_data), context); | |
114 } | 174 } |
115 | 175 |
116 // static | 176 // static |
117 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithUrl( | 177 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithUrl( |
118 BrowserContext* context, | 178 BrowserContext* context, |
119 const Extension* extension, | 179 const Extension* extension, |
120 const std::string& handler_id, | 180 const std::string& handler_id, |
121 const GURL& url, | 181 const GURL& url, |
122 const GURL& referrer_url) { | 182 const GURL& referrer_url) { |
123 app_runtime::LaunchData launch_data; | 183 app_runtime::LaunchData launch_data; |
124 launch_data.id.reset(new std::string(handler_id)); | 184 launch_data.id.reset(new std::string(handler_id)); |
125 launch_data.url.reset(new std::string(url.spec())); | 185 launch_data.url.reset(new std::string(url.spec())); |
126 launch_data.referrer_url.reset(new std::string(referrer_url.spec())); | 186 launch_data.referrer_url.reset(new std::string(referrer_url.spec())); |
187 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) { | |
188 launch_data.source = app_runtime::LAUNCH_SOURCE_URL_HANDLER; | |
189 ReportMetrics(app_runtime::LAUNCH_SOURCE_URL_HANDLER); | |
190 } | |
127 DispatchOnLaunchedEventImpl( | 191 DispatchOnLaunchedEventImpl( |
128 extension->id(), launch_data.ToValue().Pass(), context); | 192 extension->id(), launch_data.ToValue().Pass(), context); |
129 } | 193 } |
130 | 194 |
131 } // namespace extensions | 195 } // namespace extensions |
OLD | NEW |