Chromium Code Reviews| 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 "components/copresence/handlers/gcm_handler.h" | |
| 6 | |
| 7 #include "base/base64.h" | |
| 8 #include "base/bind.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/strings/string_util.h" | |
| 11 #include "components/copresence/handlers/directive_handler.h" | |
| 12 #include "components/copresence/proto/push_message.pb.h" | |
| 13 #include "components/gcm_driver/gcm_driver.h" | |
| 14 | |
| 15 using gcm::GCMClient; | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // TODO(ckehoe): Move this to a common library. | |
| 20 bool Base64Decode(std::string data, std::string* out) { | |
| 21 // Convert from URL-safe. | |
| 22 base::ReplaceChars(data, "-", "+", &data); | |
| 23 base::ReplaceChars(data, "_", "/", &data); | |
| 24 | |
| 25 // Add padding if needed. | |
| 26 while (data.size() % 4) | |
| 27 data.push_back('='); | |
| 28 | |
| 29 // Decode. | |
| 30 return base::Base64Decode(data, out); | |
| 31 } | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 35 namespace copresence { | |
| 36 | |
| 37 const char GCMHandler::kCopresenceAppId[] = | |
| 38 "com.google.android.gms.location.copresence"; | |
| 39 const char GCMHandler::kCopresenceSenderId[] = "745476177629"; | |
| 40 const char GCMHandler::kGcmMessageKey[] = "PUSH_MESSAGE"; | |
| 41 | |
| 42 | |
| 43 // Public functions. | |
| 44 | |
| 45 GCMHandler::GCMHandler(gcm::GCMDriver* gcm_driver, | |
| 46 DirectiveHandler* directive_handler) | |
| 47 : driver_(gcm_driver), | |
| 48 directive_handler_(directive_handler), | |
| 49 registration_callback_(base::Bind(&GCMHandler::RegistrationComplete, | |
| 50 base::Unretained(this))) { | |
| 51 DCHECK(driver_); | |
| 52 DCHECK(directive_handler_); | |
| 53 | |
| 54 driver_->AddAppHandler(kCopresenceAppId, this); | |
| 55 driver_->Register(kCopresenceAppId, | |
| 56 std::vector<std::string>(1, kCopresenceSenderId), | |
| 57 // We cancel this callback on destruction. | |
| 58 registration_callback_.callback()); | |
| 59 } | |
| 60 | |
| 61 GCMHandler::~GCMHandler() { | |
| 62 if (driver_) | |
| 63 driver_->RemoveAppHandler(kCopresenceAppId); | |
| 64 registration_callback_.Cancel(); | |
|
rkc
2014/11/07 20:24:15
Looking at the cancelable callback code, I *think*
Charlie
2014/11/07 20:50:12
Done.
| |
| 65 } | |
| 66 | |
| 67 void GCMHandler::GetGcmId(const RegistrationCallback& callback) { | |
| 68 if (gcm_id_.empty()) { | |
| 69 pending_id_requests_.push_back(callback); | |
| 70 } else { | |
| 71 callback.Run(gcm_id_); | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 void GCMHandler::ShutdownHandler() { | |
| 76 // The GCMDriver is going away. Make sure we don't try to contact it. | |
| 77 driver_ = nullptr; | |
| 78 } | |
| 79 | |
| 80 void GCMHandler::OnMessage(const std::string& app_id, | |
| 81 const GCMClient::IncomingMessage& message) { | |
| 82 DCHECK_EQ(kCopresenceAppId, app_id); | |
| 83 DVLOG(2) << "Incoming GCM message"; | |
| 84 | |
| 85 const auto& content = message.data.find(kGcmMessageKey); | |
| 86 if (content == message.data.end()) { | |
| 87 LOG(ERROR) << "GCM message missing data key"; | |
| 88 return; | |
| 89 } | |
| 90 | |
| 91 std::string serialized_message; | |
| 92 if (!Base64Decode(content->second, &serialized_message)) { | |
| 93 LOG(ERROR) << "Couldn't decode GCM message"; | |
| 94 return; | |
| 95 } | |
| 96 | |
| 97 PushMessage push_message; | |
| 98 if (!push_message.ParseFromString(serialized_message)) { | |
| 99 LOG(ERROR) << "GCM message contained invalid proto"; | |
| 100 return; | |
| 101 } | |
| 102 | |
| 103 if (push_message.type() != PushMessage::REPORT) { | |
| 104 DVLOG(2) << "Discarding non-report GCM message"; | |
| 105 return; | |
| 106 } | |
| 107 | |
| 108 DVLOG(3) << "Processing " << push_message.report().directive_size() | |
| 109 << " directive(s) from GCM message"; | |
| 110 for (const Directive& directive : push_message.report().directive()) | |
| 111 directive_handler_->AddDirective(directive); | |
| 112 | |
| 113 int message_count = push_message.report().subscribed_message_size(); | |
| 114 LOG_IF(WARNING, message_count > 0) | |
| 115 << "Discarding " << message_count << " copresence messages sent via GCM"; | |
| 116 } | |
| 117 | |
| 118 void GCMHandler::OnMessagesDeleted(const std::string& app_id) { | |
| 119 DCHECK_EQ(kCopresenceAppId, app_id); | |
|
rkc
2014/11/07 20:24:15
app_id will become an unused argument on release b
Charlie
2014/11/07 20:50:12
I think this is ok, since it's an argument as oppo
| |
| 120 DVLOG(2) << "GCM message overflow reported"; | |
| 121 } | |
| 122 | |
| 123 void GCMHandler::OnSendError( | |
| 124 const std::string& app_id, | |
| 125 const GCMClient::SendErrorDetails& send_error_details) { | |
|
rkc
2014/11/07 20:24:15
/* app_id */
/* send_error_details */
here and all
Charlie
2014/11/07 20:50:11
Done.
| |
| 126 NOTREACHED() << "Copresence clients should not be sending GCM messages"; | |
| 127 } | |
| 128 | |
| 129 void GCMHandler::OnSendAcknowledged(const std::string& app_id, | |
| 130 const std::string& message_id) { | |
| 131 NOTREACHED() << "Copresence clients should not be sending GCM messages"; | |
| 132 } | |
| 133 | |
| 134 bool GCMHandler::CanHandle(const std::string& app_id) const { | |
| 135 return app_id == kCopresenceAppId; | |
| 136 } | |
| 137 | |
| 138 | |
| 139 // Private functions. | |
| 140 | |
| 141 void GCMHandler::RegistrationComplete(const std::string& registration_id, | |
| 142 GCMClient::Result result) { | |
| 143 if (result == GCMClient::SUCCESS) { | |
| 144 DVLOG(2) << "GCM registration successful. ID: " << registration_id; | |
| 145 gcm_id_ = registration_id; | |
| 146 } else { | |
| 147 LOG(ERROR) << "GCM registration failed with error " << result; | |
| 148 } | |
| 149 | |
| 150 for (const RegistrationCallback& callback : pending_id_requests_) { | |
| 151 callback.Run(result == GCMClient::SUCCESS ? | |
| 152 registration_id : std::string()); | |
| 153 } | |
| 154 pending_id_requests_.clear(); | |
| 155 } | |
| 156 | |
| 157 } // namespace copresence | |
| OLD | NEW |