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