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

Side by Side Diff: google_apis/gcm/gcm_client_impl.cc

Issue 215363007: [GCM] Adding basic G-services handling (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 months 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "google_apis/gcm/gcm_client_impl.h" 5 #include "google_apis/gcm/gcm_client_impl.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
13 #include "base/sequenced_task_runner.h" 13 #include "base/sequenced_task_runner.h"
14 #include "base/strings/string_number_conversions.h"
14 #include "base/time/default_clock.h" 15 #include "base/time/default_clock.h"
15 #include "google_apis/gcm/base/mcs_message.h" 16 #include "google_apis/gcm/base/mcs_message.h"
16 #include "google_apis/gcm/base/mcs_util.h" 17 #include "google_apis/gcm/base/mcs_util.h"
17 #include "google_apis/gcm/engine/checkin_request.h" 18 #include "google_apis/gcm/engine/checkin_request.h"
18 #include "google_apis/gcm/engine/connection_factory_impl.h" 19 #include "google_apis/gcm/engine/connection_factory_impl.h"
19 #include "google_apis/gcm/engine/gcm_store_impl.h" 20 #include "google_apis/gcm/engine/gcm_store_impl.h"
20 #include "google_apis/gcm/engine/mcs_client.h" 21 #include "google_apis/gcm/engine/mcs_client.h"
21 #include "google_apis/gcm/protocol/mcs.pb.h" 22 #include "google_apis/gcm/protocol/mcs.pb.h"
22 #include "net/http/http_network_session.h" 23 #include "net/http/http_network_session.h"
23 #include "net/url_request/url_request_context.h" 24 #include "net/url_request/url_request_context.h"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 SEND_ERROR, // Error sending a message. 68 SEND_ERROR, // Error sending a message.
68 }; 69 };
69 70
70 // MCS endpoints. SSL Key pinning is done automatically due to the *.google.com 71 // MCS endpoints. SSL Key pinning is done automatically due to the *.google.com
71 // pinning rule. 72 // pinning rule.
72 // Note: modifying the endpoints will affect the ability to compare the 73 // Note: modifying the endpoints will affect the ability to compare the
73 // GCM.CurrentEnpoint histogram across versions. 74 // GCM.CurrentEnpoint histogram across versions.
74 const char kMCSEndpointMain[] = "https://mtalk.google.com:5228"; 75 const char kMCSEndpointMain[] = "https://mtalk.google.com:5228";
75 const char kMCSEndpointFallback[] = "https://mtalk.google.com:443"; 76 const char kMCSEndpointFallback[] = "https://mtalk.google.com:443";
76 77
78 const char kCheckinIntervalKey[] = "checkin_interval";
77 const int kMaxRegistrationRetries = 5; 79 const int kMaxRegistrationRetries = 5;
78 const char kMessageTypeDataMessage[] = "gcm"; 80 const char kMessageTypeDataMessage[] = "gcm";
79 const char kMessageTypeDeletedMessagesKey[] = "deleted_messages"; 81 const char kMessageTypeDeletedMessagesKey[] = "deleted_messages";
80 const char kMessageTypeKey[] = "message_type"; 82 const char kMessageTypeKey[] = "message_type";
81 const char kMessageTypeSendErrorKey[] = "send_error"; 83 const char kMessageTypeSendErrorKey[] = "send_error";
82 const char kSendErrorMessageIdKey[] = "google.message_id"; 84 const char kSendErrorMessageIdKey[] = "google.message_id";
83 const char kSendMessageFromValue[] = "gcm@chrome.com"; 85 const char kSendMessageFromValue[] = "gcm@chrome.com";
84 const int64 kDefaultUserSerialNumber = 0LL; 86 const int64 kDefaultUserSerialNumber = 0LL;
85 87
86 GCMClient::Result ToGCMClientResult(MCSClient::MessageSendStatus status) { 88 GCMClient::Result ToGCMClientResult(MCSClient::MessageSendStatus status) {
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 207
206 if (!result->success) { 208 if (!result->success) {
207 ResetState(); 209 ResetState();
208 return; 210 return;
209 } 211 }
210 212
211 registrations_ = result->registrations; 213 registrations_ = result->registrations;
212 214
213 device_checkin_info_.android_id = result->device_android_id; 215 device_checkin_info_.android_id = result->device_android_id;
214 device_checkin_info_.secret = result->device_security_token; 216 device_checkin_info_.secret = result->device_security_token;
217 g_services_settings_ = result->g_services_settings;
215 InitializeMCSClient(result.Pass()); 218 InitializeMCSClient(result.Pass());
216 if (!device_checkin_info_.IsValid()) { 219 if (!device_checkin_info_.IsValid()) {
217 device_checkin_info_.Reset(); 220 device_checkin_info_.Reset();
218 state_ = INITIAL_DEVICE_CHECKIN; 221 state_ = INITIAL_DEVICE_CHECKIN;
219 StartCheckin(device_checkin_info_); 222 StartCheckin(device_checkin_info_);
220 return; 223 return;
224 } else {
fgorski 2014/03/28 14:30:51 remove
fgorski 2014/03/29 01:09:32 Done.
221 } 225 }
222 226
223 OnReady(); 227 OnReady();
224 } 228 }
225 229
226 void GCMClientImpl::InitializeMCSClient( 230 void GCMClientImpl::InitializeMCSClient(
227 scoped_ptr<GCMStore::LoadResult> result) { 231 scoped_ptr<GCMStore::LoadResult> result) {
228 std::vector<GURL> endpoints; 232 std::vector<GURL> endpoints;
229 endpoints.push_back(GURL(kMCSEndpointMain)); 233 endpoints.push_back(GURL(kMCSEndpointMain));
230 endpoints.push_back(GURL(kMCSEndpointFallback)); 234 endpoints.push_back(GURL(kMCSEndpointFallback));
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 weak_ptr_factory_.GetWeakPtr()), 291 weak_ptr_factory_.GetWeakPtr()),
288 kDefaultBackoffPolicy, 292 kDefaultBackoffPolicy,
289 chrome_build_proto_, 293 chrome_build_proto_,
290 checkin_info.android_id, 294 checkin_info.android_id,
291 checkin_info.secret, 295 checkin_info.secret,
292 account_ids_, 296 account_ids_,
293 url_request_context_getter_)); 297 url_request_context_getter_));
294 checkin_request_->Start(); 298 checkin_request_->Start();
295 } 299 }
296 300
297 void GCMClientImpl::OnCheckinCompleted(uint64 android_id, 301 void GCMClientImpl::ScheduleNextCheckin(
fgorski 2014/03/28 14:30:51 Consider renaming to SchedulePeriodicCheckin
fgorski 2014/03/29 01:09:32 Done.
298 uint64 security_token) { 302 const base::Time& last_checkin_time,
303 const CheckinInfo& checkin_info) {
304 GServicesSettingsMap::const_iterator iter =
305 g_services_settings_.find(kCheckinIntervalKey);
306 // If we don't have the setting or string parsing fails, 0 is a reasonable
fgorski 2014/03/28 14:30:51 consider 2 days as should be the current default f
fgorski 2014/03/29 01:09:32 Done.
307 // value.
308 int64 checkin_interval_sec = 0LL;
309 if (iter != g_services_settings_.end())
310 base::StringToInt64(iter->second, &checkin_interval_sec);
311 base::TimeDelta checkin_interval =
312 base::TimeDelta::FromSeconds(checkin_interval_sec);
313 base::TimeDelta time_to_next_checkin =
314 last_checkin_time + checkin_interval - base::Time::Now();
315 base::MessageLoop::current()->PostDelayedTask(
316 FROM_HERE,
317 base::Bind(&GCMClientImpl::StartCheckin,
318 weak_ptr_factory_.GetWeakPtr(),
319 checkin_info),
320 time_to_next_checkin);
321 }
322
323 void GCMClientImpl::OnCheckinCompleted(
324 const checkin_proto::AndroidCheckinResponse& checkin_response) {
299 checkin_request_.reset(); 325 checkin_request_.reset();
300 326
301 CheckinInfo checkin_info; 327 if (!checkin_response.has_android_id() ||
302 checkin_info.android_id = android_id; 328 !checkin_response.has_security_token()) {
303 checkin_info.secret = security_token;
304
305 if (!checkin_info.IsValid()) {
306 // TODO(fgorski): I don't think a retry here will help, we should probalby 329 // TODO(fgorski): I don't think a retry here will help, we should probalby
307 // start over. By checking in with (0, 0). 330 // start over. By checking in with (0, 0).
308 return; 331 return;
309 } 332 }
310 333
334 CheckinInfo checkin_info;
335 checkin_info.android_id = checkin_response.android_id();
336 checkin_info.secret = checkin_response.security_token();
337
338 // Process the gservices to get the checkin_interval here.
339 UpdateGServicesSettings(checkin_response);
340
311 if (state_ == INITIAL_DEVICE_CHECKIN) { 341 if (state_ == INITIAL_DEVICE_CHECKIN) {
312 OnFirstTimeDeviceCheckinCompleted(checkin_info); 342 OnFirstTimeDeviceCheckinCompleted(checkin_info);
313 } else { 343 } else {
314 DCHECK_EQ(READY, state_); 344 DCHECK_EQ(READY, state_);
315 if (device_checkin_info_.android_id != checkin_info.android_id || 345 if (device_checkin_info_.android_id != checkin_info.android_id ||
316 device_checkin_info_.secret != checkin_info.secret) { 346 device_checkin_info_.secret != checkin_info.secret) {
317 ResetState(); 347 ResetState();
318 } else {
319 // TODO(fgorski): Reset the checkin timer.
320 } 348 }
321 } 349 }
350
351 if (device_checkin_info_.IsValid())
352 ScheduleNextCheckin(base::Time::Now(), device_checkin_info_);
322 } 353 }
323 354
324 void GCMClientImpl::SetDeviceCredentialsCallback(bool success) { 355 void GCMClientImpl::SetDeviceCredentialsCallback(bool success) {
325 // TODO(fgorski): This is one of the signals that store needs a rebuild. 356 // TODO(fgorski): This is one of the signals that store needs a rebuild.
326 DCHECK(success); 357 DCHECK(success);
327 } 358 }
328 359
329 void GCMClientImpl::UpdateRegistrationCallback(bool success) { 360 void GCMClientImpl::UpdateRegistrationCallback(bool success) {
330 // TODO(fgorski): This is one of the signals that store needs a rebuild. 361 // TODO(fgorski): This is one of the signals that store needs a rebuild.
331 DCHECK(success); 362 DCHECK(success);
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after
657 send_error_details.additional_data.find(kSendErrorMessageIdKey); 688 send_error_details.additional_data.find(kSendErrorMessageIdKey);
658 if (iter != send_error_details.additional_data.end()) { 689 if (iter != send_error_details.additional_data.end()) {
659 send_error_details.message_id = iter->second; 690 send_error_details.message_id = iter->second;
660 send_error_details.additional_data.erase(iter); 691 send_error_details.additional_data.erase(iter);
661 } 692 }
662 693
663 delegate_->OnMessageSendError(data_message_stanza.category(), 694 delegate_->OnMessageSendError(data_message_stanza.category(),
664 send_error_details); 695 send_error_details);
665 } 696 }
666 697
698 void GCMClientImpl::UpdateGServicesSettings(
699 const checkin_proto::AndroidCheckinResponse& checkin_response) {
700 std::vector<std::string> settings_to_remove;
701 std::map<std::string, std::string> settings_to_add;
702
703 // Check if we are only dealing with a diff of settings or full update.
704 if (checkin_response.has_settings_diff() &&
705 !checkin_response.settings_diff()) {
706 for (GServicesSettingsMap::const_iterator it = g_services_settings_.begin();
707 it != g_services_settings_.end(); ++it) {
708 settings_to_remove.push_back(it->first);
709 }
710 g_services_settings_.clear();
711 }
712
713 for (int i = 0; i < checkin_response.setting_size(); ++i) {
714 std::string name = checkin_response.setting(i).name();
fgorski 2014/03/28 14:30:51 add a comment explaining what the if statement doe
fgorski 2014/03/29 01:09:32 Done.
715 if (name.find("delete_") == 0) {
fgorski 2014/03/28 14:30:51 use const
fgorski 2014/03/29 01:09:32 Done.
716 name = name.substr(7);
fgorski 2014/03/28 14:30:51 use arraysize with the const above.
fgorski 2014/03/29 01:09:32 Done.
717 GServicesSettingsMap::iterator iter = g_services_settings_.find(name);
718 if (iter != g_services_settings_.end()) {
719 settings_to_remove.push_back(name);
720 g_services_settings_.erase(iter);
721 }
722 } else {
723 std::string value = checkin_response.setting(i).value();
724 g_services_settings_[name] = value;
725 settings_to_add[name] = value;
726 }
727 }
728
729 gcm_store_->UpdateGServicesSettings(
fgorski 2014/03/28 14:30:51 Pass the current time based on the clock_ clock_-
fgorski 2014/03/29 01:09:32 Done.
730 settings_to_remove,
731 settings_to_add,
732 base::Bind(&GCMClientImpl::UpdateGServicesSettingsCallback,
733 weak_ptr_factory_.GetWeakPtr()));
734 }
735
736 void GCMClientImpl::UpdateGServicesSettingsCallback(bool success) {
737 DCHECK(success);
738 }
739
667 } // namespace gcm 740 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698