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

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: Delimiter + restart test 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
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
46 const ExtensionSet* extensions =
47 ExtensionSystem::Get(profile_)->extension_service()->extensions();
48 std::set<std::string> push_messaging_extensions;
49 for (ExtensionSet::const_iterator it = extensions->begin();
50 it != extensions->end(); ++it) {
51 const Extension* extension = *it;
52 if (extension->HasAPIPermission(APIPermission::kPushMessaging)) {
53 push_messaging_extensions.insert(extension->id());
54 }
55 }
56 handler_.reset(new PushMessagingInvalidationHandler(
57 pss, this, push_messaging_extensions));
58
59 // Register for extension load/unload as well, so we can update any
60 // registrations as appropriate.
61 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
62 content::Source<Profile>(profile_->GetOriginalProfile()));
63 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
64 content::Source<Profile>(profile_->GetOriginalProfile()));
65 }
66
67 void PushMessagingEventRouter::Shutdown() {
68 handler_.reset();
Munjal (Google) 2012/08/20 19:26:00 Please add a comment describing why we have explic
dcheng 2012/08/20 20:58:21 Done.
69 }
70
71 void PushMessagingEventRouter::SetMapperForTest(
72 scoped_ptr<PushMessagingInvalidationMapper> mapper) {
73 handler_ = mapper.Pass();
74 }
75
76 void PushMessagingEventRouter::TriggerMessageForTest(
77 const std::string& extension_id,
78 int subchannel,
79 const std::string& payload) {
80 OnMessage(extension_id, subchannel, payload);
28 } 81 }
29 82
30 void PushMessagingEventRouter::OnMessage(const std::string& extension_id, 83 void PushMessagingEventRouter::OnMessage(const std::string& extension_id,
31 int subchannel, 84 int subchannel,
32 const std::string& payload) { 85 const std::string& payload) {
33 glue::Message message; 86 glue::Message message;
34 message.subchannel_id = subchannel; 87 message.subchannel_id = subchannel;
35 message.payload = payload; 88 message.payload = payload;
36 89
37 scoped_ptr<base::ListValue> args(glue::OnMessage::Create(message)); 90 scoped_ptr<base::ListValue> args(glue::OnMessage::Create(message));
38 profile_->GetExtensionEventRouter()->DispatchEventToExtension( 91 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension(
39 extension_id, 92 extension_id,
40 event_names::kOnPushMessage, 93 event_names::kOnPushMessage,
41 args.Pass(), 94 args.Pass(),
42 profile_, 95 profile_,
43 GURL()); 96 GURL());
44 } 97 }
45 98
99 void PushMessagingEventRouter::Observe(
100 int type,
101 const content::NotificationSource& source,
102 const content::NotificationDetails& details) {
103 switch (type) {
104 case chrome::NOTIFICATION_EXTENSION_LOADED: {
105 const Extension* extension = content::Details<Extension>(details).ptr();
106 if (extension->HasAPIPermission(APIPermission::kPushMessaging)) {
107 handler_->RegisterExtension(extension->id());
108 }
109 break;
110 }
111 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
112 const Extension* extension =
113 content::Details<UnloadedExtensionInfo>(details)->extension;
114 if (extension->HasAPIPermission(APIPermission::kPushMessaging)) {
115 handler_->UnregisterExtension(extension->id());
116 }
117 break;
118 }
119 default:
120 NOTREACHED();
121 }
122 }
123
46 } // namespace extensions 124 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698