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 using copresence::AUDIO_CONFIGURATION_AUDIBLE; | |
13 using copresence::AUDIO_CONFIGURATION_UNKNOWN; | |
14 using copresence::BROADCAST_AND_SCAN; | |
15 using copresence::BROADCAST_ONLY; | |
16 using copresence::BROADCAST_SCAN_CONFIGURATION_UNKNOWN; | |
17 using copresence::BroadcastScanConfiguration; | |
18 using copresence::ReportRequest; | |
19 using copresence::SCAN_ONLY; | |
20 using copresence::TokenExchangeStrategy; | |
21 | |
22 using extensions::api::copresence::Strategy; | |
23 | |
24 namespace { | |
25 | |
26 const int kDefaultTimeToLiveMs = 5 * 60 * 1000; // 5 minutes. | |
27 const int kMaxTimeToLiveMs = 24 * 60 * 60 * 1000; // 24 hours. | |
28 | |
29 // Checks and returns the ttl provided by the user. If invalid, returns -1. | |
30 int SanitizeTtl(int* user_ttl) { | |
31 return !user_ttl | |
32 ? kDefaultTimeToLiveMs | |
33 : (*user_ttl <= 0 || *user_ttl > kMaxTimeToLiveMs ? -1 : *user_ttl); | |
34 } | |
35 | |
36 BroadcastScanConfiguration TranslateStrategy(const Strategy& strategy) { | |
37 bool only_broadcast = strategy.only_broadcast && *strategy.only_broadcast; | |
38 bool only_scan = strategy.only_scan && *strategy.only_scan; | |
39 | |
40 if (only_broadcast && only_scan) | |
41 return BROADCAST_AND_SCAN; | |
42 if (only_broadcast) | |
43 return BROADCAST_ONLY; | |
44 if (only_scan) | |
45 return SCAN_ONLY; | |
46 | |
47 return BROADCAST_SCAN_CONFIGURATION_UNKNOWN; | |
48 } | |
49 | |
50 // The strategy may be null (unspecified), so we pass it as a pointer. | |
51 void SetTokenExchangeStrategy(const Strategy* strategy, | |
52 BroadcastScanConfiguration default_config, | |
53 TokenExchangeStrategy* strategy_proto) { | |
54 if (strategy) { | |
55 BroadcastScanConfiguration config; | |
56 if (strategy->low_power && *(strategy->low_power)) { | |
57 config = BROADCAST_SCAN_CONFIGURATION_UNKNOWN; | |
58 } else { | |
59 config = TranslateStrategy(*strategy); | |
60 if (config == BROADCAST_SCAN_CONFIGURATION_UNKNOWN) | |
61 config = default_config; | |
62 } | |
63 | |
64 strategy_proto->set_broadcast_scan_configuration(config); | |
65 strategy_proto->set_audio_configuration( | |
66 strategy->audible && *strategy->audible ? AUDIO_CONFIGURATION_AUDIBLE | |
67 : AUDIO_CONFIGURATION_UNKNOWN); | |
68 } else { | |
69 strategy_proto->set_broadcast_scan_configuration(default_config); | |
70 strategy_proto->set_audio_configuration(AUDIO_CONFIGURATION_UNKNOWN); | |
71 } | |
72 } | |
73 | |
74 } // namespace | |
75 | |
76 namespace extensions { | |
77 | |
78 using api::copresence::Operation; | |
79 | |
80 // Adds a publish operation to the report request. Returns false if the | |
81 // publish operation was invalid. | |
82 bool AddPublishToRequest(const std::string& app_id, | |
83 const api::copresence::PublishOperation& publish, | |
84 ReportRequest* request) { | |
85 copresence::PublishedMessage* publish_proto = | |
86 request->mutable_manage_messages_request()->add_message_to_publish(); | |
87 publish_proto->mutable_access_policy()->mutable_acl()->set_acl_type( | |
88 copresence::NO_ACL_CHECK); | |
89 publish_proto->set_id(publish.id); | |
90 publish_proto->mutable_message()->mutable_type()->set_type( | |
91 publish.message.type); | |
92 publish_proto->mutable_message()->set_payload(publish.message.payload.data(), | |
93 publish.message.payload.size()); | |
94 | |
95 int ttl = SanitizeTtl(publish.time_to_live_millis.get()); | |
96 if (ttl < 0) | |
97 return false; | |
98 publish_proto->mutable_access_policy()->set_ttl_millis(ttl); | |
99 | |
100 SetTokenExchangeStrategy(publish.strategies.get(), | |
101 BROADCAST_ONLY, | |
102 publish_proto->mutable_token_exchange_strategy()); | |
103 | |
104 DVLOG(2) << "Publishing message of type " << publish.message.type << ":\n" | |
105 << std::string(publish.message.payload.begin(), | |
106 publish.message.payload.end()); | |
107 // TODO(ckehoe): Validate that required fields are non-empty, etc. | |
108 return true; | |
109 } | |
110 | |
111 // Adds an unpublish operation to the report request. Returns false if the | |
112 // publish id was invalid. | |
113 bool AddUnpublishToRequest(const std::string& publish_id, | |
114 ReportRequest* request) { | |
115 if (publish_id.empty()) | |
116 return false; | |
117 | |
118 request->mutable_manage_messages_request()->add_id_to_unpublish(publish_id); | |
119 DVLOG(2) << "Unpublishing message \"" << publish_id << "\""; | |
120 return true; | |
121 } | |
122 | |
123 // Adds a subscribe operation to the report request. Returns false if the | |
124 // subscription operation was invalid. | |
125 bool AddSubscribeToRequest( | |
126 const std::string& app_id, | |
127 const api::copresence::SubscribeOperation& subscription, | |
128 SubscriptionToAppMap* apps_by_subscription_id, | |
129 ReportRequest* request) { | |
130 // Associate the subscription id with the app id. | |
131 SubscriptionToAppMap::iterator previous_subscription = | |
132 apps_by_subscription_id->find(subscription.id); | |
133 if (previous_subscription == apps_by_subscription_id->end()) { | |
134 (*apps_by_subscription_id)[subscription.id] = app_id; | |
135 } else if (previous_subscription->second == app_id) { | |
136 VLOG(2) << "Overwriting subscription id \"" << subscription.id | |
137 << "\" for app \"" << app_id << "\""; | |
138 } else { | |
139 // A conflicting association exists already. | |
140 VLOG(1) << "Subscription id \"" << subscription.id | |
141 << "\" used by two apps: \"" << previous_subscription->second | |
142 << "\" and \"" << app_id << "\""; | |
143 return false; | |
144 } | |
145 | |
146 // Convert from IDL to server subscription format. | |
147 copresence::Subscription* subscription_proto = | |
148 request->mutable_manage_subscriptions_request()->add_subscription(); | |
149 subscription_proto->set_id(subscription.id); | |
150 int ttl = SanitizeTtl(subscription.time_to_live_millis.get()); | |
151 if (ttl < 0) | |
152 return false; | |
153 subscription_proto->set_ttl_millis(ttl); | |
154 | |
155 subscription_proto->mutable_message_type()->set_type( | |
156 subscription.filter.type); | |
157 | |
158 SetTokenExchangeStrategy( | |
159 subscription.strategies.get(), | |
160 SCAN_ONLY, | |
161 subscription_proto->mutable_token_exchange_strategy()); | |
162 | |
163 DVLOG(2) << "Subscribing for messages of type " << subscription.filter.type; | |
164 // TODO(ckehoe): Validate that required fields are non-empty, etc. | |
165 return true; | |
166 } | |
167 | |
168 // Adds an unpublish operation to the report request. Returns false if the | |
169 // subscription id was invalid. | |
170 bool AddUnsubscribeToRequest(const std::string& app_id, | |
171 const std::string& subscription_id, | |
172 SubscriptionToAppMap* apps_by_subscription_id, | |
173 ReportRequest* request) { | |
174 if (subscription_id.empty()) | |
175 return false; | |
176 | |
177 // Check that this subscription id belongs to this app. | |
178 SubscriptionToAppMap::iterator subscription = | |
179 apps_by_subscription_id->find(subscription_id); | |
180 if (subscription == apps_by_subscription_id->end()) { | |
181 LOG(ERROR) << "No such subscription \"" << subscription_id | |
182 << "\". Cannot unsubscribe."; | |
183 return false; | |
184 } else if (subscription->second != app_id) { | |
185 LOG(ERROR) << "Subscription \"" << subscription_id | |
186 << "\" does not belong to app \"" << app_id | |
187 << "\". Cannot unsubscribe."; | |
188 return false; | |
189 } else { | |
190 apps_by_subscription_id->erase(subscription); | |
191 } | |
192 | |
193 request->mutable_manage_subscriptions_request()->add_id_to_unsubscribe( | |
194 subscription_id); | |
195 DVLOG(2) << "Cancelling subscription \"" << subscription_id << "\" for app \"" | |
196 << app_id << "\""; | |
197 return true; | |
198 } | |
199 | |
200 bool PrepareReportRequestProto(const std::vector<Operation>& operations, | |
201 const std::string& app_id, | |
202 SubscriptionToAppMap* apps_by_subscription_id, | |
203 ReportRequest* request) { | |
204 for (const Operation& op : operations) { | |
205 // Verify our object has exactly one operation. | |
206 if (static_cast<int>(op.publish != nullptr) + | |
207 static_cast<int>(op.subscribe != nullptr) + | |
208 static_cast<int>(op.unpublish != nullptr) + | |
209 static_cast<int>(op.unsubscribe != nullptr) != | |
210 1) { | |
211 return false; | |
212 } | |
213 | |
214 if (op.publish) { | |
215 if (!AddPublishToRequest(app_id, *(op.publish), request)) | |
216 return false; | |
217 } else if (op.subscribe) { | |
218 if (!AddSubscribeToRequest(app_id, *(op.subscribe), | |
219 apps_by_subscription_id, request)) | |
220 return false; | |
221 } else if (op.unpublish) { | |
222 if (!AddUnpublishToRequest(op.unpublish->unpublish_id, request)) | |
223 return false; | |
224 } else { // if (op.unsubscribe) | |
225 if (!AddUnsubscribeToRequest(app_id, op.unsubscribe->unsubscribe_id, | |
226 apps_by_subscription_id, request)) | |
227 return false; | |
228 } | |
229 } | |
230 | |
231 return true; | |
232 } | |
233 | |
234 } // namespace extensions | |
OLD | NEW |