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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h"
8 #include "base/callback.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/tracked_objects.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h"
13 #include "chrome/browser/chromeos/settings/token_encryptor.h"
14 #include "chromeos/cryptohome/system_salt_getter.h"
15 #include "content/public/browser/browser_thread.h"
20 DeviceOAuth2TokenServiceFactory
* g_factory
= NULL
;
24 DeviceOAuth2TokenServiceFactory::DeviceOAuth2TokenServiceFactory()
25 : initialized_(false),
27 weak_ptr_factory_(this) {
30 DeviceOAuth2TokenServiceFactory::~DeviceOAuth2TokenServiceFactory() {
31 delete token_service_
;
35 void DeviceOAuth2TokenServiceFactory::Get(const GetCallback
& callback
) {
36 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
39 base::MessageLoop::current()->PostTask(
42 static_cast<DeviceOAuth2TokenService
*>(NULL
)));
46 g_factory
->RunAsync(callback
);
50 void DeviceOAuth2TokenServiceFactory::Initialize() {
51 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
54 g_factory
= new DeviceOAuth2TokenServiceFactory
;
55 g_factory
->CreateTokenService();
59 void DeviceOAuth2TokenServiceFactory::Shutdown() {
60 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
68 void DeviceOAuth2TokenServiceFactory::CreateTokenService() {
69 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
71 SystemSaltGetter::Get()->GetSystemSalt(
72 base::Bind(&DeviceOAuth2TokenServiceFactory::DidGetSystemSalt
,
73 weak_ptr_factory_
.GetWeakPtr()));
76 void DeviceOAuth2TokenServiceFactory::DidGetSystemSalt(
77 const std::string
& system_salt
) {
78 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
79 DCHECK(!token_service_
);
81 if (system_salt
.empty()) {
82 LOG(ERROR
) << "Failed to get the system salt";
84 token_service_
= new DeviceOAuth2TokenService(
85 g_browser_process
->system_request_context(),
86 g_browser_process
->local_state(),
87 new CryptohomeTokenEncryptor(system_salt
));
89 // Mark that the factory is initialized.
92 // Run callbacks regardless of whether token_service_ is created or not,
93 // but don't run callbacks immediately. Each callback would cause an
94 // interesting action, hence running them consecutively could be
95 // potentially expensive and dangerous.
96 while (!pending_callbacks_
.empty()) {
97 base::MessageLoop::current()->PostTask(
99 base::Bind(pending_callbacks_
.front(), token_service_
));
100 pending_callbacks_
.pop();
104 void DeviceOAuth2TokenServiceFactory::RunAsync(const GetCallback
& callback
) {
105 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
108 base::MessageLoop::current()->PostTask(
110 base::Bind(callback
, token_service_
));
114 pending_callbacks_
.push(callback
);
117 } // namespace chromeos