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