| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_base.h" | 5 #include "chrome/browser/policy/cloud/user_policy_signin_service_base.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 11 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
| 12 #include "chrome/browser/policy/browser_policy_connector.h" | 12 #include "chrome/browser/policy/browser_policy_connector.h" |
| 13 #include "chrome/browser/policy/cloud/device_management_service.h" | 13 #include "chrome/browser/policy/cloud/device_management_service.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/policy/cloud/user_cloud_policy_manager_factory.h" | 15 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/signin/signin_manager.h" | 17 #include "chrome/browser/signin/signin_manager.h" |
| 18 #include "chrome/browser/signin/signin_manager_factory.h" | 18 #include "chrome/browser/signin/signin_manager_factory.h" |
| 19 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
| 20 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 21 #include "content/public/browser/notification_source.h" | 21 #include "content/public/browser/notification_source.h" |
| 22 | 22 |
| 23 namespace policy { | 23 namespace policy { |
| 24 | 24 |
| 25 UserPolicySigninServiceBase::UserPolicySigninServiceBase( | 25 UserPolicySigninServiceBase::UserPolicySigninServiceBase( |
| 26 Profile* profile, | 26 Profile* profile, |
| 27 PrefService* local_state, | 27 PrefService* local_state, |
| 28 DeviceManagementService* device_management_service) | 28 DeviceManagementService* device_management_service, |
| 29 SigninManager* signin) |
| 29 : profile_(profile), | 30 : profile_(profile), |
| 30 local_state_(local_state), | 31 local_state_(local_state), |
| 31 device_management_service_(device_management_service), | 32 device_management_service_(device_management_service), |
| 33 signin_manager_(signin), |
| 32 weak_factory_(this) { | 34 weak_factory_(this) { |
| 33 if (profile_->GetPrefs()->GetBoolean(prefs::kDisableCloudPolicyOnSignin)) | 35 if (profile_->GetPrefs()->GetBoolean(prefs::kDisableCloudPolicyOnSignin)) |
| 34 return; | 36 return; |
| 35 | 37 |
| 36 // Initialize/shutdown the UserCloudPolicyManager when the user signs out. | 38 // Initialize/shutdown the UserCloudPolicyManager when the user signs out. |
| 37 registrar_.Add(this, | 39 registrar_.Add(this, |
| 38 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, | 40 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, |
| 39 content::Source<Profile>(profile)); | 41 content::Source<Profile>(profile)); |
| 40 | 42 |
| 41 // Register a listener to be called back once the current profile has finished | 43 // Register a listener to be called back once the current profile has finished |
| (...skipping 23 matching lines...) Expand all Loading... |
| 65 // Now initiate a policy fetch. | 67 // Now initiate a policy fetch. |
| 66 manager->core()->service()->RefreshPolicy(callback); | 68 manager->core()->service()->RefreshPolicy(callback); |
| 67 } | 69 } |
| 68 | 70 |
| 69 void UserPolicySigninServiceBase::Observe( | 71 void UserPolicySigninServiceBase::Observe( |
| 70 int type, | 72 int type, |
| 71 const content::NotificationSource& source, | 73 const content::NotificationSource& source, |
| 72 const content::NotificationDetails& details) { | 74 const content::NotificationDetails& details) { |
| 73 // If using a TestingProfile with no SigninManager or UserCloudPolicyManager, | 75 // If using a TestingProfile with no SigninManager or UserCloudPolicyManager, |
| 74 // skip initialization. | 76 // skip initialization. |
| 75 if (!GetManager() || !SigninManagerFactory::GetForProfile(profile_)) { | 77 if (!GetManager() || !signin_manager()) { |
| 76 DVLOG(1) << "Skipping initialization for tests due to missing components."; | 78 DVLOG(1) << "Skipping initialization for tests due to missing components."; |
| 77 return; | 79 return; |
| 78 } | 80 } |
| 79 | 81 |
| 80 switch (type) { | 82 switch (type) { |
| 81 case chrome::NOTIFICATION_GOOGLE_SIGNED_OUT: | 83 case chrome::NOTIFICATION_GOOGLE_SIGNED_OUT: |
| 82 ShutdownUserCloudPolicyManager(); | 84 ShutdownUserCloudPolicyManager(); |
| 83 break; | 85 break; |
| 84 case chrome::NOTIFICATION_PROFILE_ADDED: | 86 case chrome::NOTIFICATION_PROFILE_ADDED: |
| 85 // A new profile has been loaded - if it's signed in, then initialize the | 87 // A new profile has been loaded - if it's signed in, then initialize the |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 base::Bind( | 128 base::Bind( |
| 127 &UserPolicySigninServiceBase::ShutdownUserCloudPolicyManager, | 129 &UserPolicySigninServiceBase::ShutdownUserCloudPolicyManager, |
| 128 weak_factory_.GetWeakPtr())); | 130 weak_factory_.GetWeakPtr())); |
| 129 } else { | 131 } else { |
| 130 DVLOG(1) << "Error fetching policy: " << client->status(); | 132 DVLOG(1) << "Error fetching policy: " << client->status(); |
| 131 } | 133 } |
| 132 } | 134 } |
| 133 } | 135 } |
| 134 | 136 |
| 135 void UserPolicySigninServiceBase::Shutdown() { | 137 void UserPolicySigninServiceBase::Shutdown() { |
| 138 PrepareForUserCloudPolicyManagerShutdown(); |
| 139 } |
| 140 |
| 141 void UserPolicySigninServiceBase::PrepareForUserCloudPolicyManagerShutdown() { |
| 136 UserCloudPolicyManager* manager = GetManager(); | 142 UserCloudPolicyManager* manager = GetManager(); |
| 137 if (manager && manager->core()->client()) | 143 if (manager && manager->core()->client()) |
| 138 manager->core()->client()->RemoveObserver(this); | 144 manager->core()->client()->RemoveObserver(this); |
| 139 if (manager && manager->core()->service()) | 145 if (manager && manager->core()->service()) |
| 140 manager->core()->service()->RemoveObserver(this); | 146 manager->core()->service()->RemoveObserver(this); |
| 141 } | 147 } |
| 142 | 148 |
| 143 // static | 149 // static |
| 144 bool UserPolicySigninServiceBase::ShouldForceLoadPolicy() { | 150 bool UserPolicySigninServiceBase::ShouldForceLoadPolicy() { |
| 145 return CommandLine::ForCurrentProcess()->HasSwitch( | 151 return CommandLine::ForCurrentProcess()->HasSwitch( |
| (...skipping 28 matching lines...) Expand all Loading... |
| 174 if (username.empty()) | 180 if (username.empty()) |
| 175 return false; // Not signed in. | 181 return false; // Not signed in. |
| 176 | 182 |
| 177 if (ShouldForceLoadPolicy()) | 183 if (ShouldForceLoadPolicy()) |
| 178 return true; | 184 return true; |
| 179 | 185 |
| 180 return !BrowserPolicyConnector::IsNonEnterpriseUser(username); | 186 return !BrowserPolicyConnector::IsNonEnterpriseUser(username); |
| 181 } | 187 } |
| 182 | 188 |
| 183 void UserPolicySigninServiceBase::InitializeOnProfileReady() { | 189 void UserPolicySigninServiceBase::InitializeOnProfileReady() { |
| 184 SigninManager* signin_manager = | 190 std::string username = signin_manager()->GetAuthenticatedUsername(); |
| 185 SigninManagerFactory::GetForProfile(profile_); | |
| 186 std::string username = signin_manager->GetAuthenticatedUsername(); | |
| 187 if (username.empty()) | 191 if (username.empty()) |
| 188 ShutdownUserCloudPolicyManager(); | 192 ShutdownUserCloudPolicyManager(); |
| 189 else | 193 else |
| 190 InitializeForSignedInUser(username); | 194 InitializeForSignedInUser(username); |
| 191 } | 195 } |
| 192 | 196 |
| 193 void UserPolicySigninServiceBase::InitializeForSignedInUser( | 197 void UserPolicySigninServiceBase::InitializeForSignedInUser( |
| 194 const std::string& username) { | 198 const std::string& username) { |
| 195 DCHECK(!username.empty()); | 199 DCHECK(!username.empty()); |
| 196 if (!ShouldLoadPolicyForUser(username)) { | 200 if (!ShouldLoadPolicyForUser(username)) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 223 manager->Connect(local_state_, client.Pass()); | 227 manager->Connect(local_state_, client.Pass()); |
| 224 DCHECK(manager->core()->service()); | 228 DCHECK(manager->core()->service()); |
| 225 | 229 |
| 226 // Observe the client to detect errors fetching policy. | 230 // Observe the client to detect errors fetching policy. |
| 227 manager->core()->client()->AddObserver(this); | 231 manager->core()->client()->AddObserver(this); |
| 228 // Observe the service to determine when it's initialized. | 232 // Observe the service to determine when it's initialized. |
| 229 manager->core()->service()->AddObserver(this); | 233 manager->core()->service()->AddObserver(this); |
| 230 } | 234 } |
| 231 | 235 |
| 232 void UserPolicySigninServiceBase::ShutdownUserCloudPolicyManager() { | 236 void UserPolicySigninServiceBase::ShutdownUserCloudPolicyManager() { |
| 233 Shutdown(); | 237 PrepareForUserCloudPolicyManagerShutdown(); |
| 234 UserCloudPolicyManager* manager = GetManager(); | 238 UserCloudPolicyManager* manager = GetManager(); |
| 235 if (manager) | 239 if (manager) |
| 236 manager->DisconnectAndRemovePolicy(); | 240 manager->DisconnectAndRemovePolicy(); |
| 237 } | 241 } |
| 238 | 242 |
| 239 UserCloudPolicyManager* UserPolicySigninServiceBase::GetManager() { | 243 UserCloudPolicyManager* UserPolicySigninServiceBase::GetManager() { |
| 240 return UserCloudPolicyManagerFactory::GetForProfile(profile_); | 244 return UserCloudPolicyManagerFactory::GetForProfile(profile_); |
| 241 } | 245 } |
| 242 | 246 |
| 243 } // namespace policy | 247 } // namespace policy |
| OLD | NEW |