Index: chrome/browser/policy/cros_user_policy_identity_strategy.cc |
diff --git a/chrome/browser/policy/user_policy_identity_strategy.cc b/chrome/browser/policy/cros_user_policy_identity_strategy.cc |
similarity index 55% |
copy from chrome/browser/policy/user_policy_identity_strategy.cc |
copy to chrome/browser/policy/cros_user_policy_identity_strategy.cc |
index e599101b8b6c38af06fad286eadd71af07c11fc6..a0eab52589c835d73e561a28b7327b1c5f1585b0 100644 |
--- a/chrome/browser/policy/user_policy_identity_strategy.cc |
+++ b/chrome/browser/policy/cros_user_policy_identity_strategy.cc |
@@ -2,9 +2,11 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "chrome/browser/policy/user_policy_identity_strategy.h" |
+#include "chrome/browser/policy/cros_user_policy_identity_strategy.h" |
+#include "base/file_util.h" |
#include "chrome/browser/browser_signin.h" |
+#include "chrome/browser/chromeos/login/user_manager.h" |
#include "chrome/browser/net/gaia/token_service.h" |
#include "chrome/browser/policy/proto/device_management_backend.pb.h" |
#include "chrome/browser/policy/proto/device_management_constants.h" |
@@ -16,70 +18,70 @@ |
#include "content/common/notification_service.h" |
#include "content/common/notification_source.h" |
-#if defined(OS_CHROMEOS) |
-#include "chrome/browser/chromeos/login/user_manager.h" |
-#endif |
- |
namespace policy { |
namespace em = enterprise_management; |
-UserPolicyIdentityStrategy::UserPolicyIdentityStrategy( |
- Profile* profile, |
- const FilePath& cache_file) |
- : profile_(profile), |
- ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
- cache_ = new UserPolicyTokenCache(weak_ptr_factory_.GetWeakPtr(), cache_file); |
+CrosUserPolicyIdentityStrategy::CrosUserPolicyIdentityStrategy(Profile* profile) |
+ : should_register_(false), |
+ profile_(profile) { |
registrar_.Add(this, |
NotificationType::TOKEN_AVAILABLE, |
Source<TokenService>(profile->GetTokenService())); |
// Register for the event of user login. The device management token won't |
// be fetched until we know the domain of the currently logged in user. |
-#if defined(OS_CHROMEOS) |
- registrar_.Add(this, |
+ registrar_.Add(this, |
NotificationType::LOGIN_USER_CHANGED, |
NotificationService::AllSources()); |
-#else |
- registrar_.Add(this, |
- NotificationType::GOOGLE_SIGNIN_SUCCESSFUL, |
- Source<Profile>(profile_)); |
-#endif |
} |
-UserPolicyIdentityStrategy::~UserPolicyIdentityStrategy() {} |
+CrosUserPolicyIdentityStrategy::~CrosUserPolicyIdentityStrategy() {} |
+ |
+void CrosUserPolicyIdentityStrategy::SetDeviceCredentials( |
+ const std::string& device_id, |
+ const std::string& token) { |
+ should_register_ = true; |
+ device_id_ = device_id; |
+ device_token_ = token; |
+ NotifyDeviceTokenChanged(); |
+} |
-void UserPolicyIdentityStrategy::LoadTokenCache() { |
- cache_->Load(); |
+void CrosUserPolicyIdentityStrategy::EnableRegistration() { |
+ should_register_ = true; |
+ CheckAndTriggerFetch(); |
} |
-std::string UserPolicyIdentityStrategy::GetDeviceToken() { |
+std::string CrosUserPolicyIdentityStrategy::GetDeviceToken() { |
return device_token_; |
} |
-std::string UserPolicyIdentityStrategy::GetDeviceID() { |
+std::string CrosUserPolicyIdentityStrategy::GetDeviceID() { |
return device_id_; |
} |
-std::string UserPolicyIdentityStrategy::GetMachineID() { |
+std::string CrosUserPolicyIdentityStrategy::GetMachineID() { |
return std::string(); |
} |
-std::string UserPolicyIdentityStrategy::GetMachineModel() { |
+std::string CrosUserPolicyIdentityStrategy::GetMachineModel() { |
return std::string(); |
} |
em::DeviceRegisterRequest_Type |
-UserPolicyIdentityStrategy::GetPolicyRegisterType() { |
+CrosUserPolicyIdentityStrategy::GetPolicyRegisterType() { |
return em::DeviceRegisterRequest::USER; |
} |
-std::string UserPolicyIdentityStrategy::GetPolicyType() { |
+std::string CrosUserPolicyIdentityStrategy::GetPolicyType() { |
return kChromeUserPolicyType; |
} |
-bool UserPolicyIdentityStrategy::GetCredentials(std::string* username, |
- std::string* auth_token) { |
+bool CrosUserPolicyIdentityStrategy::GetCredentials(std::string* username, |
+ std::string* auth_token) { |
+ if (!should_register_) |
+ return false; |
+ |
*username = GetCurrentUser(); |
*auth_token = profile_->GetTokenService()->GetTokenForService( |
GaiaConstants::kDeviceManagementService); |
@@ -87,26 +89,20 @@ bool UserPolicyIdentityStrategy::GetCredentials(std::string* username, |
return !username->empty() && !auth_token->empty() && !device_id_.empty(); |
} |
-void UserPolicyIdentityStrategy::OnDeviceTokenAvailable( |
+void CrosUserPolicyIdentityStrategy::OnDeviceTokenAvailable( |
const std::string& token) { |
DCHECK(!device_id_.empty()); |
device_token_ = token; |
- cache_->Store(device_token_, device_id_); |
NotifyDeviceTokenChanged(); |
} |
-std::string UserPolicyIdentityStrategy::GetCurrentUser() { |
-#if defined(OS_CHROMEOS) |
- // TODO(mnissler) On CrOS it seems impossible to figure out what user belongs |
- // to a profile. Revisit after multi-profile support landed. |
+std::string CrosUserPolicyIdentityStrategy::GetCurrentUser() { |
return chromeos::UserManager::Get()->logged_in_user().email(); |
-#else |
- return profile_->GetBrowserSignin()->GetSignedInUsername(); |
-#endif |
} |
-void UserPolicyIdentityStrategy::CheckAndTriggerFetch() { |
- if (!GetCurrentUser().empty() && |
+void CrosUserPolicyIdentityStrategy::CheckAndTriggerFetch() { |
+ if (should_register_ && |
+ !GetCurrentUser().empty() && |
profile_->GetTokenService()->HasTokenForService( |
GaiaConstants::kDeviceManagementService)) { |
// For user tokens, there is no actual identifier. We generate a random |
@@ -116,41 +112,25 @@ void UserPolicyIdentityStrategy::CheckAndTriggerFetch() { |
} |
} |
-void UserPolicyIdentityStrategy::OnTokenCacheLoaded( |
- const std::string& token, |
- const std::string& device_id) { |
- if (!token.empty() && !device_id.empty()) { |
- device_token_ = token; |
- device_id_ = device_id; |
- NotifyDeviceTokenChanged(); |
- } else { |
- CheckAndTriggerFetch(); |
- } |
-} |
- |
-void UserPolicyIdentityStrategy::Observe(NotificationType type, |
- const NotificationSource& source, |
- const NotificationDetails& details) { |
+void CrosUserPolicyIdentityStrategy::Observe( |
+ NotificationType type, |
+ const NotificationSource& source, |
+ const NotificationDetails& details) { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
if (type == NotificationType::TOKEN_AVAILABLE) { |
if (Source<TokenService>(source).ptr() == profile_->GetTokenService()) { |
const TokenService::TokenAvailableDetails* token_details = |
Details<const TokenService::TokenAvailableDetails>(details).ptr(); |
- if (token_details->service() == GaiaConstants::kDeviceManagementService) |
+ if (token_details->service() == GaiaConstants::kDeviceManagementService) { |
if (device_token_.empty()) { |
// Request a new device management server token, but only in case we |
// don't already have it. |
CheckAndTriggerFetch(); |
} |
+ } |
} |
-#if defined(OS_CHROMEOS) |
} else if (type == NotificationType::LOGIN_USER_CHANGED) { |
CheckAndTriggerFetch(); |
-#else |
- } else if (type == NotificationType::GOOGLE_SIGNIN_SUCCESSFUL) { |
- if (profile_ == Source<Profile>(source).ptr()) |
- CheckAndTriggerFetch(); |
-#endif |
} else { |
NOTREACHED(); |
} |