Index: chrome/browser/policy/device_token_fetcher.cc |
diff --git a/chrome/browser/policy/device_token_fetcher.cc b/chrome/browser/policy/device_token_fetcher.cc |
index a1ba00e9a172e3103fe8ee52ebcbfba96fd05dc9..e7f2acc8fadb34067e559e3d623ba67bddff8e63 100644 |
--- a/chrome/browser/policy/device_token_fetcher.cc |
+++ b/chrome/browser/policy/device_token_fetcher.cc |
@@ -9,6 +9,7 @@ |
#include "base/singleton.h" |
#include "chrome/browser/guid.h" |
#include "chrome/browser/net/gaia/token_service.h" |
+#include "chrome/browser/policy/proto/device_management_local.pb.h" |
#include "chrome/common/chrome_paths.h" |
#include "chrome/common/net/gaia/gaia_constants.h" |
#include "chrome/common/notification_details.h" |
@@ -18,6 +19,8 @@ |
namespace policy { |
+namespace em = enterprise_management; |
+ |
DeviceTokenFetcher::DeviceTokenFetcher( |
DeviceManagementBackend* backend, |
const FilePath& token_path) |
@@ -64,7 +67,8 @@ void DeviceTokenFetcher::HandleRegisterResponse( |
FROM_HERE, |
NewRunnableFunction(&WriteDeviceTokenToDisk, |
token_path_, |
- device_token_)); |
+ device_token_, |
+ device_id_)); |
SetState(kStateHasDeviceToken); |
} else { |
NOTREACHED(); |
@@ -93,19 +97,23 @@ void DeviceTokenFetcher::StartFetching() { |
void DeviceTokenFetcher::AttemptTokenLoadFromDisk() { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
- FetcherState new_state = kStateFailure; |
if (file_util::PathExists(token_path_)) { |
- std::string device_token; |
- if (file_util::ReadFileToString(token_path_, &device_token_)) { |
- new_state = kStateHasDeviceToken; |
+ std::string data; |
+ em::DeviceCredentials device_credentials; |
+ if (file_util::ReadFileToString(token_path_, &data) && |
+ device_credentials.ParseFromArray(data.c_str(), data.size())) { |
+ device_token_ = device_credentials.device_token(); |
+ device_id_ = device_credentials.device_id(); |
+ if (!device_token_.empty() && !device_id_.empty()) { |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, |
+ FROM_HERE, |
+ NewRunnableMethod(this, |
+ &DeviceTokenFetcher::SetState, |
+ kStateHasDeviceToken)); |
+ return; |
+ } |
} |
- BrowserThread::PostTask( |
- BrowserThread::UI, |
- FROM_HERE, |
- NewRunnableMethod(this, |
- &DeviceTokenFetcher::SetState, |
- new_state)); |
- return; |
} |
BrowserThread::PostTask( |
@@ -128,7 +136,7 @@ void DeviceTokenFetcher::SendServerRequestIfPossible() { |
em::DeviceRegisterRequest register_request; |
SetState(kStateRequestingDeviceTokenFromServer); |
backend_->ProcessRegisterRequest(auth_token_, |
- GenerateNewDeviceID(), |
+ GetDeviceID(), |
register_request, |
this); |
} |
@@ -145,6 +153,15 @@ std::string DeviceTokenFetcher::GetDeviceToken() { |
return device_token_; |
} |
+std::string DeviceTokenFetcher::GetDeviceID() { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ // As long as access to this is only allowed from the UI thread, no explicit |
+ // locking is necessary to prevent the ID from being generated twice. |
+ if (device_id_.empty()) |
+ device_id_ = GenerateNewDeviceID(); |
+ return device_id_; |
+} |
+ |
void DeviceTokenFetcher::SetState(FetcherState state) { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
if (state_ == state) |
@@ -174,10 +191,15 @@ bool DeviceTokenFetcher::IsTokenValid() const { |
// static |
void DeviceTokenFetcher::WriteDeviceTokenToDisk( |
const FilePath& path, |
- const std::string& device_token) { |
- file_util::WriteFile(path, |
- device_token.c_str(), |
- device_token.length()); |
+ const std::string& device_token, |
+ const std::string& device_id) { |
+ em::DeviceCredentials device_credentials; |
+ device_credentials.set_device_token(device_token); |
+ device_credentials.set_device_id(device_id); |
+ std::string data; |
+ bool no_error = device_credentials.SerializeToString(&data); |
+ DCHECK(no_error); |
+ file_util::WriteFile(path, data.c_str(), data.length()); |
} |
// static |