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

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

Issue 710513004: Adding GCM support to the copresence component. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adding a unit test 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[] = "copresence";
fgorski 2014/11/07 17:40:43 is there a chance you use a longer app ID? What is
Charlie 2014/11/07 19:42:29 Done.
38 const char GCMHandler::kCopresenceSenderId[] = "745476177629";
39 const char GCMHandler::kGcmMessageKey[] = "PUSH_MESSAGE";
40
41
42 // Public functions.
43
44 GCMHandler::GCMHandler(gcm::GCMDriver* gcm_driver,
45 DirectiveHandler* directive_handler)
46 : driver_(gcm_driver),
fgorski 2014/11/07 17:40:44 4 spaces before :
Charlie 2014/11/07 19:42:30 Done.
47 directive_handler_(directive_handler) {
48 DCHECK(driver_);
49 DCHECK(directive_handler_);
50
51 driver_->AddAppHandler(kCopresenceAppId, this);
52
53 // TODO(ckehoe): Since the GCMHandler doesn't check if this callback is null
54 // before running it, we just have to hope that registration doesn't complete
55 // while we are shutting down. Even a CancelableCallback would just get
56 // nulled out and still crash.
rkc 2014/11/07 18:39:05 This is not true. A cancelable callback will inval
Charlie 2014/11/07 19:42:30 Ah, seems this is a difference in behavior between
57 //
58 // Additionally, CancelableCallback has a bug for the 2-argument case.
59 // See https://codereview.chromium.org/565123002
60 driver_->Register(kCopresenceAppId,
61 std::vector<std::string>(1, kCopresenceSenderId),
62 base::Bind(&GCMHandler::RegistrationComplete,
63 base::Unretained(this)));
fgorski 2014/11/07 17:40:44 use a WeakPtrFactory, that way the comment above i
Charlie 2014/11/07 19:42:29 Done.
64 }
65
66 GCMHandler::~GCMHandler() {
67 if (driver_)
68 driver_->RemoveAppHandler(kCopresenceAppId);
69 }
70
71 void GCMHandler::RegistrationComplete(const std::string& registration_id,
72 GCMClient::Result result) {
73 if (result == GCMClient::SUCCESS) {
74 DVLOG(2) << "GCM registration successful. ID: " << registration_id;
75 gcm_id_ = registration_id;
76 } else {
77 LOG(ERROR) << "GCM registration failed with error " << result;
78 }
79
80 for (const RegistrationCallback& callback : pending_id_requests_) {
81 callback.Run(result == GCMClient::SUCCESS ?
82 registration_id : std::string());
83 }
84 }
85
86 void GCMHandler::GetGcmId(const RegistrationCallback& callback) {
87 if (gcm_id_.empty()) {
88 pending_id_requests_.push_back(callback);
89 } else {
90 callback.Run(gcm_id_);
91 }
92 }
93
94 void GCMHandler::ShutdownHandler() {
95 // The GCMDriver is going away. Make sure we don't try to contact it.
96 driver_ = nullptr;
97 }
98
99 void GCMHandler::OnMessage(const std::string& app_id,
100 const GCMClient::IncomingMessage& message) {
101 DCHECK_EQ(kCopresenceAppId, app_id);
102 DVLOG(2) << "Incoming GCM message";
103
104 const auto& content = message.data.find(kGcmMessageKey);
105 if (content == message.data.end()) {
106 LOG(ERROR) << "GCM message missing data key";
107 return;
108 }
109
110 std::string serialized_message;
111 if (!Base64Decode(content->second, &serialized_message)) {
112 LOG(ERROR) << "Couldn't decode GCM message";
113 return;
114 }
115
116 PushMessage push_message;
117 if (!push_message.ParseFromString(serialized_message)) {
118 LOG(ERROR) << "GCM message contained invalid proto";
119 return;
120 }
121
122 if (push_message.type() != PushMessage::REPORT) {
123 DVLOG(2) << "Discarding non-report GCM message";
124 return;
125 }
126
127 DVLOG(3) << "Processing " << push_message.report().directive_size()
128 << " directive(s) from GCM message";
129 for (const Directive& directive : push_message.report().directive())
130 directive_handler_->AddDirective(directive);
131
132 int message_count = push_message.report().subscribed_message_size();
133 LOG_IF(WARNING, message_count > 0)
134 << "Discarding " << message_count << " copresence messages sent via GCM";
135 }
136
137 void GCMHandler::OnMessagesDeleted(const std::string& app_id) {
138 DCHECK_EQ(kCopresenceAppId, app_id);
139 LOG(ERROR) << "GCM message overflow reported";
fgorski 2014/11/07 17:40:44 This is definitely not an error. If the device is
Charlie 2014/11/07 19:42:29 Fixed. I think we'd like to keep the other errors,
140 }
141
142 void GCMHandler::OnSendError(
143 const std::string& app_id,
144 const GCMClient::SendErrorDetails& send_error_details) {
145 NOTREACHED() << "Copresence clients should not be sending GCM messages";
146 }
147
148 void GCMHandler::OnSendAcknowledged(const std::string& app_id,
149 const std::string& message_id) {
150 NOTREACHED() << "Copresence clients should not be sending GCM messages";
151 }
152
153 bool GCMHandler::CanHandle(const std::string& app_id) const {
154 return app_id == kCopresenceAppId;
155 }
156
157 } // namespace copresence
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698