| 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 NewRunnableFunction(&file_util::Delete, token_path_, false)); |
| 91 } | 94 } |
| 92 SetState(kStateFailure); | 95 SetState(kStateFailure); |
| 93 } | 96 } |
| 94 | 97 |
| 98 void DeviceTokenFetcher::Restart() { |
| 99 DCHECK(!IsTokenPending()); |
| 100 device_token_.clear(); |
| 101 device_token_load_complete_event_.Reset(); |
| 102 MakeReadyToRequestDeviceToken(); |
| 103 } |
| 104 |
| 95 void DeviceTokenFetcher::StartFetching() { | 105 void DeviceTokenFetcher::StartFetching() { |
| 96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 97 if (state_ == kStateNotStarted) { | 107 if (state_ == kStateNotStarted) { |
| 98 SetState(kStateLoadDeviceTokenFromDisk); | 108 SetState(kStateLoadDeviceTokenFromDisk); |
| 99 // The file calls for loading the persisted token must be deferred to the | 109 // The file calls for loading the persisted token must be deferred to the |
| 100 // FILE thread. | 110 // FILE thread. |
| 101 BrowserThread::PostTask( | 111 BrowserThread::PostTask( |
| 102 BrowserThread::FILE, | 112 BrowserThread::FILE, |
| 103 FROM_HERE, | 113 FROM_HERE, |
| 104 NewRunnableMethod(this, | 114 NewRunnableMethod(this, |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 return device_id_; | 189 return device_id_; |
| 180 } | 190 } |
| 181 | 191 |
| 182 void DeviceTokenFetcher::SetState(FetcherState state) { | 192 void DeviceTokenFetcher::SetState(FetcherState state) { |
| 183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 184 if (state_ == state) | 194 if (state_ == state) |
| 185 return; | 195 return; |
| 186 state_ = state; | 196 state_ = state; |
| 187 if (state == kStateFailure) { | 197 if (state == kStateFailure) { |
| 188 device_token_load_complete_event_.Signal(); | 198 device_token_load_complete_event_.Signal(); |
| 199 NotifyTokenError(); |
| 189 } else if (state == kStateHasDeviceToken) { | 200 } else if (state == kStateHasDeviceToken) { |
| 190 device_token_load_complete_event_.Signal(); | 201 device_token_load_complete_event_.Signal(); |
| 191 NotificationService::current()->Notify( | 202 NotifyTokenSuccess(); |
| 192 NotificationType::DEVICE_TOKEN_AVAILABLE, | |
| 193 Source<DeviceTokenFetcher>(this), | |
| 194 NotificationService::NoDetails()); | |
| 195 } | 203 } |
| 196 } | 204 } |
| 197 | 205 |
| 198 void DeviceTokenFetcher::GetDeviceTokenPath(FilePath* token_path) const { | 206 void DeviceTokenFetcher::GetDeviceTokenPath(FilePath* token_path) const { |
| 199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 200 *token_path = token_path_; | 208 *token_path = token_path_; |
| 201 } | 209 } |
| 202 | 210 |
| 203 bool DeviceTokenFetcher::IsTokenValid() const { | 211 bool DeviceTokenFetcher::IsTokenValid() const { |
| 204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 218 DCHECK(no_error); | 226 DCHECK(no_error); |
| 219 file_util::WriteFile(path, data.c_str(), data.length()); | 227 file_util::WriteFile(path, data.c_str(), data.length()); |
| 220 } | 228 } |
| 221 | 229 |
| 222 // static | 230 // static |
| 223 std::string DeviceTokenFetcher::GenerateNewDeviceID() { | 231 std::string DeviceTokenFetcher::GenerateNewDeviceID() { |
| 224 return guid::GenerateGUID(); | 232 return guid::GenerateGUID(); |
| 225 } | 233 } |
| 226 | 234 |
| 227 } // namespace policy | 235 } // namespace policy |
| OLD | NEW |