Chromium Code Reviews| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/memory/ptr_util.h" | |
| 11 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 13 #include "base/values.h" | 14 #include "base/values.h" |
| 14 #include "extensions/browser/entry_info.h" | 15 #include "extensions/browser/entry_info.h" |
| 15 #include "extensions/browser/event_router.h" | 16 #include "extensions/browser/event_router.h" |
| 16 #include "extensions/browser/extension_prefs.h" | 17 #include "extensions/browser/extension_prefs.h" |
| 17 #include "extensions/browser/extensions_browser_client.h" | 18 #include "extensions/browser/extensions_browser_client.h" |
| 18 #include "extensions/browser/granted_file_entry.h" | 19 #include "extensions/browser/granted_file_entry.h" |
| 19 #include "extensions/common/api/app_runtime.h" | 20 #include "extensions/common/api/app_runtime.h" |
| 20 #include "extensions/common/constants.h" | 21 #include "extensions/common/constants.h" |
| 21 #include "extensions/common/feature_switch.h" | 22 #include "extensions/common/feature_switch.h" |
| 22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 23 | 24 |
| 24 using content::BrowserContext; | 25 using content::BrowserContext; |
| 25 | 26 |
| 26 namespace extensions { | 27 namespace extensions { |
| 27 | 28 |
| 28 namespace app_runtime = api::app_runtime; | 29 namespace app_runtime = api::app_runtime; |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 33 // Converts an ActionType instance to an app_runtime::ActionType instance. | |
| 34 app_runtime::ActionType MapActionType(ActionType action_type) { | |
| 35 switch (action_type) { | |
| 36 case ActionType::NEW_NOTE: | |
| 37 return app_runtime::ActionType::ACTION_TYPE_NEW_NOTE; | |
| 38 default: | |
|
Daniel Erat
2016/08/10 22:19:24
can you leave out the default block here to get a
jdufault
2016/08/10 22:33:35
Done.
| |
| 39 return app_runtime::ActionType::ACTION_TYPE_NONE; | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 // Converts an ActionData instance to an app_runtime::ActionData instance. | |
| 44 std::unique_ptr<app_runtime::ActionData> BuildActionData( | |
| 45 ActionData action_data) { | |
| 46 auto data = base::WrapUnique(new app_runtime::ActionData()); | |
| 47 data->action_type = MapActionType(action_data.action_type); | |
| 48 return data; | |
| 49 } | |
| 50 | |
| 32 void DispatchOnEmbedRequestedEventImpl( | 51 void DispatchOnEmbedRequestedEventImpl( |
| 33 const std::string& extension_id, | 52 const std::string& extension_id, |
| 34 std::unique_ptr<base::DictionaryValue> app_embedding_request_data, | 53 std::unique_ptr<base::DictionaryValue> app_embedding_request_data, |
| 35 content::BrowserContext* context) { | 54 content::BrowserContext* context) { |
| 36 std::unique_ptr<base::ListValue> args(new base::ListValue()); | 55 std::unique_ptr<base::ListValue> args(new base::ListValue()); |
| 37 args->Append(std::move(app_embedding_request_data)); | 56 args->Append(std::move(app_embedding_request_data)); |
| 38 std::unique_ptr<Event> event( | 57 std::unique_ptr<Event> event( |
| 39 new Event(events::APP_RUNTIME_ON_EMBED_REQUESTED, | 58 new Event(events::APP_RUNTIME_ON_EMBED_REQUESTED, |
| 40 app_runtime::OnEmbedRequested::kEventName, std::move(args))); | 59 app_runtime::OnEmbedRequested::kEventName, std::move(args))); |
| 41 event->restrict_to_browser_context = context; | 60 event->restrict_to_browser_context = context; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 std::unique_ptr<base::DictionaryValue> embed_app_data, | 149 std::unique_ptr<base::DictionaryValue> embed_app_data, |
| 131 const Extension* extension) { | 150 const Extension* extension) { |
| 132 DispatchOnEmbedRequestedEventImpl(extension->id(), std::move(embed_app_data), | 151 DispatchOnEmbedRequestedEventImpl(extension->id(), std::move(embed_app_data), |
| 133 context); | 152 context); |
| 134 } | 153 } |
| 135 | 154 |
| 136 // static | 155 // static |
| 137 void AppRuntimeEventRouter::DispatchOnLaunchedEvent( | 156 void AppRuntimeEventRouter::DispatchOnLaunchedEvent( |
| 138 BrowserContext* context, | 157 BrowserContext* context, |
| 139 const Extension* extension, | 158 const Extension* extension, |
| 140 extensions::AppLaunchSource source) { | 159 extensions::AppLaunchSource source, |
| 160 base::Optional<ActionData> action_data) { | |
| 141 app_runtime::LaunchData launch_data; | 161 app_runtime::LaunchData launch_data; |
| 142 | 162 |
| 143 app_runtime::LaunchSource source_enum = getLaunchSourceEnum(source); | 163 app_runtime::LaunchSource source_enum = getLaunchSourceEnum(source); |
| 144 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) { | 164 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) { |
| 145 launch_data.source = source_enum; | 165 launch_data.source = source_enum; |
| 146 } | 166 } |
| 167 | |
| 168 if (action_data) | |
| 169 launch_data.action_data = BuildActionData(action_data.value()); | |
| 170 | |
| 147 DispatchOnLaunchedEventImpl(extension->id(), source_enum, | 171 DispatchOnLaunchedEventImpl(extension->id(), source_enum, |
| 148 launch_data.ToValue(), context); | 172 launch_data.ToValue(), context); |
| 149 } | 173 } |
| 150 | 174 |
| 151 // static | 175 // static |
| 152 void AppRuntimeEventRouter::DispatchOnRestartedEvent( | 176 void AppRuntimeEventRouter::DispatchOnRestartedEvent( |
| 153 BrowserContext* context, | 177 BrowserContext* context, |
| 154 const Extension* extension) { | 178 const Extension* extension) { |
| 155 std::unique_ptr<base::ListValue> arguments(new base::ListValue()); | 179 std::unique_ptr<base::ListValue> arguments(new base::ListValue()); |
| 156 std::unique_ptr<Event> event(new Event(events::APP_RUNTIME_ON_RESTARTED, | 180 std::unique_ptr<Event> event(new Event(events::APP_RUNTIME_ON_RESTARTED, |
| 157 app_runtime::OnRestarted::kEventName, | 181 app_runtime::OnRestarted::kEventName, |
| 158 std::move(arguments))); | 182 std::move(arguments))); |
| 159 event->restrict_to_browser_context = context; | 183 event->restrict_to_browser_context = context; |
| 160 EventRouter::Get(context) | 184 EventRouter::Get(context) |
| 161 ->DispatchEventToExtension(extension->id(), std::move(event)); | 185 ->DispatchEventToExtension(extension->id(), std::move(event)); |
| 162 } | 186 } |
| 163 | 187 |
| 164 // static | 188 // static |
| 165 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithFileEntries( | 189 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithFileEntries( |
| 166 BrowserContext* context, | 190 BrowserContext* context, |
| 167 const Extension* extension, | 191 const Extension* extension, |
| 192 extensions::AppLaunchSource source, | |
| 168 const std::string& handler_id, | 193 const std::string& handler_id, |
| 169 const std::vector<EntryInfo>& entries, | 194 const std::vector<EntryInfo>& entries, |
| 170 const std::vector<GrantedFileEntry>& file_entries) { | 195 const std::vector<GrantedFileEntry>& file_entries, |
| 196 base::Optional<ActionData> action_data) { | |
| 197 app_runtime::LaunchSource source_enum = getLaunchSourceEnum(source); | |
| 198 | |
| 171 // TODO(sergeygs): Use the same way of creating an event (using the generated | 199 // TODO(sergeygs): Use the same way of creating an event (using the generated |
| 172 // boilerplate) as below in DispatchOnLaunchedEventWithUrl. | 200 // boilerplate) as below in DispatchOnLaunchedEventWithUrl. |
| 173 std::unique_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue); | 201 std::unique_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue); |
| 174 launch_data->SetString("id", handler_id); | 202 launch_data->SetString("id", handler_id); |
| 175 | 203 |
| 176 app_runtime::LaunchSource source_enum = | |
| 177 app_runtime::LAUNCH_SOURCE_FILE_HANDLER; | |
| 178 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) { | 204 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) { |
| 179 launch_data->SetString("source", app_runtime::ToString(source_enum)); | 205 launch_data->SetString("source", app_runtime::ToString(source_enum)); |
| 180 } | 206 } |
| 181 | 207 |
| 208 if (action_data) { | |
| 209 launch_data->Set("action_data", | |
| 210 BuildActionData(action_data.value())->ToValue()); | |
| 211 } | |
| 212 | |
| 182 std::unique_ptr<base::ListValue> items(new base::ListValue); | 213 std::unique_ptr<base::ListValue> items(new base::ListValue); |
| 183 DCHECK(file_entries.size() == entries.size()); | 214 DCHECK(file_entries.size() == entries.size()); |
| 184 for (size_t i = 0; i < file_entries.size(); ++i) { | 215 for (size_t i = 0; i < file_entries.size(); ++i) { |
| 185 std::unique_ptr<base::DictionaryValue> launch_item( | 216 std::unique_ptr<base::DictionaryValue> launch_item( |
| 186 new base::DictionaryValue); | 217 new base::DictionaryValue); |
| 187 | 218 |
| 188 launch_item->SetString("fileSystemId", file_entries[i].filesystem_id); | 219 launch_item->SetString("fileSystemId", file_entries[i].filesystem_id); |
| 189 launch_item->SetString("baseName", file_entries[i].registered_name); | 220 launch_item->SetString("baseName", file_entries[i].registered_name); |
| 190 launch_item->SetString("mimeType", entries[i].mime_type); | 221 launch_item->SetString("mimeType", entries[i].mime_type); |
| 191 launch_item->SetString("entryId", file_entries[i].id); | 222 launch_item->SetString("entryId", file_entries[i].id); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 211 launch_data.url.reset(new std::string(url.spec())); | 242 launch_data.url.reset(new std::string(url.spec())); |
| 212 launch_data.referrer_url.reset(new std::string(referrer_url.spec())); | 243 launch_data.referrer_url.reset(new std::string(referrer_url.spec())); |
| 213 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) { | 244 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) { |
| 214 launch_data.source = source_enum; | 245 launch_data.source = source_enum; |
| 215 } | 246 } |
| 216 DispatchOnLaunchedEventImpl(extension->id(), source_enum, | 247 DispatchOnLaunchedEventImpl(extension->id(), source_enum, |
| 217 launch_data.ToValue(), context); | 248 launch_data.ToValue(), context); |
| 218 } | 249 } |
| 219 | 250 |
| 220 } // namespace extensions | 251 } // namespace extensions |
| OLD | NEW |