| Index: chrome/browser/chromeos/settings/device_oauth2_token_service_factory.cc
|
| diff --git a/chrome/browser/chromeos/settings/device_oauth2_token_service_factory.cc b/chrome/browser/chromeos/settings/device_oauth2_token_service_factory.cc
|
| index 5f7fb306267a4372e837ea45a33e6e37620349e6..253a8339e8f51adc59b3f58772e1b5586cb28827 100644
|
| --- a/chrome/browser/chromeos/settings/device_oauth2_token_service_factory.cc
|
| +++ b/chrome/browser/chromeos/settings/device_oauth2_token_service_factory.cc
|
| @@ -4,11 +4,14 @@
|
|
|
| #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h"
|
|
|
| +#include "base/bind.h"
|
| +#include "base/callback.h"
|
| #include "base/message_loop/message_loop.h"
|
| #include "base/tracked_objects.h"
|
| #include "chrome/browser/browser_process.h"
|
| #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h"
|
| #include "chrome/browser/chromeos/settings/token_encryptor.h"
|
| +#include "chromeos/cryptohome/system_salt_getter.h"
|
| #include "content/public/browser/browser_thread.h"
|
|
|
| namespace chromeos {
|
| @@ -19,10 +22,9 @@ DeviceOAuth2TokenServiceFactory* g_factory = NULL;
|
| } // namespace
|
|
|
| DeviceOAuth2TokenServiceFactory::DeviceOAuth2TokenServiceFactory()
|
| - : token_service_(new DeviceOAuth2TokenService(
|
| - g_browser_process->system_request_context(),
|
| - g_browser_process->local_state(),
|
| - new CryptohomeTokenEncryptor)) {
|
| + : initialized_(false),
|
| + token_service_(NULL),
|
| + weak_ptr_factory_(this) {
|
| }
|
|
|
| DeviceOAuth2TokenServiceFactory::~DeviceOAuth2TokenServiceFactory() {
|
| @@ -33,30 +35,15 @@ DeviceOAuth2TokenServiceFactory::~DeviceOAuth2TokenServiceFactory() {
|
| void DeviceOAuth2TokenServiceFactory::Get(const GetCallback& callback) {
|
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
|
|
|
| - DeviceOAuth2TokenService* token_service = NULL;
|
| - if (g_factory)
|
| - token_service = g_factory->token_service_;
|
| -
|
| - // TODO(satorux): Implement async initialization logic for
|
| - // DeviceOAuth2TokenService. crbug.com/309959.
|
| - // Here's how that should work:
|
| - //
|
| - // if token_service is ready:
|
| - // run callback asynchronously via MessageLoop
|
| - // return
|
| - //
|
| - // add callback to the pending callback list
|
| - //
|
| - // if there is only one pending callback:
|
| - // start getting the system salt asynchronously...
|
| - //
|
| - // upon receiving the system salt:
|
| - // create CryptohomeTokenEncryptor with that key
|
| - // create DeviceOAuth2TokenService
|
| - // run all the pending callbacks
|
| - base::MessageLoop::current()->PostTask(
|
| - FROM_HERE,
|
| - base::Bind(callback, token_service));
|
| + if (!g_factory) {
|
| + base::MessageLoop::current()->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(callback,
|
| + static_cast<DeviceOAuth2TokenService*>(NULL)));
|
| + return;
|
| + }
|
| +
|
| + g_factory->RunAsync(callback);
|
| }
|
|
|
| // static
|
| @@ -65,6 +52,7 @@ void DeviceOAuth2TokenServiceFactory::Initialize() {
|
|
|
| DCHECK(!g_factory);
|
| g_factory = new DeviceOAuth2TokenServiceFactory;
|
| + g_factory->CreateTokenService();
|
| }
|
|
|
| // static
|
| @@ -77,4 +65,53 @@ void DeviceOAuth2TokenServiceFactory::Shutdown() {
|
| }
|
| }
|
|
|
| +void DeviceOAuth2TokenServiceFactory::CreateTokenService() {
|
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
|
| +
|
| + SystemSaltGetter::Get()->GetSystemSalt(
|
| + base::Bind(&DeviceOAuth2TokenServiceFactory::DidGetSystemSalt,
|
| + weak_ptr_factory_.GetWeakPtr()));
|
| +}
|
| +
|
| +void DeviceOAuth2TokenServiceFactory::DidGetSystemSalt(
|
| + const std::string& system_salt) {
|
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
|
| + DCHECK(!token_service_);
|
| +
|
| + if (system_salt.empty()) {
|
| + LOG(ERROR) << "Failed to get the system salt";
|
| + } else {
|
| + token_service_= new DeviceOAuth2TokenService(
|
| + g_browser_process->system_request_context(),
|
| + g_browser_process->local_state(),
|
| + new CryptohomeTokenEncryptor(system_salt));
|
| + }
|
| + // Mark that the factory is initialized.
|
| + initialized_ = true;
|
| +
|
| + // Run callbacks regardless of whether token_service_ is created or not,
|
| + // but don't run callbacks immediately. Each callback would cause an
|
| + // interesting action, hence running them consecutively could be
|
| + // potentially expensive and dangerous.
|
| + while (!pending_callbacks_.empty()) {
|
| + base::MessageLoop::current()->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(pending_callbacks_.front(), token_service_));
|
| + pending_callbacks_.pop();
|
| + }
|
| +}
|
| +
|
| +void DeviceOAuth2TokenServiceFactory::RunAsync(const GetCallback& callback) {
|
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
|
| +
|
| + if (initialized_) {
|
| + base::MessageLoop::current()->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(callback, token_service_));
|
| + return;
|
| + }
|
| +
|
| + pending_callbacks_.push(callback);
|
| +}
|
| +
|
| } // namespace chromeos
|
|
|