Index: chrome/browser/policy/user_policy_identity_strategy.cc |
diff --git a/chrome/browser/policy/user_policy_identity_strategy.cc b/chrome/browser/policy/user_policy_identity_strategy.cc |
index ae04c1414fafd9d0438780df1e7abc2eafc1e06b..9bcb53d6af864a8874130058c42744d5d34fc354 100644 |
--- a/chrome/browser/policy/user_policy_identity_strategy.cc |
+++ b/chrome/browser/policy/user_policy_identity_strategy.cc |
@@ -5,22 +5,12 @@ |
#include "chrome/browser/policy/user_policy_identity_strategy.h" |
#include "base/file_util.h" |
-#include "chrome/browser/browser_signin.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" |
#include "chrome/browser/policy/proto/device_management_local.pb.h" |
-#include "chrome/browser/profiles/profile.h" |
#include "chrome/common/guid.h" |
#include "chrome/common/net/gaia/gaia_constants.h" |
#include "content/browser/browser_thread.h" |
-#include "content/common/notification_details.h" |
-#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 { |
@@ -37,6 +27,10 @@ class UserPolicyIdentityStrategy::TokenCache |
void Load(); |
void Store(const std::string& token, const std::string& device_id); |
+ bool load_finished() { |
+ return load_finished_; |
+ } |
+ |
private: |
friend class base::RefCountedThreadSafe< |
UserPolicyIdentityStrategy::TokenCache>; |
@@ -49,6 +43,8 @@ class UserPolicyIdentityStrategy::TokenCache |
const base::WeakPtr<UserPolicyIdentityStrategy> identity_strategy_; |
const FilePath cache_file_; |
+ bool load_finished_; |
+ bool load_initiated_; |
DISALLOW_COPY_AND_ASSIGN(TokenCache); |
}; |
@@ -57,10 +53,14 @@ UserPolicyIdentityStrategy::TokenCache::TokenCache( |
const base::WeakPtr<UserPolicyIdentityStrategy>& identity_strategy, |
const FilePath& cache_file) |
: identity_strategy_(identity_strategy), |
- cache_file_(cache_file) {} |
+ cache_file_(cache_file), |
+ load_finished_(false), |
+ load_initiated_(false) {} |
void UserPolicyIdentityStrategy::TokenCache::Load() { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ DCHECK(!load_initiated_); |
+ load_initiated_ = true; |
BrowserThread::PostTask( |
BrowserThread::FILE, FROM_HERE, |
NewRunnableMethod( |
@@ -108,6 +108,8 @@ void UserPolicyIdentityStrategy::TokenCache::NotifyOnUIThread( |
const std::string& token, |
const std::string& device_id) { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ DCHECK(!load_finished_); |
+ load_finished_ = true; |
if (identity_strategy_.get()) |
identity_strategy_->OnCacheLoaded(token, device_id); |
} |
@@ -137,26 +139,11 @@ void UserPolicyIdentityStrategy::TokenCache::StoreOnFileThread( |
} |
UserPolicyIdentityStrategy::UserPolicyIdentityStrategy( |
- Profile* profile, |
+ const std::string& user_name, |
const FilePath& cache_file) |
- : profile_(profile), |
+ : user_name_(user_name), |
ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
cache_ = new TokenCache(weak_ptr_factory_.GetWeakPtr(), cache_file); |
- 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, |
- NotificationType::LOGIN_USER_CHANGED, |
- NotificationService::AllSources()); |
-#else |
- registrar_.Add(this, |
- NotificationType::GOOGLE_SIGNIN_SUCCESSFUL, |
- Source<Profile>(profile_)); |
-#endif |
} |
UserPolicyIdentityStrategy::~UserPolicyIdentityStrategy() {} |
@@ -192,9 +179,8 @@ std::string UserPolicyIdentityStrategy::GetPolicyType() { |
bool UserPolicyIdentityStrategy::GetCredentials(std::string* username, |
std::string* auth_token) { |
- *username = GetCurrentUser(); |
- *auth_token = profile_->GetTokenService()->GetTokenForService( |
- GaiaConstants::kDeviceManagementService); |
+ *username = user_name_; |
+ *auth_token = auth_token_; |
return !username->empty() && !auth_token->empty() && !device_id_.empty(); |
} |
@@ -207,20 +193,9 @@ void UserPolicyIdentityStrategy::OnDeviceTokenAvailable( |
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. |
- return chromeos::UserManager::Get()->logged_in_user().email(); |
-#else |
- return profile_->GetBrowserSignin()->GetSignedInUsername(); |
-#endif |
-} |
- |
void UserPolicyIdentityStrategy::CheckAndTriggerFetch() { |
- if (!GetCurrentUser().empty() && |
- profile_->GetTokenService()->HasTokenForService( |
- GaiaConstants::kDeviceManagementService)) { |
+ if (!user_name_.empty() && !auth_token_.empty() && cache_->load_finished()) { |
+ |
// For user tokens, there is no actual identifier. We generate a random |
// identifier instead each time we ask for the token. |
device_id_ = guid::GenerateGUID(); |
@@ -228,6 +203,15 @@ void UserPolicyIdentityStrategy::CheckAndTriggerFetch() { |
} |
} |
+void UserPolicyIdentityStrategy::SetAuthToken(const std::string& auth_token) { |
+ auth_token_ = auth_token; |
+ |
+ // Request a new device management server token, but only in case we |
+ // don't already have it. |
+ if (device_token_.empty()) |
+ CheckAndTriggerFetch(); |
+} |
+ |
void UserPolicyIdentityStrategy::OnCacheLoaded(const std::string& token, |
const std::string& device_id) { |
if (!token.empty() && !device_id.empty()) { |
@@ -239,32 +223,5 @@ void UserPolicyIdentityStrategy::OnCacheLoaded(const std::string& token, |
} |
} |
-void UserPolicyIdentityStrategy::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 (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(); |
- } |
-} |
} // namespace policy |