OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/policy/device_token_fetcher.h" | 5 #include "chrome/browser/policy/device_token_fetcher.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
9 #include "base/singleton.h" | 9 #include "base/singleton.h" |
10 #include "chrome/browser/guid.h" | 10 #include "chrome/browser/guid.h" |
11 #include "chrome/browser/net/gaia/token_service.h" | 11 #include "chrome/browser/net/gaia/token_service.h" |
12 #include "chrome/browser/policy/proto/device_management_local.pb.h" | 12 #include "chrome/browser/policy/proto/device_management_local.pb.h" |
13 #include "chrome/common/chrome_paths.h" | 13 #include "chrome/common/chrome_paths.h" |
14 #include "chrome/common/net/gaia/gaia_constants.h" | 14 #include "chrome/common/net/gaia/gaia_constants.h" |
15 #include "chrome/common/notification_details.h" | 15 #include "chrome/common/notification_details.h" |
16 #include "chrome/common/notification_service.h" | 16 #include "chrome/common/notification_service.h" |
17 #include "chrome/common/notification_source.h" | 17 #include "chrome/common/notification_source.h" |
18 #include "chrome/common/notification_type.h" | 18 #include "chrome/common/notification_type.h" |
19 | 19 |
20 namespace policy { | 20 namespace policy { |
21 | 21 |
22 namespace em = enterprise_management; | 22 namespace em = enterprise_management; |
23 | 23 |
24 DeviceTokenFetcher::DeviceTokenFetcher( | 24 DeviceTokenFetcher::DeviceTokenFetcher( |
25 DeviceManagementBackend* backend, | 25 DeviceManagementBackend* backend, |
26 TokenService* token_service, | |
26 const FilePath& token_path) | 27 const FilePath& token_path) |
27 : token_path_(token_path), | 28 : token_path_(token_path), |
28 backend_(backend), | 29 backend_(backend), |
30 token_service_(token_service), | |
29 state_(kStateNotStarted), | 31 state_(kStateNotStarted), |
30 device_token_load_complete_event_(true, false) { | 32 device_token_load_complete_event_(true, false) { |
31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
32 // The token fetcher gets initialized AuthTokens for the device management | 34 |
33 // server are available. Install a notification observer to ensure that the | 35 auth_token_ = token_service_->GetTokenForService( |
34 // device management token gets fetched after the AuthTokens are available. | 36 GaiaConstants::kDeviceManagementService); |
35 registrar_.Add(this, | 37 |
36 NotificationType::TOKEN_AVAILABLE, | 38 if (!HasAuthToken()) { |
Nico
2010/11/18 14:34:24
You check in |Observe|, so this isn't really neces
Mattias Nissler (ping if slow)
2010/11/18 15:08:16
Removed. Done.
| |
37 NotificationService::AllSources()); | 39 registrar_.Add(this, |
40 NotificationType::TOKEN_AVAILABLE, | |
41 Source<TokenService>(token_service_)); | |
42 } | |
38 } | 43 } |
39 | 44 |
40 void DeviceTokenFetcher::Observe(NotificationType type, | 45 void DeviceTokenFetcher::Observe(NotificationType type, |
41 const NotificationSource& source, | 46 const NotificationSource& source, |
42 const NotificationDetails& details) { | 47 const NotificationDetails& details) { |
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
44 if (type == NotificationType::TOKEN_AVAILABLE) { | 49 if (type == NotificationType::TOKEN_AVAILABLE) { |
45 const Source<TokenService> token_service(source); | 50 if (Source<TokenService>(source).ptr() == token_service_) { |
46 const TokenService::TokenAvailableDetails* token_details = | 51 const TokenService::TokenAvailableDetails* token_details = |
47 Details<const TokenService::TokenAvailableDetails>(details).ptr(); | 52 Details<const TokenService::TokenAvailableDetails>(details).ptr(); |
48 if (token_details->service() == GaiaConstants::kDeviceManagementService) { | 53 if (token_details->service() == GaiaConstants::kDeviceManagementService) { |
49 if (!HasAuthToken()) { | 54 if (!HasAuthToken()) { |
50 auth_token_ = token_details->token(); | 55 auth_token_ = token_details->token(); |
51 SendServerRequestIfPossible(); | 56 SendServerRequestIfPossible(); |
57 } | |
52 } | 58 } |
53 } | 59 } |
54 } else { | 60 } else { |
55 NOTREACHED(); | 61 NOTREACHED(); |
56 } | 62 } |
57 } | 63 } |
58 | 64 |
59 void DeviceTokenFetcher::HandleRegisterResponse( | 65 void DeviceTokenFetcher::HandleRegisterResponse( |
60 const em::DeviceRegisterResponse& response) { | 66 const em::DeviceRegisterResponse& response) { |
61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 // The file calls for loading the persisted token must be deferred to the | 101 // The file calls for loading the persisted token must be deferred to the |
96 // FILE thread. | 102 // FILE thread. |
97 BrowserThread::PostTask( | 103 BrowserThread::PostTask( |
98 BrowserThread::FILE, | 104 BrowserThread::FILE, |
99 FROM_HERE, | 105 FROM_HERE, |
100 NewRunnableMethod(this, | 106 NewRunnableMethod(this, |
101 &DeviceTokenFetcher::AttemptTokenLoadFromDisk)); | 107 &DeviceTokenFetcher::AttemptTokenLoadFromDisk)); |
102 } | 108 } |
103 } | 109 } |
104 | 110 |
111 void DeviceTokenFetcher::Shutdown() { | |
112 token_service_ = NULL; | |
113 backend_ = NULL; | |
114 } | |
115 | |
105 void DeviceTokenFetcher::AttemptTokenLoadFromDisk() { | 116 void DeviceTokenFetcher::AttemptTokenLoadFromDisk() { |
106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
107 if (file_util::PathExists(token_path_)) { | 118 if (file_util::PathExists(token_path_)) { |
108 std::string data; | 119 std::string data; |
109 em::DeviceCredentials device_credentials; | 120 em::DeviceCredentials device_credentials; |
110 if (file_util::ReadFileToString(token_path_, &data) && | 121 if (file_util::ReadFileToString(token_path_, &data) && |
111 device_credentials.ParseFromArray(data.c_str(), data.size())) { | 122 device_credentials.ParseFromArray(data.c_str(), data.size())) { |
112 device_token_ = device_credentials.device_token(); | 123 device_token_ = device_credentials.device_token(); |
113 device_id_ = device_credentials.device_id(); | 124 device_id_ = device_credentials.device_id(); |
114 if (!device_token_.empty() && !device_id_.empty()) { | 125 if (!device_token_.empty() && !device_id_.empty()) { |
(...skipping 17 matching lines...) Expand all Loading... | |
132 | 143 |
133 void DeviceTokenFetcher::MakeReadyToRequestDeviceToken() { | 144 void DeviceTokenFetcher::MakeReadyToRequestDeviceToken() { |
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
135 SetState(kStateReadyToRequestDeviceTokenFromServer); | 146 SetState(kStateReadyToRequestDeviceTokenFromServer); |
136 SendServerRequestIfPossible(); | 147 SendServerRequestIfPossible(); |
137 } | 148 } |
138 | 149 |
139 void DeviceTokenFetcher::SendServerRequestIfPossible() { | 150 void DeviceTokenFetcher::SendServerRequestIfPossible() { |
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
141 if (state_ == kStateReadyToRequestDeviceTokenFromServer | 152 if (state_ == kStateReadyToRequestDeviceTokenFromServer |
142 && HasAuthToken()) { | 153 && HasAuthToken() |
154 && backend_) { | |
143 em::DeviceRegisterRequest register_request; | 155 em::DeviceRegisterRequest register_request; |
144 SetState(kStateRequestingDeviceTokenFromServer); | 156 SetState(kStateRequestingDeviceTokenFromServer); |
145 backend_->ProcessRegisterRequest(auth_token_, | 157 backend_->ProcessRegisterRequest(auth_token_, |
146 GetDeviceID(), | 158 GetDeviceID(), |
147 register_request, | 159 register_request, |
148 this); | 160 this); |
149 } | 161 } |
150 } | 162 } |
151 | 163 |
152 bool DeviceTokenFetcher::IsTokenPending() { | 164 bool DeviceTokenFetcher::IsTokenPending() { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 DCHECK(no_error); | 220 DCHECK(no_error); |
209 file_util::WriteFile(path, data.c_str(), data.length()); | 221 file_util::WriteFile(path, data.c_str(), data.length()); |
210 } | 222 } |
211 | 223 |
212 // static | 224 // static |
213 std::string DeviceTokenFetcher::GenerateNewDeviceID() { | 225 std::string DeviceTokenFetcher::GenerateNewDeviceID() { |
214 return guid::GenerateGUID(); | 226 return guid::GenerateGUID(); |
215 } | 227 } |
216 | 228 |
217 } // namespace policy | 229 } // namespace policy |
OLD | NEW |