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/chromeos/settings/device_oauth2_token_service_factory.h
" | 5 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h
" |
6 | 6 |
| 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/tracked_objects.h" |
7 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
8 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" | 10 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" |
9 #include "chrome/browser/chromeos/settings/token_encryptor.h" | 11 #include "chrome/browser/chromeos/settings/token_encryptor.h" |
10 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
11 | 13 |
12 namespace chromeos { | 14 namespace chromeos { |
| 15 namespace { |
13 | 16 |
14 static DeviceOAuth2TokenService* g_device_oauth2_token_service_ = NULL; | 17 DeviceOAuth2TokenServiceFactory* g_factory = NULL; |
15 | 18 |
16 DeviceOAuth2TokenServiceFactory::DeviceOAuth2TokenServiceFactory() { | 19 } // namespace |
| 20 |
| 21 DeviceOAuth2TokenServiceFactory::DeviceOAuth2TokenServiceFactory() |
| 22 : token_service_(new DeviceOAuth2TokenService( |
| 23 g_browser_process->system_request_context(), |
| 24 g_browser_process->local_state(), |
| 25 new CryptohomeTokenEncryptor)) { |
| 26 } |
| 27 |
| 28 DeviceOAuth2TokenServiceFactory::~DeviceOAuth2TokenServiceFactory() { |
| 29 delete token_service_; |
17 } | 30 } |
18 | 31 |
19 // static | 32 // static |
20 DeviceOAuth2TokenService* DeviceOAuth2TokenServiceFactory::Get() { | 33 void DeviceOAuth2TokenServiceFactory::Get(const GetCallback& callback) { |
21 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 34 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
22 return g_device_oauth2_token_service_; | 35 |
| 36 DeviceOAuth2TokenService* token_service = NULL; |
| 37 if (g_factory) |
| 38 token_service = g_factory->token_service_; |
| 39 |
| 40 // TODO(satorux): Implement async initialization logic for |
| 41 // DeviceOAuth2TokenService. crbug.com/309959. |
| 42 // Here's how that should work: |
| 43 // |
| 44 // if token_service is ready: |
| 45 // run callback asynchronously via MessageLoop |
| 46 // return |
| 47 // |
| 48 // add callback to the pending callback list |
| 49 // |
| 50 // if there is only one pending callback: |
| 51 // start getting the system salt asynchronously... |
| 52 // |
| 53 // upon receiving the system salt: |
| 54 // create CryptohomeTokenEncryptor with that key |
| 55 // create DeviceOAuth2TokenService |
| 56 // run all the pending callbacks |
| 57 base::MessageLoop::current()->PostTask( |
| 58 FROM_HERE, |
| 59 base::Bind(callback, token_service)); |
23 } | 60 } |
24 | 61 |
25 // static | 62 // static |
26 void DeviceOAuth2TokenServiceFactory::Initialize() { | 63 void DeviceOAuth2TokenServiceFactory::Initialize() { |
27 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 64 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
28 DCHECK(!g_device_oauth2_token_service_); | 65 |
29 g_device_oauth2_token_service_ = new DeviceOAuth2TokenService( | 66 DCHECK(!g_factory); |
30 g_browser_process->system_request_context(), | 67 g_factory = new DeviceOAuth2TokenServiceFactory; |
31 g_browser_process->local_state(), | |
32 new CryptohomeTokenEncryptor); | |
33 } | 68 } |
34 | 69 |
35 // static | 70 // static |
36 void DeviceOAuth2TokenServiceFactory::Shutdown() { | 71 void DeviceOAuth2TokenServiceFactory::Shutdown() { |
37 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 72 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
38 if (g_device_oauth2_token_service_) { | 73 |
39 delete g_device_oauth2_token_service_; | 74 if (g_factory) { |
40 g_device_oauth2_token_service_ = NULL; | 75 delete g_factory; |
| 76 g_factory = NULL; |
41 } | 77 } |
42 } | 78 } |
43 | 79 |
44 } // namespace chromeos | 80 } // namespace chromeos |
OLD | NEW |