Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / policy / enterprise_install_attributes.h
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 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_ENTERPRISE_INSTALL_ATTRIBUTES_H_
6 #define CHROME_BROWSER_CHROMEOS_POLICY_ENTERPRISE_INSTALL_ATTRIBUTES_H_
7
8 #include <map>
9 #include <string>
10
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/files/file_path.h"
15 #include "base/gtest_prod_util.h"
16 #include "base/memory/weak_ptr.h"
17 #include "chromeos/dbus/cryptohome_client.h"
18 #include "chromeos/dbus/dbus_method_call_status.h"
19 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
20
21 namespace policy {
22
23 // Brokers access to the enterprise-related installation-time attributes on
24 // ChromeOS.
25 // TODO(zelidrag, mnissler): Rename + move this class - http://crbug.com/249513.
26 class EnterpriseInstallAttributes {
27  public:
28   // EnterpriseInstallAttributes status codes.  Do not change the numeric ids or
29   // the meaning of the existing codes to preserve the interpretability of old
30   // logfiles.
31   enum LockResult {
32     LOCK_SUCCESS = 0,          // Success.
33     LOCK_NOT_READY = 1,        // Backend/TPM still initializing.
34     LOCK_TIMEOUT = 2,          // Backend/TPM timed out.
35     LOCK_BACKEND_INVALID = 3,  // Backend failed to initialize.
36     LOCK_ALREADY_LOCKED = 4,   // TPM has already been locked.
37     LOCK_SET_ERROR = 5,        // Failed to set attributes.
38     LOCK_FINALIZE_ERROR = 6,   // Backend failed to lock.
39     LOCK_READBACK_ERROR = 7,   // Inconsistency reading back registration data.
40     LOCK_WRONG_DOMAIN = 8,     // Device already registered to another domain.
41   };
42
43   // A callback to handle responses of methods returning a LockResult value.
44   typedef base::Callback<void(LockResult lock_result)> LockResultCallback;
45
46   // Return serialized InstallAttributes of an enterprise-owned configuration.
47   static std::string GetEnterpriseOwnedInstallAttributesBlobForTesting(
48       const std::string& user_name);
49
50   explicit EnterpriseInstallAttributes(
51       chromeos::CryptohomeClient* cryptohome_client);
52   ~EnterpriseInstallAttributes();
53
54   // Reads data from the cache file which is created early during the boot
55   // process.  The cache file is used to work around slow cryptohome startup,
56   // which takes a while to register its DBus interface.  See
57   // http://crosbug.com/37367 for background on this.
58   void ReadCacheFile(const base::FilePath& cache_file);
59
60   // Makes sure the local caches for enterprise-related install attributes are
61   // up-to-date with what cryptohome has. This method checks the readiness of
62   // attributes and read them if ready. Actual read will be performed in
63   // ReadAttributesIfReady().
64   void ReadImmutableAttributes(const base::Closure& callback);
65
66   // Locks the device to be an enterprise device registered by the given user.
67   // This can also be called after the lock has already been taken, in which
68   // case it checks that the passed user agrees with the locked attribute.
69   // |callback| must not be null and is called with the result.
70   void LockDevice(const std::string& user,
71                   DeviceMode device_mode,
72                   const std::string& device_id,
73                   const LockResultCallback& callback);
74
75   // Checks whether this is an enterprise device.
76   bool IsEnterpriseDevice();
77
78   // Checks whether this is a consumer kiosk enabled device.
79   bool IsConsumerKioskDeviceWithAutoLaunch();
80
81   // Gets the domain this device belongs to or an empty string if the device is
82   // not an enterprise device.
83   std::string GetDomain();
84
85   // Gets the user that registered the device. Returns an empty string if the
86   // device is not an enterprise device.
87   std::string GetRegistrationUser();
88
89   // Gets the device id that was generated when the device was registered.
90   // Returns an empty string if the device is not an enterprise device or the
91   // device id was not stored in the lockbox (prior to R19).
92   std::string GetDeviceId();
93
94   // Gets the mode the device was enrolled to. The return value for devices that
95   // are not locked yet will be DEVICE_MODE_UNKNOWN.
96   DeviceMode GetMode();
97
98  protected:
99   bool device_locked_;
100   std::string registration_user_;
101   std::string registration_domain_;
102   std::string registration_device_id_;
103   DeviceMode registration_mode_;
104
105  private:
106   FRIEND_TEST_ALL_PREFIXES(EnterpriseInstallAttributesTest,
107                            DeviceLockedFromOlderVersion);
108   FRIEND_TEST_ALL_PREFIXES(EnterpriseInstallAttributesTest,
109                            ReadCacheFile);
110   FRIEND_TEST_ALL_PREFIXES(EnterpriseInstallAttributesTest,
111                            ReadCacheFileForConsumerKiosk);
112   FRIEND_TEST_ALL_PREFIXES(EnterpriseInstallAttributesTest,
113                            VerifyFakeInstallAttributesCache);
114
115   // Constants for the possible device modes that can be stored in the lockbox.
116   static const char kConsumerDeviceMode[];
117   static const char kEnterpriseDeviceMode[];
118   static const char kRetailKioskDeviceMode[];
119   static const char kConsumerKioskDeviceMode[];
120   static const char kUnknownDeviceMode[];
121
122   // Field names in the lockbox.
123   static const char kAttrEnterpriseDeviceId[];
124   static const char kAttrEnterpriseDomain[];
125   static const char kAttrEnterpriseMode[];
126   static const char kAttrEnterpriseOwned[];
127   static const char kAttrEnterpriseUser[];
128   static const char kAttrConsumerKioskEnabled[];
129
130   // Translates DeviceMode constants to strings used in the lockbox.
131   std::string GetDeviceModeString(DeviceMode mode);
132
133   // Translates strings used in the lockbox to DeviceMode values.
134   DeviceMode GetDeviceModeFromString(const std::string& mode);
135
136   // Decodes the install attributes provided in |attr_map|.
137   void DecodeInstallAttributes(
138       const std::map<std::string, std::string>& attr_map);
139
140   // Helper for ReadImmutableAttributes.
141   void ReadAttributesIfReady(
142       const base::Closure& callback,
143       chromeos::DBusMethodCallStatus call_status,
144       bool result);
145
146   // Helper for LockDevice(). Handles the result of InstallAttributesIsReady()
147   // and continue processing LockDevice if the result is true.
148   void LockDeviceIfAttributesIsReady(
149       const std::string& user,
150       DeviceMode device_mode,
151       const std::string& device_id,
152       const LockResultCallback& callback,
153       chromeos::DBusMethodCallStatus call_status,
154       bool result);
155
156   // Confirms the registered user and invoke the callback.
157   void OnReadImmutableAttributes(const std::string& user,
158                                  const LockResultCallback& callback);
159
160   chromeos::CryptohomeClient* cryptohome_client_;
161
162   base::WeakPtrFactory<EnterpriseInstallAttributes> weak_ptr_factory_;
163
164   DISALLOW_COPY_AND_ASSIGN(EnterpriseInstallAttributes);
165 };
166
167 }  // namespace policy
168
169 #endif  // CHROME_BROWSER_CHROMEOS_POLICY_ENTERPRISE_INSTALL_ATTRIBUTES_H_