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

Side by Side Diff: chrome/browser/extensions/api/app/app_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: all comments except listener/cleanup code Created 8 years, 4 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/app_api.h" 5 #include "chrome/browser/extensions/api/app/app_api.h"
6 6
7 #include "base/json/json_writer.h" 7 #include "base/json/json_writer.h"
8 #include "base/string_number_conversions.h" 8 #include "base/string_number_conversions.h"
9 #include "base/time.h" 9 #include "base/time.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/app_notification_manager.h" 12 #include "chrome/browser/extensions/app_notification_manager.h"
13 #include "chrome/browser/extensions/event_router.h" 13 #include "chrome/browser/extensions/event_router.h"
14 #include "chrome/browser/extensions/extension_service.h" 14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/web_intent_callbacks.h"
15 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/common/chrome_notification_types.h" 17 #include "chrome/common/chrome_notification_types.h"
17 #include "chrome/common/extensions/extension.h" 18 #include "chrome/common/extensions/extension.h"
18 #include "chrome/common/extensions/extension_constants.h" 19 #include "chrome/common/extensions/extension_constants.h"
19 #include "content/public/browser/notification_service.h" 20 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/web_intents_dispatcher.h"
20 #include "googleurl/src/gurl.h" 22 #include "googleurl/src/gurl.h"
21 #include "webkit/glue/web_intent_data.h" 23 #include "webkit/glue/web_intent_data.h"
22 24
23 namespace { 25 namespace {
24 26
25 const char kBodyTextKey[] = "bodyText"; 27 const char kBodyTextKey[] = "bodyText";
26 const char kExtensionIdKey[] = "extensionId"; 28 const char kExtensionIdKey[] = "extensionId";
27 const char kLinkTextKey[] = "linkText"; 29 const char kLinkTextKey[] = "linkText";
28 const char kLinkUrlKey[] = "linkUrl"; 30 const char kLinkUrlKey[] = "linkUrl";
29 const char kTitleKey[] = "title"; 31 const char kTitleKey[] = "title";
32 const char kIntentIdKey[] = "intentId";
33 const char kIntentSuccessKey[] = "success";
34 const char kIntentDataKey[] = "data";
30 35
31 const char kInvalidExtensionIdError[] = 36 const char kInvalidExtensionIdError[] =
32 "Invalid extension id"; 37 "Invalid extension id";
33 const char kMissingLinkTextError[] = 38 const char kMissingLinkTextError[] =
34 "You must specify linkText if you use linkUrl"; 39 "You must specify linkText if you use linkUrl";
40 const char kCallbackNotFoundError[] =
41 "WebIntent callback not found; perhaps already responded to";
35 const char kOnLaunchedEvent[] = "experimental.app.onLaunched"; 42 const char kOnLaunchedEvent[] = "experimental.app.onLaunched";
36
37 } // anonymous namespace 43 } // anonymous namespace
38 44
39 namespace extensions { 45 namespace extensions {
40 46
41 bool AppNotifyFunction::RunImpl() { 47 bool AppNotifyFunction::RunImpl() {
42 if (!include_incognito() && profile_->IsOffTheRecord()) { 48 if (!include_incognito() && profile_->IsOffTheRecord()) {
43 error_ = extension_misc::kAppNotificationsIncognitoError; 49 error_ = extension_misc::kAppNotificationsIncognitoError;
44 return false; 50 return false;
45 } 51 }
46 52
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 return false; 117 return false;
112 } 118 }
113 } 119 }
114 120
115 AppNotificationManager* manager = 121 AppNotificationManager* manager =
116 profile()->GetExtensionService()->app_notification_manager(); 122 profile()->GetExtensionService()->app_notification_manager();
117 manager->ClearAll(id); 123 manager->ClearAll(id);
118 return true; 124 return true;
119 } 125 }
120 126
127 bool PostIntentResponseFunction::RunImpl() {
128 DictionaryValue* details;
129 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &details));
130 EXTENSION_FUNCTION_VALIDATE(details != NULL);
131
132 std::string intent_id;
133 EXTENSION_FUNCTION_VALIDATE(details->GetString(kIntentIdKey, &intent_id));
134
135 WebIntentCallbacks* callbacks = WebIntentCallbacks::Get(profile());
136 content::WebIntentsDispatcher* intents_dispatcher =
137 callbacks->RetrieveCallback(intent_id);
138 if (!intents_dispatcher) {
139 error_ = kCallbackNotFoundError;
140 return false;
141 }
142
143 webkit_glue::WebIntentReplyType reply_type =
144 webkit_glue::WEB_INTENT_REPLY_FAILURE;
145 bool success;
146 EXTENSION_FUNCTION_VALIDATE(details->GetBoolean(kIntentSuccessKey, &success));
147 if (success)
148 reply_type = webkit_glue::WEB_INTENT_REPLY_SUCCESS;
149
150 std::string data;
151 EXTENSION_FUNCTION_VALIDATE(details->GetString(kIntentDataKey, &data));
152
153 LOG(INFO) << "PostIntentResponseFunction(), intent_id=" << intent_id
154 << ", success=" << (reply_type == webkit_glue::WEB_INTENT_REPLY_SUCCESS)
155 << ", data=``" << data << "``";
156
157 intents_dispatcher->SendReplyMessage(reply_type, UTF8ToUTF16(data));
158 return true;
159 }
160
121 // static. 161 // static.
122 void AppEventRouter::DispatchOnLaunchedEvent( 162 void AppEventRouter::DispatchOnLaunchedEvent(
123 Profile* profile, const Extension* extension) { 163 Profile* profile, const Extension* extension) {
124 profile->GetExtensionEventRouter()->DispatchEventToExtension( 164 profile->GetExtensionEventRouter()->DispatchEventToExtension(
125 extension->id(), kOnLaunchedEvent, "[]", NULL, GURL()); 165 extension->id(), kOnLaunchedEvent, "[]", NULL, GURL());
126 } 166 }
127 167
128 // static. 168 // static.
129 void AppEventRouter::DispatchOnLaunchedEventWithFileEntry( 169 void AppEventRouter::DispatchOnLaunchedEventWithFileEntry(
130 Profile* profile, const Extension* extension, const string16& action, 170 Profile* profile, const Extension* extension,
131 const std::string& file_system_id, const std::string& base_name) { 171 const string16& action, const std::string& file_system_id,
172 const std::string& base_name) {
132 ListValue args; 173 ListValue args;
133 DictionaryValue* launch_data = new DictionaryValue(); 174 DictionaryValue* launch_data = new DictionaryValue();
134 DictionaryValue* intent = new DictionaryValue(); 175 DictionaryValue* intent = new DictionaryValue();
135 intent->SetString("action", UTF16ToUTF8(action)); 176 intent->SetString("action", UTF16ToUTF8(action));
136 intent->SetString("type", "chrome-extension://fileentry"); 177 intent->SetString("type", "chrome-extension://fileentry");
137 launch_data->Set("intent", intent); 178 launch_data->Set("intent", intent);
138 args.Append(launch_data); 179 args.Append(launch_data);
139 DictionaryValue* intent_data = new DictionaryValue(); 180 DictionaryValue* intent_data = new DictionaryValue();
140 intent_data->SetString("format", "fileEntry"); 181 intent_data->SetString("format", "fileEntry");
141 intent_data->SetString("fileSystemId", file_system_id); 182 intent_data->SetString("fileSystemId", file_system_id);
142 intent_data->SetString("baseName", base_name); 183 intent_data->SetString("baseName", base_name);
143 args.Append(intent_data); 184 args.Append(intent_data);
144 std::string json_args; 185 std::string json_args;
145 base::JSONWriter::Write(&args, &json_args); 186 base::JSONWriter::Write(&args, &json_args);
146 profile->GetExtensionEventRouter()->DispatchEventToExtension( 187 profile->GetExtensionEventRouter()->DispatchEventToExtension(
147 extension->id(), kOnLaunchedEvent, json_args, NULL, GURL()); 188 extension->id(), kOnLaunchedEvent, json_args, NULL, GURL());
148 } 189 }
149 190
150 // static. 191 // static.
151 void AppEventRouter::DispatchOnLaunchedEventWithWebIntent( 192 void AppEventRouter::DispatchOnLaunchedEventWithWebIntent(
152 Profile* profile, const Extension* extension, 193 Profile* profile, const Extension* extension,
153 const webkit_glue::WebIntentData web_intent_data) { 194 content::WebIntentsDispatcher* intents_dispatcher) {
195 webkit_glue::WebIntentData web_intent_data = intents_dispatcher->GetIntent();
196 WebIntentCallbacks* callbacks = WebIntentCallbacks::Get(profile);
197 std::string intent_id = callbacks->RegisterCallback(intents_dispatcher);
198
154 ListValue args; 199 ListValue args;
200 args.Append(base::Value::CreateStringValue(intent_id));
155 DictionaryValue* launch_data = new DictionaryValue(); 201 DictionaryValue* launch_data = new DictionaryValue();
156 DictionaryValue* intent = new DictionaryValue(); 202 DictionaryValue* intent = new DictionaryValue();
157 intent->SetString("action", UTF16ToUTF8(web_intent_data.action)); 203 intent->SetString("action", UTF16ToUTF8(web_intent_data.action));
158 intent->SetString("type", UTF16ToUTF8(web_intent_data.type)); 204 intent->SetString("type", UTF16ToUTF8(web_intent_data.type));
159 launch_data->Set("intent", intent); 205 launch_data->Set("intent", intent);
160 args.Append(launch_data); 206 args.Append(launch_data);
161 DictionaryValue* intent_data; 207 DictionaryValue* intent_data;
162 switch (web_intent_data.data_type) { 208 switch (web_intent_data.data_type) {
163 case webkit_glue::WebIntentData::SERIALIZED: 209 case webkit_glue::WebIntentData::SERIALIZED:
164 intent_data = new DictionaryValue(); 210 intent_data = new DictionaryValue();
(...skipping 16 matching lines...) Expand all
181 NOTREACHED(); 227 NOTREACHED();
182 break; 228 break;
183 } 229 }
184 std::string json_args; 230 std::string json_args;
185 base::JSONWriter::Write(&args, &json_args); 231 base::JSONWriter::Write(&args, &json_args);
186 profile->GetExtensionEventRouter()->DispatchEventToExtension( 232 profile->GetExtensionEventRouter()->DispatchEventToExtension(
187 extension->id(), kOnLaunchedEvent, json_args, NULL, GURL()); 233 extension->id(), kOnLaunchedEvent, json_args, NULL, GURL());
188 } 234 }
189 235
190 } // namespace extensions 236 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698