enterprise_install_attributes.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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/memory/weak_ptr.h"
16#include "chromeos/dbus/cryptohome_client.h"
17#include "chromeos/dbus/dbus_method_call_status.h"
18#include "components/policy/core/common/cloud/cloud_policy_constants.h"
19
20namespace policy {
21
22// Brokers access to the enterprise-related installation-time attributes on
23// ChromeOS.
24// TODO(zelidrag, mnissler): Rename + move this class - http://crbug.com/249513.
25class EnterpriseInstallAttributes {
26 public:
27  // Return codes for LockDevice().
28  enum LockResult {
29    LOCK_SUCCESS,
30    LOCK_NOT_READY,
31    LOCK_BACKEND_ERROR,
32    LOCK_WRONG_USER,
33  };
34
35  // A callback to handle responses of methods returning a LockResult value.
36  typedef base::Callback<void(LockResult lock_result)> LockResultCallback;
37
38  // Constants for the possible device modes that can be stored in the lockbox.
39  static const char kConsumerDeviceMode[];
40  static const char kEnterpiseDeviceMode[];
41  static const char kRetailKioskDeviceMode[];
42  static const char kConsumerKioskDeviceMode[];
43  static const char kUnknownDeviceMode[];
44
45  // Field names in the lockbox.
46  static const char kAttrEnterpriseDeviceId[];
47  static const char kAttrEnterpriseDomain[];
48  static const char kAttrEnterpriseMode[];
49  static const char kAttrEnterpriseOwned[];
50  static const char kAttrEnterpriseUser[];
51  static const char kAttrConsumerKioskEnabled[];
52
53  explicit EnterpriseInstallAttributes(
54      chromeos::CryptohomeClient* cryptohome_client);
55  ~EnterpriseInstallAttributes();
56
57  // Reads data from the cache file which is created early during the boot
58  // process.  The cache file is used to work around slow cryptohome startup,
59  // which takes a while to register its DBus interface.  See
60  // http://crosbug.com/37367 for background on this.
61  void ReadCacheFile(const base::FilePath& cache_file);
62
63  // Makes sure the local caches for enterprise-related install attributes are
64  // up-to-date with what cryptohome has. This method checks the readiness of
65  // attributes and read them if ready. Actual read will be performed in
66  // ReadAttributesIfReady().
67  void ReadImmutableAttributes(const base::Closure& callback);
68
69  // Locks the device to be an enterprise device registered by the given user.
70  // This can also be called after the lock has already been taken, in which
71  // case it checks that the passed user agrees with the locked attribute.
72  // |callback| must not be null and is called with the result.
73  void LockDevice(const std::string& user,
74                  DeviceMode device_mode,
75                  const std::string& device_id,
76                  const LockResultCallback& callback);
77
78  // Checks whether this is an enterprise device.
79  bool IsEnterpriseDevice();
80
81  // Checks whether this is a consumer kiosk enabled device.
82  bool IsConsumerKioskDeviceWithAutoLaunch();
83
84  // Gets the domain this device belongs to or an empty string if the device is
85  // not an enterprise device.
86  std::string GetDomain();
87
88  // Gets the user that registered the device. Returns an empty string if the
89  // device is not an enterprise device.
90  std::string GetRegistrationUser();
91
92  // Gets the device id that was generated when the device was registered.
93  // Returns an empty string if the device is not an enterprise device or the
94  // device id was not stored in the lockbox (prior to R19).
95  std::string GetDeviceId();
96
97  // Gets the mode the device was enrolled to. The return value for devices that
98  // are not locked yet will be DEVICE_MODE_UNKNOWN.
99  DeviceMode GetMode();
100
101 protected:
102  bool device_locked_;
103  std::string registration_user_;
104  std::string registration_domain_;
105  std::string registration_device_id_;
106  DeviceMode registration_mode_;
107
108 private:
109  // Decodes the install attributes provided in |attr_map|.
110  void DecodeInstallAttributes(
111      const std::map<std::string, std::string>& attr_map);
112
113  // Helper for ReadImmutableAttributes.
114  void ReadAttributesIfReady(
115      const base::Closure& callback,
116      chromeos::DBusMethodCallStatus call_status,
117      bool result);
118
119  // Helper for LockDevice(). Handles the result of InstallAttributesIsReady()
120  // and continue processing LockDevice if the result is true.
121  void LockDeviceIfAttributesIsReady(
122      const std::string& user,
123      DeviceMode device_mode,
124      const std::string& device_id,
125      const LockResultCallback& callback,
126      chromeos::DBusMethodCallStatus call_status,
127      bool result);
128
129  // Confirms the registered user and invoke the callback.
130  void OnReadImmutableAttributes(const std::string& user,
131                                 const LockResultCallback& callback);
132
133  chromeos::CryptohomeClient* cryptohome_client_;
134
135  base::WeakPtrFactory<EnterpriseInstallAttributes> weak_ptr_factory_;
136
137  DISALLOW_COPY_AND_ASSIGN(EnterpriseInstallAttributes);
138};
139
140}  // namespace policy
141
142#endif  // CHROME_BROWSER_CHROMEOS_POLICY_ENTERPRISE_INSTALL_ATTRIBUTES_H_
143