| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/gcm/gcm_api.h" | 5 #include "chrome/browser/extensions/api/gcm/gcm_api.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/services/gcm/gcm_driver.h" |
| 16 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 17 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
| 17 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 18 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| 18 #include "chrome/common/extensions/api/gcm.h" | 19 #include "chrome/common/extensions/api/gcm.h" |
| 20 #include "extensions/browser/event_router.h" |
| 19 #include "extensions/common/extension.h" | 21 #include "extensions/common/extension.h" |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 23 const size_t kMaximumMessageSize = 4096; // in bytes. | 25 const size_t kMaximumMessageSize = 4096; // in bytes. |
| 24 const char kCollapseKey[] = "collapse_key"; | 26 const char kCollapseKey[] = "collapse_key"; |
| 25 const char kGoogDotRestrictedPrefix[] = "goog."; | 27 const char kGoogDotRestrictedPrefix[] = "goog."; |
| 26 const char kGoogleRestrictedPrefix[] = "google"; | 28 const char kGoogleRestrictedPrefix[] = "google"; |
| 27 | 29 |
| 28 // Error messages. | 30 // Error messages. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 Profile* profile = Profile::FromBrowserContext(browser_context()); | 92 Profile* profile = Profile::FromBrowserContext(browser_context()); |
| 91 | 93 |
| 92 // GCM is not supported in incognito mode. | 94 // GCM is not supported in incognito mode. |
| 93 if (profile->IsOffTheRecord()) | 95 if (profile->IsOffTheRecord()) |
| 94 return false; | 96 return false; |
| 95 | 97 |
| 96 return gcm::GCMProfileService::GetGCMEnabledState(profile) != | 98 return gcm::GCMProfileService::GetGCMEnabledState(profile) != |
| 97 gcm::GCMProfileService::ALWAYS_DISABLED; | 99 gcm::GCMProfileService::ALWAYS_DISABLED; |
| 98 } | 100 } |
| 99 | 101 |
| 100 gcm::GCMProfileService* GcmApiFunction::GCMProfileService() const { | 102 gcm::GCMDriver* GcmApiFunction::GetGCMDriver() const { |
| 101 return gcm::GCMProfileServiceFactory::GetForProfile( | 103 return gcm::GCMProfileServiceFactory::GetForProfile( |
| 102 Profile::FromBrowserContext(browser_context())); | 104 Profile::FromBrowserContext(browser_context()))->driver(); |
| 103 } | 105 } |
| 104 | 106 |
| 105 GcmRegisterFunction::GcmRegisterFunction() {} | 107 GcmRegisterFunction::GcmRegisterFunction() {} |
| 106 | 108 |
| 107 GcmRegisterFunction::~GcmRegisterFunction() {} | 109 GcmRegisterFunction::~GcmRegisterFunction() {} |
| 108 | 110 |
| 109 bool GcmRegisterFunction::DoWork() { | 111 bool GcmRegisterFunction::DoWork() { |
| 110 scoped_ptr<api::gcm::Register::Params> params( | 112 scoped_ptr<api::gcm::Register::Params> params( |
| 111 api::gcm::Register::Params::Create(*args_)); | 113 api::gcm::Register::Params::Create(*args_)); |
| 112 EXTENSION_FUNCTION_VALIDATE(params.get()); | 114 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 113 | 115 |
| 114 GCMProfileService()->Register( | 116 GetGCMDriver()->Register( |
| 115 GetExtension()->id(), | 117 GetExtension()->id(), |
| 116 params->sender_ids, | 118 params->sender_ids, |
| 117 base::Bind(&GcmRegisterFunction::CompleteFunctionWithResult, this)); | 119 base::Bind(&GcmRegisterFunction::CompleteFunctionWithResult, this)); |
| 118 | 120 |
| 119 return true; | 121 return true; |
| 120 } | 122 } |
| 121 | 123 |
| 122 void GcmRegisterFunction::CompleteFunctionWithResult( | 124 void GcmRegisterFunction::CompleteFunctionWithResult( |
| 123 const std::string& registration_id, | 125 const std::string& registration_id, |
| 124 gcm::GCMClient::Result result) { | 126 gcm::GCMClient::Result result) { |
| 125 SetResult(new base::StringValue(registration_id)); | 127 SetResult(new base::StringValue(registration_id)); |
| 126 SetError(GcmResultToError(result)); | 128 SetError(GcmResultToError(result)); |
| 127 SendResponse(gcm::GCMClient::SUCCESS == result); | 129 SendResponse(gcm::GCMClient::SUCCESS == result); |
| 128 } | 130 } |
| 129 | 131 |
| 130 GcmUnregisterFunction::GcmUnregisterFunction() {} | 132 GcmUnregisterFunction::GcmUnregisterFunction() {} |
| 131 | 133 |
| 132 GcmUnregisterFunction::~GcmUnregisterFunction() {} | 134 GcmUnregisterFunction::~GcmUnregisterFunction() {} |
| 133 | 135 |
| 134 bool GcmUnregisterFunction::DoWork() { | 136 bool GcmUnregisterFunction::DoWork() { |
| 135 UMA_HISTOGRAM_BOOLEAN("GCM.APICallUnregister", true); | 137 UMA_HISTOGRAM_BOOLEAN("GCM.APICallUnregister", true); |
| 136 | 138 |
| 137 GCMProfileService()->Unregister( | 139 GetGCMDriver()->Unregister( |
| 138 GetExtension()->id(), | 140 GetExtension()->id(), |
| 139 base::Bind(&GcmUnregisterFunction::CompleteFunctionWithResult, this)); | 141 base::Bind(&GcmUnregisterFunction::CompleteFunctionWithResult, this)); |
| 140 | 142 |
| 141 return true; | 143 return true; |
| 142 } | 144 } |
| 143 | 145 |
| 144 void GcmUnregisterFunction::CompleteFunctionWithResult( | 146 void GcmUnregisterFunction::CompleteFunctionWithResult( |
| 145 gcm::GCMClient::Result result) { | 147 gcm::GCMClient::Result result) { |
| 146 SetError(GcmResultToError(result)); | 148 SetError(GcmResultToError(result)); |
| 147 SendResponse(gcm::GCMClient::SUCCESS == result); | 149 SendResponse(gcm::GCMClient::SUCCESS == result); |
| 148 } | 150 } |
| 149 | 151 |
| 150 GcmSendFunction::GcmSendFunction() {} | 152 GcmSendFunction::GcmSendFunction() {} |
| 151 | 153 |
| 152 GcmSendFunction::~GcmSendFunction() {} | 154 GcmSendFunction::~GcmSendFunction() {} |
| 153 | 155 |
| 154 bool GcmSendFunction::DoWork() { | 156 bool GcmSendFunction::DoWork() { |
| 155 scoped_ptr<api::gcm::Send::Params> params( | 157 scoped_ptr<api::gcm::Send::Params> params( |
| 156 api::gcm::Send::Params::Create(*args_)); | 158 api::gcm::Send::Params::Create(*args_)); |
| 157 EXTENSION_FUNCTION_VALIDATE(params.get()); | 159 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 158 EXTENSION_FUNCTION_VALIDATE( | 160 EXTENSION_FUNCTION_VALIDATE( |
| 159 ValidateMessageData(params->message.data.additional_properties)); | 161 ValidateMessageData(params->message.data.additional_properties)); |
| 160 | 162 |
| 161 gcm::GCMClient::OutgoingMessage outgoing_message; | 163 gcm::GCMClient::OutgoingMessage outgoing_message; |
| 162 outgoing_message.id = params->message.message_id; | 164 outgoing_message.id = params->message.message_id; |
| 163 outgoing_message.data = params->message.data.additional_properties; | 165 outgoing_message.data = params->message.data.additional_properties; |
| 164 if (params->message.time_to_live.get()) | 166 if (params->message.time_to_live.get()) |
| 165 outgoing_message.time_to_live = *params->message.time_to_live; | 167 outgoing_message.time_to_live = *params->message.time_to_live; |
| 166 | 168 |
| 167 GCMProfileService()->Send( | 169 GetGCMDriver()->Send( |
| 168 GetExtension()->id(), | 170 GetExtension()->id(), |
| 169 params->message.destination_id, | 171 params->message.destination_id, |
| 170 outgoing_message, | 172 outgoing_message, |
| 171 base::Bind(&GcmSendFunction::CompleteFunctionWithResult, this)); | 173 base::Bind(&GcmSendFunction::CompleteFunctionWithResult, this)); |
| 172 | 174 |
| 173 return true; | 175 return true; |
| 174 } | 176 } |
| 175 | 177 |
| 176 void GcmSendFunction::CompleteFunctionWithResult( | 178 void GcmSendFunction::CompleteFunctionWithResult( |
| 177 const std::string& message_id, | 179 const std::string& message_id, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 192 kMaximumMessageSize < iter->first.size() || | 194 kMaximumMessageSize < iter->first.size() || |
| 193 kMaximumMessageSize < iter->second.size() || | 195 kMaximumMessageSize < iter->second.size() || |
| 194 kMaximumMessageSize < total_size) | 196 kMaximumMessageSize < total_size) |
| 195 return false; | 197 return false; |
| 196 } | 198 } |
| 197 | 199 |
| 198 return total_size != 0; | 200 return total_size != 0; |
| 199 } | 201 } |
| 200 | 202 |
| 201 GcmJsEventRouter::GcmJsEventRouter(Profile* profile) : profile_(profile) { | 203 GcmJsEventRouter::GcmJsEventRouter(Profile* profile) : profile_(profile) { |
| 202 EventRouter* event_router = EventRouter::Get(profile_); | |
| 203 if (!event_router) | |
| 204 return; | |
| 205 | |
| 206 event_router->RegisterObserver(this, api::gcm::OnMessage::kEventName); | |
| 207 event_router->RegisterObserver(this, api::gcm::OnMessagesDeleted::kEventName); | |
| 208 event_router->RegisterObserver(this, api::gcm::OnSendError::kEventName); | |
| 209 } | 204 } |
| 210 | 205 |
| 211 GcmJsEventRouter::~GcmJsEventRouter() { | 206 GcmJsEventRouter::~GcmJsEventRouter() { |
| 212 EventRouter* event_router = EventRouter::Get(profile_); | |
| 213 if (event_router) | |
| 214 event_router->UnregisterObserver(this); | |
| 215 } | 207 } |
| 216 | 208 |
| 217 void GcmJsEventRouter::OnMessage( | 209 void GcmJsEventRouter::OnMessage( |
| 218 const std::string& app_id, | 210 const std::string& app_id, |
| 219 const gcm::GCMClient::IncomingMessage& message) { | 211 const gcm::GCMClient::IncomingMessage& message) { |
| 220 api::gcm::OnMessage::Message message_arg; | 212 api::gcm::OnMessage::Message message_arg; |
| 221 message_arg.data.additional_properties = message.data; | 213 message_arg.data.additional_properties = message.data; |
| 222 if (!message.collapse_key.empty()) | 214 if (!message.collapse_key.empty()) |
| 223 message_arg.collapse_key.reset(new std::string(message.collapse_key)); | 215 message_arg.collapse_key.reset(new std::string(message.collapse_key)); |
| 224 | 216 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 245 error.error_message = GcmResultToError(send_error_details.result); | 237 error.error_message = GcmResultToError(send_error_details.result); |
| 246 error.details.additional_properties = send_error_details.additional_data; | 238 error.details.additional_properties = send_error_details.additional_data; |
| 247 | 239 |
| 248 scoped_ptr<Event> event(new Event( | 240 scoped_ptr<Event> event(new Event( |
| 249 api::gcm::OnSendError::kEventName, | 241 api::gcm::OnSendError::kEventName, |
| 250 api::gcm::OnSendError::Create(error).Pass(), | 242 api::gcm::OnSendError::Create(error).Pass(), |
| 251 profile_)); | 243 profile_)); |
| 252 EventRouter::Get(profile_)->DispatchEventToExtension(app_id, event.Pass()); | 244 EventRouter::Get(profile_)->DispatchEventToExtension(app_id, event.Pass()); |
| 253 } | 245 } |
| 254 | 246 |
| 255 void GcmJsEventRouter::OnListenerAdded(const EventListenerInfo& details) { | |
| 256 if (gcm::GCMProfileService::GetGCMEnabledState(profile_) == | |
| 257 gcm::GCMProfileService::ALWAYS_DISABLED) { | |
| 258 return; | |
| 259 } | |
| 260 gcm::GCMProfileServiceFactory::GetForProfile(profile_)->Start(); | |
| 261 } | |
| 262 | |
| 263 } // namespace extensions | 247 } // namespace extensions |
| OLD | NEW |