| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/policy/user_cloud_policy_manager_chromeos.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "chrome/browser/policy/cloud_policy_refresh_scheduler.h" | |
| 10 #include "chrome/browser/policy/cloud_policy_service.h" | |
| 11 #include "chrome/browser/policy/policy_bundle.h" | |
| 12 #include "chrome/browser/policy/resource_cache.h" | |
| 13 #include "chrome/common/pref_names.h" | |
| 14 #include "net/url_request/url_request_context_getter.h" | |
| 15 | |
| 16 namespace em = enterprise_management; | |
| 17 | |
| 18 namespace policy { | |
| 19 | |
| 20 UserCloudPolicyManagerChromeOS::UserCloudPolicyManagerChromeOS( | |
| 21 scoped_ptr<CloudPolicyStore> store, | |
| 22 scoped_ptr<ResourceCache> resource_cache, | |
| 23 bool wait_for_policy_fetch) | |
| 24 : CloudPolicyManager( | |
| 25 PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType, std::string()), | |
| 26 store.get()), | |
| 27 store_(store.Pass()), | |
| 28 wait_for_policy_fetch_(wait_for_policy_fetch) { | |
| 29 if (resource_cache) { | |
| 30 component_policy_service_.reset(new ComponentCloudPolicyService( | |
| 31 this, store_.get(), resource_cache.Pass())); | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 UserCloudPolicyManagerChromeOS::~UserCloudPolicyManagerChromeOS() {} | |
| 36 | |
| 37 void UserCloudPolicyManagerChromeOS::Connect( | |
| 38 PrefService* local_state, | |
| 39 DeviceManagementService* device_management_service, | |
| 40 scoped_refptr<net::URLRequestContextGetter> request_context, | |
| 41 UserAffiliation user_affiliation) { | |
| 42 DCHECK(device_management_service); | |
| 43 DCHECK(local_state); | |
| 44 local_state_ = local_state; | |
| 45 scoped_ptr<CloudPolicyClient> cloud_policy_client( | |
| 46 new CloudPolicyClient(std::string(), std::string(), user_affiliation, | |
| 47 NULL, device_management_service)); | |
| 48 core()->Connect(cloud_policy_client.Pass()); | |
| 49 client()->AddObserver(this); | |
| 50 | |
| 51 if (component_policy_service_) | |
| 52 component_policy_service_->Connect(client(), request_context); | |
| 53 | |
| 54 if (wait_for_policy_fetch_) { | |
| 55 // If we are supposed to wait for a policy fetch, we trigger an explicit | |
| 56 // policy refresh at startup that allows us to unblock initialization once | |
| 57 // done. The refresh scheduler only gets started once that refresh | |
| 58 // completes. Note that we might have to wait for registration to happen, | |
| 59 // see OnRegistrationStateChanged() below. | |
| 60 if (client()->is_registered()) { | |
| 61 service()->RefreshPolicy( | |
| 62 base::Bind( | |
| 63 &UserCloudPolicyManagerChromeOS::OnInitialPolicyFetchComplete, | |
| 64 base::Unretained(this))); | |
| 65 } | |
| 66 } else { | |
| 67 CancelWaitForPolicyFetch(); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 void UserCloudPolicyManagerChromeOS::CancelWaitForPolicyFetch() { | |
| 72 wait_for_policy_fetch_ = false; | |
| 73 CheckAndPublishPolicy(); | |
| 74 | |
| 75 // Now that |wait_for_policy_fetch_| is guaranteed to be false, the scheduler | |
| 76 // can be started. | |
| 77 StartRefreshScheduler(); | |
| 78 } | |
| 79 | |
| 80 bool UserCloudPolicyManagerChromeOS::IsClientRegistered() const { | |
| 81 return client() && client()->is_registered(); | |
| 82 } | |
| 83 | |
| 84 void UserCloudPolicyManagerChromeOS::RegisterClient( | |
| 85 const std::string& access_token) { | |
| 86 DCHECK(client()) << "Callers must invoke Initialize() first"; | |
| 87 if (!client()->is_registered()) { | |
| 88 DVLOG(1) << "Registering client with access token: " << access_token; | |
| 89 client()->Register(em::DeviceRegisterRequest::USER, | |
| 90 access_token, std::string(), false); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 void UserCloudPolicyManagerChromeOS::Shutdown() { | |
| 95 if (client()) | |
| 96 client()->RemoveObserver(this); | |
| 97 component_policy_service_.reset(); | |
| 98 CloudPolicyManager::Shutdown(); | |
| 99 } | |
| 100 | |
| 101 bool UserCloudPolicyManagerChromeOS::IsInitializationComplete( | |
| 102 PolicyDomain domain) const { | |
| 103 if (!CloudPolicyManager::IsInitializationComplete(domain)) | |
| 104 return false; | |
| 105 if (domain == POLICY_DOMAIN_CHROME) | |
| 106 return !wait_for_policy_fetch_; | |
| 107 if (ComponentCloudPolicyService::SupportsDomain(domain) && | |
| 108 component_policy_service_) { | |
| 109 return component_policy_service_->is_initialized(); | |
| 110 } | |
| 111 return true; | |
| 112 } | |
| 113 | |
| 114 void UserCloudPolicyManagerChromeOS::RegisterPolicyDomain( | |
| 115 PolicyDomain domain, | |
| 116 const std::set<std::string>& component_ids) { | |
| 117 if (ComponentCloudPolicyService::SupportsDomain(domain) && | |
| 118 component_policy_service_) { | |
| 119 component_policy_service_->RegisterPolicyDomain(domain, component_ids); | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 scoped_ptr<PolicyBundle> UserCloudPolicyManagerChromeOS::CreatePolicyBundle() { | |
| 124 scoped_ptr<PolicyBundle> bundle = CloudPolicyManager::CreatePolicyBundle(); | |
| 125 if (component_policy_service_) | |
| 126 bundle->MergeFrom(component_policy_service_->policy()); | |
| 127 return bundle.Pass(); | |
| 128 } | |
| 129 | |
| 130 void UserCloudPolicyManagerChromeOS::OnPolicyFetched( | |
| 131 CloudPolicyClient* client) { | |
| 132 // No action required. If we're blocked on a policy fetch, we'll learn about | |
| 133 // completion of it through OnInitialPolicyFetchComplete(). | |
| 134 } | |
| 135 | |
| 136 void UserCloudPolicyManagerChromeOS::OnRegistrationStateChanged( | |
| 137 CloudPolicyClient* cloud_policy_client) { | |
| 138 DCHECK_EQ(client(), cloud_policy_client); | |
| 139 if (wait_for_policy_fetch_) { | |
| 140 // If we're blocked on the policy fetch, now is a good time to issue it. | |
| 141 if (client()->is_registered()) { | |
| 142 service()->RefreshPolicy( | |
| 143 base::Bind( | |
| 144 &UserCloudPolicyManagerChromeOS::OnInitialPolicyFetchComplete, | |
| 145 base::Unretained(this))); | |
| 146 } else { | |
| 147 // If the client has switched to not registered, we bail out as this | |
| 148 // indicates the cloud policy setup flow has been aborted. | |
| 149 CancelWaitForPolicyFetch(); | |
| 150 } | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 void UserCloudPolicyManagerChromeOS::OnClientError( | |
| 155 CloudPolicyClient* cloud_policy_client) { | |
| 156 DCHECK_EQ(client(), cloud_policy_client); | |
| 157 CancelWaitForPolicyFetch(); | |
| 158 } | |
| 159 | |
| 160 void UserCloudPolicyManagerChromeOS::OnComponentCloudPolicyRefreshNeeded() { | |
| 161 core()->RefreshSoon(); | |
| 162 } | |
| 163 | |
| 164 void UserCloudPolicyManagerChromeOS::OnComponentCloudPolicyUpdated() { | |
| 165 CheckAndPublishPolicy(); | |
| 166 StartRefreshScheduler(); | |
| 167 } | |
| 168 | |
| 169 void UserCloudPolicyManagerChromeOS::OnInitialPolicyFetchComplete( | |
| 170 bool success) { | |
| 171 CancelWaitForPolicyFetch(); | |
| 172 } | |
| 173 | |
| 174 void UserCloudPolicyManagerChromeOS::StartRefreshScheduler() { | |
| 175 if (core()->refresh_scheduler()) | |
| 176 return; // Already started. | |
| 177 | |
| 178 if (wait_for_policy_fetch_) | |
| 179 return; // Still waiting for the initial, blocking fetch. | |
| 180 | |
| 181 if (!service() || !local_state_) | |
| 182 return; // Not connected. | |
| 183 | |
| 184 if (component_policy_service_ && | |
| 185 !component_policy_service_->is_initialized()) { | |
| 186 // If the client doesn't have the list of components to fetch yet then don't | |
| 187 // start the scheduler. The |component_policy_service_| will call back into | |
| 188 // OnComponentCloudPolicyUpdated() once it's ready. | |
| 189 return; | |
| 190 } | |
| 191 | |
| 192 core()->StartRefreshScheduler(); | |
| 193 core()->TrackRefreshDelayPref(local_state_, prefs::kUserPolicyRefreshRate); | |
| 194 } | |
| 195 | |
| 196 } // namespace policy | |
| OLD | NEW |