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 if (ExtensionSystem::Get(profile_)->event_router()) { |
| 183 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( |
| 184 this, api::gcm::OnMessage::kEventName); |
| 185 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( |
| 186 this, api::gcm::OnMessagesDeleted::kEventName); |
| 187 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( |
| 188 this, api::gcm::OnSendError::kEventName); |
| 189 } |
| 190 } |
183 | 191 |
184 GcmJsEventRouter::~GcmJsEventRouter() {} | 192 GcmJsEventRouter::~GcmJsEventRouter() { |
| 193 if (ExtensionSystem::Get(profile_)->event_router()) |
| 194 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); |
| 195 } |
185 | 196 |
186 void GcmJsEventRouter::OnMessage( | 197 void GcmJsEventRouter::OnMessage( |
187 const std::string& app_id, | 198 const std::string& app_id, |
188 const gcm::GCMClient::IncomingMessage& message) { | 199 const gcm::GCMClient::IncomingMessage& message) { |
189 api::gcm::OnMessage::Message message_arg; | 200 api::gcm::OnMessage::Message message_arg; |
190 message_arg.data.additional_properties = message.data; | 201 message_arg.data.additional_properties = message.data; |
191 if (!message.collapse_key.empty()) | 202 if (!message.collapse_key.empty()) |
192 message_arg.collapse_key.reset(new std::string(message.collapse_key)); | 203 message_arg.collapse_key.reset(new std::string(message.collapse_key)); |
193 | 204 |
194 scoped_ptr<Event> event(new Event( | 205 scoped_ptr<Event> event(new Event( |
(...skipping 21 matching lines...) Expand all Loading... |
216 error.error_message = GcmResultToError(result); | 227 error.error_message = GcmResultToError(result); |
217 | 228 |
218 scoped_ptr<Event> event(new Event( | 229 scoped_ptr<Event> event(new Event( |
219 api::gcm::OnSendError::kEventName, | 230 api::gcm::OnSendError::kEventName, |
220 api::gcm::OnSendError::Create(error).Pass(), | 231 api::gcm::OnSendError::Create(error).Pass(), |
221 profile_)); | 232 profile_)); |
222 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( | 233 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( |
223 app_id, event.Pass()); | 234 app_id, event.Pass()); |
224 } | 235 } |
225 | 236 |
| 237 void GcmJsEventRouter::OnListenerAdded(const EventListenerInfo& details) { |
| 238 if (gcm::GCMProfileService::GetGCMEnabledState(profile_) == |
| 239 gcm::GCMProfileService::ALWAYS_DISABLED) { |
| 240 return; |
| 241 } |
| 242 gcm::GCMProfileServiceFactory::GetForProfile(profile_)->Start(); |
| 243 } |
| 244 |
226 } // namespace extensions | 245 } // namespace extensions |
OLD | NEW |