Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(686)

Side by Side Diff: chrome/browser/extensions/api/push_messaging/push_messaging_api.cc

Issue 11496004: Lazy initialization for PushMessagingEventRouter (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: nit fixed Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 // UnregisterObserver() may have already been called in
300 // InitializeEventRouter(), but it is safe to call it more than once.
301 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
302 push_messaging_event_router_.reset();
303 }
304
305 void PushMessagingAPI::OnListenerAdded(
306 const extensions::EventListenerInfo& details) {
307 InitializeEventRouter();
308 }
309
310 void PushMessagingAPI::InitializeEventRouterForTest() {
311 InitializeEventRouter();
312 }
313
314 PushMessagingEventRouter* PushMessagingAPI::GetEventRouterForTest() {
315 return push_messaging_event_router_.get();
316 }
317
318 void PushMessagingAPI::InitializeEventRouter() {
319 DCHECK(!push_messaging_event_router_.get());
320 push_messaging_event_router_.reset(new PushMessagingEventRouter(profile_));
321 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
322 }
323
292 } // namespace extensions 324 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698