Chromium Code Reviews| 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/event_router.h" | |
| 19 #include "extensions/browser/extension_system.h" | 18 #include "extensions/browser/extension_system.h" |
| 20 #include "extensions/common/extension.h" | 19 #include "extensions/common/extension.h" |
| 21 | 20 |
| 22 namespace { | 21 namespace { |
| 23 | 22 |
| 24 const size_t kMaximumMessageSize = 4096; // in bytes. | 23 const size_t kMaximumMessageSize = 4096; // in bytes. |
| 25 const char kCollapseKey[] = "collapse_key"; | 24 const char kCollapseKey[] = "collapse_key"; |
| 26 const char kGoogDotRestrictedPrefix[] = "goog."; | 25 const char kGoogDotRestrictedPrefix[] = "goog."; |
| 27 const char kGoogleRestrictedPrefix[] = "google"; | 26 const char kGoogleRestrictedPrefix[] = "google"; |
| 28 | 27 |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 172 if (!IsMessageKeyValid(iter->first) || | 171 if (!IsMessageKeyValid(iter->first) || |
| 173 kMaximumMessageSize < iter->first.size() || | 172 kMaximumMessageSize < iter->first.size() || |
| 174 kMaximumMessageSize < iter->second.size() || | 173 kMaximumMessageSize < iter->second.size() || |
| 175 kMaximumMessageSize < total_size) | 174 kMaximumMessageSize < total_size) |
| 176 return false; | 175 return false; |
| 177 } | 176 } |
| 178 | 177 |
| 179 return total_size != 0; | 178 return total_size != 0; |
| 180 } | 179 } |
| 181 | 180 |
| 182 GcmJsEventRouter::GcmJsEventRouter(Profile* profile) : profile_(profile) {} | 181 GcmJsEventRouter::GcmJsEventRouter(Profile* profile) : profile_(profile) { |
| 182 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | |
| 183 this, api::gcm::OnMessage::kEventName); | |
| 184 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | |
| 185 this, api::gcm::OnMessagesDeleted::kEventName); | |
| 186 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | |
| 187 this, api::gcm::OnSendError::kEventName); | |
| 188 } | |
| 183 | 189 |
| 184 GcmJsEventRouter::~GcmJsEventRouter() {} | 190 GcmJsEventRouter::~GcmJsEventRouter() { |
| 191 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); | |
| 192 } | |
| 185 | 193 |
| 186 void GcmJsEventRouter::OnMessage( | 194 void GcmJsEventRouter::OnMessage( |
| 187 const std::string& app_id, | 195 const std::string& app_id, |
| 188 const gcm::GCMClient::IncomingMessage& message) { | 196 const gcm::GCMClient::IncomingMessage& message) { |
| 189 api::gcm::OnMessage::Message message_arg; | 197 api::gcm::OnMessage::Message message_arg; |
| 190 message_arg.data.additional_properties = message.data; | 198 message_arg.data.additional_properties = message.data; |
| 191 if (!message.collapse_key.empty()) | 199 if (!message.collapse_key.empty()) |
| 192 message_arg.collapse_key.reset(new std::string(message.collapse_key)); | 200 message_arg.collapse_key.reset(new std::string(message.collapse_key)); |
| 193 | 201 |
| 194 scoped_ptr<Event> event(new Event( | 202 scoped_ptr<Event> event(new Event( |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 216 error.error_message = GcmResultToError(result); | 224 error.error_message = GcmResultToError(result); |
| 217 | 225 |
| 218 scoped_ptr<Event> event(new Event( | 226 scoped_ptr<Event> event(new Event( |
| 219 api::gcm::OnSendError::kEventName, | 227 api::gcm::OnSendError::kEventName, |
| 220 api::gcm::OnSendError::Create(error).Pass(), | 228 api::gcm::OnSendError::Create(error).Pass(), |
| 221 profile_)); | 229 profile_)); |
| 222 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( | 230 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( |
| 223 app_id, event.Pass()); | 231 app_id, event.Pass()); |
| 224 } | 232 } |
| 225 | 233 |
| 234 void GcmJsEventRouter::OnListenerAdded(const EventListenerInfo& details) { | |
| 235 if (gcm::GCMProfileService::GetGCMEnabledState(profile_) == | |
| 236 gcm::GCMProfileService::ALWAYS_DISABLED) | |
|
fgorski
2014/03/07 17:56:07
nit: {} would make it more clear, given that if ha
| |
| 237 return; | |
| 238 gcm::GCMProfileServiceFactory::GetForProfile(profile_)->Start(); | |
| 239 } | |
| 240 | |
| 226 } // namespace extensions | 241 } // namespace extensions |
| OLD | NEW |