Home | History | Annotate | Download | only in install_attributes
      1 // Copyright 2016 The Chromium OS 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 LIBBRILLO_INSTALL_ATTRIBUTES_LIBINSTALLATTRIBUTES_H_
      6 #define LIBBRILLO_INSTALL_ATTRIBUTES_LIBINSTALLATTRIBUTES_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     11 #include <base/files/file_path.h>
     12 #include <brillo/brillo_export.h>
     13 
     14 // Simple caching reader for the (verified) install attributes, a TPM-backed
     15 // write once read many store.  Install attributes may be written exactly once
     16 // by a single, atomic write-and-lock operation encompassing zero or more
     17 // attributes.  Once locked, install attributes cannot be re-written unless TPM
     18 // is reset (eg. by powerwashing the device).
     19 class BRILLO_EXPORT InstallAttributesReader {
     20  public:
     21   static const char kAttrMode[];
     22 
     23   // Constants for the possible device modes.
     24   static const char kDeviceModeConsumer[];
     25   static const char kDeviceModeEnterprise[];
     26   static const char kDeviceModeEnterpriseAD[];
     27   static const char kDeviceModeLegacyRetail[];
     28   static const char kDeviceModeConsumerKiosk[];
     29 
     30   InstallAttributesReader();
     31   virtual ~InstallAttributesReader();
     32 
     33   // Try to load install attributes (unless cached already) and return the
     34   // attribute for |key| or an empty string in case |key| doesn't exist or in
     35   // case install attributes couldn't (yet) be loaded.  The latter is expected
     36   // during OOBE (install attributes haven't yet been finalized) or early in the
     37   // boot sequence (install attributes haven't yet been verified).
     38   const std::string& GetAttribute(const std::string& key);
     39 
     40   // Try to load install attributes (unless cached already) and return whether
     41   // they have yet been written-and-locked.
     42   bool IsLocked();
     43 
     44  protected:
     45   // Attributes cache.
     46   std::map<std::string, std::string> attributes_;
     47 
     48   // Path to the *verified* install attributes file on disk.
     49   base::FilePath install_attributes_path_;
     50 
     51   // Whether install attributes have been read successfully.  Reading a file
     52   // containing an empty attributes proto indicates consumer mode and counts as
     53   // successful, too.
     54   bool initialized_ = false;
     55 
     56 private:
     57   // Try to load the verified install attributes from disk.  This is expected to
     58   // fail when install attributes haven't yet been finalized (OOBE) or verified
     59   // (early in the boot sequence).
     60   void TryToLoad();
     61 
     62   // Empty string to return on error.
     63   std::string empty_string_;
     64 };
     65 
     66 #endif  // LIBBRILLO_LIBINSTALLATTRIBUTES_H_
     67