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