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

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

Issue 10826156: Plumb invalidations from Tango to the extensions code for the Push Messaging API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix gmock and maybe Android build Created 8 years, 4 months 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 | Annotate | Revision Log
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>
8
7 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/logging.h"
8 #include "base/values.h" 11 #include "base/values.h"
12 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati on_handler.h"
9 #include "chrome/browser/extensions/event_names.h" 13 #include "chrome/browser/extensions/event_names.h"
10 #include "chrome/browser/extensions/event_router.h" 14 #include "chrome/browser/extensions/event_router.h"
15 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/extensions/extension_system.h"
11 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/sync/profile_sync_service.h"
19 #include "chrome/browser/sync/profile_sync_service_factory.h"
20 #include "chrome/common/chrome_notification_types.h"
12 #include "chrome/common/extensions/api/experimental_push_messaging.h" 21 #include "chrome/common/extensions/api/experimental_push_messaging.h"
13 #include "content/public/browser/browser_thread.h" 22 #include "chrome/common/extensions/extension.h"
23 #include "chrome/common/extensions/extension_set.h"
24 #include "chrome/common/extensions/permissions/api_permission.h"
25 #include "content/public/browser/notification_details.h"
26 #include "content/public/browser/notification_source.h"
14 #include "googleurl/src/gurl.h" 27 #include "googleurl/src/gurl.h"
15 28
16 namespace extensions { 29 namespace extensions {
17 30
18 namespace glue = extensions::api::experimental_push_messaging; 31 namespace glue = api::experimental_push_messaging;
19 32
20 PushMessagingEventRouter::PushMessagingEventRouter(Profile* profile) 33 PushMessagingEventRouter::PushMessagingEventRouter(Profile* profile)
21 : profile_(profile) { 34 : profile_(profile) {
22 } 35 }
23 36
24 PushMessagingEventRouter::~PushMessagingEventRouter() {} 37 PushMessagingEventRouter::~PushMessagingEventRouter() {}
25 38
Pete Williamson 2012/08/13 18:24:09 Might be good to add a comment here: // Find out a
dcheng 2012/08/13 20:44:14 In general, we try to avoid comments that describe
26 void PushMessagingEventRouter::Init() { 39 void PushMessagingEventRouter::Init() {
27 // TODO(dcheng): Add hooks into InvalidationHandler when landed. 40 ProfileSyncService* pss = ProfileSyncServiceFactory::GetForProfile(profile_);
41 // This may be NULL; for example, for the ChromeOS guest user. In these cases,
42 // just return without setting up anything, since it won't work anyway.
43 if (!pss)
44 return;
45
Pete Williamson 2012/08/13 18:24:09 Might be good to add a comment: // Register for al
dcheng 2012/08/13 20:44:14 Done.
46 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
47 content::Source<Profile>(profile_->GetOriginalProfile()));
48 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
49 content::Source<Profile>(profile_->GetOriginalProfile()));
50 const ExtensionSet* extensions =
51 ExtensionSystem::Get(profile_)->extension_service()->extensions();
52 std::set<std::string> push_messaging_extensions;
Pete Williamson 2012/08/13 18:24:09 // Iterate over the list of installed extensions a
dcheng 2012/08/13 20:44:14 See above.
53 for (ExtensionSet::const_iterator it = extensions->begin();
54 it != extensions->end(); ++it) {
55 const Extension* extension = *it;
56 if (extension->HasAPIPermission(APIPermission::kPushMessaging)) {
57 push_messaging_extensions.insert(extension->id());
58 }
59 }
Pete Williamson 2012/08/13 18:24:09 // now register all the object IDs for the push m
dcheng 2012/08/13 20:44:14 See above.
60 handler_.reset(new PushMessagingInvalidationHandler(
61 pss, this, push_messaging_extensions));
62 }
63
64 void PushMessagingEventRouter::Shutdown() {
65 handler_.reset();
66 }
67
68 void PushMessagingEventRouter::SetMapperForTest(
69 scoped_ptr<PushMessagingInvalidationMapper> mapper) {
70 handler_ = mapper.Pass();
28 } 71 }
29 72
30 void PushMessagingEventRouter::OnMessage(const std::string& extension_id, 73 void PushMessagingEventRouter::OnMessage(const std::string& extension_id,
31 int subchannel, 74 int subchannel,
32 const std::string& payload) { 75 const std::string& payload) {
33 glue::Message message; 76 glue::Message message;
34 message.subchannel_id = subchannel; 77 message.subchannel_id = subchannel;
35 message.payload = payload; 78 message.payload = payload;
36 79
37 scoped_ptr<base::ListValue> args(glue::OnMessage::Create(message)); 80 scoped_ptr<base::ListValue> args(glue::OnMessage::Create(message));
38 profile_->GetExtensionEventRouter()->DispatchEventToExtension( 81 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension(
39 extension_id, 82 extension_id,
40 event_names::kOnPushMessage, 83 event_names::kOnPushMessage,
41 args.Pass(), 84 args.Pass(),
42 profile_, 85 profile_,
43 GURL()); 86 GURL());
44 } 87 }
45 88
89 void PushMessagingEventRouter::Observe(
90 int type,
91 const content::NotificationSource& source,
92 const content::NotificationDetails& details) {
93 switch (type) {
94 case chrome::NOTIFICATION_EXTENSION_LOADED: {
95 const Extension* extension = content::Details<Extension>(details).ptr();
96 if (extension->HasAPIPermission(APIPermission::kPushMessaging)) {
97 handler_->RegisterExtension(extension->id());
98 }
99 break;
100 }
101 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
Mihai Parparita -not on Chrome 2012/08/11 00:47:27 There's also NOTIFICATION_EXTENSION_PERMISSIONS_UP
dcheng 2012/08/13 20:44:14 Hmm. Thinking about it more, I'm not sure if push
102 const Extension* extension =
103 content::Details<UnloadedExtensionInfo>(details)->extension;
104 if (extension->HasAPIPermission(APIPermission::kPushMessaging)) {
105 handler_->UnregisterExtension(extension->id());
106 }
107 break;
108 }
109 default:
110 NOTREACHED();
111 }
112 }
113
46 } // namespace extensions 114 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698