Chromium Code Reviews| 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/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 Loading... | |
| 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 if (details->HasKey(kIntentIdKey)) | |
|
koz (OOO until 15th September)
2012/08/08 04:47:30
Is this not always present?
thorogood
2012/08/08 07:15:49
I've removed the HasKey checks, since yes, EXTENSI
| |
| 134 EXTENSION_FUNCTION_VALIDATE(details->GetString(kIntentIdKey, &intent_id)); | |
| 135 | |
| 136 WebIntentCallbacks* callbacks = WebIntentCallbacks::Get(profile()); | |
| 137 content::WebIntentsDispatcher* intents_dispatcher = | |
| 138 callbacks->RetrieveCallback(intent_id); | |
| 139 if (!intents_dispatcher) { | |
| 140 error_ = kCallbackNotFoundError; | |
| 141 return false; | |
| 142 } | |
| 143 | |
| 144 webkit_glue::WebIntentReplyType reply_type = | |
| 145 webkit_glue::WEB_INTENT_REPLY_FAILURE; | |
| 146 if (details->HasKey(kIntentSuccessKey)) { | |
| 147 bool success; | |
| 148 EXTENSION_FUNCTION_VALIDATE( | |
| 149 details->GetBoolean(kIntentSuccessKey, &success)); | |
| 150 if (success) | |
| 151 reply_type = webkit_glue::WEB_INTENT_REPLY_SUCCESS; | |
| 152 } | |
| 153 | |
| 154 Value* result; | |
|
koz (OOO until 15th September)
2012/08/08 04:47:30
Initialize to NULL.
thorogood
2012/08/08 07:15:49
No longer relevant, as now I'm using a std::string
| |
| 155 if (details->HasKey(kIntentDataKey)) | |
| 156 EXTENSION_FUNCTION_VALIDATE(details->Get(kIntentDataKey, &result)); | |
| 157 else | |
| 158 result = Value::CreateNullValue(); // TODO: need to free this | |
| 159 std::string json_args; | |
| 160 base::JSONWriter::Write(result, &json_args); | |
| 161 | |
| 162 LOG(INFO) << "PostIntentResponseFunction(), intent_id=" << intent_id | |
| 163 << ", success=" << (reply_type == webkit_glue::WEB_INTENT_REPLY_SUCCESS) | |
| 164 << ", data=``" << json_args << "``"; | |
| 165 | |
| 166 // NOTE: The original intent callback only seems to receive "NULL". | |
| 167 intents_dispatcher->SendReplyMessage(reply_type, UTF8ToUTF16(json_args)); | |
| 168 return true; | |
| 169 } | |
| 170 | |
| 121 // static. | 171 // static. |
| 122 void AppEventRouter::DispatchOnLaunchedEvent( | 172 void AppEventRouter::DispatchOnLaunchedEvent( |
| 123 Profile* profile, const Extension* extension) { | 173 Profile* profile, const Extension* extension) { |
| 124 profile->GetExtensionEventRouter()->DispatchEventToExtension( | 174 profile->GetExtensionEventRouter()->DispatchEventToExtension( |
| 125 extension->id(), kOnLaunchedEvent, "[]", NULL, GURL()); | 175 extension->id(), kOnLaunchedEvent, "[]", NULL, GURL()); |
| 126 } | 176 } |
| 127 | 177 |
| 128 // static. | 178 // static. |
| 129 void AppEventRouter::DispatchOnLaunchedEventWithFileEntry( | 179 void AppEventRouter::DispatchOnLaunchedEventWithFileEntry( |
| 130 Profile* profile, const Extension* extension, const string16& action, | 180 Profile* profile, const Extension* extension, |
| 131 const std::string& file_system_id, const std::string& base_name) { | 181 const string16& action, const std::string& file_system_id, |
| 182 const std::string& base_name) { | |
| 132 ListValue args; | 183 ListValue args; |
| 133 DictionaryValue* launch_data = new DictionaryValue(); | 184 DictionaryValue* launch_data = new DictionaryValue(); |
| 134 DictionaryValue* intent = new DictionaryValue(); | 185 DictionaryValue* intent = new DictionaryValue(); |
| 135 intent->SetString("action", UTF16ToUTF8(action)); | 186 intent->SetString("action", UTF16ToUTF8(action)); |
| 136 intent->SetString("type", "chrome-extension://fileentry"); | 187 intent->SetString("type", "chrome-extension://fileentry"); |
| 137 launch_data->Set("intent", intent); | 188 launch_data->Set("intent", intent); |
| 138 args.Append(launch_data); | 189 args.Append(launch_data); |
| 139 DictionaryValue* intent_data = new DictionaryValue(); | 190 DictionaryValue* intent_data = new DictionaryValue(); |
| 140 intent_data->SetString("format", "fileEntry"); | 191 intent_data->SetString("format", "fileEntry"); |
| 141 intent_data->SetString("fileSystemId", file_system_id); | 192 intent_data->SetString("fileSystemId", file_system_id); |
| 142 intent_data->SetString("baseName", base_name); | 193 intent_data->SetString("baseName", base_name); |
| 143 args.Append(intent_data); | 194 args.Append(intent_data); |
| 144 std::string json_args; | 195 std::string json_args; |
| 145 base::JSONWriter::Write(&args, &json_args); | 196 base::JSONWriter::Write(&args, &json_args); |
| 146 profile->GetExtensionEventRouter()->DispatchEventToExtension( | 197 profile->GetExtensionEventRouter()->DispatchEventToExtension( |
| 147 extension->id(), kOnLaunchedEvent, json_args, NULL, GURL()); | 198 extension->id(), kOnLaunchedEvent, json_args, NULL, GURL()); |
| 148 } | 199 } |
| 149 | 200 |
| 150 // static. | 201 // static. |
| 151 void AppEventRouter::DispatchOnLaunchedEventWithWebIntent( | 202 void AppEventRouter::DispatchOnLaunchedEventWithWebIntent( |
| 152 Profile* profile, const Extension* extension, | 203 Profile* profile, const Extension* extension, |
| 153 const webkit_glue::WebIntentData web_intent_data) { | 204 content::WebIntentsDispatcher* intents_dispatcher) { |
| 205 webkit_glue::WebIntentData web_intent_data = intents_dispatcher->GetIntent(); | |
| 206 WebIntentCallbacks* callbacks = WebIntentCallbacks::Get(profile); | |
| 207 std::string intent_id = callbacks->RegisterCallback(intents_dispatcher); | |
| 208 | |
| 154 ListValue args; | 209 ListValue args; |
| 210 args.Append(base::Value::CreateStringValue(intent_id)); | |
| 155 DictionaryValue* launch_data = new DictionaryValue(); | 211 DictionaryValue* launch_data = new DictionaryValue(); |
| 156 DictionaryValue* intent = new DictionaryValue(); | 212 DictionaryValue* intent = new DictionaryValue(); |
| 157 intent->SetString("action", UTF16ToUTF8(web_intent_data.action)); | 213 intent->SetString("action", UTF16ToUTF8(web_intent_data.action)); |
| 158 intent->SetString("type", UTF16ToUTF8(web_intent_data.type)); | 214 intent->SetString("type", UTF16ToUTF8(web_intent_data.type)); |
| 159 launch_data->Set("intent", intent); | 215 launch_data->Set("intent", intent); |
| 160 args.Append(launch_data); | 216 args.Append(launch_data); |
| 161 DictionaryValue* intent_data; | 217 DictionaryValue* intent_data; |
| 162 switch (web_intent_data.data_type) { | 218 switch (web_intent_data.data_type) { |
| 163 case webkit_glue::WebIntentData::SERIALIZED: | 219 case webkit_glue::WebIntentData::SERIALIZED: |
| 164 intent_data = new DictionaryValue(); | 220 intent_data = new DictionaryValue(); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 181 NOTREACHED(); | 237 NOTREACHED(); |
| 182 break; | 238 break; |
| 183 } | 239 } |
| 184 std::string json_args; | 240 std::string json_args; |
| 185 base::JSONWriter::Write(&args, &json_args); | 241 base::JSONWriter::Write(&args, &json_args); |
| 186 profile->GetExtensionEventRouter()->DispatchEventToExtension( | 242 profile->GetExtensionEventRouter()->DispatchEventToExtension( |
| 187 extension->id(), kOnLaunchedEvent, json_args, NULL, GURL()); | 243 extension->id(), kOnLaunchedEvent, json_args, NULL, GURL()); |
| 188 } | 244 } |
| 189 | 245 |
| 190 } // namespace extensions | 246 } // namespace extensions |
| OLD | NEW |