| 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 "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 device_management_service, | 36 device_management_service, |
| 37 policy_manager, | 37 policy_manager, |
| 38 signin_manager, | 38 signin_manager, |
| 39 system_request_context), | 39 system_request_context), |
| 40 profile_(profile), | 40 profile_(profile), |
| 41 oauth2_token_service_(token_service) { | 41 oauth2_token_service_(token_service) { |
| 42 // ProfileOAuth2TokenService should not yet have loaded its tokens since this | 42 // ProfileOAuth2TokenService should not yet have loaded its tokens since this |
| 43 // happens in the background after PKS initialization - so this service | 43 // happens in the background after PKS initialization - so this service |
| 44 // should always be created before the oauth token is available. | 44 // should always be created before the oauth token is available. |
| 45 DCHECK(!oauth2_token_service_->RefreshTokenIsAvailable( | 45 DCHECK(!oauth2_token_service_->RefreshTokenIsAvailable( |
| 46 oauth2_token_service_->GetPrimaryAccountId())); | 46 signin_manager->GetAuthenticatedAccountId())); |
| 47 | 47 |
| 48 // Listen for an OAuth token to become available so we can register a client | 48 // Listen for an OAuth token to become available so we can register a client |
| 49 // if for some reason the client is not already registered (for example, if | 49 // if for some reason the client is not already registered (for example, if |
| 50 // the policy load failed during initial signin). | 50 // the policy load failed during initial signin). |
| 51 oauth2_token_service_->AddObserver(this); | 51 oauth2_token_service_->AddObserver(this); |
| 52 } | 52 } |
| 53 | 53 |
| 54 UserPolicySigninService::~UserPolicySigninService() { | 54 UserPolicySigninService::~UserPolicySigninService() { |
| 55 } | 55 } |
| 56 | 56 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 void UserPolicySigninService::OnRefreshTokenAvailable( | 109 void UserPolicySigninService::OnRefreshTokenAvailable( |
| 110 const std::string& account_id) { | 110 const std::string& account_id) { |
| 111 // If using a TestingProfile with no UserCloudPolicyManager, skip | 111 // If using a TestingProfile with no UserCloudPolicyManager, skip |
| 112 // initialization. | 112 // initialization. |
| 113 if (!policy_manager()) { | 113 if (!policy_manager()) { |
| 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 // Ignore OAuth tokens for any account but the primary one. | 118 // Ignore OAuth tokens for any account but the primary one. |
| 119 if (account_id != oauth2_token_service_->GetPrimaryAccountId()) | 119 if (account_id != signin_manager()->GetAuthenticatedAccountId()) |
| 120 return; | 120 return; |
| 121 | 121 |
| 122 // ProfileOAuth2TokenService now has a refresh token so initialize the | 122 // ProfileOAuth2TokenService now has a refresh token so initialize the |
| 123 // UserCloudPolicyManager. | 123 // UserCloudPolicyManager. |
| 124 InitializeForSignedInUser(signin_manager()->GetAuthenticatedUsername(), | 124 InitializeForSignedInUser(signin_manager()->GetAuthenticatedUsername(), |
| 125 profile_->GetRequestContext()); | 125 profile_->GetRequestContext()); |
| 126 } | 126 } |
| 127 | 127 |
| 128 void UserPolicySigninService::InitializeUserCloudPolicyManager( | 128 void UserPolicySigninService::InitializeUserCloudPolicyManager( |
| 129 const std::string& username, | 129 const std::string& username, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 146 UserCloudPolicyManager* manager = policy_manager(); | 146 UserCloudPolicyManager* manager = policy_manager(); |
| 147 DCHECK_EQ(service, manager->core()->service()); | 147 DCHECK_EQ(service, manager->core()->service()); |
| 148 DCHECK(service->IsInitializationComplete()); | 148 DCHECK(service->IsInitializationComplete()); |
| 149 // The service is now initialized - if the client is not yet registered, then | 149 // The service is now initialized - if the client is not yet registered, then |
| 150 // it means that there is no cached policy and so we need to initiate a new | 150 // it means that there is no cached policy and so we need to initiate a new |
| 151 // client registration. | 151 // client registration. |
| 152 DVLOG_IF(1, manager->IsClientRegistered()) | 152 DVLOG_IF(1, manager->IsClientRegistered()) |
| 153 << "Client already registered - not fetching DMToken"; | 153 << "Client already registered - not fetching DMToken"; |
| 154 if (!manager->IsClientRegistered()) { | 154 if (!manager->IsClientRegistered()) { |
| 155 if (!oauth2_token_service_->RefreshTokenIsAvailable( | 155 if (!oauth2_token_service_->RefreshTokenIsAvailable( |
| 156 oauth2_token_service_->GetPrimaryAccountId())) { | 156 signin_manager()->GetAuthenticatedAccountId())) { |
| 157 // No token yet - this class listens for OnRefreshTokenAvailable() | 157 // No token yet - this class listens for OnRefreshTokenAvailable() |
| 158 // and will re-attempt registration once the token is available. | 158 // and will re-attempt registration once the token is available. |
| 159 DLOG(WARNING) << "No OAuth Refresh Token - delaying policy download"; | 159 DLOG(WARNING) << "No OAuth Refresh Token - delaying policy download"; |
| 160 return; | 160 return; |
| 161 } | 161 } |
| 162 RegisterCloudPolicyService(); | 162 RegisterCloudPolicyService(); |
| 163 } | 163 } |
| 164 // If client is registered now, prohibit signout. | 164 // If client is registered now, prohibit signout. |
| 165 ProhibitSignoutIfNeeded(); | 165 ProhibitSignoutIfNeeded(); |
| 166 } | 166 } |
| 167 | 167 |
| 168 void UserPolicySigninService::RegisterCloudPolicyService() { | 168 void UserPolicySigninService::RegisterCloudPolicyService() { |
| 169 DCHECK(!policy_manager()->IsClientRegistered()); | 169 DCHECK(!policy_manager()->IsClientRegistered()); |
| 170 DVLOG(1) << "Fetching new DM Token"; | 170 DVLOG(1) << "Fetching new DM Token"; |
| 171 // Do nothing if already starting the registration process. | 171 // Do nothing if already starting the registration process. |
| 172 if (registration_helper_) | 172 if (registration_helper_) |
| 173 return; | 173 return; |
| 174 | 174 |
| 175 // Start the process of registering the CloudPolicyClient. Once it completes, | 175 // Start the process of registering the CloudPolicyClient. Once it completes, |
| 176 // policy fetch will automatically happen. | 176 // policy fetch will automatically happen. |
| 177 registration_helper_.reset(new CloudPolicyClientRegistrationHelper( | 177 registration_helper_.reset(new CloudPolicyClientRegistrationHelper( |
| 178 policy_manager()->core()->client(), | 178 policy_manager()->core()->client(), |
| 179 ShouldForceLoadPolicy(), | 179 ShouldForceLoadPolicy(), |
| 180 enterprise_management::DeviceRegisterRequest::BROWSER)); | 180 enterprise_management::DeviceRegisterRequest::BROWSER)); |
| 181 registration_helper_->StartRegistration( | 181 registration_helper_->StartRegistration( |
| 182 oauth2_token_service_, | 182 oauth2_token_service_, |
| 183 oauth2_token_service_->GetPrimaryAccountId(), | 183 signin_manager()->GetAuthenticatedAccountId(), |
| 184 base::Bind(&UserPolicySigninService::OnRegistrationComplete, | 184 base::Bind(&UserPolicySigninService::OnRegistrationComplete, |
| 185 base::Unretained(this))); | 185 base::Unretained(this))); |
| 186 } | 186 } |
| 187 | 187 |
| 188 void UserPolicySigninService::OnRegistrationComplete() { | 188 void UserPolicySigninService::OnRegistrationComplete() { |
| 189 ProhibitSignoutIfNeeded(); | 189 ProhibitSignoutIfNeeded(); |
| 190 registration_helper_.reset(); | 190 registration_helper_.reset(); |
| 191 } | 191 } |
| 192 | 192 |
| 193 void UserPolicySigninService::ProhibitSignoutIfNeeded() { | 193 void UserPolicySigninService::ProhibitSignoutIfNeeded() { |
| 194 if (policy_manager()->IsClientRegistered()) { | 194 if (policy_manager()->IsClientRegistered()) { |
| 195 DVLOG(1) << "User is registered for policy - prohibiting signout"; | 195 DVLOG(1) << "User is registered for policy - prohibiting signout"; |
| 196 signin_manager()->ProhibitSignout(true); | 196 signin_manager()->ProhibitSignout(true); |
| 197 } | 197 } |
| 198 } | 198 } |
| 199 | 199 |
| 200 } // namespace policy | 200 } // namespace policy |
| OLD | NEW |