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

Side by Side Diff: components/copresence/handlers/gcm_handler.cc

Issue 712833002: Undoing revert, cl is not the cause of gcm crash on canary. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@2214
Patch Set: Created 6 years, 1 month 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
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 "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 AsWeakPtr())) {
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 registration_callback_);
58 }
59
60 GCMHandler::~GCMHandler() {
61 if (driver_)
62 driver_->RemoveAppHandler(kCopresenceAppId);
63 }
64
65 void GCMHandler::GetGcmId(const RegistrationCallback& callback) {
66 if (gcm_id_.empty()) {
67 pending_id_requests_.push_back(callback);
68 } else {
69 callback.Run(gcm_id_);
70 }
71 }
72
73 void GCMHandler::ShutdownHandler() {
74 // The GCMDriver is going away. Make sure we don't try to contact it.
75 driver_ = nullptr;
76 }
77
78 void GCMHandler::OnMessage(const std::string& app_id,
79 const GCMClient::IncomingMessage& message) {
80 DCHECK_EQ(kCopresenceAppId, app_id);
81 DVLOG(2) << "Incoming GCM message";
82
83 const auto& content = message.data.find(kGcmMessageKey);
84 if (content == message.data.end()) {
85 LOG(ERROR) << "GCM message missing data key";
86 return;
87 }
88
89 std::string serialized_message;
90 if (!Base64Decode(content->second, &serialized_message)) {
91 LOG(ERROR) << "Couldn't decode GCM message";
92 return;
93 }
94
95 PushMessage push_message;
96 if (!push_message.ParseFromString(serialized_message)) {
97 LOG(ERROR) << "GCM message contained invalid proto";
98 return;
99 }
100
101 if (push_message.type() != PushMessage::REPORT) {
102 DVLOG(2) << "Discarding non-report GCM message";
103 return;
104 }
105
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);
110
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";
114 }
115
116 void GCMHandler::OnMessagesDeleted(const std::string& app_id) {
117 DCHECK_EQ(kCopresenceAppId, app_id);
118 DVLOG(2) << "GCM message overflow reported";
119 }
120
121 void GCMHandler::OnSendError(
122 const std::string& /* app_id */,
123 const GCMClient::SendErrorDetails& /* send_error_details */) {
124 NOTREACHED() << "Copresence clients should not be sending GCM messages";
125 }
126
127 void GCMHandler::OnSendAcknowledged(const std::string& /* app_id */,
128 const std::string& /* message_id */) {
129 NOTREACHED() << "Copresence clients should not be sending GCM messages";
130 }
131
132 bool GCMHandler::CanHandle(const std::string& app_id) const {
133 return app_id == kCopresenceAppId;
134 }
135
136
137 // Private functions.
138
139 void GCMHandler::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;
146 }
147
148 for (const RegistrationCallback& callback : pending_id_requests_) {
149 callback.Run(result == GCMClient::SUCCESS ?
150 registration_id : std::string());
151 }
152 pending_id_requests_.clear();
153 }
154
155 } // namespace copresence
OLDNEW
« no previous file with comments | « components/copresence/handlers/gcm_handler.h ('k') | components/copresence/handlers/gcm_handler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698