| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/app_runtime/app_runtime_api.h" | 5 #include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/string16.h" | 8 #include "base/string16.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 const char kIntentIdKey[] = "intentId"; | 24 const char kIntentIdKey[] = "intentId"; |
| 25 const char kIntentSuccessKey[] = "success"; | 25 const char kIntentSuccessKey[] = "success"; |
| 26 const char kIntentDataKey[] = "data"; | 26 const char kIntentDataKey[] = "data"; |
| 27 const char kOnLaunchedEvent[] = "app.runtime.onLaunched"; | 27 const char kOnLaunchedEvent[] = "app.runtime.onLaunched"; |
| 28 const char kOnRestartedEvent[] = "app.runtime.onRestarted"; | 28 const char kOnRestartedEvent[] = "app.runtime.onRestarted"; |
| 29 | 29 |
| 30 const char kCallbackNotFoundError[] = | 30 const char kCallbackNotFoundError[] = |
| 31 "WebIntent callback not found; perhaps already responded to"; | 31 "WebIntent callback not found; perhaps already responded to"; |
| 32 | 32 |
| 33 void DispatchOnLaunchedEventImpl(const std::string& extension_id, |
| 34 scoped_ptr<base::ListValue> args, |
| 35 Profile* profile) { |
| 36 extensions::ExtensionSystem* system = |
| 37 extensions::ExtensionSystem::Get(profile); |
| 38 // Special case: normally, extensions add their own lazy event listeners. |
| 39 // However, since the extension might have just been enabled, it hasn't had a |
| 40 // chance to register for events. So we register on its behalf. If the |
| 41 // extension does not actually have a listener, the event will just be |
| 42 // ignored (but an app that doesn't listen for the onLaunched event doesn't |
| 43 // make sense anyway). |
| 44 system->event_router()->AddLazyEventListener(kOnLaunchedEvent, extension_id); |
| 45 system->event_router()->DispatchEventToExtension( |
| 46 extension_id, kOnLaunchedEvent, args.Pass(), profile, GURL()); |
| 47 system->event_router()->RemoveLazyEventListener(kOnLaunchedEvent, |
| 48 extension_id); |
| 49 } |
| 50 |
| 33 } // anonymous namespace | 51 } // anonymous namespace |
| 34 | 52 |
| 35 namespace extensions { | 53 namespace extensions { |
| 36 | 54 |
| 37 // static. | 55 // static. |
| 38 void AppEventRouter::DispatchOnLaunchedEvent( | 56 void AppEventRouter::DispatchOnLaunchedEvent( |
| 39 Profile* profile, const Extension* extension) { | 57 Profile* profile, const Extension* extension) { |
| 40 scoped_ptr<ListValue> arguments(new ListValue()); | 58 scoped_ptr<ListValue> arguments(new ListValue()); |
| 41 extensions::ExtensionSystem::Get(profile)->event_router()-> | 59 DispatchOnLaunchedEventImpl(extension->id(), arguments.Pass(), profile); |
| 42 DispatchEventToExtension(extension->id(), kOnLaunchedEvent, | |
| 43 arguments.Pass(), profile, GURL()); | |
| 44 } | 60 } |
| 45 | 61 |
| 46 // static. | 62 // static. |
| 47 void AppEventRouter::DispatchOnRestartedEvent( | 63 void AppEventRouter::DispatchOnRestartedEvent( |
| 48 Profile* profile, const Extension* extension) { | 64 Profile* profile, const Extension* extension) { |
| 49 scoped_ptr<ListValue> arguments(new ListValue()); | 65 scoped_ptr<ListValue> arguments(new ListValue()); |
| 50 extensions::ExtensionSystem::Get(profile)->event_router()-> | 66 extensions::ExtensionSystem::Get(profile)->event_router()-> |
| 51 DispatchEventToExtension(extension->id(), kOnRestartedEvent, | 67 DispatchEventToExtension(extension->id(), kOnRestartedEvent, |
| 52 arguments.Pass(), profile, GURL()); | 68 arguments.Pass(), profile, GURL()); |
| 53 } | 69 } |
| 54 | 70 |
| 55 // static. | 71 // static. |
| 56 void AppEventRouter::DispatchOnLaunchedEventWithFileEntry( | 72 void AppEventRouter::DispatchOnLaunchedEventWithFileEntry( |
| 57 Profile* profile, const Extension* extension, const string16& action, | 73 Profile* profile, const Extension* extension, const string16& action, |
| 58 const std::string& handler_id, const std::string& mime_type, | 74 const std::string& handler_id, const std::string& mime_type, |
| 59 const std::string& file_system_id, const std::string& base_name) { | 75 const std::string& file_system_id, const std::string& base_name) { |
| 60 scoped_ptr<ListValue> args(new ListValue()); | 76 scoped_ptr<ListValue> args(new ListValue()); |
| 61 DictionaryValue* launch_data = new DictionaryValue(); | 77 DictionaryValue* launch_data = new DictionaryValue(); |
| 62 launch_data->SetString("id", handler_id); | 78 launch_data->SetString("id", handler_id); |
| 63 DictionaryValue* launch_item = new DictionaryValue; | 79 DictionaryValue* launch_item = new DictionaryValue; |
| 64 launch_item->SetString("fileSystemId", file_system_id); | 80 launch_item->SetString("fileSystemId", file_system_id); |
| 65 launch_item->SetString("baseName", base_name); | 81 launch_item->SetString("baseName", base_name); |
| 66 launch_item->SetString("mimeType", mime_type); | 82 launch_item->SetString("mimeType", mime_type); |
| 67 ListValue* items = new ListValue; | 83 ListValue* items = new ListValue; |
| 68 items->Append(launch_item); | 84 items->Append(launch_item); |
| 69 launch_data->Set("items", items); | 85 launch_data->Set("items", items); |
| 70 args->Append(launch_data); | 86 args->Append(launch_data); |
| 71 extensions::ExtensionSystem::Get(profile)->event_router()-> | 87 DispatchOnLaunchedEventImpl(extension->id(), args.Pass(), profile); |
| 72 DispatchEventToExtension(extension->id(), kOnLaunchedEvent, args.Pass(), | |
| 73 profile, GURL()); | |
| 74 } | 88 } |
| 75 | 89 |
| 76 // static. | 90 // static. |
| 77 void AppEventRouter::DispatchOnLaunchedEventWithWebIntent( | 91 void AppEventRouter::DispatchOnLaunchedEventWithWebIntent( |
| 78 Profile* profile, const Extension* extension, | 92 Profile* profile, const Extension* extension, |
| 79 content::WebIntentsDispatcher* intents_dispatcher, | 93 content::WebIntentsDispatcher* intents_dispatcher, |
| 80 content::WebContents* source) { | 94 content::WebContents* source) { |
| 81 webkit_glue::WebIntentData web_intent_data = intents_dispatcher->GetIntent(); | 95 webkit_glue::WebIntentData web_intent_data = intents_dispatcher->GetIntent(); |
| 82 scoped_ptr<ListValue> args(new ListValue()); | 96 scoped_ptr<ListValue> args(new ListValue()); |
| 83 DictionaryValue* launch_data = new DictionaryValue(); | 97 DictionaryValue* launch_data = new DictionaryValue(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 break; | 133 break; |
| 120 default: | 134 default: |
| 121 NOTREACHED(); | 135 NOTREACHED(); |
| 122 break; | 136 break; |
| 123 } | 137 } |
| 124 DCHECK(args->GetSize() == 2); // intent_id must be our third argument. | 138 DCHECK(args->GetSize() == 2); // intent_id must be our third argument. |
| 125 WebIntentCallbacks* callbacks = WebIntentCallbacks::Get(profile); | 139 WebIntentCallbacks* callbacks = WebIntentCallbacks::Get(profile); |
| 126 int intent_id = | 140 int intent_id = |
| 127 callbacks->RegisterCallback(extension, intents_dispatcher, source); | 141 callbacks->RegisterCallback(extension, intents_dispatcher, source); |
| 128 args->Append(base::Value::CreateIntegerValue(intent_id)); | 142 args->Append(base::Value::CreateIntegerValue(intent_id)); |
| 129 extensions::ExtensionSystem::Get(profile)->event_router()-> | 143 DispatchOnLaunchedEventImpl(extension->id(), args.Pass(), profile); |
| 130 DispatchEventToExtension(extension->id(), kOnLaunchedEvent, args.Pass(), | |
| 131 profile, GURL()); | |
| 132 } | 144 } |
| 133 | 145 |
| 134 bool AppRuntimePostIntentResponseFunction::RunImpl() { | 146 bool AppRuntimePostIntentResponseFunction::RunImpl() { |
| 135 DictionaryValue* details = NULL; | 147 DictionaryValue* details = NULL; |
| 136 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); | 148 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); |
| 137 | 149 |
| 138 int intent_id = 0; | 150 int intent_id = 0; |
| 139 EXTENSION_FUNCTION_VALIDATE(details->GetInteger(kIntentIdKey, &intent_id)); | 151 EXTENSION_FUNCTION_VALIDATE(details->GetInteger(kIntentIdKey, &intent_id)); |
| 140 | 152 |
| 141 WebIntentCallbacks* callbacks = WebIntentCallbacks::Get(profile()); | 153 WebIntentCallbacks* callbacks = WebIntentCallbacks::Get(profile()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 154 reply_type = webkit_glue::WEB_INTENT_REPLY_SUCCESS; | 166 reply_type = webkit_glue::WEB_INTENT_REPLY_SUCCESS; |
| 155 | 167 |
| 156 std::string data; | 168 std::string data; |
| 157 EXTENSION_FUNCTION_VALIDATE(details->GetString(kIntentDataKey, &data)); | 169 EXTENSION_FUNCTION_VALIDATE(details->GetString(kIntentDataKey, &data)); |
| 158 | 170 |
| 159 intents_dispatcher->SendReplyMessage(reply_type, UTF8ToUTF16(data)); | 171 intents_dispatcher->SendReplyMessage(reply_type, UTF8ToUTF16(data)); |
| 160 return true; | 172 return true; |
| 161 } | 173 } |
| 162 | 174 |
| 163 } // namespace extensions | 175 } // namespace extensions |
| OLD | NEW |