OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/sync/profile_sync_service.h" | 5 #include "chrome/browser/sync/profile_sync_service.h" |
6 | 6 |
7 #include <cstddef> | 7 #include <cstddef> |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <utility> | 10 #include <utility> |
(...skipping 628 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
639 // be there. | 639 // be there. |
640 InitializeBackend(!HasSyncSetupCompleted()); | 640 InitializeBackend(!HasSyncSetupCompleted()); |
641 } | 641 } |
642 | 642 |
643 void ProfileSyncService::OnGetTokenSuccess( | 643 void ProfileSyncService::OnGetTokenSuccess( |
644 const OAuth2TokenService::Request* request, | 644 const OAuth2TokenService::Request* request, |
645 const std::string& access_token, | 645 const std::string& access_token, |
646 const base::Time& expiration_time) { | 646 const base::Time& expiration_time) { |
647 DCHECK_EQ(access_token_request_, request); | 647 DCHECK_EQ(access_token_request_, request); |
648 access_token_request_.reset(); | 648 access_token_request_.reset(); |
649 // Reset backoff time after successful response. | |
650 request_access_token_backoff_.Reset(); | |
651 access_token_ = access_token; | 649 access_token_ = access_token; |
652 | 650 |
653 if (sync_prefs_.SyncHasAuthError()) { | 651 if (sync_prefs_.SyncHasAuthError()) { |
654 sync_prefs_.SetSyncAuthError(false); | 652 sync_prefs_.SetSyncAuthError(false); |
655 UMA_HISTOGRAM_ENUMERATION("Sync.SyncAuthError", | 653 UMA_HISTOGRAM_ENUMERATION("Sync.SyncAuthError", |
656 AUTH_ERROR_FIXED, | 654 AUTH_ERROR_FIXED, |
657 AUTH_ERROR_LIMIT); | 655 AUTH_ERROR_LIMIT); |
658 } | 656 } |
659 | 657 |
660 if (backend_) | 658 if (backend_) |
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1123 } | 1121 } |
1124 | 1122 |
1125 } // namespace | 1123 } // namespace |
1126 | 1124 |
1127 void ProfileSyncService::OnConnectionStatusChange( | 1125 void ProfileSyncService::OnConnectionStatusChange( |
1128 syncer::ConnectionStatus status) { | 1126 syncer::ConnectionStatus status) { |
1129 if (status == syncer::CONNECTION_AUTH_ERROR) { | 1127 if (status == syncer::CONNECTION_AUTH_ERROR) { |
1130 // Sync server returned error indicating that access token is invalid. It | 1128 // Sync server returned error indicating that access token is invalid. It |
1131 // could be either expired or access is revoked. Let's request another | 1129 // could be either expired or access is revoked. Let's request another |
1132 // access token and if access is revoked then request for token will fail | 1130 // access token and if access is revoked then request for token will fail |
1133 // with corresponding error. | 1131 // with corresponding error. If access token is repeatedly reported |
1134 RequestAccessToken(); | 1132 // invalid, there may be some issues with server, e.g. authentication |
| 1133 // state is inconsistent on sync and token server. In that case, we |
| 1134 // backoff token requests exponentially to avoid hammering token server |
| 1135 // too much and to avoid getting same token due to token server's caching |
| 1136 // policy. |request_access_token_retry_timer_| is used to backoff request |
| 1137 // triggered by both auth error and failure talking to GAIA server. |
| 1138 // Therefore, we're likely to reach the backoff ceiling more quickly than |
| 1139 // you would expect from looking at the BackoffPolicy if both types of |
| 1140 // errors happen. We shouldn't receive two errors back-to-back without |
| 1141 // attempting a token/sync request in between, thus crank up request delay |
| 1142 // unnecessary. This is because we won't make a sync request if we hit an |
| 1143 // error until GAIA succeeds at sending a new token, and we won't request |
| 1144 // a new token unless sync reports a token failure. But to be safe, don't |
| 1145 // schedule request if this happens. |
| 1146 if (request_access_token_retry_timer_.IsRunning()) { |
| 1147 NOTREACHED(); |
| 1148 } else if (request_access_token_backoff_.failure_count() == 0) { |
| 1149 // First time request without delay. Currently invalid token is used |
| 1150 // to initialize sync backend and we'll always end up here. We don't |
| 1151 // want to delay initialization. |
| 1152 request_access_token_backoff_.InformOfRequest(false); |
| 1153 RequestAccessToken(); |
| 1154 } else { |
| 1155 request_access_token_backoff_.InformOfRequest(false); |
| 1156 request_access_token_retry_timer_.Start( |
| 1157 FROM_HERE, |
| 1158 request_access_token_backoff_.GetTimeUntilRelease(), |
| 1159 base::Bind(&ProfileSyncService::RequestAccessToken, |
| 1160 weak_factory_.GetWeakPtr())); |
| 1161 } |
1135 } else { | 1162 } else { |
| 1163 // Reset backoff time after successful connection. |
| 1164 if (status == syncer::CONNECTION_OK) { |
| 1165 // Request shouldn't be scheduled at this time. But if it is, it's |
| 1166 // possible that sync flips between OK and auth error states rapidly, |
| 1167 // thus hammers token server. To be safe, only reset backoff delay when |
| 1168 // no scheduled request. |
| 1169 if (request_access_token_retry_timer_.IsRunning()) { |
| 1170 NOTREACHED(); |
| 1171 } else { |
| 1172 request_access_token_backoff_.Reset(); |
| 1173 } |
| 1174 } |
| 1175 |
1136 const GoogleServiceAuthError auth_error = | 1176 const GoogleServiceAuthError auth_error = |
1137 ConnectionStatusToAuthError(status); | 1177 ConnectionStatusToAuthError(status); |
1138 DVLOG(1) << "Connection status change: " << auth_error.ToString(); | 1178 DVLOG(1) << "Connection status change: " << auth_error.ToString(); |
1139 UpdateAuthErrorState(auth_error); | 1179 UpdateAuthErrorState(auth_error); |
1140 } | 1180 } |
1141 } | 1181 } |
1142 | 1182 |
1143 void ProfileSyncService::OnStopSyncingPermanently() { | 1183 void ProfileSyncService::OnStopSyncingPermanently() { |
1144 sync_prefs_.SetStartSuppressed(true); | 1184 sync_prefs_.SetStartSuppressed(true); |
1145 DisableForUser(); | 1185 DisableForUser(); |
(...skipping 1020 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2166 #endif | 2206 #endif |
2167 } | 2207 } |
2168 | 2208 |
2169 // TODO(fgorski): Use GetPrimaryAccountId() when it's available. | 2209 // TODO(fgorski): Use GetPrimaryAccountId() when it's available. |
2170 return signin_->GetAuthenticatedUsername(); | 2210 return signin_->GetAuthenticatedUsername(); |
2171 } | 2211 } |
2172 | 2212 |
2173 WeakHandle<syncer::JsEventHandler> ProfileSyncService::GetJsEventHandler() { | 2213 WeakHandle<syncer::JsEventHandler> ProfileSyncService::GetJsEventHandler() { |
2174 return MakeWeakHandle(sync_js_controller_.AsWeakPtr()); | 2214 return MakeWeakHandle(sync_js_controller_.AsWeakPtr()); |
2175 } | 2215 } |
OLD | NEW |