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/bind.h" | |
8 #include "base/callback.h" | |
7 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
8 #include "base/tracked_objects.h" | 10 #include "base/tracked_objects.h" |
9 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" | 12 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" |
11 #include "chrome/browser/chromeos/settings/token_encryptor.h" | 13 #include "chrome/browser/chromeos/settings/token_encryptor.h" |
14 #include "chromeos/cryptohome/system_salt_getter.h" | |
12 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
13 | 16 |
14 namespace chromeos { | 17 namespace chromeos { |
15 namespace { | 18 namespace { |
16 | 19 |
17 DeviceOAuth2TokenServiceFactory* g_factory = NULL; | 20 DeviceOAuth2TokenServiceFactory* g_factory = NULL; |
18 | 21 |
19 } // namespace | 22 } // namespace |
20 | 23 |
21 DeviceOAuth2TokenServiceFactory::DeviceOAuth2TokenServiceFactory() | 24 DeviceOAuth2TokenServiceFactory::DeviceOAuth2TokenServiceFactory() |
22 : token_service_(new DeviceOAuth2TokenService( | 25 : token_service_(NULL), |
23 g_browser_process->system_request_context(), | 26 weak_ptr_factory_(this) { |
24 g_browser_process->local_state(), | |
25 new CryptohomeTokenEncryptor)) { | |
26 } | 27 } |
27 | 28 |
28 DeviceOAuth2TokenServiceFactory::~DeviceOAuth2TokenServiceFactory() { | 29 DeviceOAuth2TokenServiceFactory::~DeviceOAuth2TokenServiceFactory() { |
29 delete token_service_; | 30 delete token_service_; |
30 } | 31 } |
31 | 32 |
32 // static | 33 // static |
33 void DeviceOAuth2TokenServiceFactory::Get(const GetCallback& callback) { | 34 void DeviceOAuth2TokenServiceFactory::Get(const GetCallback& callback) { |
34 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 35 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
35 | 36 |
36 DeviceOAuth2TokenService* token_service = NULL; | 37 if (!g_factory) { |
37 if (g_factory) | 38 base::MessageLoop::current()->PostTask( |
38 token_service = g_factory->token_service_; | 39 FROM_HERE, |
40 base::Bind(callback, | |
41 static_cast<DeviceOAuth2TokenService*>(NULL))); | |
42 return; | |
43 } | |
39 | 44 |
40 // TODO(satorux): Implement async initialization logic for | 45 g_factory->RunAsync(callback); |
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)); | |
60 } | 46 } |
61 | 47 |
62 // static | 48 // static |
63 void DeviceOAuth2TokenServiceFactory::Initialize() { | 49 void DeviceOAuth2TokenServiceFactory::Initialize() { |
64 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 50 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
65 | 51 |
66 DCHECK(!g_factory); | 52 DCHECK(!g_factory); |
67 g_factory = new DeviceOAuth2TokenServiceFactory; | 53 g_factory = new DeviceOAuth2TokenServiceFactory; |
54 g_factory->CreateTokenService(); | |
68 } | 55 } |
69 | 56 |
70 // static | 57 // static |
71 void DeviceOAuth2TokenServiceFactory::Shutdown() { | 58 void DeviceOAuth2TokenServiceFactory::Shutdown() { |
72 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 59 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
73 | 60 |
74 if (g_factory) { | 61 if (g_factory) { |
75 delete g_factory; | 62 delete g_factory; |
76 g_factory = NULL; | 63 g_factory = NULL; |
77 } | 64 } |
78 } | 65 } |
79 | 66 |
67 void DeviceOAuth2TokenServiceFactory::CreateTokenService() { | |
68 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
69 | |
70 SystemSaltGetter::Get()->GetSystemSalt( | |
71 base::Bind(&DeviceOAuth2TokenServiceFactory::DidGetSystemSalt, | |
72 weak_ptr_factory_.GetWeakPtr())); | |
73 } | |
74 | |
75 void DeviceOAuth2TokenServiceFactory::DidGetSystemSalt( | |
76 const std::string& system_salt) { | |
77 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
78 DCHECK(!token_service_); | |
hashimoto
2013/10/24 06:50:00
Can we handle system_salt.empty() case or emit an
satorux1
2013/10/24 07:33:08
If it's empty CryptohomeTokenEncryptor won't work
| |
79 | |
80 token_service_= new DeviceOAuth2TokenService( | |
81 g_browser_process->system_request_context(), | |
82 g_browser_process->local_state(), | |
83 new CryptohomeTokenEncryptor(system_salt)); | |
84 | |
85 for (size_t i = 0; i < pending_callbacks_.size(); ++i) | |
86 pending_callbacks_[i].Run(token_service_); | |
87 pending_callbacks_.clear(); | |
88 } | |
89 | |
90 void DeviceOAuth2TokenServiceFactory::RunAsync(const GetCallback& callback) { | |
91 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
92 | |
93 if (token_service_) { | |
94 DCHECK(pending_callbacks_.empty()); | |
hashimoto
2013/10/24 06:50:00
Does this DCHECK mean pending_callbacks_[i].Run sh
satorux1
2013/10/24 07:33:08
This is to check no pending callbacks are left (th
hashimoto
2013/10/24 07:49:01
Sorry for being unclear, what I worried about was
satorux1
2013/10/24 08:31:28
Ah that's a very tricky case. I changed the loop t
| |
95 base::MessageLoop::current()->PostTask( | |
96 FROM_HERE, | |
97 base::Bind(callback, token_service_)); | |
98 return; | |
99 } | |
100 | |
101 pending_callbacks_.push_back(callback); | |
102 } | |
103 | |
80 } // namespace chromeos | 104 } // namespace chromeos |
OLD | NEW |