Chromium Code Reviews| 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/push_messaging/push_messaging_api.h" | 5 #include "chrome/browser/extensions/api/push_messaging/push_messaging_api.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/extensions/api/push_messaging/push_messaging_api_factor y.h" | |
| 13 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati on_handler.h" | 14 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati on_handler.h" |
| 14 #include "chrome/browser/extensions/event_names.h" | 15 #include "chrome/browser/extensions/event_names.h" |
| 15 #include "chrome/browser/extensions/event_router.h" | 16 #include "chrome/browser/extensions/event_router.h" |
| 16 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
| 17 #include "chrome/browser/extensions/extension_system.h" | 18 #include "chrome/browser/extensions/extension_system.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/signin/token_service.h" | 20 #include "chrome/browser/signin/token_service.h" |
| 20 #include "chrome/browser/signin/token_service_factory.h" | 21 #include "chrome/browser/signin/token_service_factory.h" |
| 21 #include "chrome/browser/sync/profile_sync_service.h" | 22 #include "chrome/browser/sync/profile_sync_service.h" |
| 22 #include "chrome/browser/sync/profile_sync_service_factory.h" | 23 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 68 // Register for extension load/unload as well, so we can update any | 69 // Register for extension load/unload as well, so we can update any |
| 69 // registrations as appropriate. | 70 // registrations as appropriate. |
| 70 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 71 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 71 content::Source<Profile>(profile_->GetOriginalProfile())); | 72 content::Source<Profile>(profile_->GetOriginalProfile())); |
| 72 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 73 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 73 content::Source<Profile>(profile_->GetOriginalProfile())); | 74 content::Source<Profile>(profile_->GetOriginalProfile())); |
| 74 } | 75 } |
| 75 | 76 |
| 76 PushMessagingEventRouter::~PushMessagingEventRouter() {} | 77 PushMessagingEventRouter::~PushMessagingEventRouter() {} |
| 77 | 78 |
| 78 void PushMessagingEventRouter::Shutdown() { | |
| 79 // We need an explicit Shutdown() due to the dependencies among the various | |
| 80 // ProfileKeyedServices. ProfileSyncService depends on ExtensionSystem, so | |
| 81 // it is destroyed before us in the destruction phase of Profile shutdown. | |
| 82 // As a result, we need to drop any references to it in the Shutdown() phase | |
| 83 // instead. | |
| 84 handler_.reset(); | |
| 85 } | |
| 86 | |
| 87 void PushMessagingEventRouter::SetMapperForTest( | 79 void PushMessagingEventRouter::SetMapperForTest( |
| 88 scoped_ptr<PushMessagingInvalidationMapper> mapper) { | 80 scoped_ptr<PushMessagingInvalidationMapper> mapper) { |
| 89 handler_ = mapper.Pass(); | 81 handler_ = mapper.Pass(); |
| 90 } | 82 } |
| 91 | 83 |
| 92 void PushMessagingEventRouter::TriggerMessageForTest( | 84 void PushMessagingEventRouter::TriggerMessageForTest( |
| 93 const std::string& extension_id, | 85 const std::string& extension_id, |
| 94 int subchannel, | 86 int subchannel, |
| 95 const std::string& payload) { | 87 const std::string& payload) { |
| 96 OnMessage(extension_id, subchannel, payload); | 88 OnMessage(extension_id, subchannel, payload); |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 std::string error_text = error.error_message(); | 274 std::string error_text = error.error_message(); |
| 283 // If the error message is blank, see if we can set it from the state. | 275 // If the error message is blank, see if we can set it from the state. |
| 284 if (error_text.empty() && | 276 if (error_text.empty() && |
| 285 (0 != error.state())) { | 277 (0 != error.state())) { |
| 286 error_text = base::IntToString(error.state()); | 278 error_text = base::IntToString(error.state()); |
| 287 } | 279 } |
| 288 | 280 |
| 289 ReportResult(std::string(), error_text); | 281 ReportResult(std::string(), error_text); |
| 290 } | 282 } |
| 291 | 283 |
| 284 PushMessagingAPI::PushMessagingAPI(Profile* profile) | |
| 285 : profile_(profile) { | |
| 286 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver( | |
| 287 this, event_names::kOnPushMessage); | |
| 288 } | |
| 289 | |
| 290 PushMessagingAPI::~PushMessagingAPI() { | |
| 291 } | |
| 292 | |
| 293 // static | |
| 294 PushMessagingAPI* PushMessagingAPI::Get(Profile* profile) { | |
| 295 return PushMessagingAPIFactory::GetForProfile(profile); | |
| 296 } | |
| 297 | |
| 298 void PushMessagingAPI::Shutdown() { | |
| 299 // It's safe to call UnregisterObserver more than once. | |
|
akalin
2012/12/12 21:39:15
This comment could be clearer. How about:
// Unr
Joe Thomas
2012/12/13 08:34:43
Done.
| |
| 300 // UnregisterObserver would have already called when the listener | |
| 301 // got added. | |
| 302 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); | |
| 303 push_messaging_event_router_.reset(); | |
| 304 } | |
| 305 | |
| 306 void PushMessagingAPI::OnListenerAdded( | |
| 307 const extensions::EventListenerInfo& details) { | |
| 308 InitializeEventRouter(); | |
| 309 } | |
| 310 | |
| 311 void PushMessagingAPI::InitializeEventRouterForTest() { | |
| 312 InitializeEventRouter(); | |
| 313 } | |
| 314 | |
| 315 PushMessagingEventRouter* PushMessagingAPI::GetEventRouterForTest() { | |
| 316 return push_messaging_event_router_.get(); | |
| 317 } | |
| 318 | |
| 319 void PushMessagingAPI::InitializeEventRouter() { | |
| 320 DCHECK(!push_messaging_event_router_.get()); | |
| 321 push_messaging_event_router_.reset(new PushMessagingEventRouter(profile_)); | |
| 322 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); | |
| 323 } | |
| 324 | |
| 292 } // namespace extensions | 325 } // namespace extensions |
| OLD | NEW |