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