| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/engine/checkin_request.h" | 5 #include "google_apis/gcm/engine/checkin_request.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "google_apis/gcm/protocol/checkin.pb.h" | 10 #include "google_apis/gcm/protocol/checkin.pb.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 // histogram enum accordingly. | 39 // histogram enum accordingly. |
| 40 STATUS_COUNT | 40 STATUS_COUNT |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 void RecordCheckinStatusToUMA(CheckinRequestStatus status) { | 43 void RecordCheckinStatusToUMA(CheckinRequestStatus status) { |
| 44 UMA_HISTOGRAM_ENUMERATION("GCM.CheckinRequestStatus", status, STATUS_COUNT); | 44 UMA_HISTOGRAM_ENUMERATION("GCM.CheckinRequestStatus", status, STATUS_COUNT); |
| 45 } | 45 } |
| 46 | 46 |
| 47 } // namespace | 47 } // namespace |
| 48 | 48 |
| 49 CheckinRequest::RequestInfo::RequestInfo( | 49 CheckinRequest::CheckinRequest( |
| 50 const CheckinRequestCallback& callback, |
| 51 const net::BackoffEntry::Policy& backoff_policy, |
| 52 const checkin_proto::ChromeBuildProto& chrome_build_proto, |
| 50 uint64 android_id, | 53 uint64 android_id, |
| 51 uint64 security_token, | 54 uint64 security_token, |
| 52 const std::string& settings_digest, | |
| 53 const std::vector<std::string>& account_ids, | 55 const std::vector<std::string>& account_ids, |
| 54 const checkin_proto::ChromeBuildProto& chrome_build_proto) | |
| 55 : android_id(android_id), | |
| 56 security_token(security_token), | |
| 57 settings_digest(settings_digest), | |
| 58 account_ids(account_ids), | |
| 59 chrome_build_proto(chrome_build_proto) { | |
| 60 } | |
| 61 | |
| 62 CheckinRequest::RequestInfo::~RequestInfo() {} | |
| 63 | |
| 64 CheckinRequest::CheckinRequest( | |
| 65 const RequestInfo& request_info, | |
| 66 const net::BackoffEntry::Policy& backoff_policy, | |
| 67 const CheckinRequestCallback& callback, | |
| 68 net::URLRequestContextGetter* request_context_getter) | 56 net::URLRequestContextGetter* request_context_getter) |
| 69 : request_context_getter_(request_context_getter), | 57 : request_context_getter_(request_context_getter), |
| 70 callback_(callback), | 58 callback_(callback), |
| 71 backoff_entry_(&backoff_policy), | 59 backoff_entry_(&backoff_policy), |
| 72 request_info_(request_info), | 60 chrome_build_proto_(chrome_build_proto), |
| 61 android_id_(android_id), |
| 62 security_token_(security_token), |
| 63 account_ids_(account_ids), |
| 73 weak_ptr_factory_(this) { | 64 weak_ptr_factory_(this) { |
| 74 } | 65 } |
| 75 | 66 |
| 76 CheckinRequest::~CheckinRequest() {} | 67 CheckinRequest::~CheckinRequest() {} |
| 77 | 68 |
| 78 void CheckinRequest::Start() { | 69 void CheckinRequest::Start() { |
| 79 DCHECK(!url_fetcher_.get()); | 70 DCHECK(!url_fetcher_.get()); |
| 80 | 71 |
| 81 checkin_proto::AndroidCheckinRequest request; | 72 checkin_proto::AndroidCheckinRequest request; |
| 82 request.set_id(request_info_.android_id); | 73 request.set_id(android_id_); |
| 83 request.set_security_token(request_info_.security_token); | 74 request.set_security_token(security_token_); |
| 84 request.set_user_serial_number(kDefaultUserSerialNumber); | 75 request.set_user_serial_number(kDefaultUserSerialNumber); |
| 85 request.set_version(kRequestVersionValue); | 76 request.set_version(kRequestVersionValue); |
| 86 if (!request_info_.settings_digest.empty()) | |
| 87 request.set_digest(request_info_.settings_digest); | |
| 88 | 77 |
| 89 checkin_proto::AndroidCheckinProto* checkin = request.mutable_checkin(); | 78 checkin_proto::AndroidCheckinProto* checkin = request.mutable_checkin(); |
| 90 checkin->mutable_chrome_build()->CopyFrom(request_info_.chrome_build_proto); | 79 checkin->mutable_chrome_build()->CopyFrom(chrome_build_proto_); |
| 91 #if defined(CHROME_OS) | 80 #if defined(CHROME_OS) |
| 92 checkin->set_type(checkin_proto::DEVICE_CHROME_OS); | 81 checkin->set_type(checkin_proto::DEVICE_CHROME_OS); |
| 93 #else | 82 #else |
| 94 checkin->set_type(checkin_proto::DEVICE_CHROME_BROWSER); | 83 checkin->set_type(checkin_proto::DEVICE_CHROME_BROWSER); |
| 95 #endif | 84 #endif |
| 96 | 85 |
| 97 for (std::vector<std::string>::const_iterator iter = | 86 for (std::vector<std::string>::const_iterator iter = account_ids_.begin(); |
| 98 request_info_.account_ids.begin(); | 87 iter != account_ids_.end(); |
| 99 iter != request_info_.account_ids.end(); | |
| 100 ++iter) { | 88 ++iter) { |
| 101 request.add_account_cookie("[" + *iter + "]"); | 89 request.add_account_cookie("[" + *iter + "]"); |
| 102 } | 90 } |
| 103 | 91 |
| 104 std::string upload_data; | 92 std::string upload_data; |
| 105 CHECK(request.SerializeToString(&upload_data)); | 93 CHECK(request.SerializeToString(&upload_data)); |
| 106 | 94 |
| 107 url_fetcher_.reset( | 95 url_fetcher_.reset( |
| 108 net::URLFetcher::Create(GURL(kCheckinURL), net::URLFetcher::POST, this)); | 96 net::URLFetcher::Create(GURL(kCheckinURL), net::URLFetcher::POST, this)); |
| 109 url_fetcher_->SetRequestContext(request_context_getter_); | 97 url_fetcher_->SetRequestContext(request_context_getter_); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 net::HttpStatusCode response_status = static_cast<net::HttpStatusCode>( | 134 net::HttpStatusCode response_status = static_cast<net::HttpStatusCode>( |
| 147 source->GetResponseCode()); | 135 source->GetResponseCode()); |
| 148 if (response_status == net::HTTP_BAD_REQUEST || | 136 if (response_status == net::HTTP_BAD_REQUEST || |
| 149 response_status == net::HTTP_UNAUTHORIZED) { | 137 response_status == net::HTTP_UNAUTHORIZED) { |
| 150 // BAD_REQUEST indicates that the request was malformed. | 138 // BAD_REQUEST indicates that the request was malformed. |
| 151 // UNAUTHORIZED indicates that security token didn't match the android id. | 139 // UNAUTHORIZED indicates that security token didn't match the android id. |
| 152 LOG(ERROR) << "No point retrying the checkin with status: " | 140 LOG(ERROR) << "No point retrying the checkin with status: " |
| 153 << response_status << ". Checkin failed."; | 141 << response_status << ". Checkin failed."; |
| 154 RecordCheckinStatusToUMA(response_status == net::HTTP_BAD_REQUEST ? | 142 RecordCheckinStatusToUMA(response_status == net::HTTP_BAD_REQUEST ? |
| 155 HTTP_BAD_REQUEST : HTTP_UNAUTHORIZED); | 143 HTTP_BAD_REQUEST : HTTP_UNAUTHORIZED); |
| 156 callback_.Run(response_proto); | 144 callback_.Run(0,0); |
| 157 return; | 145 return; |
| 158 } | 146 } |
| 159 | 147 |
| 160 if (response_status != net::HTTP_OK || | 148 if (response_status != net::HTTP_OK || |
| 161 !source->GetResponseAsString(&response_string) || | 149 !source->GetResponseAsString(&response_string) || |
| 162 !response_proto.ParseFromString(response_string)) { | 150 !response_proto.ParseFromString(response_string)) { |
| 163 LOG(ERROR) << "Failed to get checkin response. HTTP Status: " | 151 LOG(ERROR) << "Failed to get checkin response. HTTP Status: " |
| 164 << response_status << ". Retrying."; | 152 << response_status << ". Retrying."; |
| 165 RecordCheckinStatusToUMA(response_status != net::HTTP_OK ? | 153 RecordCheckinStatusToUMA(response_status != net::HTTP_OK ? |
| 166 HTTP_NOT_OK : RESPONSE_PARSING_FAILED); | 154 HTTP_NOT_OK : RESPONSE_PARSING_FAILED); |
| 167 RetryWithBackoff(true); | 155 RetryWithBackoff(true); |
| 168 return; | 156 return; |
| 169 } | 157 } |
| 170 | 158 |
| 171 if (!response_proto.has_android_id() || | 159 if (!response_proto.has_android_id() || |
| 172 !response_proto.has_security_token() || | 160 !response_proto.has_security_token() || |
| 173 response_proto.android_id() == 0 || | 161 response_proto.android_id() == 0 || |
| 174 response_proto.security_token() == 0) { | 162 response_proto.security_token() == 0) { |
| 175 LOG(ERROR) << "Android ID or security token is 0. Retrying."; | 163 LOG(ERROR) << "Android ID or security token is 0. Retrying."; |
| 176 RecordCheckinStatusToUMA(ZERO_ID_OR_TOKEN); | 164 RecordCheckinStatusToUMA(ZERO_ID_OR_TOKEN); |
| 177 RetryWithBackoff(true); | 165 RetryWithBackoff(true); |
| 178 return; | 166 return; |
| 179 } | 167 } |
| 180 | 168 |
| 181 RecordCheckinStatusToUMA(SUCCESS); | 169 RecordCheckinStatusToUMA(SUCCESS); |
| 182 callback_.Run(response_proto); | 170 callback_.Run(response_proto.android_id(), response_proto.security_token()); |
| 183 } | 171 } |
| 184 | 172 |
| 185 } // namespace gcm | 173 } // namespace gcm |
| OLD | NEW |