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" |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 } | 80 } |
81 } | 81 } |
82 | 82 |
83 void DeviceTokenFetcher::OnError(DeviceManagementBackend::ErrorCode code) { | 83 void DeviceTokenFetcher::OnError(DeviceManagementBackend::ErrorCode code) { |
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
85 // For privacy reasons, delete all identifying data when this device is not | 85 // For privacy reasons, delete all identifying data when this device is not |
86 // managed. | 86 // managed. |
87 if (code == DeviceManagementBackend::kErrorServiceManagementNotSupported) { | 87 if (code == DeviceManagementBackend::kErrorServiceManagementNotSupported) { |
88 device_token_ = std::string(); | 88 device_token_ = std::string(); |
89 device_id_ = std::string(); | 89 device_id_ = std::string(); |
90 file_util::Delete(token_path_, false); | 90 BrowserThread::PostTask( |
| 91 BrowserThread::FILE, |
| 92 FROM_HERE, |
| 93 // The Windows compiler needs explicit template instantiation. |
| 94 NewRunnableFunction<bool(*)(const FilePath&, bool), FilePath, bool>( |
| 95 &file_util::Delete, token_path_, false)); |
| 96 SetState(kStateNotManaged); |
| 97 return; |
91 } | 98 } |
92 SetState(kStateFailure); | 99 SetState(kStateFailure); |
93 } | 100 } |
94 | 101 |
| 102 void DeviceTokenFetcher::Restart() { |
| 103 DCHECK(!IsTokenPending()); |
| 104 device_token_.clear(); |
| 105 device_token_load_complete_event_.Reset(); |
| 106 MakeReadyToRequestDeviceToken(); |
| 107 } |
| 108 |
95 void DeviceTokenFetcher::StartFetching() { | 109 void DeviceTokenFetcher::StartFetching() { |
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
97 if (state_ == kStateNotStarted) { | 111 if (state_ == kStateNotStarted) { |
98 SetState(kStateLoadDeviceTokenFromDisk); | 112 SetState(kStateLoadDeviceTokenFromDisk); |
99 // The file calls for loading the persisted token must be deferred to the | 113 // The file calls for loading the persisted token must be deferred to the |
100 // FILE thread. | 114 // FILE thread. |
101 BrowserThread::PostTask( | 115 BrowserThread::PostTask( |
102 BrowserThread::FILE, | 116 BrowserThread::FILE, |
103 FROM_HERE, | 117 FROM_HERE, |
104 NewRunnableMethod(this, | 118 NewRunnableMethod(this, |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 return device_id_; | 193 return device_id_; |
180 } | 194 } |
181 | 195 |
182 void DeviceTokenFetcher::SetState(FetcherState state) { | 196 void DeviceTokenFetcher::SetState(FetcherState state) { |
183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
184 if (state_ == state) | 198 if (state_ == state) |
185 return; | 199 return; |
186 state_ = state; | 200 state_ = state; |
187 if (state == kStateFailure) { | 201 if (state == kStateFailure) { |
188 device_token_load_complete_event_.Signal(); | 202 device_token_load_complete_event_.Signal(); |
| 203 NotifyTokenError(); |
| 204 } else if (state == kStateNotManaged) { |
| 205 device_token_load_complete_event_.Signal(); |
| 206 NotifyNotManaged(); |
189 } else if (state == kStateHasDeviceToken) { | 207 } else if (state == kStateHasDeviceToken) { |
190 device_token_load_complete_event_.Signal(); | 208 device_token_load_complete_event_.Signal(); |
191 NotificationService::current()->Notify( | 209 NotifyTokenSuccess(); |
192 NotificationType::DEVICE_TOKEN_AVAILABLE, | |
193 Source<DeviceTokenFetcher>(this), | |
194 NotificationService::NoDetails()); | |
195 } | 210 } |
196 } | 211 } |
197 | 212 |
198 void DeviceTokenFetcher::GetDeviceTokenPath(FilePath* token_path) const { | 213 void DeviceTokenFetcher::GetDeviceTokenPath(FilePath* token_path) const { |
199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
200 *token_path = token_path_; | 215 *token_path = token_path_; |
201 } | 216 } |
202 | 217 |
203 bool DeviceTokenFetcher::IsTokenValid() const { | 218 bool DeviceTokenFetcher::IsTokenValid() const { |
204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 219 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 13 matching lines...) Expand all Loading... |
218 DCHECK(no_error); | 233 DCHECK(no_error); |
219 file_util::WriteFile(path, data.c_str(), data.length()); | 234 file_util::WriteFile(path, data.c_str(), data.length()); |
220 } | 235 } |
221 | 236 |
222 // static | 237 // static |
223 std::string DeviceTokenFetcher::GenerateNewDeviceID() { | 238 std::string DeviceTokenFetcher::GenerateNewDeviceID() { |
224 return guid::GenerateGUID(); | 239 return guid::GenerateGUID(); |
225 } | 240 } |
226 | 241 |
227 } // namespace policy | 242 } // namespace policy |
OLD | NEW |