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); |
| 37 |
35 registrar_.Add(this, | 38 registrar_.Add(this, |
36 NotificationType::TOKEN_AVAILABLE, | 39 NotificationType::TOKEN_AVAILABLE, |
37 NotificationService::AllSources()); | 40 Source<TokenService>(token_service_)); |
38 } | 41 } |
39 | 42 |
40 void DeviceTokenFetcher::Observe(NotificationType type, | 43 void DeviceTokenFetcher::Observe(NotificationType type, |
41 const NotificationSource& source, | 44 const NotificationSource& source, |
42 const NotificationDetails& details) { | 45 const NotificationDetails& details) { |
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
44 if (type == NotificationType::TOKEN_AVAILABLE) { | 47 if (type == NotificationType::TOKEN_AVAILABLE) { |
45 const Source<TokenService> token_service(source); | 48 if (Source<TokenService>(source).ptr() == token_service_) { |
46 const TokenService::TokenAvailableDetails* token_details = | 49 const TokenService::TokenAvailableDetails* token_details = |
47 Details<const TokenService::TokenAvailableDetails>(details).ptr(); | 50 Details<const TokenService::TokenAvailableDetails>(details).ptr(); |
48 if (token_details->service() == GaiaConstants::kDeviceManagementService) { | 51 if (token_details->service() == GaiaConstants::kDeviceManagementService) { |
49 if (!HasAuthToken()) { | 52 if (!HasAuthToken()) { |
50 auth_token_ = token_details->token(); | 53 auth_token_ = token_details->token(); |
51 SendServerRequestIfPossible(); | 54 SendServerRequestIfPossible(); |
| 55 } |
52 } | 56 } |
53 } | 57 } |
54 } else { | 58 } else { |
55 NOTREACHED(); | 59 NOTREACHED(); |
56 } | 60 } |
57 } | 61 } |
58 | 62 |
59 void DeviceTokenFetcher::HandleRegisterResponse( | 63 void DeviceTokenFetcher::HandleRegisterResponse( |
60 const em::DeviceRegisterResponse& response) { | 64 const em::DeviceRegisterResponse& response) { |
61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 65 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 | 99 // The file calls for loading the persisted token must be deferred to the |
96 // FILE thread. | 100 // FILE thread. |
97 BrowserThread::PostTask( | 101 BrowserThread::PostTask( |
98 BrowserThread::FILE, | 102 BrowserThread::FILE, |
99 FROM_HERE, | 103 FROM_HERE, |
100 NewRunnableMethod(this, | 104 NewRunnableMethod(this, |
101 &DeviceTokenFetcher::AttemptTokenLoadFromDisk)); | 105 &DeviceTokenFetcher::AttemptTokenLoadFromDisk)); |
102 } | 106 } |
103 } | 107 } |
104 | 108 |
| 109 void DeviceTokenFetcher::Shutdown() { |
| 110 token_service_ = NULL; |
| 111 backend_ = NULL; |
| 112 } |
| 113 |
105 void DeviceTokenFetcher::AttemptTokenLoadFromDisk() { | 114 void DeviceTokenFetcher::AttemptTokenLoadFromDisk() { |
106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
107 if (file_util::PathExists(token_path_)) { | 116 if (file_util::PathExists(token_path_)) { |
108 std::string data; | 117 std::string data; |
109 em::DeviceCredentials device_credentials; | 118 em::DeviceCredentials device_credentials; |
110 if (file_util::ReadFileToString(token_path_, &data) && | 119 if (file_util::ReadFileToString(token_path_, &data) && |
111 device_credentials.ParseFromArray(data.c_str(), data.size())) { | 120 device_credentials.ParseFromArray(data.c_str(), data.size())) { |
112 device_token_ = device_credentials.device_token(); | 121 device_token_ = device_credentials.device_token(); |
113 device_id_ = device_credentials.device_id(); | 122 device_id_ = device_credentials.device_id(); |
114 if (!device_token_.empty() && !device_id_.empty()) { | 123 if (!device_token_.empty() && !device_id_.empty()) { |
(...skipping 17 matching lines...) Expand all Loading... |
132 | 141 |
133 void DeviceTokenFetcher::MakeReadyToRequestDeviceToken() { | 142 void DeviceTokenFetcher::MakeReadyToRequestDeviceToken() { |
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
135 SetState(kStateReadyToRequestDeviceTokenFromServer); | 144 SetState(kStateReadyToRequestDeviceTokenFromServer); |
136 SendServerRequestIfPossible(); | 145 SendServerRequestIfPossible(); |
137 } | 146 } |
138 | 147 |
139 void DeviceTokenFetcher::SendServerRequestIfPossible() { | 148 void DeviceTokenFetcher::SendServerRequestIfPossible() { |
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
141 if (state_ == kStateReadyToRequestDeviceTokenFromServer | 150 if (state_ == kStateReadyToRequestDeviceTokenFromServer |
142 && HasAuthToken()) { | 151 && HasAuthToken() |
| 152 && backend_) { |
143 em::DeviceRegisterRequest register_request; | 153 em::DeviceRegisterRequest register_request; |
144 SetState(kStateRequestingDeviceTokenFromServer); | 154 SetState(kStateRequestingDeviceTokenFromServer); |
145 backend_->ProcessRegisterRequest(auth_token_, | 155 backend_->ProcessRegisterRequest(auth_token_, |
146 GetDeviceID(), | 156 GetDeviceID(), |
147 register_request, | 157 register_request, |
148 this); | 158 this); |
149 } | 159 } |
150 } | 160 } |
151 | 161 |
152 bool DeviceTokenFetcher::IsTokenPending() { | 162 bool DeviceTokenFetcher::IsTokenPending() { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 DCHECK(no_error); | 218 DCHECK(no_error); |
209 file_util::WriteFile(path, data.c_str(), data.length()); | 219 file_util::WriteFile(path, data.c_str(), data.length()); |
210 } | 220 } |
211 | 221 |
212 // static | 222 // static |
213 std::string DeviceTokenFetcher::GenerateNewDeviceID() { | 223 std::string DeviceTokenFetcher::GenerateNewDeviceID() { |
214 return guid::GenerateGUID(); | 224 return guid::GenerateGUID(); |
215 } | 225 } |
216 | 226 |
217 } // namespace policy | 227 } // namespace policy |
OLD | NEW |