| 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 <string> | 7 #include "base/bind.h" |
| 8 | 8 #include "base/bind_helpers.h" |
| 9 #include "base/command_line.h" | |
| 10 #include "base/file_path.h" | |
| 11 #include "base/message_loop.h" | |
| 12 #include "base/path_service.h" | |
| 13 #include "chrome/browser/policy/cloud_policy_refresh_scheduler.h" | |
| 14 #include "chrome/browser/policy/cloud_policy_service.h" | 9 #include "chrome/browser/policy/cloud_policy_service.h" |
| 15 #include "chrome/browser/policy/policy_constants.h" | 10 #include "chrome/browser/policy/policy_constants.h" |
| 16 #include "chrome/common/chrome_paths.h" | |
| 17 #include "chrome/common/chrome_switches.h" | |
| 18 #include "chrome/common/pref_names.h" | 11 #include "chrome/common/pref_names.h" |
| 19 | 12 |
| 20 #if defined(OS_CHROMEOS) | |
| 21 #include "chrome/browser/policy/user_cloud_policy_store_chromeos.h" | |
| 22 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 23 #include "chromeos/dbus/session_manager_client.h" | |
| 24 #endif | |
| 25 | |
| 26 namespace policy { | 13 namespace policy { |
| 27 | 14 |
| 28 namespace { | |
| 29 | |
| 30 #if defined(OS_CHROMEOS) | |
| 31 // Paths for the legacy policy caches in the profile directory. | |
| 32 // TODO(mnissler): Remove once the number of pre-M20 clients becomes negligible. | |
| 33 | |
| 34 // Subdirectory in the user's profile for storing user policies. | |
| 35 const FilePath::CharType kPolicyDir[] = FILE_PATH_LITERAL("Device Management"); | |
| 36 // File in the above directory for storing user policy dmtokens. | |
| 37 const FilePath::CharType kTokenCacheFile[] = FILE_PATH_LITERAL("Token"); | |
| 38 // File in the above directory for storing user policy data. | |
| 39 const FilePath::CharType kPolicyCacheFile[] = FILE_PATH_LITERAL("Policy"); | |
| 40 #endif | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 UserCloudPolicyManager::UserCloudPolicyManager( | 15 UserCloudPolicyManager::UserCloudPolicyManager( |
| 45 scoped_ptr<CloudPolicyStore> store, | 16 scoped_ptr<CloudPolicyStore> store, |
| 46 bool wait_for_policy_fetch) | 17 bool wait_for_policy_fetch) |
| 47 : wait_for_policy_fetch_(wait_for_policy_fetch), | 18 : CloudPolicyManager(store.Pass()), |
| 48 wait_for_policy_refresh_(false), | 19 wait_for_policy_fetch_(wait_for_policy_fetch) {} |
| 49 store_(store.Pass()) { | |
| 50 store_->Load(); | |
| 51 store_->AddObserver(this); | |
| 52 } | |
| 53 | 20 |
| 54 UserCloudPolicyManager::~UserCloudPolicyManager() { | 21 UserCloudPolicyManager::~UserCloudPolicyManager() { |
| 55 Shutdown(); | 22 if (cloud_policy_client()) |
| 56 store_->RemoveObserver(this); | 23 cloud_policy_client()->RemoveObserver(this); |
| 57 } | 24 } |
| 58 | 25 |
| 59 // static | 26 // static |
| 60 scoped_ptr<UserCloudPolicyManager> UserCloudPolicyManager::Create( | 27 scoped_ptr<UserCloudPolicyManager> UserCloudPolicyManager::Create( |
| 61 Profile* profile, | 28 Profile* profile, |
| 62 bool wait_for_policy_fetch) { | 29 bool wait_for_policy_fetch) { |
| 63 scoped_ptr<CloudPolicyStore> store = | 30 scoped_ptr<CloudPolicyStore> store = |
| 64 CloudPolicyStore::CreateUserPolicyStore(profile); | 31 CloudPolicyStore::CreateUserPolicyStore(profile); |
| 65 return scoped_ptr<UserCloudPolicyManager>( | 32 return scoped_ptr<UserCloudPolicyManager>( |
| 66 new UserCloudPolicyManager(store.Pass(), wait_for_policy_fetch)); | 33 new UserCloudPolicyManager(store.Pass(), wait_for_policy_fetch)); |
| 67 } | 34 } |
| 68 | 35 |
| 69 void UserCloudPolicyManager::Initialize(PrefService* prefs, | 36 void UserCloudPolicyManager::Initialize( |
| 70 DeviceManagementService* service, | 37 PrefService* local_state, |
| 71 UserAffiliation user_affiliation) { | 38 DeviceManagementService* device_management_service, |
| 72 DCHECK(service); | 39 UserAffiliation user_affiliation) { |
| 73 DCHECK(prefs); | 40 DCHECK(device_management_service); |
| 74 DCHECK(!service_.get()); | 41 DCHECK(local_state); |
| 75 prefs_ = prefs; | 42 local_state_ = local_state; |
| 76 scoped_ptr<CloudPolicyClient> client( | 43 scoped_ptr<CloudPolicyClient> client( |
| 77 new CloudPolicyClient(std::string(), std::string(), user_affiliation, | 44 new CloudPolicyClient(std::string(), std::string(), user_affiliation, |
| 78 POLICY_SCOPE_USER, NULL, service)); | 45 POLICY_SCOPE_USER, NULL, |
| 79 service_.reset(new CloudPolicyService(client.Pass(), store_.get())); | 46 device_management_service)); |
| 80 service_->client()->AddObserver(this); | 47 InitializeService(client.Pass()); |
| 48 cloud_policy_client()->AddObserver(this); |
| 81 | 49 |
| 82 if (wait_for_policy_fetch_) { | 50 if (wait_for_policy_fetch_) { |
| 83 // If we are supposed to wait for a policy fetch, we trigger an explicit | 51 // If we are supposed to wait for a policy fetch, we trigger an explicit |
| 84 // policy refresh at startup that allows us to unblock initialization once | 52 // policy refresh at startup that allows us to unblock initialization once |
| 85 // done. The refresh scheduler only gets started once that refresh | 53 // done. The refresh scheduler only gets started once that refresh |
| 86 // completes. Note that we might have to wait for registration to happen, | 54 // completes. Note that we might have to wait for registration to happen, |
| 87 // see OnRegistrationStateChanged() below. | 55 // see OnRegistrationStateChanged() below. |
| 88 if (service_->client()->is_registered()) { | 56 if (cloud_policy_client()->is_registered()) { |
| 89 service_->RefreshPolicy( | 57 cloud_policy_service()->RefreshPolicy( |
| 90 base::Bind(&UserCloudPolicyManager::OnInitialPolicyFetchComplete, | 58 base::Bind(&UserCloudPolicyManager::OnInitialPolicyFetchComplete, |
| 91 base::Unretained(this))); | 59 base::Unretained(this))); |
| 92 } | 60 } |
| 93 } else { | 61 } else { |
| 94 CancelWaitForPolicyFetch(); | 62 CancelWaitForPolicyFetch(); |
| 95 } | 63 } |
| 96 } | 64 } |
| 97 | 65 |
| 98 void UserCloudPolicyManager::ShutdownAndRemovePolicy() { | 66 void UserCloudPolicyManager::ShutdownAndRemovePolicy() { |
| 99 Shutdown(); | 67 if (cloud_policy_client()) |
| 100 store_->Clear(); | 68 cloud_policy_client()->RemoveObserver(this); |
| 69 ShutdownService(); |
| 70 local_state_ = NULL; |
| 71 cloud_policy_store()->Clear(); |
| 101 } | 72 } |
| 102 | 73 |
| 103 void UserCloudPolicyManager::CancelWaitForPolicyFetch() { | 74 void UserCloudPolicyManager::CancelWaitForPolicyFetch() { |
| 104 wait_for_policy_fetch_ = false; | 75 wait_for_policy_fetch_ = false; |
| 105 CheckAndPublishPolicy(); | 76 CheckAndPublishPolicy(); |
| 106 | 77 |
| 107 // Now that |wait_for_policy_fetch_| is guaranteed to be false, the scheduler | 78 // Now that |wait_for_policy_fetch_| is guaranteed to be false, the scheduler |
| 108 // can be started. | 79 // can be started. |
| 109 if (service_.get() && !refresh_scheduler_.get() && prefs_) { | 80 if (cloud_policy_service() && local_state_) |
| 110 refresh_scheduler_.reset( | 81 StartRefreshScheduler(local_state_, prefs::kUserPolicyRefreshRate); |
| 111 new CloudPolicyRefreshScheduler( | |
| 112 service_->client(), store_.get(), prefs_, | |
| 113 prefs::kUserPolicyRefreshRate, | |
| 114 MessageLoop::current()->message_loop_proxy())); | |
| 115 } | |
| 116 } | 82 } |
| 117 | 83 |
| 118 bool UserCloudPolicyManager::IsClientRegistered() const { | 84 bool UserCloudPolicyManager::IsClientRegistered() const { |
| 119 if (!service_.get()) | 85 return cloud_policy_client() && cloud_policy_client()->is_registered(); |
| 120 return false; | |
| 121 return service_->client()->is_registered(); | |
| 122 } | 86 } |
| 123 | 87 |
| 124 void UserCloudPolicyManager::RegisterClient(const std::string& access_token) { | 88 void UserCloudPolicyManager::RegisterClient(const std::string& access_token) { |
| 125 DCHECK(cloud_policy_service()) << "Callers must invoke Initialize() first"; | 89 DCHECK(cloud_policy_client()) << "Callers must invoke Initialize() first"; |
| 126 if (!cloud_policy_service()->client()->is_registered()) { | 90 if (!cloud_policy_client()->is_registered()) { |
| 127 DVLOG(1) << "Registering client with access token: " << access_token; | 91 DVLOG(1) << "Registering client with access token: " << access_token; |
| 128 cloud_policy_service()->client()->Register(access_token); | 92 cloud_policy_client()->Register(access_token); |
| 129 } | 93 } |
| 130 } | 94 } |
| 131 | 95 |
| 132 bool UserCloudPolicyManager::IsInitializationComplete() const { | 96 bool UserCloudPolicyManager::IsInitializationComplete() const { |
| 133 return store_->is_initialized() && !wait_for_policy_fetch_; | 97 return CloudPolicyManager::IsInitializationComplete() && |
| 134 } | 98 !wait_for_policy_fetch_; |
| 135 | |
| 136 void UserCloudPolicyManager::RefreshPolicies() { | |
| 137 if (service_.get()) { | |
| 138 wait_for_policy_refresh_ = true; | |
| 139 service_->RefreshPolicy( | |
| 140 base::Bind(&UserCloudPolicyManager::OnRefreshComplete, | |
| 141 base::Unretained(this))); | |
| 142 } else { | |
| 143 OnRefreshComplete(); | |
| 144 } | |
| 145 } | 99 } |
| 146 | 100 |
| 147 void UserCloudPolicyManager::OnPolicyFetched(CloudPolicyClient* client) { | 101 void UserCloudPolicyManager::OnPolicyFetched(CloudPolicyClient* client) { |
| 148 // No action required. If we're blocked on a policy fetch, we'll learn about | 102 // No action required. If we're blocked on a policy fetch, we'll learn about |
| 149 // completion of it through OnInitialPolicyFetchComplete(). | 103 // completion of it through OnInitialPolicyFetchComplete(). |
| 150 } | 104 } |
| 151 | 105 |
| 152 void UserCloudPolicyManager::OnRegistrationStateChanged( | 106 void UserCloudPolicyManager::OnRegistrationStateChanged( |
| 153 CloudPolicyClient* client) { | 107 CloudPolicyClient* client) { |
| 108 DCHECK_EQ(cloud_policy_client(), client); |
| 154 if (wait_for_policy_fetch_) { | 109 if (wait_for_policy_fetch_) { |
| 155 // If we're blocked on the policy fetch, now is a good time to issue it. | 110 // If we're blocked on the policy fetch, now is a good time to issue it. |
| 156 if (service_->client()->is_registered()) { | 111 if (cloud_policy_client()->is_registered()) { |
| 157 service_->RefreshPolicy( | 112 cloud_policy_service()->RefreshPolicy( |
| 158 base::Bind(&UserCloudPolicyManager::OnInitialPolicyFetchComplete, | 113 base::Bind(&UserCloudPolicyManager::OnInitialPolicyFetchComplete, |
| 159 base::Unretained(this))); | 114 base::Unretained(this))); |
| 160 } else { | 115 } else { |
| 161 // If the client has switched to not registered, we bail out as this | 116 // If the client has switched to not registered, we bail out as this |
| 162 // indicates the cloud policy setup flow has been aborted. | 117 // indicates the cloud policy setup flow has been aborted. |
| 163 CancelWaitForPolicyFetch(); | 118 CancelWaitForPolicyFetch(); |
| 164 } | 119 } |
| 165 } | 120 } |
| 166 } | 121 } |
| 167 | 122 |
| 168 void UserCloudPolicyManager::OnClientError(CloudPolicyClient* client) { | 123 void UserCloudPolicyManager::OnClientError(CloudPolicyClient* client) { |
| 124 DCHECK_EQ(cloud_policy_client(), client); |
| 169 CancelWaitForPolicyFetch(); | 125 CancelWaitForPolicyFetch(); |
| 170 } | 126 } |
| 171 | 127 |
| 172 void UserCloudPolicyManager::CheckAndPublishPolicy() { | |
| 173 if (IsInitializationComplete() && !wait_for_policy_refresh_) { | |
| 174 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); | |
| 175 bundle->Get(POLICY_DOMAIN_CHROME, std::string()).CopyFrom( | |
| 176 store_->policy_map()); | |
| 177 UpdatePolicy(bundle.Pass()); | |
| 178 } | |
| 179 } | |
| 180 | |
| 181 void UserCloudPolicyManager::OnStoreLoaded(CloudPolicyStore* store) { | |
| 182 CheckAndPublishPolicy(); | |
| 183 } | |
| 184 | |
| 185 void UserCloudPolicyManager::OnStoreError(CloudPolicyStore* store) { | |
| 186 // No action required, the old policy is still valid. | |
| 187 } | |
| 188 | |
| 189 void UserCloudPolicyManager::OnInitialPolicyFetchComplete() { | 128 void UserCloudPolicyManager::OnInitialPolicyFetchComplete() { |
| 190 CancelWaitForPolicyFetch(); | 129 CancelWaitForPolicyFetch(); |
| 191 } | 130 } |
| 192 | 131 |
| 193 void UserCloudPolicyManager::OnRefreshComplete() { | |
| 194 wait_for_policy_refresh_ = false; | |
| 195 CheckAndPublishPolicy(); | |
| 196 } | |
| 197 | |
| 198 void UserCloudPolicyManager::Shutdown() { | |
| 199 refresh_scheduler_.reset(); | |
| 200 if (service_.get()) | |
| 201 service_->client()->RemoveObserver(this); | |
| 202 service_.reset(); | |
| 203 prefs_ = NULL; | |
| 204 } | |
| 205 | |
| 206 } // namespace policy | 132 } // namespace policy |
| OLD | NEW |