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, |
| 46 app_runtime::LaunchSource source, |
43 scoped_ptr<base::DictionaryValue> launch_data, | 47 scoped_ptr<base::DictionaryValue> launch_data, |
44 BrowserContext* context) { | 48 BrowserContext* context) { |
| 49 UMA_HISTOGRAM_ENUMERATION( |
| 50 "Extensions.AppLaunchSource", source, NUM_APP_LAUNCH_SOURCES); |
| 51 |
45 // "Forced app mode" is true for Chrome OS kiosk mode. | 52 // "Forced app mode" is true for Chrome OS kiosk mode. |
46 launch_data->SetBoolean( | 53 launch_data->SetBoolean( |
47 "isKioskSession", | 54 "isKioskSession", |
48 ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode()); | 55 ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode()); |
49 scoped_ptr<base::ListValue> args(new base::ListValue()); | 56 scoped_ptr<base::ListValue> args(new base::ListValue()); |
50 args->Append(launch_data.release()); | 57 args->Append(launch_data.release()); |
51 scoped_ptr<Event> event( | 58 scoped_ptr<Event> event( |
52 new Event(app_runtime::OnLaunched::kEventName, args.Pass())); | 59 new Event(app_runtime::OnLaunched::kEventName, args.Pass())); |
53 event->restrict_to_browser_context = context; | 60 event->restrict_to_browser_context = context; |
54 EventRouter::Get(context) | 61 EventRouter::Get(context) |
55 ->DispatchEventWithLazyListener(extension_id, event.Pass()); | 62 ->DispatchEventWithLazyListener(extension_id, event.Pass()); |
56 ExtensionPrefs::Get(context) | 63 ExtensionPrefs::Get(context) |
57 ->SetLastLaunchTime(extension_id, base::Time::Now()); | 64 ->SetLastLaunchTime(extension_id, base::Time::Now()); |
58 } | 65 } |
59 | 66 |
| 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 |
| 87 case extensions::SOURCE_SYSTEM_TRAY: |
| 88 return app_runtime::LAUNCH_SOURCE_SYSTEM_TRAY; |
| 89 case extensions::SOURCE_ABOUT_PAGE: |
| 90 return app_runtime::LAUNCH_SOURCE_ABOUT_PAGE; |
| 91 case extensions::SOURCE_KEYBOARD: |
| 92 return app_runtime::LAUNCH_SOURCE_KEYBOARD; |
| 93 |
| 94 default: |
| 95 return app_runtime::LAUNCH_SOURCE_NONE; |
| 96 } |
| 97 } |
| 98 |
60 } // namespace | 99 } // namespace |
61 | 100 |
62 // static | 101 // static |
63 void AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent( | 102 void AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent( |
64 content::BrowserContext* context, | 103 content::BrowserContext* context, |
65 scoped_ptr<base::DictionaryValue> embed_app_data, | 104 scoped_ptr<base::DictionaryValue> embed_app_data, |
66 const Extension* extension) { | 105 const Extension* extension) { |
67 DispatchOnEmbedRequestedEventImpl( | 106 DispatchOnEmbedRequestedEventImpl( |
68 extension->id(), embed_app_data.Pass(), context); | 107 extension->id(), embed_app_data.Pass(), context); |
69 } | 108 } |
70 | 109 |
71 // static | 110 // static |
72 void AppRuntimeEventRouter::DispatchOnLaunchedEvent( | 111 void AppRuntimeEventRouter::DispatchOnLaunchedEvent( |
73 BrowserContext* context, | 112 BrowserContext* context, |
74 const Extension* extension) { | 113 const Extension* extension, |
75 scoped_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue()); | 114 extensions::AppLaunchSource source) { |
76 DispatchOnLaunchedEventImpl(extension->id(), launch_data.Pass(), context); | 115 app_runtime::LaunchData launch_data; |
| 116 |
| 117 app_runtime::LaunchSource source_enum = getLaunchSourceEnum(source); |
| 118 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) { |
| 119 launch_data.source = source_enum; |
| 120 } |
| 121 DispatchOnLaunchedEventImpl( |
| 122 extension->id(), source_enum, 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 scoped_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue); |
101 launch_data->SetString("id", handler_id); | 147 launch_data->SetString("id", handler_id); |
| 148 |
| 149 app_runtime::LaunchSource source_enum = |
| 150 app_runtime::LAUNCH_SOURCE_FILE_HANDLER; |
| 151 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) { |
| 152 launch_data->SetString("source", app_runtime::ToString(source_enum)); |
| 153 } |
| 154 |
102 scoped_ptr<base::ListValue> items(new base::ListValue); | 155 scoped_ptr<base::ListValue> items(new base::ListValue); |
103 DCHECK(file_entries.size() == mime_types.size()); | 156 DCHECK(file_entries.size() == mime_types.size()); |
104 for (size_t i = 0; i < file_entries.size(); ++i) { | 157 for (size_t i = 0; i < file_entries.size(); ++i) { |
105 scoped_ptr<base::DictionaryValue> launch_item(new base::DictionaryValue); | 158 scoped_ptr<base::DictionaryValue> launch_item(new base::DictionaryValue); |
| 159 |
106 launch_item->SetString("fileSystemId", file_entries[i].filesystem_id); | 160 launch_item->SetString("fileSystemId", file_entries[i].filesystem_id); |
107 launch_item->SetString("baseName", file_entries[i].registered_name); | 161 launch_item->SetString("baseName", file_entries[i].registered_name); |
108 launch_item->SetString("mimeType", mime_types[i]); | 162 launch_item->SetString("mimeType", mime_types[i]); |
109 launch_item->SetString("entryId", file_entries[i].id); | 163 launch_item->SetString("entryId", file_entries[i].id); |
110 items->Append(launch_item.release()); | 164 items->Append(launch_item.release()); |
111 } | 165 } |
112 launch_data->Set("items", items.release()); | 166 launch_data->Set("items", items.release()); |
113 DispatchOnLaunchedEventImpl(extension->id(), launch_data.Pass(), context); | 167 DispatchOnLaunchedEventImpl( |
| 168 extension->id(), source_enum, launch_data.Pass(), context); |
114 } | 169 } |
115 | 170 |
116 // static | 171 // static |
117 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithUrl( | 172 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithUrl( |
118 BrowserContext* context, | 173 BrowserContext* context, |
119 const Extension* extension, | 174 const Extension* extension, |
120 const std::string& handler_id, | 175 const std::string& handler_id, |
121 const GURL& url, | 176 const GURL& url, |
122 const GURL& referrer_url) { | 177 const GURL& referrer_url) { |
123 app_runtime::LaunchData launch_data; | 178 app_runtime::LaunchData launch_data; |
| 179 app_runtime::LaunchSource source_enum = |
| 180 app_runtime::LAUNCH_SOURCE_URL_HANDLER; |
124 launch_data.id.reset(new std::string(handler_id)); | 181 launch_data.id.reset(new std::string(handler_id)); |
125 launch_data.url.reset(new std::string(url.spec())); | 182 launch_data.url.reset(new std::string(url.spec())); |
126 launch_data.referrer_url.reset(new std::string(referrer_url.spec())); | 183 launch_data.referrer_url.reset(new std::string(referrer_url.spec())); |
| 184 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) { |
| 185 launch_data.source = source_enum; |
| 186 } |
127 DispatchOnLaunchedEventImpl( | 187 DispatchOnLaunchedEventImpl( |
128 extension->id(), launch_data.ToValue().Pass(), context); | 188 extension->id(), source_enum, launch_data.ToValue().Pass(), context); |
129 } | 189 } |
130 | 190 |
131 } // namespace extensions | 191 } // namespace extensions |
OLD | NEW |