| 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/time/time.h" | 7 #include "base/time/time.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "extensions/browser/event_router.h" | 9 #include "extensions/browser/event_router.h" |
| 10 #include "extensions/browser/extension_prefs.h" | 10 #include "extensions/browser/extension_prefs.h" |
| 11 #include "extensions/browser/extension_system.h" | 11 #include "extensions/browser/extension_system.h" |
| 12 #include "extensions/browser/extensions_browser_client.h" | 12 #include "extensions/browser/extensions_browser_client.h" |
| 13 #include "extensions/browser/granted_file_entry.h" | 13 #include "extensions/browser/granted_file_entry.h" |
| 14 #include "extensions/common/api/app_runtime.h" | 14 #include "extensions/common/api/app_runtime.h" |
| 15 #include "extensions/common/constants.h" |
| 16 #include "extensions/common/feature_switch.h" |
| 15 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 16 | 18 |
| 17 using content::BrowserContext; | 19 using content::BrowserContext; |
| 18 | 20 |
| 19 namespace extensions { | 21 namespace extensions { |
| 20 | 22 |
| 21 namespace app_runtime = core_api::app_runtime; | 23 namespace app_runtime = core_api::app_runtime; |
| 22 | 24 |
| 23 namespace { | 25 namespace { |
| 24 | 26 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 50 args->Append(launch_data.release()); | 52 args->Append(launch_data.release()); |
| 51 scoped_ptr<Event> event( | 53 scoped_ptr<Event> event( |
| 52 new Event(app_runtime::OnLaunched::kEventName, args.Pass())); | 54 new Event(app_runtime::OnLaunched::kEventName, args.Pass())); |
| 53 event->restrict_to_browser_context = context; | 55 event->restrict_to_browser_context = context; |
| 54 EventRouter::Get(context) | 56 EventRouter::Get(context) |
| 55 ->DispatchEventWithLazyListener(extension_id, event.Pass()); | 57 ->DispatchEventWithLazyListener(extension_id, event.Pass()); |
| 56 ExtensionPrefs::Get(context) | 58 ExtensionPrefs::Get(context) |
| 57 ->SetLastLaunchTime(extension_id, base::Time::Now()); | 59 ->SetLastLaunchTime(extension_id, base::Time::Now()); |
| 58 } | 60 } |
| 59 | 61 |
| 62 app_runtime::LaunchSource getLaunchSourceEnum( |
| 63 extensions::AppLaunchSource source) { |
| 64 switch (source) { |
| 65 case extensions::SOURCE_APP_LAUNCHER: |
| 66 return app_runtime::LAUNCH_SOURCE_APP_LAUNCHER; |
| 67 case extensions::SOURCE_NEW_TAB_PAGE: |
| 68 return app_runtime::LAUNCH_SOURCE_NEW_TAB_PAGE; |
| 69 case extensions::SOURCE_RESTART: |
| 70 return app_runtime::LAUNCH_SOURCE_RESTART; |
| 71 case extensions::SOURCE_RELOAD: |
| 72 return app_runtime::LAUNCH_SOURCE_RELOAD; |
| 73 case extensions::SOURCE_FILE_HANDLER: |
| 74 return app_runtime::LAUNCH_SOURCE_FILE_HANDLER; |
| 75 case extensions::SOURCE_URL_HANDLER: |
| 76 return app_runtime::LAUNCH_SOURCE_URL_HANDLER; |
| 77 |
| 78 case extensions::SOURCE_GETHELP_SYSTEM_TRAY: |
| 79 return app_runtime::LAUNCH_SOURCE_GETHELP_SYSTEM_TRAY; |
| 80 case extensions::SOURCE_GETHELP_ABOUT_PAGE: |
| 81 return app_runtime::LAUNCH_SOURCE_GETHELP_ABOUT_PAGE; |
| 82 case extensions::SOURCE_GETHELP_KEYBOARD: |
| 83 return app_runtime::LAUNCH_SOURCE_GETHELP_KEYBOARD; |
| 84 |
| 85 default: |
| 86 return app_runtime::LAUNCH_SOURCE_NONE; |
| 87 } |
| 88 } |
| 89 |
| 60 } // namespace | 90 } // namespace |
| 61 | 91 |
| 62 // static | 92 // static |
| 63 void AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent( | 93 void AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent( |
| 64 content::BrowserContext* context, | 94 content::BrowserContext* context, |
| 65 scoped_ptr<base::DictionaryValue> embed_app_data, | 95 scoped_ptr<base::DictionaryValue> embed_app_data, |
| 66 const Extension* extension) { | 96 const Extension* extension) { |
| 67 DispatchOnEmbedRequestedEventImpl( | 97 DispatchOnEmbedRequestedEventImpl( |
| 68 extension->id(), embed_app_data.Pass(), context); | 98 extension->id(), embed_app_data.Pass(), context); |
| 69 } | 99 } |
| 70 | 100 |
| 71 // static | 101 // static |
| 72 void AppRuntimeEventRouter::DispatchOnLaunchedEvent( | 102 void AppRuntimeEventRouter::DispatchOnLaunchedEvent( |
| 73 BrowserContext* context, | 103 BrowserContext* context, |
| 74 const Extension* extension) { | 104 const Extension* extension) { |
| 75 scoped_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue()); | 105 scoped_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue()); |
| 76 DispatchOnLaunchedEventImpl(extension->id(), launch_data.Pass(), context); | 106 DispatchOnLaunchedEventImpl(extension->id(), launch_data.Pass(), context); |
| 77 } | 107 } |
| 78 | 108 |
| 79 // static | 109 // static |
| 110 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithSource( |
| 111 BrowserContext* context, |
| 112 const Extension* extension, |
| 113 extensions::AppLaunchSource source) { |
| 114 app_runtime::LaunchData launch_data; |
| 115 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) { |
| 116 launch_data.source = getLaunchSourceEnum(source); |
| 117 } |
| 118 DispatchOnLaunchedEventImpl( |
| 119 extension->id(), launch_data.ToValue().Pass(), context); |
| 120 } |
| 121 |
| 122 // static |
| 80 void AppRuntimeEventRouter::DispatchOnRestartedEvent( | 123 void AppRuntimeEventRouter::DispatchOnRestartedEvent( |
| 81 BrowserContext* context, | 124 BrowserContext* context, |
| 82 const Extension* extension) { | 125 const Extension* extension) { |
| 83 scoped_ptr<base::ListValue> arguments(new base::ListValue()); | 126 scoped_ptr<base::ListValue> arguments(new base::ListValue()); |
| 84 scoped_ptr<Event> event( | 127 scoped_ptr<Event> event( |
| 85 new Event(app_runtime::OnRestarted::kEventName, arguments.Pass())); | 128 new Event(app_runtime::OnRestarted::kEventName, arguments.Pass())); |
| 86 event->restrict_to_browser_context = context; | 129 event->restrict_to_browser_context = context; |
| 87 EventRouter::Get(context) | 130 EventRouter::Get(context) |
| 88 ->DispatchEventToExtension(extension->id(), event.Pass()); | 131 ->DispatchEventToExtension(extension->id(), event.Pass()); |
| 89 } | 132 } |
| 90 | 133 |
| 91 // static | 134 // static |
| 92 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithFileEntries( | 135 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithFileEntries( |
| 93 BrowserContext* context, | 136 BrowserContext* context, |
| 94 const Extension* extension, | 137 const Extension* extension, |
| 95 const std::string& handler_id, | 138 const std::string& handler_id, |
| 96 const std::vector<std::string>& mime_types, | 139 const std::vector<std::string>& mime_types, |
| 97 const std::vector<GrantedFileEntry>& file_entries) { | 140 const std::vector<GrantedFileEntry>& file_entries) { |
| 98 // TODO(sergeygs): Use the same way of creating an event (using the generated | 141 // TODO(sergeygs): Use the same way of creating an event (using the generated |
| 99 // boilerplate) as below in DispatchOnLaunchedEventWithUrl. | 142 // boilerplate) as below in DispatchOnLaunchedEventWithUrl. |
| 100 scoped_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue); | 143 base::DictionaryValue* launch_data = new base::DictionaryValue(); |
| 101 launch_data->SetString("id", handler_id); | 144 launch_data->SetString("id", handler_id); |
| 102 scoped_ptr<base::ListValue> items(new base::ListValue); | 145 launch_data->SetString( |
| 146 "source", app_runtime::ToString(app_runtime::LAUNCH_SOURCE_FILE_HANDLER)); |
| 147 |
| 148 base::ListValue* items = new base::ListValue(); |
| 103 DCHECK(file_entries.size() == mime_types.size()); | 149 DCHECK(file_entries.size() == mime_types.size()); |
| 104 for (size_t i = 0; i < file_entries.size(); ++i) { | 150 for (size_t i = 0; i < file_entries.size(); ++i) { |
| 105 scoped_ptr<base::DictionaryValue> launch_item(new base::DictionaryValue); | 151 base::DictionaryValue* launch_item = new base::DictionaryValue(); |
| 106 launch_item->SetString("fileSystemId", file_entries[i].filesystem_id); | 152 |
| 107 launch_item->SetString("baseName", file_entries[i].registered_name); | 153 base::DictionaryValue* raw_entry = new base::DictionaryValue(); |
| 108 launch_item->SetString("mimeType", mime_types[i]); | 154 raw_entry->SetString("fileSystemId", file_entries[i].filesystem_id); |
| 109 launch_item->SetString("entryId", file_entries[i].id); | 155 raw_entry->SetString("baseName", file_entries[i].registered_name); |
| 110 items->Append(launch_item.release()); | 156 raw_entry->SetString("entryId", file_entries[i].id); |
| 157 |
| 158 launch_item->Set("rawEntry", raw_entry); |
| 159 launch_item->SetString("type", mime_types[i]); |
| 160 |
| 161 items->Append(launch_item); |
| 111 } | 162 } |
| 112 launch_data->Set("items", items.release()); | 163 launch_data->Set("items", items); |
| 113 DispatchOnLaunchedEventImpl(extension->id(), launch_data.Pass(), context); | 164 DispatchOnLaunchedEventImpl( |
| 165 extension->id(), |
| 166 scoped_ptr<base::DictionaryValue>(launch_data).Pass(), |
| 167 context); |
| 114 } | 168 } |
| 115 | 169 |
| 116 // static | 170 // static |
| 117 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithUrl( | 171 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithUrl( |
| 118 BrowserContext* context, | 172 BrowserContext* context, |
| 119 const Extension* extension, | 173 const Extension* extension, |
| 120 const std::string& handler_id, | 174 const std::string& handler_id, |
| 121 const GURL& url, | 175 const GURL& url, |
| 122 const GURL& referrer_url) { | 176 const GURL& referrer_url) { |
| 123 app_runtime::LaunchData launch_data; | 177 app_runtime::LaunchData launch_data; |
| 124 launch_data.id.reset(new std::string(handler_id)); | 178 launch_data.id.reset(new std::string(handler_id)); |
| 125 launch_data.url.reset(new std::string(url.spec())); | 179 launch_data.url.reset(new std::string(url.spec())); |
| 126 launch_data.referrer_url.reset(new std::string(referrer_url.spec())); | 180 launch_data.referrer_url.reset(new std::string(referrer_url.spec())); |
| 181 if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) { |
| 182 launch_data.source = app_runtime::LAUNCH_SOURCE_URL_HANDLER; |
| 183 } |
| 127 DispatchOnLaunchedEventImpl( | 184 DispatchOnLaunchedEventImpl( |
| 128 extension->id(), launch_data.ToValue().Pass(), context); | 185 extension->id(), launch_data.ToValue().Pass(), context); |
| 129 } | 186 } |
| 130 | 187 |
| 131 } // namespace extensions | 188 } // namespace extensions |
| OLD | NEW |