Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(754)

Side by Side Diff: chrome/browser/extensions/api/app_runtime/app_runtime_api.cc

Issue 10828172: Allow platform apps to respond to Web Intents (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: about to submit Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 case webkit_glue::WebIntentData::FILESYSTEM: 102 case webkit_glue::WebIntentData::FILESYSTEM:
91 intent_data = new DictionaryValue(); 103 intent_data = new DictionaryValue();
92 intent_data->SetString("format", "filesystem"); 104 intent_data->SetString("format", "filesystem");
93 intent_data->SetString("fileSystemId", web_intent_data.filesystem_id); 105 intent_data->SetString("fileSystemId", web_intent_data.filesystem_id);
94 intent_data->SetString("baseName", web_intent_data.root_name); 106 intent_data->SetString("baseName", web_intent_data.root_name);
95 args->Append(intent_data); 107 args->Append(intent_data);
96 break; 108 break;
97 default: 109 default:
98 NOTREACHED(); 110 NOTREACHED();
99 break; 111 break;
100 } 112 }
113 DCHECK(args->GetSize() == 2); // intent_id must be our third argument.
114 WebIntentCallbacks* callbacks = WebIntentCallbacks::Get(profile);
115 int intent_id =
116 callbacks->RegisterCallback(extension, intents_dispatcher, source);
117 args->Append(base::Value::CreateIntegerValue(intent_id));
101 profile->GetExtensionEventRouter()->DispatchEventToExtension( 118 profile->GetExtensionEventRouter()->DispatchEventToExtension(
102 extension->id(), kOnLaunchedEvent, args.Pass(), NULL, GURL()); 119 extension->id(), kOnLaunchedEvent, args.Pass(), NULL, GURL());
103 } 120 }
104 121
122 bool AppRuntimePostIntentResponseFunction::RunImpl() {
123 DictionaryValue* details = NULL;
124 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details));
125
126 int intent_id = 0;
127 EXTENSION_FUNCTION_VALIDATE(details->GetInteger(kIntentIdKey, &intent_id));
128
129 WebIntentCallbacks* callbacks = WebIntentCallbacks::Get(profile());
130 content::WebIntentsDispatcher* intents_dispatcher =
131 callbacks->RetrieveCallback(GetExtension(), intent_id);
132 if (!intents_dispatcher) {
133 error_ = kCallbackNotFoundError;
134 return false;
135 }
136
137 webkit_glue::WebIntentReplyType reply_type =
138 webkit_glue::WEB_INTENT_REPLY_FAILURE;
139 bool success;
140 EXTENSION_FUNCTION_VALIDATE(details->GetBoolean(kIntentSuccessKey, &success));
141 if (success)
142 reply_type = webkit_glue::WEB_INTENT_REPLY_SUCCESS;
143
144 std::string data;
145 EXTENSION_FUNCTION_VALIDATE(details->GetString(kIntentDataKey, &data));
146
147 intents_dispatcher->SendReplyMessage(reply_type, UTF8ToUTF16(data));
148 return true;
149 }
150
105 } // namespace extensions 151 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/app_runtime/app_runtime_api.h ('k') | chrome/browser/extensions/platform_app_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698