| 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/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 15 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
| 16 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 16 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| 17 #include "chrome/common/extensions/api/gcm.h" | 17 #include "chrome/common/extensions/api/gcm.h" |
| 18 #include "extensions/browser/extension_system.h" | |
| 19 #include "extensions/common/extension.h" | 18 #include "extensions/common/extension.h" |
| 20 | 19 |
| 21 namespace { | 20 namespace { |
| 22 | 21 |
| 23 const size_t kMaximumMessageSize = 4096; // in bytes. | 22 const size_t kMaximumMessageSize = 4096; // in bytes. |
| 24 const char kCollapseKey[] = "collapse_key"; | 23 const char kCollapseKey[] = "collapse_key"; |
| 25 const char kGoogDotRestrictedPrefix[] = "goog."; | 24 const char kGoogDotRestrictedPrefix[] = "goog."; |
| 26 const char kGoogleRestrictedPrefix[] = "google"; | 25 const char kGoogleRestrictedPrefix[] = "google"; |
| 27 | 26 |
| 28 // Error messages. | 27 // Error messages. |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 kMaximumMessageSize < iter->first.size() || | 189 kMaximumMessageSize < iter->first.size() || |
| 191 kMaximumMessageSize < iter->second.size() || | 190 kMaximumMessageSize < iter->second.size() || |
| 192 kMaximumMessageSize < total_size) | 191 kMaximumMessageSize < total_size) |
| 193 return false; | 192 return false; |
| 194 } | 193 } |
| 195 | 194 |
| 196 return total_size != 0; | 195 return total_size != 0; |
| 197 } | 196 } |
| 198 | 197 |
| 199 GcmJsEventRouter::GcmJsEventRouter(Profile* profile) : profile_(profile) { | 198 GcmJsEventRouter::GcmJsEventRouter(Profile* profile) : profile_(profile) { |
| 200 if (ExtensionSystem::Get(profile_)->event_router()) { | 199 EventRouter* event_router = EventRouter::Get(profile_); |
| 201 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | 200 if (!event_router) |
| 202 this, api::gcm::OnMessage::kEventName); | 201 return; |
| 203 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | 202 |
| 204 this, api::gcm::OnMessagesDeleted::kEventName); | 203 event_router->RegisterObserver(this, api::gcm::OnMessage::kEventName); |
| 205 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | 204 event_router->RegisterObserver(this, api::gcm::OnMessagesDeleted::kEventName); |
| 206 this, api::gcm::OnSendError::kEventName); | 205 event_router->RegisterObserver(this, api::gcm::OnSendError::kEventName); |
| 207 } | |
| 208 } | 206 } |
| 209 | 207 |
| 210 GcmJsEventRouter::~GcmJsEventRouter() { | 208 GcmJsEventRouter::~GcmJsEventRouter() { |
| 211 if (ExtensionSystem::Get(profile_)->event_router()) | 209 EventRouter* event_router = EventRouter::Get(profile_); |
| 212 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); | 210 if (event_router) |
| 211 event_router->UnregisterObserver(this); |
| 213 } | 212 } |
| 214 | 213 |
| 215 void GcmJsEventRouter::OnMessage( | 214 void GcmJsEventRouter::OnMessage( |
| 216 const std::string& app_id, | 215 const std::string& app_id, |
| 217 const gcm::GCMClient::IncomingMessage& message) { | 216 const gcm::GCMClient::IncomingMessage& message) { |
| 218 api::gcm::OnMessage::Message message_arg; | 217 api::gcm::OnMessage::Message message_arg; |
| 219 message_arg.data.additional_properties = message.data; | 218 message_arg.data.additional_properties = message.data; |
| 220 if (!message.collapse_key.empty()) | 219 if (!message.collapse_key.empty()) |
| 221 message_arg.collapse_key.reset(new std::string(message.collapse_key)); | 220 message_arg.collapse_key.reset(new std::string(message.collapse_key)); |
| 222 | 221 |
| 223 scoped_ptr<Event> event(new Event( | 222 scoped_ptr<Event> event(new Event( |
| 224 api::gcm::OnMessage::kEventName, | 223 api::gcm::OnMessage::kEventName, |
| 225 api::gcm::OnMessage::Create(message_arg).Pass(), | 224 api::gcm::OnMessage::Create(message_arg).Pass(), |
| 226 profile_)); | 225 profile_)); |
| 227 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( | 226 EventRouter::Get(profile_)->DispatchEventToExtension(app_id, event.Pass()); |
| 228 app_id, event.Pass()); | |
| 229 } | 227 } |
| 230 | 228 |
| 231 void GcmJsEventRouter::OnMessagesDeleted(const std::string& app_id) { | 229 void GcmJsEventRouter::OnMessagesDeleted(const std::string& app_id) { |
| 232 scoped_ptr<Event> event(new Event( | 230 scoped_ptr<Event> event(new Event( |
| 233 api::gcm::OnMessagesDeleted::kEventName, | 231 api::gcm::OnMessagesDeleted::kEventName, |
| 234 api::gcm::OnMessagesDeleted::Create().Pass(), | 232 api::gcm::OnMessagesDeleted::Create().Pass(), |
| 235 profile_)); | 233 profile_)); |
| 236 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( | 234 EventRouter::Get(profile_)->DispatchEventToExtension(app_id, event.Pass()); |
| 237 app_id, event.Pass()); | |
| 238 } | 235 } |
| 239 | 236 |
| 240 void GcmJsEventRouter::OnSendError( | 237 void GcmJsEventRouter::OnSendError( |
| 241 const std::string& app_id, | 238 const std::string& app_id, |
| 242 const gcm::GCMClient::SendErrorDetails& send_error_details) { | 239 const gcm::GCMClient::SendErrorDetails& send_error_details) { |
| 243 api::gcm::OnSendError::Error error; | 240 api::gcm::OnSendError::Error error; |
| 244 error.message_id.reset(new std::string(send_error_details.message_id)); | 241 error.message_id.reset(new std::string(send_error_details.message_id)); |
| 245 error.error_message = GcmResultToError(send_error_details.result); | 242 error.error_message = GcmResultToError(send_error_details.result); |
| 246 error.details.additional_properties = send_error_details.additional_data; | 243 error.details.additional_properties = send_error_details.additional_data; |
| 247 | 244 |
| 248 scoped_ptr<Event> event(new Event( | 245 scoped_ptr<Event> event(new Event( |
| 249 api::gcm::OnSendError::kEventName, | 246 api::gcm::OnSendError::kEventName, |
| 250 api::gcm::OnSendError::Create(error).Pass(), | 247 api::gcm::OnSendError::Create(error).Pass(), |
| 251 profile_)); | 248 profile_)); |
| 252 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( | 249 EventRouter::Get(profile_)->DispatchEventToExtension(app_id, event.Pass()); |
| 253 app_id, event.Pass()); | |
| 254 } | 250 } |
| 255 | 251 |
| 256 void GcmJsEventRouter::OnListenerAdded(const EventListenerInfo& details) { | 252 void GcmJsEventRouter::OnListenerAdded(const EventListenerInfo& details) { |
| 257 if (gcm::GCMProfileService::GetGCMEnabledState(profile_) == | 253 if (gcm::GCMProfileService::GetGCMEnabledState(profile_) == |
| 258 gcm::GCMProfileService::ALWAYS_DISABLED) { | 254 gcm::GCMProfileService::ALWAYS_DISABLED) { |
| 259 return; | 255 return; |
| 260 } | 256 } |
| 261 gcm::GCMProfileServiceFactory::GetForProfile(profile_)->Start(); | 257 gcm::GCMProfileServiceFactory::GetForProfile(profile_)->Start(); |
| 262 } | 258 } |
| 263 | 259 |
| 264 } // namespace extensions | 260 } // namespace extensions |
| OLD | NEW |