Home | History | Annotate | Download | only in policy
      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 
     20 namespace 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.
     25 class 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. The cache file is used to work around slow
     58   // cryptohome startup, which takes a while to register its DBus interface.
     59   // See http://crosbug.com/37367 for background on this.
     60   void ReadCacheFile(const base::FilePath& cache_file);
     61 
     62   // Makes sure the local caches for enterprise-related install attributes are
     63   // up-to-date with what cryptohome has. This method checks the readiness of
     64   // attributes and read them if ready. Actual read will be performed in
     65   // ReadAttributesIfReady().
     66   void ReadImmutableAttributes(const base::Closure& callback);
     67 
     68   // Locks the device to be an enterprise device registered by the given user.
     69   // This can also be called after the lock has already been taken, in which
     70   // case it checks that the passed user agrees with the locked attribute.
     71   // |callback| must not be null and is called with the result.
     72   void LockDevice(const std::string& user,
     73                   DeviceMode device_mode,
     74                   const std::string& device_id,
     75                   const LockResultCallback& callback);
     76 
     77   // Checks whether this is an enterprise device.
     78   bool IsEnterpriseDevice();
     79 
     80   // Checks whether this is a consumer kiosk enabled device.
     81   bool IsConsumerKioskDevice();
     82 
     83   // Gets the domain this device belongs to or an empty string if the device is
     84   // not an enterprise device.
     85   std::string GetDomain();
     86 
     87   // Gets the user that registered the device. Returns an empty string if the
     88   // device is not an enterprise device.
     89   std::string GetRegistrationUser();
     90 
     91   // Gets the device id that was generated when the device was registered.
     92   // Returns an empty string if the device is not an enterprise device or the
     93   // device id was not stored in the lockbox (prior to R19).
     94   std::string GetDeviceId();
     95 
     96   // Gets the mode the device was enrolled to. The return value for devices that
     97   // are not locked yet will be DEVICE_MODE_UNKNOWN.
     98   DeviceMode GetMode();
     99 
    100  protected:
    101   bool device_locked_;
    102   std::string registration_user_;
    103   std::string registration_domain_;
    104   std::string registration_device_id_;
    105   DeviceMode registration_mode_;
    106 
    107  private:
    108   // Decodes the install attributes provided in |attr_map|.
    109   void DecodeInstallAttributes(
    110       const std::map<std::string, std::string>& attr_map);
    111 
    112   // Helper for ReadImmutableAttributes.
    113   void ReadAttributesIfReady(
    114       const base::Closure& callback,
    115       chromeos::DBusMethodCallStatus call_status,
    116       bool result);
    117 
    118   // Helper for LockDevice(). Handles the result of InstallAttributesIsReady()
    119   // and continue processing LockDevice if the result is true.
    120   void LockDeviceIfAttributesIsReady(
    121       const std::string& user,
    122       DeviceMode device_mode,
    123       const std::string& device_id,
    124       const LockResultCallback& callback,
    125       chromeos::DBusMethodCallStatus call_status,
    126       bool result);
    127 
    128   // Confirms the registered user and invoke the callback.
    129   void OnReadImmutableAttributes(const std::string& user,
    130                                  const LockResultCallback& callback);
    131 
    132   chromeos::CryptohomeClient* cryptohome_client_;
    133 
    134   base::WeakPtrFactory<EnterpriseInstallAttributes> weak_ptr_factory_;
    135 
    136   DISALLOW_COPY_AND_ASSIGN(EnterpriseInstallAttributes);
    137 };
    138 
    139 }  // namespace policy
    140 
    141 #endif  // CHROME_BROWSER_CHROMEOS_POLICY_ENTERPRISE_INSTALL_ATTRIBUTES_H_
    142