Switch from a DataPipe per DecoderBuffer to a single one.
[chromium-blink-merge.git] / components / copresence / handlers / gcm_handler_impl.cc
blobccf720310f4d544d6279aa05fa2b42b6d5080a2d
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.
5 #include "components/copresence/handlers/gcm_handler_impl.h"
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"
15 using gcm::GCMClient;
17 namespace {
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);
25 // Add padding if needed.
26 while (data.size() % 4)
27 data.push_back('=');
29 // Decode.
30 return base::Base64Decode(data, out);
33 } // namespace
35 namespace copresence {
37 const char GCMHandlerImpl::kCopresenceAppId[] =
38 "com.google.android.gms.location.copresence";
39 const char GCMHandlerImpl::kCopresenceSenderId[] = "745476177629";
40 const char GCMHandlerImpl::kGcmMessageKey[] = "PUSH_MESSAGE";
43 // Public functions.
45 GCMHandlerImpl::GCMHandlerImpl(gcm::GCMDriver* gcm_driver,
46 DirectiveHandler* directive_handler)
47 : driver_(gcm_driver),
48 directive_handler_(directive_handler),
49 registration_callback_(base::Bind(&GCMHandlerImpl::RegistrationComplete,
50 base::Unretained(this))) {
51 DCHECK(driver_);
52 DCHECK(directive_handler_);
54 driver_->AddAppHandler(kCopresenceAppId, this);
55 driver_->Register(kCopresenceAppId,
56 std::vector<std::string>(1, kCopresenceSenderId),
57 registration_callback_.callback());
60 GCMHandlerImpl::~GCMHandlerImpl() {
61 if (driver_)
62 driver_->RemoveAppHandler(kCopresenceAppId);
65 void GCMHandlerImpl::GetGcmId(const RegistrationCallback& callback) {
66 if (gcm_id_.empty()) {
67 pending_id_requests_.push_back(callback);
68 } else {
69 callback.Run(gcm_id_);
73 void GCMHandlerImpl::ShutdownHandler() {
74 // The GCMDriver is going away. Make sure we don't try to contact it.
75 driver_ = nullptr;
78 void GCMHandlerImpl::OnMessage(const std::string& app_id,
79 const GCMClient::IncomingMessage& message) {
80 DCHECK_EQ(kCopresenceAppId, app_id);
81 DVLOG(2) << "Incoming GCM message";
83 const auto& content = message.data.find(kGcmMessageKey);
84 if (content == message.data.end()) {
85 LOG(ERROR) << "GCM message missing data key";
86 return;
89 std::string serialized_message;
90 if (!Base64Decode(content->second, &serialized_message)) {
91 LOG(ERROR) << "Couldn't decode GCM message";
92 return;
95 PushMessage push_message;
96 if (!push_message.ParseFromString(serialized_message)) {
97 LOG(ERROR) << "GCM message contained invalid proto";
98 return;
101 if (push_message.type() != PushMessage::REPORT) {
102 DVLOG(2) << "Discarding non-report GCM message";
103 return;
106 DVLOG(3) << "Processing " << push_message.report().directive_size()
107 << " directive(s) from GCM message";
108 for (const Directive& directive : push_message.report().directive())
109 directive_handler_->AddDirective(directive);
111 int message_count = push_message.report().subscribed_message_size();
112 LOG_IF(WARNING, message_count > 0)
113 << "Discarding " << message_count << " copresence messages sent via GCM";
116 void GCMHandlerImpl::OnMessagesDeleted(const std::string& app_id) {
117 DCHECK_EQ(kCopresenceAppId, app_id);
118 DVLOG(2) << "GCM message overflow reported";
121 void GCMHandlerImpl::OnSendError(
122 const std::string& /* app_id */,
123 const GCMClient::SendErrorDetails& /* send_error_details */) {
124 NOTREACHED() << "Copresence clients should not be sending GCM messages";
127 void GCMHandlerImpl::OnSendAcknowledged(const std::string& /* app_id */,
128 const std::string& /* message_id */) {
129 NOTREACHED() << "Copresence clients should not be sending GCM messages";
132 bool GCMHandlerImpl::CanHandle(const std::string& app_id) const {
133 return app_id == kCopresenceAppId;
137 // Private functions.
139 void GCMHandlerImpl::RegistrationComplete(const std::string& registration_id,
140 GCMClient::Result result) {
141 if (result == GCMClient::SUCCESS) {
142 DVLOG(2) << "GCM registration successful. ID: " << registration_id;
143 gcm_id_ = registration_id;
144 } else {
145 LOG(ERROR) << "GCM registration failed with error " << result;
148 for (const RegistrationCallback& callback : pending_id_requests_) {
149 callback.Run(result == GCMClient::SUCCESS ?
150 registration_id : std::string());
152 pending_id_requests_.clear();
155 } // namespace copresence