| 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/user_cloud_policy_manager.h" | 5 #include "chrome/browser/policy/user_cloud_policy_manager_chromeos.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 "chrome/browser/policy/cloud_policy_service.h" | 9 #include "chrome/browser/policy/cloud_policy_service.h" |
| 10 #include "chrome/browser/policy/policy_types.h" | 10 #include "chrome/browser/policy/policy_types.h" |
| 11 #include "chrome/common/pref_names.h" | 11 #include "chrome/common/pref_names.h" |
| 12 | 12 |
| 13 namespace policy { | 13 namespace policy { |
| 14 | 14 |
| 15 UserCloudPolicyManager::UserCloudPolicyManager( | 15 UserCloudPolicyManagerChromeOS::UserCloudPolicyManagerChromeOS( |
| 16 scoped_ptr<CloudPolicyStore> store, | 16 scoped_ptr<CloudPolicyStore> store, |
| 17 bool wait_for_policy_fetch) | 17 bool wait_for_policy_fetch) |
| 18 : CloudPolicyManager(store.Pass()), | 18 : CloudPolicyManager(store.get()), |
| 19 store_(store.Pass()), |
| 19 wait_for_policy_fetch_(wait_for_policy_fetch) {} | 20 wait_for_policy_fetch_(wait_for_policy_fetch) {} |
| 20 | 21 |
| 21 UserCloudPolicyManager::~UserCloudPolicyManager() {} | 22 UserCloudPolicyManagerChromeOS::~UserCloudPolicyManagerChromeOS() {} |
| 22 | 23 |
| 23 // static | 24 void UserCloudPolicyManagerChromeOS::Initialize( |
| 24 scoped_ptr<UserCloudPolicyManager> UserCloudPolicyManager::Create( | |
| 25 Profile* profile, | |
| 26 PolicyInit policy_init) { | |
| 27 scoped_ptr<CloudPolicyStore> store = | |
| 28 CloudPolicyStore::CreateUserPolicyStore( | |
| 29 profile, policy_init == POLICY_INIT_IMMEDIATELY); | |
| 30 return make_scoped_ptr(new UserCloudPolicyManager( | |
| 31 store.Pass(), policy_init == POLICY_INIT_REFRESH_FROM_SERVER)); | |
| 32 } | |
| 33 | |
| 34 void UserCloudPolicyManager::Initialize( | |
| 35 PrefService* local_state, | 25 PrefService* local_state, |
| 36 DeviceManagementService* device_management_service, | 26 DeviceManagementService* device_management_service, |
| 37 UserAffiliation user_affiliation) { | 27 UserAffiliation user_affiliation) { |
| 38 DCHECK(device_management_service); | 28 DCHECK(device_management_service); |
| 39 DCHECK(local_state); | 29 DCHECK(local_state); |
| 40 local_state_ = local_state; | 30 local_state_ = local_state; |
| 41 scoped_ptr<CloudPolicyClient> client( | 31 scoped_ptr<CloudPolicyClient> client( |
| 42 new CloudPolicyClient(std::string(), std::string(), user_affiliation, | 32 new CloudPolicyClient(std::string(), std::string(), user_affiliation, |
| 43 POLICY_SCOPE_USER, NULL, | 33 POLICY_SCOPE_USER, NULL, |
| 44 device_management_service)); | 34 device_management_service)); |
| 45 InitializeService(client.Pass()); | 35 InitializeService(client.Pass()); |
| 46 cloud_policy_client()->AddObserver(this); | 36 cloud_policy_client()->AddObserver(this); |
| 47 | 37 |
| 48 if (wait_for_policy_fetch_) { | 38 if (wait_for_policy_fetch_) { |
| 49 // If we are supposed to wait for a policy fetch, we trigger an explicit | 39 // If we are supposed to wait for a policy fetch, we trigger an explicit |
| 50 // policy refresh at startup that allows us to unblock initialization once | 40 // policy refresh at startup that allows us to unblock initialization once |
| 51 // done. The refresh scheduler only gets started once that refresh | 41 // done. The refresh scheduler only gets started once that refresh |
| 52 // completes. Note that we might have to wait for registration to happen, | 42 // completes. Note that we might have to wait for registration to happen, |
| 53 // see OnRegistrationStateChanged() below. | 43 // see OnRegistrationStateChanged() below. |
| 54 if (cloud_policy_client()->is_registered()) { | 44 if (cloud_policy_client()->is_registered()) { |
| 55 cloud_policy_service()->RefreshPolicy( | 45 cloud_policy_service()->RefreshPolicy( |
| 56 base::Bind(&UserCloudPolicyManager::OnInitialPolicyFetchComplete, | 46 base::Bind( |
| 57 base::Unretained(this))); | 47 &UserCloudPolicyManagerChromeOS::OnInitialPolicyFetchComplete, |
| 48 base::Unretained(this))); |
| 58 } | 49 } |
| 59 } else { | 50 } else { |
| 60 CancelWaitForPolicyFetch(); | 51 CancelWaitForPolicyFetch(); |
| 61 } | 52 } |
| 62 } | 53 } |
| 63 | 54 |
| 64 void UserCloudPolicyManager::ShutdownAndRemovePolicy() { | 55 void UserCloudPolicyManagerChromeOS::CancelWaitForPolicyFetch() { |
| 65 if (cloud_policy_client()) | |
| 66 cloud_policy_client()->RemoveObserver(this); | |
| 67 ShutdownService(); | |
| 68 local_state_ = NULL; | |
| 69 cloud_policy_store()->Clear(); | |
| 70 } | |
| 71 | |
| 72 void UserCloudPolicyManager::CancelWaitForPolicyFetch() { | |
| 73 wait_for_policy_fetch_ = false; | 56 wait_for_policy_fetch_ = false; |
| 74 CheckAndPublishPolicy(); | 57 CheckAndPublishPolicy(); |
| 75 | 58 |
| 76 // Now that |wait_for_policy_fetch_| is guaranteed to be false, the scheduler | 59 // Now that |wait_for_policy_fetch_| is guaranteed to be false, the scheduler |
| 77 // can be started. | 60 // can be started. |
| 78 if (cloud_policy_service() && local_state_) | 61 if (cloud_policy_service() && local_state_) |
| 79 StartRefreshScheduler(local_state_, prefs::kUserPolicyRefreshRate); | 62 StartRefreshScheduler(local_state_, prefs::kUserPolicyRefreshRate); |
| 80 } | 63 } |
| 81 | 64 |
| 82 bool UserCloudPolicyManager::IsClientRegistered() const { | 65 bool UserCloudPolicyManagerChromeOS::IsClientRegistered() const { |
| 83 return cloud_policy_client() && cloud_policy_client()->is_registered(); | 66 return cloud_policy_client() && cloud_policy_client()->is_registered(); |
| 84 } | 67 } |
| 85 | 68 |
| 86 void UserCloudPolicyManager::RegisterClient(const std::string& access_token) { | 69 void UserCloudPolicyManagerChromeOS::RegisterClient( |
| 70 const std::string& access_token) { |
| 87 DCHECK(cloud_policy_client()) << "Callers must invoke Initialize() first"; | 71 DCHECK(cloud_policy_client()) << "Callers must invoke Initialize() first"; |
| 88 if (!cloud_policy_client()->is_registered()) { | 72 if (!cloud_policy_client()->is_registered()) { |
| 89 DVLOG(1) << "Registering client with access token: " << access_token; | 73 DVLOG(1) << "Registering client with access token: " << access_token; |
| 90 cloud_policy_client()->Register(access_token); | 74 cloud_policy_client()->Register(access_token); |
| 91 } | 75 } |
| 92 } | 76 } |
| 93 | 77 |
| 94 void UserCloudPolicyManager::Shutdown() { | 78 void UserCloudPolicyManagerChromeOS::Shutdown() { |
| 95 if (cloud_policy_client()) | 79 if (cloud_policy_client()) |
| 96 cloud_policy_client()->RemoveObserver(this); | 80 cloud_policy_client()->RemoveObserver(this); |
| 97 CloudPolicyManager::Shutdown(); | 81 CloudPolicyManager::Shutdown(); |
| 98 } | 82 } |
| 99 | 83 |
| 100 bool UserCloudPolicyManager::IsInitializationComplete() const { | 84 bool UserCloudPolicyManagerChromeOS::IsInitializationComplete() const { |
| 101 return CloudPolicyManager::IsInitializationComplete() && | 85 return CloudPolicyManager::IsInitializationComplete() && |
| 102 !wait_for_policy_fetch_; | 86 !wait_for_policy_fetch_; |
| 103 } | 87 } |
| 104 | 88 |
| 105 void UserCloudPolicyManager::OnPolicyFetched(CloudPolicyClient* client) { | 89 void UserCloudPolicyManagerChromeOS::OnPolicyFetched( |
| 90 CloudPolicyClient* client) { |
| 106 // No action required. If we're blocked on a policy fetch, we'll learn about | 91 // No action required. If we're blocked on a policy fetch, we'll learn about |
| 107 // completion of it through OnInitialPolicyFetchComplete(). | 92 // completion of it through OnInitialPolicyFetchComplete(). |
| 108 } | 93 } |
| 109 | 94 |
| 110 void UserCloudPolicyManager::OnRegistrationStateChanged( | 95 void UserCloudPolicyManagerChromeOS::OnRegistrationStateChanged( |
| 111 CloudPolicyClient* client) { | 96 CloudPolicyClient* client) { |
| 112 DCHECK_EQ(cloud_policy_client(), client); | 97 DCHECK_EQ(cloud_policy_client(), client); |
| 113 if (wait_for_policy_fetch_) { | 98 if (wait_for_policy_fetch_) { |
| 114 // If we're blocked on the policy fetch, now is a good time to issue it. | 99 // If we're blocked on the policy fetch, now is a good time to issue it. |
| 115 if (cloud_policy_client()->is_registered()) { | 100 if (cloud_policy_client()->is_registered()) { |
| 116 cloud_policy_service()->RefreshPolicy( | 101 cloud_policy_service()->RefreshPolicy( |
| 117 base::Bind(&UserCloudPolicyManager::OnInitialPolicyFetchComplete, | 102 base::Bind( |
| 118 base::Unretained(this))); | 103 &UserCloudPolicyManagerChromeOS::OnInitialPolicyFetchComplete, |
| 104 base::Unretained(this))); |
| 119 } else { | 105 } else { |
| 120 // If the client has switched to not registered, we bail out as this | 106 // If the client has switched to not registered, we bail out as this |
| 121 // indicates the cloud policy setup flow has been aborted. | 107 // indicates the cloud policy setup flow has been aborted. |
| 122 CancelWaitForPolicyFetch(); | 108 CancelWaitForPolicyFetch(); |
| 123 } | 109 } |
| 124 } | 110 } |
| 125 } | 111 } |
| 126 | 112 |
| 127 void UserCloudPolicyManager::OnClientError(CloudPolicyClient* client) { | 113 void UserCloudPolicyManagerChromeOS::OnClientError(CloudPolicyClient* client) { |
| 128 DCHECK_EQ(cloud_policy_client(), client); | 114 DCHECK_EQ(cloud_policy_client(), client); |
| 129 CancelWaitForPolicyFetch(); | 115 CancelWaitForPolicyFetch(); |
| 130 } | 116 } |
| 131 | 117 |
| 132 void UserCloudPolicyManager::OnInitialPolicyFetchComplete() { | 118 void UserCloudPolicyManagerChromeOS::OnInitialPolicyFetchComplete() { |
| 133 CancelWaitForPolicyFetch(); | 119 CancelWaitForPolicyFetch(); |
| 134 } | 120 } |
| 135 | 121 |
| 136 } // namespace policy | 122 } // namespace policy |
| OLD | NEW |