| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/cloud/user_policy_signin_service.h" | 5 #include "chrome/browser/policy/cloud/user_policy_signin_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
| 13 #include "chrome/browser/policy/cloud/cloud_policy_client_registration_helper.h" | 13 #include "chrome/browser/policy/cloud/cloud_policy_client_registration_helper.h" |
| 14 #include "chrome/browser/policy/cloud/user_cloud_policy_manager.h" | 14 #include "chrome/browser/policy/cloud/user_cloud_policy_manager.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/profiles/profile_manager.h" | 16 #include "chrome/browser/profiles/profile_manager.h" |
| 17 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
| 18 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 17 #include "chrome/browser/signin/signin_manager.h" | 19 #include "chrome/browser/signin/signin_manager.h" |
| 18 #include "chrome/browser/signin/signin_manager_factory.h" | 20 #include "chrome/browser/signin/signin_manager_factory.h" |
| 19 #include "chrome/browser/signin/token_service.h" | |
| 20 #include "chrome/browser/signin/token_service_factory.h" | |
| 21 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| 22 #include "content/public/browser/notification_details.h" | 22 #include "content/public/browser/notification_details.h" |
| 23 #include "content/public/browser/notification_source.h" | 23 #include "content/public/browser/notification_source.h" |
| 24 #include "google_apis/gaia/gaia_constants.h" | 24 #include "google_apis/gaia/gaia_constants.h" |
| 25 | 25 |
| 26 namespace policy { | 26 namespace policy { |
| 27 | 27 |
| 28 UserPolicySigninService::UserPolicySigninService( | 28 UserPolicySigninService::UserPolicySigninService( |
| 29 Profile* profile, | 29 Profile* profile, |
| 30 PrefService* local_state, | 30 PrefService* local_state, |
| 31 DeviceManagementService* device_management_service) | 31 DeviceManagementService* device_management_service, |
| 32 SigninManager* signin_manager, |
| 33 ProfileOAuth2TokenService* token_service) |
| 32 : UserPolicySigninServiceBase(profile, | 34 : UserPolicySigninServiceBase(profile, |
| 33 local_state, | 35 local_state, |
| 34 device_management_service) { | 36 device_management_service, |
| 37 signin_manager), |
| 38 oauth2_token_service_(token_service) { |
| 35 if (profile->GetPrefs()->GetBoolean(prefs::kDisableCloudPolicyOnSignin)) | 39 if (profile->GetPrefs()->GetBoolean(prefs::kDisableCloudPolicyOnSignin)) |
| 36 return; | 40 return; |
| 37 | 41 |
| 42 // ProfileOAuth2TokenService should not yet have loaded its tokens since this |
| 43 // happens in the background after PKS initialization - so this service |
| 44 // should always be created before the oauth token is available. |
| 45 DCHECK(!oauth2_token_service_->RefreshTokenIsAvailable()); |
| 46 |
| 38 // Listen for an OAuth token to become available so we can register a client | 47 // Listen for an OAuth token to become available so we can register a client |
| 39 // if for some reason the client is not already registered (for example, if | 48 // if for some reason the client is not already registered (for example, if |
| 40 // the policy load failed during initial signin). | 49 // the policy load failed during initial signin). |
| 41 registrar()->Add(this, | 50 oauth2_token_service_->AddObserver(this); |
| 42 chrome::NOTIFICATION_TOKEN_AVAILABLE, | |
| 43 content::Source<TokenService>( | |
| 44 TokenServiceFactory::GetForProfile(profile))); | |
| 45 | |
| 46 // TokenService should not yet have loaded its tokens since this happens in | |
| 47 // the background after PKS initialization - so this service should always be | |
| 48 // created before the oauth token is available. | |
| 49 DCHECK(!TokenServiceFactory::GetForProfile(profile)->HasOAuthLoginToken()); | |
| 50 } | 51 } |
| 51 | 52 |
| 52 UserPolicySigninService::~UserPolicySigninService() {} | 53 UserPolicySigninService::~UserPolicySigninService() { |
| 54 } |
| 53 | 55 |
| 54 void UserPolicySigninService::Shutdown() { | 56 void UserPolicySigninService::PrepareForUserCloudPolicyManagerShutdown() { |
| 55 // Stop any pending registration helper activity. We do this here instead of | 57 // Stop any pending registration helper activity. We do this here instead of |
| 56 // in the destructor because we want to shutdown the registration helper | 58 // in the destructor because we want to shutdown the registration helper |
| 57 // before UserCloudPolicyManager shuts down the CloudPolicyClient. | 59 // before UserCloudPolicyManager shuts down the CloudPolicyClient. |
| 58 registration_helper_.reset(); | 60 registration_helper_.reset(); |
| 61 |
| 62 UserPolicySigninServiceBase::PrepareForUserCloudPolicyManagerShutdown(); |
| 63 } |
| 64 |
| 65 void UserPolicySigninService::Shutdown() { |
| 59 UserPolicySigninServiceBase::Shutdown(); | 66 UserPolicySigninServiceBase::Shutdown(); |
| 67 oauth2_token_service_->RemoveObserver(this); |
| 60 } | 68 } |
| 61 | 69 |
| 62 void UserPolicySigninService::RegisterPolicyClient( | 70 void UserPolicySigninService::RegisterPolicyClient( |
| 63 const std::string& username, | 71 const std::string& username, |
| 64 const std::string& oauth2_refresh_token, | 72 const std::string& oauth2_refresh_token, |
| 65 const PolicyRegistrationCallback& callback) { | 73 const PolicyRegistrationCallback& callback) { |
| 66 DCHECK(!oauth2_refresh_token.empty()); | 74 DCHECK(!oauth2_refresh_token.empty()); |
| 67 | 75 |
| 68 // Create a new CloudPolicyClient for fetching the DMToken. | 76 // Create a new CloudPolicyClient for fetching the DMToken. |
| 69 scoped_ptr<CloudPolicyClient> policy_client = PrepareToRegister(username); | 77 scoped_ptr<CloudPolicyClient> policy_client = PrepareToRegister(username); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 91 scoped_ptr<CloudPolicyClient> client, | 99 scoped_ptr<CloudPolicyClient> client, |
| 92 PolicyRegistrationCallback callback) { | 100 PolicyRegistrationCallback callback) { |
| 93 registration_helper_.reset(); | 101 registration_helper_.reset(); |
| 94 if (!client->is_registered()) { | 102 if (!client->is_registered()) { |
| 95 // Registration failed, so free the client and pass NULL to the callback. | 103 // Registration failed, so free the client and pass NULL to the callback. |
| 96 client.reset(); | 104 client.reset(); |
| 97 } | 105 } |
| 98 callback.Run(client.Pass()); | 106 callback.Run(client.Pass()); |
| 99 } | 107 } |
| 100 | 108 |
| 101 void UserPolicySigninService::Observe( | 109 void UserPolicySigninService::OnRefreshTokenAvailable( |
| 102 int type, | 110 const std::string& account_id) { |
| 103 const content::NotificationSource& source, | 111 // If using a TestingProfile with no UserCloudPolicyManager, skip |
| 104 const content::NotificationDetails& details) { | 112 // initialization. |
| 105 | 113 if (!GetManager()) { |
| 106 if (profile()->IsManaged()) { | |
| 107 registrar()->RemoveAll(); | |
| 108 return; | |
| 109 } | |
| 110 | |
| 111 // If using a TestingProfile with no SigninManager or UserCloudPolicyManager, | |
| 112 // skip initialization. | |
| 113 if (!GetManager() || !SigninManagerFactory::GetForProfile(profile())) { | |
| 114 DVLOG(1) << "Skipping initialization for tests due to missing components."; | 114 DVLOG(1) << "Skipping initialization for tests due to missing components."; |
| 115 return; | 115 return; |
| 116 } | 116 } |
| 117 | 117 |
| 118 switch (type) { | 118 std::string username = signin_manager()->GetAuthenticatedUsername(); |
| 119 case chrome::NOTIFICATION_TOKEN_AVAILABLE: { | 119 // Should not have OAuth tokens if the user isn't signed in. |
| 120 const TokenService::TokenAvailableDetails& token_details = | 120 DCHECK(!username.empty()); |
| 121 *(content::Details<const TokenService::TokenAvailableDetails>( | 121 // ProfileOAuth2TokenService now has a refresh token so initialize the |
| 122 details).ptr()); | 122 // UserCloudPolicyManager. |
| 123 if (token_details.service() == | 123 InitializeForSignedInUser(username); |
| 124 GaiaConstants::kGaiaOAuth2LoginRefreshToken) { | |
| 125 SigninManager* signin_manager = | |
| 126 SigninManagerFactory::GetForProfile(profile()); | |
| 127 std::string username = signin_manager->GetAuthenticatedUsername(); | |
| 128 // Should not have GAIA tokens if the user isn't signed in. | |
| 129 DCHECK(!username.empty()); | |
| 130 // TokenService now has a refresh token (implying that the user is | |
| 131 // signed in) so initialize the UserCloudPolicyManager. | |
| 132 InitializeForSignedInUser(username); | |
| 133 } | |
| 134 break; | |
| 135 } | |
| 136 default: | |
| 137 UserPolicySigninServiceBase::Observe(type, source, details); | |
| 138 } | |
| 139 } | 124 } |
| 140 | 125 |
| 141 void UserPolicySigninService::InitializeUserCloudPolicyManager( | 126 void UserPolicySigninService::InitializeUserCloudPolicyManager( |
| 142 scoped_ptr<CloudPolicyClient> client) { | 127 scoped_ptr<CloudPolicyClient> client) { |
| 143 UserPolicySigninServiceBase::InitializeUserCloudPolicyManager(client.Pass()); | 128 UserPolicySigninServiceBase::InitializeUserCloudPolicyManager(client.Pass()); |
| 144 ProhibitSignoutIfNeeded(); | 129 ProhibitSignoutIfNeeded(); |
| 145 } | 130 } |
| 146 | 131 |
| 147 void UserPolicySigninService::ShutdownUserCloudPolicyManager() { | 132 void UserPolicySigninService::ShutdownUserCloudPolicyManager() { |
| 148 UserCloudPolicyManager* manager = GetManager(); | 133 UserCloudPolicyManager* manager = GetManager(); |
| 149 if (manager) { | 134 // Allow the user to signout again. |
| 150 // Allow the user to signout again. | 135 if (manager) |
| 151 SigninManagerFactory::GetForProfile(profile())->ProhibitSignout(false); | 136 signin_manager()->ProhibitSignout(false); |
| 152 } | |
| 153 UserPolicySigninServiceBase::ShutdownUserCloudPolicyManager(); | 137 UserPolicySigninServiceBase::ShutdownUserCloudPolicyManager(); |
| 154 } | 138 } |
| 155 | 139 |
| 156 void UserPolicySigninService::OnInitializationCompleted( | 140 void UserPolicySigninService::OnInitializationCompleted( |
| 157 CloudPolicyService* service) { | 141 CloudPolicyService* service) { |
| 158 UserCloudPolicyManager* manager = GetManager(); | 142 UserCloudPolicyManager* manager = GetManager(); |
| 159 DCHECK_EQ(service, manager->core()->service()); | 143 DCHECK_EQ(service, manager->core()->service()); |
| 160 DCHECK(service->IsInitializationComplete()); | 144 DCHECK(service->IsInitializationComplete()); |
| 161 // The service is now initialized - if the client is not yet registered, then | 145 // The service is now initialized - if the client is not yet registered, then |
| 162 // it means that there is no cached policy and so we need to initiate a new | 146 // it means that there is no cached policy and so we need to initiate a new |
| 163 // client registration. | 147 // client registration. |
| 164 DVLOG_IF(1, manager->IsClientRegistered()) | 148 DVLOG_IF(1, manager->IsClientRegistered()) |
| 165 << "Client already registered - not fetching DMToken"; | 149 << "Client already registered - not fetching DMToken"; |
| 166 if (!manager->IsClientRegistered()) { | 150 if (!manager->IsClientRegistered()) { |
| 167 std::string token = TokenServiceFactory::GetForProfile(profile())-> | 151 if (!oauth2_token_service_->RefreshTokenIsAvailable()) { |
| 168 GetOAuth2LoginRefreshToken(); | 152 // No token yet - this class listens for OnRefreshTokenAvailable() |
| 169 if (token.empty()) { | |
| 170 // No token yet - this class listens for NOTIFICATION_TOKEN_AVAILABLE | |
| 171 // and will re-attempt registration once the token is available. | 153 // and will re-attempt registration once the token is available. |
| 172 DLOG(WARNING) << "No OAuth Refresh Token - delaying policy download"; | 154 DLOG(WARNING) << "No OAuth Refresh Token - delaying policy download"; |
| 173 return; | 155 return; |
| 174 } | 156 } |
| 175 RegisterCloudPolicyService(token); | 157 RegisterCloudPolicyService(); |
| 176 } | 158 } |
| 177 // If client is registered now, prohibit signout. | 159 // If client is registered now, prohibit signout. |
| 178 ProhibitSignoutIfNeeded(); | 160 ProhibitSignoutIfNeeded(); |
| 179 } | 161 } |
| 180 | 162 |
| 181 void UserPolicySigninService::RegisterCloudPolicyService( | 163 void UserPolicySigninService::RegisterCloudPolicyService() { |
| 182 const std::string& login_token) { | |
| 183 DCHECK(!GetManager()->IsClientRegistered()); | 164 DCHECK(!GetManager()->IsClientRegistered()); |
| 184 DVLOG(1) << "Fetching new DM Token"; | 165 DVLOG(1) << "Fetching new DM Token"; |
| 185 // Do nothing if already starting the registration process. | 166 // Do nothing if already starting the registration process. |
| 186 if (registration_helper_) | 167 if (registration_helper_) |
| 187 return; | 168 return; |
| 188 | 169 |
| 189 // Start the process of registering the CloudPolicyClient. Once it completes, | 170 // Start the process of registering the CloudPolicyClient. Once it completes, |
| 190 // policy fetch will automatically happen. | 171 // policy fetch will automatically happen. |
| 191 registration_helper_.reset(new CloudPolicyClientRegistrationHelper( | 172 registration_helper_.reset(new CloudPolicyClientRegistrationHelper( |
| 192 profile()->GetRequestContext(), | 173 profile()->GetRequestContext(), |
| 193 GetManager()->core()->client(), | 174 GetManager()->core()->client(), |
| 194 ShouldForceLoadPolicy(), | 175 ShouldForceLoadPolicy(), |
| 195 enterprise_management::DeviceRegisterRequest::BROWSER)); | 176 enterprise_management::DeviceRegisterRequest::BROWSER)); |
| 196 registration_helper_->StartRegistrationWithLoginToken( | 177 registration_helper_->StartRegistration( |
| 197 login_token, | 178 oauth2_token_service_, |
| 179 signin_manager()->GetAuthenticatedUsername(), |
| 198 base::Bind(&UserPolicySigninService::OnRegistrationComplete, | 180 base::Bind(&UserPolicySigninService::OnRegistrationComplete, |
| 199 base::Unretained(this))); | 181 base::Unretained(this))); |
| 200 } | 182 } |
| 201 | 183 |
| 202 void UserPolicySigninService::OnRegistrationComplete() { | 184 void UserPolicySigninService::OnRegistrationComplete() { |
| 203 ProhibitSignoutIfNeeded(); | 185 ProhibitSignoutIfNeeded(); |
| 204 registration_helper_.reset(); | 186 registration_helper_.reset(); |
| 205 } | 187 } |
| 206 | 188 |
| 207 void UserPolicySigninService::ProhibitSignoutIfNeeded() { | 189 void UserPolicySigninService::ProhibitSignoutIfNeeded() { |
| 208 if (GetManager()->IsClientRegistered()) { | 190 if (GetManager()->IsClientRegistered()) { |
| 209 DVLOG(1) << "User is registered for policy - prohibiting signout"; | 191 DVLOG(1) << "User is registered for policy - prohibiting signout"; |
| 210 SigninManager* signin_manager = | 192 signin_manager()->ProhibitSignout(true); |
| 211 SigninManagerFactory::GetForProfile(profile()); | |
| 212 signin_manager->ProhibitSignout(true); | |
| 213 } | 193 } |
| 214 } | 194 } |
| 215 | 195 |
| 216 } // namespace policy | 196 } // namespace policy |
| OLD | NEW |