| 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" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/event_router.h" | 12 #include "chrome/browser/extensions/event_router.h" |
| 13 #include "chrome/browser/extensions/web_intent_callbacks.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/extensions/extension.h" | 15 #include "chrome/common/extensions/extension.h" |
| 16 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/browser/web_intents_dispatcher.h" |
| 15 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
| 16 #include "webkit/glue/web_intent_data.h" | 19 #include "webkit/glue/web_intent_data.h" |
| 17 | 20 |
| 18 namespace { | 21 namespace { |
| 19 | 22 |
| 23 const char kIntentIdKey[] = "intentId"; |
| 24 const char kIntentSuccessKey[] = "success"; |
| 25 const char kIntentDataKey[] = "data"; |
| 20 const char kOnLaunchedEvent[] = "app.runtime.onLaunched"; | 26 const char kOnLaunchedEvent[] = "app.runtime.onLaunched"; |
| 21 | 27 |
| 28 const char kCallbackNotFoundError[] = |
| 29 "WebIntent callback not found; perhaps already responded to"; |
| 30 |
| 22 } // anonymous namespace | 31 } // anonymous namespace |
| 23 | 32 |
| 24 namespace extensions { | 33 namespace extensions { |
| 25 | 34 |
| 26 // static. | 35 // static. |
| 27 void AppEventRouter::DispatchOnLaunchedEvent( | 36 void AppEventRouter::DispatchOnLaunchedEvent( |
| 28 Profile* profile, const Extension* extension) { | 37 Profile* profile, const Extension* extension) { |
| 29 scoped_ptr<ListValue> arguments(new ListValue()); | 38 scoped_ptr<ListValue> arguments(new ListValue()); |
| 30 profile->GetExtensionEventRouter()->DispatchEventToExtension( | 39 profile->GetExtensionEventRouter()->DispatchEventToExtension( |
| 31 extension->id(), kOnLaunchedEvent, arguments.Pass(), NULL, GURL()); | 40 extension->id(), kOnLaunchedEvent, arguments.Pass(), NULL, GURL()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 49 // NOTE: This second argument is dropped before being dispatched to the client | 58 // NOTE: This second argument is dropped before being dispatched to the client |
| 50 // code. | 59 // code. |
| 51 args->Append(intent_data); | 60 args->Append(intent_data); |
| 52 profile->GetExtensionEventRouter()->DispatchEventToExtension( | 61 profile->GetExtensionEventRouter()->DispatchEventToExtension( |
| 53 extension->id(), kOnLaunchedEvent, args.Pass(), NULL, GURL()); | 62 extension->id(), kOnLaunchedEvent, args.Pass(), NULL, GURL()); |
| 54 } | 63 } |
| 55 | 64 |
| 56 // static. | 65 // static. |
| 57 void AppEventRouter::DispatchOnLaunchedEventWithWebIntent( | 66 void AppEventRouter::DispatchOnLaunchedEventWithWebIntent( |
| 58 Profile* profile, const Extension* extension, | 67 Profile* profile, const Extension* extension, |
| 59 const webkit_glue::WebIntentData web_intent_data) { | 68 content::WebIntentsDispatcher* intents_dispatcher, |
| 69 content::WebContents* source) { |
| 70 webkit_glue::WebIntentData web_intent_data = intents_dispatcher->GetIntent(); |
| 60 scoped_ptr<ListValue> args(new ListValue()); | 71 scoped_ptr<ListValue> args(new ListValue()); |
| 61 DictionaryValue* launch_data = new DictionaryValue(); | 72 DictionaryValue* launch_data = new DictionaryValue(); |
| 62 DictionaryValue* intent = new DictionaryValue(); | 73 DictionaryValue* intent = new DictionaryValue(); |
| 63 intent->SetString("action", UTF16ToUTF8(web_intent_data.action)); | 74 intent->SetString("action", UTF16ToUTF8(web_intent_data.action)); |
| 64 intent->SetString("type", UTF16ToUTF8(web_intent_data.type)); | 75 intent->SetString("type", UTF16ToUTF8(web_intent_data.type)); |
| 65 launch_data->Set("intent", intent); | 76 launch_data->Set("intent", intent); |
| 66 args->Append(launch_data); | 77 args->Append(launch_data); |
| 67 DictionaryValue* intent_data; | 78 DictionaryValue* intent_data; |
| 68 switch (web_intent_data.data_type) { | 79 switch (web_intent_data.data_type) { |
| 69 case webkit_glue::WebIntentData::SERIALIZED: | 80 case webkit_glue::WebIntentData::SERIALIZED: |
| 70 intent_data = new DictionaryValue(); | 81 intent_data = new DictionaryValue(); |
| 71 intent_data->SetString("format", "serialized"); | 82 intent_data->SetString("format", "serialized"); |
| 72 intent_data->SetString("data", UTF16ToUTF8(web_intent_data.data)); | 83 intent_data->SetString("data", UTF16ToUTF8(web_intent_data.data)); |
| 73 // NOTE: This second argument is dropped before being dispatched to the | 84 // NOTE: This second argument is dropped before being dispatched to the |
| 74 // client code. | 85 // client code. |
| 75 args->Append(intent_data); | 86 args->Append(intent_data); |
| 76 break; | 87 break; |
| 77 case webkit_glue::WebIntentData::UNSERIALIZED: | 88 case webkit_glue::WebIntentData::UNSERIALIZED: |
| 89 args->Append(Value::CreateNullValue()); |
| 78 intent->SetString("data", UTF16ToUTF8(web_intent_data.unserialized_data)); | 90 intent->SetString("data", UTF16ToUTF8(web_intent_data.unserialized_data)); |
| 79 break; | 91 break; |
| 80 case webkit_glue::WebIntentData::BLOB: | 92 case webkit_glue::WebIntentData::BLOB: |
| 81 intent_data = new DictionaryValue(); | 93 intent_data = new DictionaryValue(); |
| 82 intent_data->SetString("format", "blob"); | 94 intent_data->SetString("format", "blob"); |
| 83 intent_data->SetString("blobFileName", web_intent_data.blob_file.value()); | 95 intent_data->SetString("blobFileName", web_intent_data.blob_file.value()); |
| 84 intent_data->SetString("blobLength", | 96 intent_data->SetString("blobLength", |
| 85 base::Int64ToString(web_intent_data.blob_length)); | 97 base::Int64ToString(web_intent_data.blob_length)); |
| 86 // NOTE: This second argument is dropped before being dispatched to the | 98 // NOTE: This second argument is dropped before being dispatched to the |
| 87 // client code. | 99 // client code. |
| 88 args->Append(intent_data); | 100 args->Append(intent_data); |
| 89 break; | 101 break; |
| 90 default: | 102 default: |
| 91 NOTREACHED(); | 103 NOTREACHED(); |
| 92 break; | 104 break; |
| 93 } | 105 } |
| 106 DCHECK(args->GetSize() == 2); // intent_id must be our third argument. |
| 107 WebIntentCallbacks* callbacks = WebIntentCallbacks::Get(profile); |
| 108 int intent_id = |
| 109 callbacks->RegisterCallback(extension, intents_dispatcher, source); |
| 110 args->Append(base::Value::CreateIntegerValue(intent_id)); |
| 94 profile->GetExtensionEventRouter()->DispatchEventToExtension( | 111 profile->GetExtensionEventRouter()->DispatchEventToExtension( |
| 95 extension->id(), kOnLaunchedEvent, args.Pass(), NULL, GURL()); | 112 extension->id(), kOnLaunchedEvent, args.Pass(), NULL, GURL()); |
| 96 } | 113 } |
| 97 | 114 |
| 115 bool AppRuntimePostIntentResponseFunction::RunImpl() { |
| 116 DictionaryValue* details = NULL; |
| 117 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details)); |
| 118 |
| 119 int intent_id = 0; |
| 120 EXTENSION_FUNCTION_VALIDATE(details->GetInteger(kIntentIdKey, &intent_id)); |
| 121 |
| 122 WebIntentCallbacks* callbacks = WebIntentCallbacks::Get(profile()); |
| 123 content::WebIntentsDispatcher* intents_dispatcher = |
| 124 callbacks->RetrieveCallback(GetExtension(), intent_id); |
| 125 if (!intents_dispatcher) { |
| 126 error_ = kCallbackNotFoundError; |
| 127 return false; |
| 128 } |
| 129 |
| 130 webkit_glue::WebIntentReplyType reply_type = |
| 131 webkit_glue::WEB_INTENT_REPLY_FAILURE; |
| 132 bool success; |
| 133 EXTENSION_FUNCTION_VALIDATE(details->GetBoolean(kIntentSuccessKey, &success)); |
| 134 if (success) |
| 135 reply_type = webkit_glue::WEB_INTENT_REPLY_SUCCESS; |
| 136 |
| 137 std::string data; |
| 138 EXTENSION_FUNCTION_VALIDATE(details->GetString(kIntentDataKey, &data)); |
| 139 |
| 140 intents_dispatcher->SendReplyMessage(reply_type, UTF8ToUTF16(data)); |
| 141 return true; |
| 142 } |
| 143 |
| 98 } // namespace extensions | 144 } // namespace extensions |
| OLD | NEW |