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