| 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 "components/invalidation/impl/gcm_network_channel.h" | 5 #include "components/invalidation/impl/gcm_network_channel.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/base64url.h" | 9 #include "base/base64url.h" |
| 10 #include "base/i18n/time_formatting.h" | 10 #include "base/i18n/time_formatting.h" |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 weak_factory_(this) { | 124 weak_factory_(this) { |
| 125 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); | 125 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); |
| 126 delegate_->Initialize( | 126 delegate_->Initialize( |
| 127 base::Bind(&GCMNetworkChannel::OnConnectionStateChanged, | 127 base::Bind(&GCMNetworkChannel::OnConnectionStateChanged, |
| 128 weak_factory_.GetWeakPtr()), | 128 weak_factory_.GetWeakPtr()), |
| 129 base::Bind(&GCMNetworkChannel::OnStoreReset, weak_factory_.GetWeakPtr())); | 129 base::Bind(&GCMNetworkChannel::OnStoreReset, weak_factory_.GetWeakPtr())); |
| 130 Register(); | 130 Register(); |
| 131 } | 131 } |
| 132 | 132 |
| 133 GCMNetworkChannel::~GCMNetworkChannel() { | 133 GCMNetworkChannel::~GCMNetworkChannel() { |
| 134 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 134 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | 135 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); |
| 135 } | 136 } |
| 136 | 137 |
| 137 void GCMNetworkChannel::Register() { | 138 void GCMNetworkChannel::Register() { |
| 138 delegate_->Register(base::Bind(&GCMNetworkChannel::OnRegisterComplete, | 139 delegate_->Register(base::Bind(&GCMNetworkChannel::OnRegisterComplete, |
| 139 weak_factory_.GetWeakPtr())); | 140 weak_factory_.GetWeakPtr())); |
| 140 } | 141 } |
| 141 | 142 |
| 142 void GCMNetworkChannel::OnRegisterComplete( | 143 void GCMNetworkChannel::OnRegisterComplete( |
| 143 const std::string& registration_id, | 144 const std::string& registration_id, |
| 144 gcm::GCMClient::Result result) { | 145 gcm::GCMClient::Result result) { |
| 145 DCHECK(CalledOnValidThread()); | 146 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 146 if (result == gcm::GCMClient::SUCCESS) { | 147 if (result == gcm::GCMClient::SUCCESS) { |
| 147 DCHECK(!registration_id.empty()); | 148 DCHECK(!registration_id.empty()); |
| 148 DVLOG(2) << "Got registration_id"; | 149 DVLOG(2) << "Got registration_id"; |
| 149 register_backoff_entry_->Reset(); | 150 register_backoff_entry_->Reset(); |
| 150 registration_id_ = registration_id; | 151 registration_id_ = registration_id; |
| 151 if (!cached_message_.empty()) | 152 if (!cached_message_.empty()) |
| 152 RequestAccessToken(); | 153 RequestAccessToken(); |
| 153 } else { | 154 } else { |
| 154 DVLOG(2) << "Register failed: " << result; | 155 DVLOG(2) << "Register failed: " << result; |
| 155 // Retry in case of transient error. | 156 // Retry in case of transient error. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 167 } | 168 } |
| 168 default: | 169 default: |
| 169 break; | 170 break; |
| 170 } | 171 } |
| 171 } | 172 } |
| 172 diagnostic_info_.registration_id_ = registration_id_; | 173 diagnostic_info_.registration_id_ = registration_id_; |
| 173 diagnostic_info_.registration_result_ = result; | 174 diagnostic_info_.registration_result_ = result; |
| 174 } | 175 } |
| 175 | 176 |
| 176 void GCMNetworkChannel::SendMessage(const std::string& message) { | 177 void GCMNetworkChannel::SendMessage(const std::string& message) { |
| 177 DCHECK(CalledOnValidThread()); | 178 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 178 DCHECK(!message.empty()); | 179 DCHECK(!message.empty()); |
| 179 DVLOG(2) << "SendMessage"; | 180 DVLOG(2) << "SendMessage"; |
| 180 diagnostic_info_.sent_messages_count_++; | 181 diagnostic_info_.sent_messages_count_++; |
| 181 if (!cached_message_.empty()) { | 182 if (!cached_message_.empty()) { |
| 182 RecordOutgoingMessageStatus(MESSAGE_DISCARDED); | 183 RecordOutgoingMessageStatus(MESSAGE_DISCARDED); |
| 183 } | 184 } |
| 184 cached_message_ = message; | 185 cached_message_ = message; |
| 185 | 186 |
| 186 if (!registration_id_.empty()) { | 187 if (!registration_id_.empty()) { |
| 187 RequestAccessToken(); | 188 RequestAccessToken(); |
| 188 } | 189 } |
| 189 } | 190 } |
| 190 | 191 |
| 191 void GCMNetworkChannel::RequestAccessToken() { | 192 void GCMNetworkChannel::RequestAccessToken() { |
| 192 DCHECK(CalledOnValidThread()); | 193 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 193 delegate_->RequestToken(base::Bind(&GCMNetworkChannel::OnGetTokenComplete, | 194 delegate_->RequestToken(base::Bind(&GCMNetworkChannel::OnGetTokenComplete, |
| 194 weak_factory_.GetWeakPtr())); | 195 weak_factory_.GetWeakPtr())); |
| 195 } | 196 } |
| 196 | 197 |
| 197 void GCMNetworkChannel::OnGetTokenComplete( | 198 void GCMNetworkChannel::OnGetTokenComplete( |
| 198 const GoogleServiceAuthError& error, | 199 const GoogleServiceAuthError& error, |
| 199 const std::string& token) { | 200 const std::string& token) { |
| 200 DCHECK(CalledOnValidThread()); | 201 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 201 if (cached_message_.empty() || registration_id_.empty()) { | 202 if (cached_message_.empty() || registration_id_.empty()) { |
| 202 // Nothing to do. | 203 // Nothing to do. |
| 203 return; | 204 return; |
| 204 } | 205 } |
| 205 | 206 |
| 206 if (error.state() != GoogleServiceAuthError::NONE) { | 207 if (error.state() != GoogleServiceAuthError::NONE) { |
| 207 // Requesting access token failed. Persistent errors will be reported by | 208 // Requesting access token failed. Persistent errors will be reported by |
| 208 // token service. Just drop this request, cacheinvalidations will retry | 209 // token service. Just drop this request, cacheinvalidations will retry |
| 209 // sending message and at that time we'll retry requesting access token. | 210 // sending message and at that time we'll retry requesting access token. |
| 210 DVLOG(1) << "RequestAccessToken failed: " << error.ToString(); | 211 DVLOG(1) << "RequestAccessToken failed: " << error.ToString(); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 const std::string echo_header("echo-token: " + echo_token_); | 270 const std::string echo_header("echo-token: " + echo_token_); |
| 270 fetcher_->AddExtraRequestHeader(echo_header); | 271 fetcher_->AddExtraRequestHeader(echo_header); |
| 271 } | 272 } |
| 272 fetcher_->SetUploadData("application/x-protobuffer", cached_message_); | 273 fetcher_->SetUploadData("application/x-protobuffer", cached_message_); |
| 273 fetcher_->Start(); | 274 fetcher_->Start(); |
| 274 // Clear message to prevent accidentally resending it in the future. | 275 // Clear message to prevent accidentally resending it in the future. |
| 275 cached_message_.clear(); | 276 cached_message_.clear(); |
| 276 } | 277 } |
| 277 | 278 |
| 278 void GCMNetworkChannel::OnURLFetchComplete(const net::URLFetcher* source) { | 279 void GCMNetworkChannel::OnURLFetchComplete(const net::URLFetcher* source) { |
| 279 DCHECK(CalledOnValidThread()); | 280 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 280 DCHECK_EQ(fetcher_.get(), source); | 281 DCHECK_EQ(fetcher_.get(), source); |
| 281 // Free fetcher at the end of function. | 282 // Free fetcher at the end of function. |
| 282 std::unique_ptr<net::URLFetcher> fetcher = std::move(fetcher_); | 283 std::unique_ptr<net::URLFetcher> fetcher = std::move(fetcher_); |
| 283 | 284 |
| 284 net::URLRequestStatus status = fetcher->GetStatus(); | 285 net::URLRequestStatus status = fetcher->GetStatus(); |
| 285 diagnostic_info_.last_post_response_code_ = | 286 diagnostic_info_.last_post_response_code_ = |
| 286 status.is_success() ? source->GetResponseCode() : status.error(); | 287 status.is_success() ? source->GetResponseCode() : status.error(); |
| 287 | 288 |
| 288 if (status.is_success() && | 289 if (status.is_success() && |
| 289 fetcher->GetResponseCode() == net::HTTP_UNAUTHORIZED) { | 290 fetcher->GetResponseCode() == net::HTTP_UNAUTHORIZED) { |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 ENUM_CASE(gcm::GCMClient::UNKNOWN_ERROR); | 485 ENUM_CASE(gcm::GCMClient::UNKNOWN_ERROR); |
| 485 ENUM_CASE(gcm::GCMClient::INVALID_PARAMETER); | 486 ENUM_CASE(gcm::GCMClient::INVALID_PARAMETER); |
| 486 ENUM_CASE(gcm::GCMClient::ASYNC_OPERATION_PENDING); | 487 ENUM_CASE(gcm::GCMClient::ASYNC_OPERATION_PENDING); |
| 487 ENUM_CASE(gcm::GCMClient::GCM_DISABLED); | 488 ENUM_CASE(gcm::GCMClient::GCM_DISABLED); |
| 488 } | 489 } |
| 489 NOTREACHED(); | 490 NOTREACHED(); |
| 490 return ""; | 491 return ""; |
| 491 } | 492 } |
| 492 | 493 |
| 493 } // namespace syncer | 494 } // namespace syncer |
| OLD | NEW |