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

Side by Side Diff: chrome/browser/extensions/api/push_messaging/push_messaging_invalidation_handler.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: Whee 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati on_handler.h"
6
7 #include <algorithm>
8 #include <vector>
9
10 #include "base/string_number_conversions.h"
11 #include "base/string_split.h"
12 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati on_handler_delegate.h"
13 #include "chrome/browser/sync/invalidation_frontend.h"
14 #include "chrome/common/extensions/extension.h"
15 #include "google/cacheinvalidation/types.pb.h"
16
17 namespace extensions {
18
19 namespace {
20
21 const int kNumberOfSubchannels = 4;
22
23 // Chrome push messaging object IDs currently have the following format:
24 // <format type>/<GAIA ID>/<extension ID>/<subchannel>
25 // <format type> must be 'U', and <GAIA ID> is handled server-side so the client
26 // never sees it.
27 syncer::ObjectIdSet ExtensionIdToObjectIds(const std::string& extension_id) {
Pete Williamson 2012/08/20 22:41:05 Should we add unit tests for this and for ObjectId
dcheng 2012/08/20 22:45:28 This is tested through the RegisterUnregister and
28 syncer::ObjectIdSet object_ids;
29 for (int i = 0; i < kNumberOfSubchannels; ++i) {
30 std::string name("U/");
31 name += extension_id;
32 name += "/";
33 name += base::IntToString(i);
34 // TODO(dcheng): CHROME_COMPONENTS is temporary, we need to update this once
35 // we roll cacheinvalidation.
36 object_ids.insert(invalidation::ObjectId(
37 ipc::invalidation::ObjectSource::CHROME_COMPONENTS,
38 name));
39 }
40 return object_ids;
41 }
42
43 // Returns true iff the conversion was successful.
44 bool ObjectIdToExtensionAndSubchannel(const invalidation::ObjectId& object_id,
45 std::string* extension_id,
46 int* subchannel) {
47 // TODO(dcheng): CHROME_COMPONENTS is temporary, we need to update this once
48 // we roll cacheinvalidation.
49 if (object_id.source() !=
50 ipc::invalidation::ObjectSource::CHROME_COMPONENTS) {
51 DLOG(WARNING) << "Invalid source: " << object_id.source();
52 return false;
53 }
54
55 const std::string& name = object_id.name();
56 std::vector<std::string> components;
57 base::SplitStringDontTrim(name, '/', &components);
58 if (components[0] != "U") {
59 DLOG(WARNING) << "Invalid format type from object name " << name;
60 return false;
61 }
62 if (!Extension::IdIsValid(components[1])) {
63 DLOG(WARNING) << "Invalid extension ID from object name " << name;
64 return false;
65 }
66 *extension_id = components[1];
67 if (!base::StringToInt(components[2], subchannel)) {
68 DLOG(WARNING) << "Subchannel not a number from object name " << name;
69 return false;
70 }
71 if (*subchannel < 0 || *subchannel >= kNumberOfSubchannels) {
72 DLOG(WARNING) << "Subchannel out of range from object name " << name;
73 return false;
74 }
75 return true;
76 }
77
78 } // namespace
79
80 PushMessagingInvalidationHandler::PushMessagingInvalidationHandler(
81 InvalidationFrontend* service,
82 PushMessagingInvalidationHandlerDelegate* delegate,
83 const std::set<std::string>& extension_ids)
84 : service_(service),
85 registered_extensions_(extension_ids),
86 delegate_(delegate) {
87 DCHECK(service_);
88 service_->RegisterInvalidationHandler(this);
89 UpdateRegistrations();
90 }
91
92 PushMessagingInvalidationHandler::~PushMessagingInvalidationHandler() {
93 DCHECK(thread_checker_.CalledOnValidThread());
94 service_->UnregisterInvalidationHandler(this);
95 }
96
97 void PushMessagingInvalidationHandler::RegisterExtension(
98 const std::string& extension_id) {
99 DCHECK(thread_checker_.CalledOnValidThread());
100 DCHECK(Extension::IdIsValid(extension_id));
101 registered_extensions_.insert(extension_id);
102 UpdateRegistrations();
103 }
104
105 void PushMessagingInvalidationHandler::UnregisterExtension(
106 const std::string& extension_id) {
107 DCHECK(thread_checker_.CalledOnValidThread());
108 DCHECK(Extension::IdIsValid(extension_id));
109 registered_extensions_.erase(extension_id);
110 UpdateRegistrations();
111 }
112
113 void PushMessagingInvalidationHandler::OnNotificationsEnabled() {
114 DCHECK(thread_checker_.CalledOnValidThread());
115 // Nothing to do.
116 }
117
118 void PushMessagingInvalidationHandler::OnNotificationsDisabled(
119 syncer::NotificationsDisabledReason reason) {
120 DCHECK(thread_checker_.CalledOnValidThread());
121 // Nothing to do.
122 }
123
124 void PushMessagingInvalidationHandler::OnIncomingNotification(
125 const syncer::ObjectIdPayloadMap& id_payloads,
126 syncer::IncomingNotificationSource source) {
127 DCHECK(thread_checker_.CalledOnValidThread());
128 for (syncer::ObjectIdPayloadMap::const_iterator it = id_payloads.begin();
129 it != id_payloads.end(); ++it) {
130 std::string extension_id;
131 int subchannel;
132 if (ObjectIdToExtensionAndSubchannel(it->first,
133 &extension_id,
134 &subchannel)) {
135 delegate_->OnMessage(extension_id, subchannel, it->second);
136 }
137 }
138 }
139
140 void PushMessagingInvalidationHandler::UpdateRegistrations() {
141 syncer::ObjectIdSet ids;
142 for (std::set<std::string>::const_iterator it =
143 registered_extensions_.begin(); it != registered_extensions_.end();
144 ++it) {
145 const syncer::ObjectIdSet& object_ids = ExtensionIdToObjectIds(*it);
146 ids.insert(object_ids.begin(), object_ids.end());
147 }
148 service_->UpdateRegisteredInvalidationIds(this, ids);
149 }
150
151 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698