OLD | NEW |
---|---|
(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 | |
9 #include "base/string_number_conversions.h" | |
10 #include "base/stringprintf.h" | |
11 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati on_handler_observer.h" | |
12 #include "chrome/browser/sync/invalidation_service.h" | |
13 #include "google/cacheinvalidation/types.pb.h" | |
14 | |
15 namespace extensions { | |
16 | |
17 namespace { | |
18 | |
19 const int kNumberOfSubchannels = 4; | |
20 | |
21 const int kFormatTypeIndex = 0; | |
22 const int kFormatTypeLength = 1; | |
23 const int kProjectIdIndex = 1; | |
24 const int kProjectIdLength = 2; | |
25 const int kExtensionIdIndex = 3; | |
26 const int kExtensionIdLength = 32; | |
27 const int kSubchannelIndex = 35; | |
28 const int kSubchannelLength = 1; | |
29 | |
30 // Chrome Components object IDs currently have the following format: | |
31 // <1 byte format type> | |
32 // Currently the only format type defined is 'U' but we'll need to create a | |
33 // new one. | |
34 // <8 byte GAIA ID> | |
35 // Handled entirely server-side. The client never see this. | |
36 // <2 byte project ID> | |
37 // We use "PM" for Push Messaging. | |
38 // <4-48 byte project object ID> | |
39 // We use the extension ID concatenated with the subchannel. | |
40 syncer::ObjectIdSet ExtensionIdToObjectIds(const std::string& extension_id) { | |
41 syncer::ObjectIdSet object_ids; | |
42 for (int i = 0; i < kNumberOfSubchannels; ++i) { | |
43 std::string name = base::StringPrintf("UPM%s%d", extension_id.c_str(), i); | |
Munjal (Google)
2012/08/14 18:56:37
How about using some separator (e.g. |) in the obj
dcheng
2012/08/14 21:39:59
The Chrome components format didn't allow for non-
| |
44 object_ids.insert(invalidation::ObjectId( | |
45 ipc::invalidation::ObjectSource::CHROME_COMPONENTS, | |
46 name)); | |
47 } | |
48 return object_ids; | |
49 } | |
50 | |
51 // Returns true iff the conversion was successful. | |
52 bool ObjectIdToExtensionAndSubchannel(const invalidation::ObjectId& object_id, | |
53 std::string* extension_id, | |
54 int* subchannel) { | |
55 if (object_id.source() != | |
56 ipc::invalidation::ObjectSource::CHROME_COMPONENTS) { | |
57 DLOG(WARNING) << "Invalid source: " << object_id.source(); | |
58 return false; | |
59 } | |
60 | |
61 const std::string& name = object_id.name(); | |
62 const size_t kValidObjectIdLength = kFormatTypeLength + kProjectIdLength + | |
63 kExtensionIdLength + kSubchannelLength; | |
64 if (name.size() != kValidObjectIdLength) { | |
65 DLOG(WARNING) << "Invalid length for object name " << name; | |
66 return false; | |
67 } | |
68 if (name.compare(kFormatTypeIndex, kFormatTypeLength, "U")) { | |
69 DLOG(WARNING) << "Invalid format type for object name " << name; | |
70 return false; | |
71 } | |
72 if (name.compare(kProjectIdIndex, kProjectIdLength, "PM")) { | |
73 DLOG(WARNING) << "Invalid project ID for object name " << name; | |
74 return false; | |
75 } | |
76 *extension_id = name.substr(kExtensionIdIndex, kExtensionIdLength); | |
77 if (!base::StringToInt(name.substr(kSubchannelIndex, kSubchannelLength), | |
78 subchannel)) { | |
79 DLOG(WARNING) << "Subchannel not a number for object name " << name; | |
80 return false; | |
81 } | |
82 | |
83 // We don't need to check for negative since the length check makes it | |
84 // impossible. | |
85 return *subchannel <= 3; | |
Munjal (Google)
2012/08/14 18:56:37
The logic in this method will be much more robust
dcheng
2012/08/14 21:39:59
See above.
| |
86 } | |
87 | |
88 } // namespace | |
89 | |
90 PushMessagingInvalidationHandler::PushMessagingInvalidationHandler( | |
91 InvalidationService* service, | |
92 PushMessagingInvalidationHandlerObserver* observer, | |
93 const std::set<std::string>& extension_ids) | |
94 : service_(service), | |
95 observer_(observer) { | |
96 DCHECK(service_); | |
97 | |
98 service_->RegisterInvalidationHandler(this); | |
99 | |
100 for (std::set<std::string>::const_iterator it = extension_ids.begin(); | |
101 it != extension_ids.end(); ++it) { | |
102 const syncer::ObjectIdSet& object_ids = ExtensionIdToObjectIds(*it); | |
103 registered_ids_.insert(object_ids.begin(), object_ids.end()); | |
104 } | |
105 service_->UpdateRegisteredInvalidationIds(this, registered_ids_); | |
106 } | |
107 | |
108 PushMessagingInvalidationHandler::~PushMessagingInvalidationHandler() { | |
109 DCHECK(thread_checker_.CalledOnValidThread()); | |
110 service_->UnregisterInvalidationHandler(this); | |
111 } | |
112 | |
113 void PushMessagingInvalidationHandler::RegisterExtension( | |
114 const std::string& extension_id) { | |
115 DCHECK(thread_checker_.CalledOnValidThread()); | |
116 DCHECK_EQ(32U, extension_id.size()); | |
117 const syncer::ObjectIdSet& object_ids = ExtensionIdToObjectIds(extension_id); | |
118 registered_ids_.insert(object_ids.begin(), object_ids.end()); | |
119 service_->UpdateRegisteredInvalidationIds(this, registered_ids_); | |
120 } | |
121 | |
122 void PushMessagingInvalidationHandler::UnregisterExtension( | |
123 const std::string& extension_id) { | |
124 DCHECK(thread_checker_.CalledOnValidThread()); | |
125 DCHECK_EQ(32U, extension_id.size()); | |
126 const syncer::ObjectIdSet& object_ids = ExtensionIdToObjectIds(extension_id); | |
127 syncer::ObjectIdSet updated_registered_ids; | |
128 std::set_difference(registered_ids_.begin(), registered_ids_.end(), | |
129 object_ids.begin(), object_ids.end(), | |
130 std::inserter(updated_registered_ids, | |
131 updated_registered_ids.begin()), | |
132 syncer::ObjectIdLessThan()); | |
133 registered_ids_.swap(updated_registered_ids); | |
134 service_->UpdateRegisteredInvalidationIds(this, registered_ids_); | |
Munjal (Google)
2012/08/14 18:56:37
If we are keeping track of registered_ids_ just to
dcheng
2012/08/14 21:39:59
Done.
| |
135 } | |
136 | |
137 void PushMessagingInvalidationHandler::OnNotificationsEnabled() { | |
138 DCHECK(thread_checker_.CalledOnValidThread()); | |
139 // Nothing to do. | |
140 } | |
141 | |
142 void PushMessagingInvalidationHandler::OnNotificationsDisabled( | |
143 syncer::NotificationsDisabledReason reason) { | |
144 DCHECK(thread_checker_.CalledOnValidThread()); | |
145 // Nothing to do. | |
146 } | |
147 | |
148 void PushMessagingInvalidationHandler::OnIncomingNotification( | |
149 const syncer::ObjectIdPayloadMap& id_payloads, | |
150 syncer::IncomingNotificationSource source) { | |
151 DCHECK(thread_checker_.CalledOnValidThread()); | |
152 for (syncer::ObjectIdPayloadMap::const_iterator it = id_payloads.begin(); | |
153 it != id_payloads.end(); ++it) { | |
154 std::string extension_id; | |
155 int subchannel; | |
156 if (ObjectIdToExtensionAndSubchannel(it->first, | |
157 &extension_id, | |
158 &subchannel)) { | |
159 observer_->OnMessage(extension_id, subchannel, it->second); | |
160 } | |
161 } | |
162 } | |
163 | |
164 } // namespace extensions | |
OLD | NEW |