Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "chrome/browser/chromeos/settings/device_oauth2_token_service.h" | 5 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | |
| 11 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 13 #include "base/prefs/pref_registry_simple.h" | 14 #include "base/prefs/pref_registry_simple.h" |
| 14 #include "base/prefs/pref_service.h" | 15 #include "base/prefs/pref_service.h" |
| 15 #include "base/values.h" | 16 #include "base/values.h" |
| 16 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/chromeos/settings/cros_settings.h" | |
| 18 #include "chrome/browser/chromeos/settings/token_encryptor.h" | 18 #include "chrome/browser/chromeos/settings/token_encryptor.h" |
| 19 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 20 #include "chromeos/cryptohome/system_salt_getter.h" | 20 #include "chromeos/cryptohome/system_salt_getter.h" |
| 21 #include "chromeos/settings/cros_settings_names.h" | |
| 21 #include "google_apis/gaia/gaia_constants.h" | 22 #include "google_apis/gaia/gaia_constants.h" |
| 22 #include "google_apis/gaia/gaia_urls.h" | 23 #include "google_apis/gaia/gaia_urls.h" |
| 23 #include "google_apis/gaia/google_service_auth_error.h" | 24 #include "google_apis/gaia/google_service_auth_error.h" |
| 24 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h" | 25 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h" |
| 25 #include "policy/proto/device_management_backend.pb.h" | 26 #include "policy/proto/device_management_backend.pb.h" |
| 26 | 27 |
| 27 namespace chromeos { | 28 namespace chromeos { |
| 28 | 29 |
| 29 struct DeviceOAuth2TokenService::PendingRequest { | 30 struct DeviceOAuth2TokenService::PendingRequest { |
| 30 PendingRequest(const base::WeakPtr<RequestImpl>& request, | 31 PendingRequest(const base::WeakPtr<RequestImpl>& request, |
| 31 const std::string& client_id, | 32 const std::string& client_id, |
| 32 const std::string& client_secret, | 33 const std::string& client_secret, |
| 33 const ScopeSet& scopes) | 34 const ScopeSet& scopes) |
| 34 : request(request), | 35 : request(request), |
| 35 client_id(client_id), | 36 client_id(client_id), |
| 36 client_secret(client_secret), | 37 client_secret(client_secret), |
| 37 scopes(scopes) {} | 38 scopes(scopes) {} |
| 38 | 39 |
| 39 const base::WeakPtr<RequestImpl> request; | 40 const base::WeakPtr<RequestImpl> request; |
| 40 const std::string client_id; | 41 const std::string client_id; |
| 41 const std::string client_secret; | 42 const std::string client_secret; |
| 42 const ScopeSet scopes; | 43 const ScopeSet scopes; |
| 43 }; | 44 }; |
| 44 | 45 |
| 46 void DeviceOAuth2TokenService::OnServiceAccountIdentityChanged() { | |
| 47 if (!GetRobotAccountId().empty() && !refresh_token_.empty()) | |
| 48 FireRefreshTokenAvailable(GetRobotAccountId()); | |
| 49 } | |
| 50 | |
| 45 DeviceOAuth2TokenService::DeviceOAuth2TokenService( | 51 DeviceOAuth2TokenService::DeviceOAuth2TokenService( |
| 46 net::URLRequestContextGetter* getter, | 52 net::URLRequestContextGetter* getter, |
| 47 PrefService* local_state) | 53 PrefService* local_state) |
| 48 : url_request_context_getter_(getter), | 54 : url_request_context_getter_(getter), |
| 49 local_state_(local_state), | 55 local_state_(local_state), |
| 50 state_(STATE_LOADING), | 56 state_(STATE_LOADING), |
| 51 max_refresh_token_validation_retries_(3), | 57 max_refresh_token_validation_retries_(3), |
| 58 service_account_identity_subscription_( | |
| 59 CrosSettings::Get()->AddSettingsObserver( | |
| 60 kServiceAccountIdentity, | |
| 61 base::Bind( | |
| 62 &DeviceOAuth2TokenService::OnServiceAccountIdentityChanged, | |
| 63 base::Unretained(this))).Pass()), | |
| 52 weak_ptr_factory_(this) { | 64 weak_ptr_factory_(this) { |
| 53 // Pull in the system salt. | 65 // Pull in the system salt. |
| 54 SystemSaltGetter::Get()->GetSystemSalt( | 66 SystemSaltGetter::Get()->GetSystemSalt( |
| 55 base::Bind(&DeviceOAuth2TokenService::DidGetSystemSalt, | 67 base::Bind(&DeviceOAuth2TokenService::DidGetSystemSalt, |
| 56 weak_ptr_factory_.GetWeakPtr())); | 68 weak_ptr_factory_.GetWeakPtr())); |
| 57 } | 69 } |
| 58 | 70 |
| 59 DeviceOAuth2TokenService::~DeviceOAuth2TokenService() { | 71 DeviceOAuth2TokenService::~DeviceOAuth2TokenService() { |
| 60 FlushPendingRequests(false, GoogleServiceAuthError::REQUEST_CANCELED); | 72 FlushPendingRequests(false, GoogleServiceAuthError::REQUEST_CANCELED); |
| 61 FlushTokenSaveCallbacks(false); | 73 FlushTokenSaveCallbacks(false); |
| 62 } | 74 } |
| 63 | 75 |
| 64 // static | 76 // static |
| 65 void DeviceOAuth2TokenService::RegisterPrefs(PrefRegistrySimple* registry) { | 77 void DeviceOAuth2TokenService::RegisterPrefs(PrefRegistrySimple* registry) { |
| 66 registry->RegisterStringPref(prefs::kDeviceRobotAnyApiRefreshToken, | 78 registry->RegisterStringPref(prefs::kDeviceRobotAnyApiRefreshToken, |
| 67 std::string()); | 79 std::string()); |
| 68 } | 80 } |
| 69 | 81 |
| 70 void DeviceOAuth2TokenService::SetAndSaveRefreshToken( | 82 void DeviceOAuth2TokenService::SetAndSaveRefreshToken( |
| 71 const std::string& refresh_token, | 83 const std::string& refresh_token, |
| 72 const StatusCallback& result_callback) { | 84 const StatusCallback& result_callback) { |
| 73 FlushPendingRequests(false, GoogleServiceAuthError::REQUEST_CANCELED); | 85 FlushPendingRequests(false, GoogleServiceAuthError::REQUEST_CANCELED); |
| 74 | 86 |
| 75 bool waiting_for_salt = state_ == STATE_LOADING; | 87 bool waiting_for_salt = state_ == STATE_LOADING; |
| 76 refresh_token_ = refresh_token; | 88 refresh_token_ = refresh_token; |
| 77 state_ = STATE_VALIDATION_PENDING; | 89 state_ = STATE_VALIDATION_PENDING; |
| 78 FireRefreshTokenAvailable(GetRobotAccountId()); | 90 |
| 91 // We cannot announce the token yet because that requires us to also know the | |
| 92 // robot account ID, which is not available yet. It will be done from | |
| 93 // OnServiceAccountIdentityChanged() once the robot account ID becomes | |
| 94 // available. | |
|
xiyuan
2015/01/30 18:37:03
Should we call OnServiceAccountIdentityChanged her
bartfab (slow)
2015/02/02 21:01:07
Done.
| |
| 79 | 95 |
| 80 token_save_callbacks_.push_back(result_callback); | 96 token_save_callbacks_.push_back(result_callback); |
| 81 if (!waiting_for_salt) { | 97 if (!waiting_for_salt) { |
| 82 if (system_salt_.empty()) | 98 if (system_salt_.empty()) |
| 83 FlushTokenSaveCallbacks(false); | 99 FlushTokenSaveCallbacks(false); |
| 84 else | 100 else |
| 85 EncryptAndSaveToken(); | 101 EncryptAndSaveToken(); |
| 86 } | 102 } |
| 87 } | 103 } |
| 88 | 104 |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 380 GoogleServiceAuthError auth_error(error); | 396 GoogleServiceAuthError auth_error(error); |
| 381 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 397 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 382 &RequestImpl::InformConsumer, | 398 &RequestImpl::InformConsumer, |
| 383 request->AsWeakPtr(), | 399 request->AsWeakPtr(), |
| 384 auth_error, | 400 auth_error, |
| 385 std::string(), | 401 std::string(), |
| 386 base::Time())); | 402 base::Time())); |
| 387 } | 403 } |
| 388 | 404 |
| 389 } // namespace chromeos | 405 } // namespace chromeos |
| OLD | NEW |