Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / user_cloud_policy_store_chromeos.h
blob9d7a2cdf31d5df5088aafa9a60a39e994f0dd514
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.
5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_USER_CLOUD_POLICY_STORE_CHROMEOS_H_
6 #define CHROME_BROWSER_CHROMEOS_POLICY_USER_CLOUD_POLICY_STORE_CHROMEOS_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "chromeos/dbus/dbus_method_call_status.h"
18 #include "components/policy/core/common/cloud/cloud_policy_validator.h"
19 #include "components/policy/core/common/cloud/user_cloud_policy_store_base.h"
21 namespace base {
22 class SequencedTaskRunner;
25 namespace chromeos {
26 class CryptohomeClient;
27 class SessionManagerClient;
30 namespace policy {
32 class LegacyPolicyCacheLoader;
34 // Implements a cloud policy store backed by the Chrome OS' session_manager,
35 // which takes care of persisting policy to disk and is accessed via DBus calls
36 // through SessionManagerClient.
38 // Additionally, this class drives legacy UserPolicyTokenCache and
39 // UserPolicyDiskCache instances, migrating policy from these to session_manager
40 // storage on the fly.
41 class UserCloudPolicyStoreChromeOS : public UserCloudPolicyStoreBase {
42 public:
43 UserCloudPolicyStoreChromeOS(
44 chromeos::CryptohomeClient* cryptohome_client,
45 chromeos::SessionManagerClient* session_manager_client,
46 scoped_refptr<base::SequencedTaskRunner> background_task_runner,
47 const std::string& username,
48 const base::FilePath& user_policy_key_dir,
49 const base::FilePath& legacy_token_cache_file,
50 const base::FilePath& legacy_policy_cache_file);
51 ~UserCloudPolicyStoreChromeOS() override;
53 // CloudPolicyStore:
54 void Store(const enterprise_management::PolicyFetchResponse& policy) override;
55 void Load() override;
57 // Loads the policy synchronously on the current thread.
58 void LoadImmediately();
60 private:
61 // Starts validation of |policy| before storing it.
62 void ValidatePolicyForStore(
63 scoped_ptr<enterprise_management::PolicyFetchResponse> policy);
65 // Completion handler for policy validation on the Store() path.
66 // Starts a store operation if the validation succeeded.
67 void OnPolicyToStoreValidated(UserCloudPolicyValidator* validator);
69 // Called back from SessionManagerClient for policy store operations.
70 void OnPolicyStored(bool success);
72 // Called back from SessionManagerClient for policy load operations.
73 void OnPolicyRetrieved(const std::string& policy_blob);
75 // Starts validation of the loaded |policy| before installing it.
76 void ValidateRetrievedPolicy(
77 scoped_ptr<enterprise_management::PolicyFetchResponse> policy);
79 // Completion handler for policy validation on the Load() path. Installs the
80 // policy and publishes it if validation succeeded.
81 void OnRetrievedPolicyValidated(UserCloudPolicyValidator* validator);
83 // Callback for loading legacy caches.
84 void OnLegacyLoadFinished(
85 const std::string& dm_token,
86 const std::string& device_id,
87 Status status,
88 scoped_ptr<enterprise_management::PolicyFetchResponse>);
90 // Completion callback for legacy policy validation.
91 void OnLegacyPolicyValidated(const std::string& dm_token,
92 const std::string& device_id,
93 UserCloudPolicyValidator* validator);
95 // Installs legacy tokens.
96 void InstallLegacyTokens(const std::string& dm_token,
97 const std::string& device_id);
99 // Removes the passed-in legacy cache directory.
100 static void RemoveLegacyCacheDir(const base::FilePath& dir);
102 // Invokes |callback| after reloading |policy_key_|.
103 void ReloadPolicyKey(const base::Closure& callback);
105 // Reads the contents of |path| into |key|.
106 static void LoadPolicyKey(const base::FilePath& path,
107 std::string* key);
109 // Callback for the key reloading.
110 void OnPolicyKeyReloaded(std::string* key,
111 const base::Closure& callback);
113 // Invokes |callback| after creating |policy_key_|, if it hasn't been created
114 // yet; otherwise invokes |callback| immediately.
115 void EnsurePolicyKeyLoaded(const base::Closure& callback);
117 // Callback for getting the sanitized username from |cryptohome_client_|.
118 void OnGetSanitizedUsername(const base::Closure& callback,
119 chromeos::DBusMethodCallStatus call_status,
120 const std::string& sanitized_username);
122 scoped_ptr<UserCloudPolicyValidator> CreateValidatorForLoad(
123 scoped_ptr<enterprise_management::PolicyFetchResponse> policy);
125 chromeos::CryptohomeClient* cryptohome_client_;
126 chromeos::SessionManagerClient* session_manager_client_;
127 const std::string username_;
128 base::FilePath user_policy_key_dir_;
130 // TODO(mnissler): Remove all the legacy policy support members below after
131 // the number of pre-M20 clients drops back to zero.
132 base::FilePath legacy_cache_dir_;
133 scoped_ptr<LegacyPolicyCacheLoader> legacy_loader_;
134 bool legacy_caches_loaded_;
136 bool policy_key_loaded_;
137 base::FilePath policy_key_path_;
138 std::string policy_key_;
140 base::WeakPtrFactory<UserCloudPolicyStoreChromeOS> weak_factory_;
142 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyStoreChromeOS);
145 } // namespace policy
147 #endif // CHROME_BROWSER_CHROMEOS_POLICY_USER_CLOUD_POLICY_STORE_CHROMEOS_H_