OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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/copresence/copresence_translations.h" | |
6 | |
7 #include "chrome/common/extensions/api/copresence.h" | |
8 #include "components/copresence/proto/data.pb.h" | |
9 #include "components/copresence/proto/enums.pb.h" | |
10 #include "components/copresence/proto/rpcs.pb.h" | |
11 | |
12 namespace { | |
13 | |
14 const int kDefaultTimeToLiveMs = 5 * 60 * 1000; // 5 minutes. | |
15 const int kMaxTimeToLiveMs = 24 * 60 * 60 * 1000; // 24 hours. | |
16 | |
17 // Checks and returns the ttl provided by the user. If invalid, returns -1. | |
18 int SanitizeTtl(int* user_ttl) { | |
19 return !user_ttl | |
20 ? kDefaultTimeToLiveMs | |
21 : *user_ttl <= 0 || *user_ttl > kMaxTimeToLiveMs ? -1 : *user_ttl; | |
22 } | |
23 | |
24 copresence::BroadcastScanConfiguration TranslateStrategy( | |
25 extensions::api::copresence::Strategy* strategy) { | |
26 bool only_broadcast = false; | |
27 bool only_scan = false; | |
28 | |
29 if (strategy->only_broadcast && strategy->only_broadcast) | |
xiyuan
2014/08/05 21:02:35
missing '*' for the second "strategy->only_broadca
rkc
2014/08/05 22:25:22
Done.
| |
30 only_broadcast = true; | |
31 if (strategy->only_scan && *strategy->only_scan) | |
32 only_scan = true; | |
33 | |
34 if (only_broadcast && only_scan) | |
35 return copresence::BROADCAST_AND_SCAN; | |
36 if (only_broadcast) | |
37 return copresence::BROADCAST_ONLY; | |
38 if (only_scan) | |
39 return copresence::SCAN_ONLY; | |
40 | |
41 return copresence::BROADCAST_SCAN_CONFIGURATION_UNKNOWN; | |
42 } | |
43 | |
44 } // namespace | |
45 | |
46 namespace extensions { | |
47 | |
48 bool AddPublishToRequest(const std::string& app_id, | |
49 const api::copresence::PublishOperation& publish, | |
50 copresence::ReportRequest* request) { | |
51 copresence::PublishedMessage* publish_proto = | |
52 request->mutable_manage_messages_request()->add_message_to_publish(); | |
53 publish_proto->mutable_access_policy()->mutable_acl()->set_acl_type( | |
54 copresence::NO_ACL_CHECK); | |
55 publish_proto->set_id(publish.id); | |
56 publish_proto->mutable_message()->mutable_type()->set_type( | |
57 publish.message.type); | |
58 publish_proto->mutable_message()->set_payload(publish.message.payload); | |
59 | |
60 int ttl = SanitizeTtl(publish.time_to_live_millis.get()); | |
61 if (ttl < 0) | |
62 return false; | |
63 publish_proto->mutable_access_policy()->set_ttl_millis(ttl); | |
64 | |
65 // TODO(rkc): This is a temporary hack; eventually namespaces will be | |
66 // completely gone. When that happens, remove this. | |
67 publish_proto->mutable_message()->mutable_type()->set_namespace_deprecated( | |
68 app_id); | |
69 publish_proto->set_strategy(copresence::AGGRESSIVE); | |
70 | |
71 if (publish.strategies.get()) { | |
72 copresence::BroadcastScanConfiguration config = | |
73 TranslateStrategy(publish.strategies.get()); | |
74 if (config != copresence::BROADCAST_SCAN_CONFIGURATION_UNKNOWN) { | |
75 copresence::TokenExchangeStrategy* strategy_proto = | |
76 publish_proto->mutable_token_exchange_strategy(); | |
77 strategy_proto->set_broadcast_scan_configuration(config); | |
78 } | |
79 } | |
80 | |
81 DVLOG(2) << "Publishing message of type " << publish.message.type << ":\n" | |
82 << publish.message.payload; | |
83 // TODO(ckehoe): Validate that required fields are non-empty, etc. | |
84 return true; | |
85 } | |
86 | |
87 bool AddUnpublishToRequest(const std::string& publish_id, | |
88 copresence::ReportRequest* request) { | |
89 if (publish_id.empty()) | |
90 return false; | |
91 | |
92 request->mutable_manage_messages_request()->add_id_to_unpublish(publish_id); | |
93 DVLOG(2) << "Unpublishing message \"" << publish_id << "\""; | |
94 return true; | |
95 } | |
96 | |
97 bool AddSubscribeToRequest( | |
98 const std::string& app_id, | |
99 const api::copresence::SubscribeOperation& subscription, | |
100 SubscriptionToAppMap* apps_by_subscription_id, | |
101 copresence::ReportRequest* request) { | |
102 // Associate the subscription id with the app id. | |
103 std::map<std::string, std::string>::iterator previous_subscription = | |
xiyuan
2014/08/05 21:02:35
nit: SubscriptionToAppMap::iterator since we have
rkc
2014/08/05 22:25:22
Done.
| |
104 apps_by_subscription_id->find(subscription.id); | |
105 if (previous_subscription == apps_by_subscription_id->end()) { | |
106 (*apps_by_subscription_id)[subscription.id] = app_id; | |
107 } else if (previous_subscription->second == app_id) { | |
108 VLOG(2) << "Overwriting subscription id \"" << subscription.id | |
109 << "\" for app \"" << app_id << "\""; | |
110 } else { | |
111 // A conflicting association exists already. | |
112 VLOG(1) << "Subscription id \"" << subscription.id | |
113 << "\" used by two apps: \"" << previous_subscription->second | |
114 << "\" and \"" << app_id << "\""; | |
115 return false; | |
116 } | |
117 | |
118 // Convert from client to server subscription format. | |
119 copresence::Subscription* subscription_proto = | |
120 request->mutable_manage_subscriptions_request()->add_subscription(); | |
121 subscription_proto->set_id(subscription.id); | |
122 int ttl = SanitizeTtl(subscription.time_to_live_millis.get()); | |
123 if (ttl < 0) | |
124 return false; | |
125 subscription_proto->set_ttl_millis(ttl); | |
126 | |
127 // TODO(rkc): This is a temporary hack; eventually namespaces will be | |
128 // completely gone. When that happens, remove this. | |
129 subscription_proto->mutable_message_type()->set_namespace_deprecated(app_id); | |
130 subscription_proto->set_strategy(copresence::AGGRESSIVE); | |
131 | |
132 subscription_proto->mutable_message_type()->set_type( | |
133 subscription.filter.type); | |
134 | |
135 if (subscription.strategies.get()) { | |
136 copresence::BroadcastScanConfiguration config = | |
137 TranslateStrategy(subscription.strategies.get()); | |
138 if (config != copresence::BROADCAST_SCAN_CONFIGURATION_UNKNOWN) { | |
139 copresence::TokenExchangeStrategy* strategy_proto = | |
140 subscription_proto->mutable_token_exchange_strategy(); | |
141 strategy_proto->set_broadcast_scan_configuration(config); | |
142 } | |
143 } | |
144 | |
145 DVLOG(2) << "Subscribing for messages of type " << subscription.filter.type; | |
146 // TODO(ckehoe): Validate that required fields are non-empty, etc. | |
147 return true; | |
148 } | |
149 | |
150 bool AddUnsubscribeToRequest(const std::string& app_id, | |
151 const std::string& subscription_id, | |
152 SubscriptionToAppMap* apps_by_subscription_id, | |
153 copresence::ReportRequest* request) { | |
154 if (subscription_id.empty()) | |
155 return false; | |
156 | |
157 // Check that this subscription id belongs to this app. | |
158 std::map<std::string, std::string>::iterator subscription = | |
xiyuan
2014/08/05 21:02:35
nit: SubscriptionToAppMap::iterator
rkc
2014/08/05 22:25:22
Done.
| |
159 apps_by_subscription_id->find(subscription_id); | |
160 if (subscription == apps_by_subscription_id->end()) { | |
161 LOG(ERROR) << "No such subscription \"" << subscription_id | |
162 << "\". Cannot unsubscribe."; | |
163 return false; | |
164 } else if (subscription->second != app_id) { | |
165 LOG(ERROR) << "Subscription \"" << subscription_id | |
166 << "\" does not belong to app \"" << app_id | |
167 << "\". Cannot unsubscribe."; | |
168 return false; | |
169 } else { | |
170 apps_by_subscription_id->erase(subscription); | |
171 } | |
172 | |
173 request->mutable_manage_subscriptions_request()->add_id_to_unsubscribe( | |
174 subscription_id); | |
175 DVLOG(2) << "Cancelling subscription \"" << subscription_id << "\" for app \"" | |
176 << app_id << "\""; | |
177 return true; | |
178 } | |
179 | |
180 bool PrepareReportRequestProto( | |
181 const std::vector<linked_ptr<api::copresence::Operation> >& operations, | |
182 const std::string& app_id, | |
183 SubscriptionToAppMap* apps_by_subscription_id, | |
184 copresence::ReportRequest* request) { | |
185 for (size_t i = 0; i < operations.size(); ++i) { | |
186 linked_ptr<api::copresence::Operation> op = operations[i]; | |
187 DCHECK(op.get()); | |
188 switch (op->type) { | |
189 case api::copresence::OPERATION_TYPE_PUBLISH: | |
190 if (!op->publish || | |
191 !AddPublishToRequest(app_id, *(op->publish), request)) | |
192 return false; | |
193 break; | |
194 | |
195 case api::copresence::OPERATION_TYPE_SUBSCRIBE: | |
196 if (!op->subscribe || | |
197 !AddSubscribeToRequest( | |
198 app_id, *(op->subscribe), apps_by_subscription_id, request)) | |
199 return false; | |
200 break; | |
201 | |
202 case api::copresence::OPERATION_TYPE_UNPUBLISH: | |
203 if (!op->unpublish || | |
204 !AddUnpublishToRequest(op->unpublish->unpublish_id, request)) | |
205 return false; | |
206 break; | |
207 | |
208 case api::copresence::OPERATION_TYPE_UNSUBSCRIBE: | |
209 if (!op->unsubscribe || | |
210 !AddUnsubscribeToRequest(app_id, | |
211 op->unsubscribe->unsubscribe_id, | |
212 apps_by_subscription_id, | |
213 request)) | |
214 return false; | |
215 break; | |
216 | |
217 case api::copresence::OPERATION_TYPE_NONE: | |
218 default: | |
219 LOG(ERROR) << "Unrecognized operation type " << op->type; | |
220 } | |
221 } | |
222 return true; | |
223 } | |
224 | |
225 } // namespace extensions | |
OLD | NEW |