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

Side by Side Diff: chrome/browser/extensions/api/copresence/copresence_translations.cc

Issue 444513005: Add the Copresence API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 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 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;
Daniel Erat 2014/08/06 22:59:37 use parentheses for nested ternary operators
rkc 2014/08/07 02:18:33 Done.
22 }
23
24 copresence::BroadcastScanConfiguration TranslateStrategy(
25 extensions::api::copresence::Strategy* strategy) {
Daniel Erat 2014/08/06 22:59:37 const reference?
rkc 2014/08/07 02:18:33 copresence::BroadcastScanConfiguration is an enum,
rkc 2014/08/07 02:42:36 Oh, for some weird reason I thought you were sayin
26 bool only_broadcast = false;
27 bool only_scan = false;
Daniel Erat 2014/08/06 22:59:37 this isn't limited to this change, but the word "o
rkc 2014/08/07 02:18:33 There is no good way to restrict an app to setting
28
29 if (strategy->only_broadcast && *strategy->only_broadcast)
Daniel Erat 2014/08/06 22:59:37 how about just combining these with the earlier st
rkc 2014/08/07 02:18:33 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)
Daniel Erat 2014/08/06 22:59:37 is there any way to report what the problem is to
rkc 2014/08/07 02:18:33 Yes, we have a bug open tracking better error repo
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 SubscriptionToAppMap::iterator previous_subscription =
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 SubscriptionToAppMap::iterator subscription =
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
189 // Verify our object has exactly one operation.
190 if (static_cast<int>(op->publish != NULL) +
191 static_cast<int>(op->subscribe != NULL) +
192 static_cast<int>(op->unpublish != NULL) +
193 static_cast<int>(op->unsubscribe != NULL) != 1) {
194 return false;
195 }
196
197 if (op->publish) {
198 if (!AddPublishToRequest(app_id, *(op->publish), request))
199 return false;
200 }
Daniel Erat 2014/08/06 22:59:37 nit: since you're already checking earlier that on
rkc 2014/08/07 02:18:33 Done.
201
202 if (op->subscribe) {
203 if (!AddSubscribeToRequest(
204 app_id, *(op->subscribe), apps_by_subscription_id, request))
205 return false;
206 }
207
208 if (op->unpublish) {
209 if (!AddUnpublishToRequest(op->unpublish->unpublish_id, request))
210 return false;
211 }
212
213 if (op->unsubscribe) {
214 if (!AddUnsubscribeToRequest(app_id,
215 op->unsubscribe->unsubscribe_id,
216 apps_by_subscription_id,
217 request))
218 return false;
219 }
220 }
221
222 return true;
223 }
224
225 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698