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

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: patch-5 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 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 std::string error_text = error.error_message(); 275 std::string error_text = error.error_message();
284 // If the error message is blank, see if we can set it from the state. 276 // If the error message is blank, see if we can set it from the state.
285 if (error_text.empty() && 277 if (error_text.empty() &&
286 (0 != error.state())) { 278 (0 != error.state())) {
287 error_text = base::IntToString(error.state()); 279 error_text = base::IntToString(error.state());
288 } 280 }
289 281
290 ReportResult(std::string(), error_text); 282 ReportResult(std::string(), error_text);
291 } 283 }
292 284
285 PushMessagingAPI::PushMessagingAPI(Profile* profile)
286 : profile_(profile) {
287 ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
288 this, event_names::kOnPushMessage);
289 }
290
291 PushMessagingAPI::~PushMessagingAPI() {
292 }
293
294 // static
295 PushMessagingAPI* PushMessagingAPI::Get(Profile* profile) {
296 return PushMessagingAPIFactory::GetForProfile(profile);
297 }
298
299 void PushMessagingAPI::Shutdown() {
300 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
akalin 2012/12/11 22:21:53 Perhaps add a comment saying that it's okay to unr
Joe Thomas 2012/12/12 02:39:27 Done.
301 push_messaging_event_router_.reset();
302 }
303
304 void PushMessagingAPI::OnListenerAdded(
305 const extensions::EventListenerInfo& details) {
306 push_messaging_event_router_.reset(new PushMessagingEventRouter(profile_));
akalin 2012/12/11 22:21:53 perhaps DCHECK that push_messaging_event_router_ i
Joe Thomas 2012/12/12 02:39:27 Done.
307 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
308 }
309
310 void PushMessagingAPI::InitializePushMessagingEventRouterForTest() {
Yoyo Zhou 2012/12/11 22:07:27 It seems to me this isn't necessary for the tests
akalin 2012/12/11 22:23:41 If it's common for tests to trigger OnListenerAdde
Joe Thomas 2012/12/12 02:39:27 Done.
311 push_messaging_event_router_.reset(new PushMessagingEventRouter(profile_));
akalin 2012/12/11 22:21:53 can probably just call OnListenerAdded() directly
Joe Thomas 2012/12/12 02:39:27 OnListenerAdded() takes a parameter and we don't k
akalin 2012/12/12 02:41:01 Can you then add a private helper function like:
Joe Thomas 2012/12/12 03:13:49 Done.
312 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
313 }
314
315 PushMessagingEventRouter*
316 PushMessagingAPI::GetPushMessagingEventRouterForTest() {
317 return push_messaging_event_router_.get();
318 }
319
293 } // namespace extensions 320 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698