| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 1 // Copyright (c) 2011 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_POLICY_CLOUD_POLICY_CACHE_H_ | 
|  | 6 #define CHROME_BROWSER_POLICY_CLOUD_POLICY_CACHE_H_ | 
|  | 7 | 
|  | 8 #include <string> | 
|  | 9 | 
|  | 10 #include "base/file_path.h" | 
|  | 11 #include "base/gtest_prod_util.h" | 
|  | 12 #include "base/ref_counted.h" | 
|  | 13 #include "base/scoped_ptr.h" | 
|  | 14 #include "base/synchronization/lock.h" | 
|  | 15 #include "base/time.h" | 
|  | 16 #include "chrome/browser/policy/configuration_policy_provider.h" | 
|  | 17 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 
|  | 18 #include "policy/configuration_policy_type.h" | 
|  | 19 | 
|  | 20 class DictionaryValue; | 
|  | 21 class ListValue; | 
|  | 22 class Value; | 
|  | 23 | 
|  | 24 using google::protobuf::RepeatedPtrField; | 
|  | 25 | 
|  | 26 namespace policy { | 
|  | 27 | 
|  | 28 namespace em = enterprise_management; | 
|  | 29 | 
|  | 30 // Decodes a CloudPolicySettings object into two maps with mandatory and | 
|  | 31 // recommended settings, respectively. The implementation is generated code | 
|  | 32 // in policy/cloud_policy_generated.cc. | 
|  | 33 void DecodePolicy(const em::CloudPolicySettings& policy, | 
|  | 34                   ConfigurationPolicyProvider::PolicyMapType* mandatory, | 
|  | 35                   ConfigurationPolicyProvider::PolicyMapType* recommended); | 
|  | 36 | 
|  | 37 // Keeps the authoritative copy of cloud policy information as read from the | 
|  | 38 // persistence file or determined by the policy backend. The cache doesn't talk | 
|  | 39 // to the service directly, but receives updated policy information through | 
|  | 40 // SetPolicy() calls, which is then persisted and decoded into the internal | 
|  | 41 // Value representation chrome uses. | 
|  | 42 class CloudPolicyCache { | 
|  | 43  public: | 
|  | 44   typedef ConfigurationPolicyProvider::PolicyMapType PolicyMapType; | 
|  | 45 | 
|  | 46   explicit CloudPolicyCache(const FilePath& backing_file_path); | 
|  | 47   ~CloudPolicyCache(); | 
|  | 48 | 
|  | 49   // Loads policy information from the backing file. Non-existing or erroneous | 
|  | 50   // cache files are ignored. | 
|  | 51   void LoadPolicyFromFile(); | 
|  | 52 | 
|  | 53   // Resets the policy information. Returns true if the new policy is different | 
|  | 54   // from the previously stored policy. | 
|  | 55   bool SetPolicy(const em::CloudPolicyResponse& policy); | 
|  | 56   bool SetDevicePolicy(const em::DevicePolicyResponse& policy); | 
|  | 57 | 
|  | 58   // Gets the policy information. Ownership of the return value is transferred | 
|  | 59   // to the caller. | 
|  | 60   DictionaryValue* GetDevicePolicy(); | 
|  | 61   const PolicyMapType* GetMandatoryPolicy() const; | 
|  | 62   const PolicyMapType* GetRecommendedPolicy() const; | 
|  | 63 | 
|  | 64   void SetUnmanaged(); | 
|  | 65   bool is_unmanaged() const { | 
|  | 66     return is_unmanaged_; | 
|  | 67   } | 
|  | 68 | 
|  | 69   // Returns the time at which the policy was last fetched. | 
|  | 70   base::Time last_policy_refresh_time() const { | 
|  | 71     return last_policy_refresh_time_; | 
|  | 72   } | 
|  | 73 | 
|  | 74   // Returns true if this cache holds (old-style) device policy that should be | 
|  | 75   // given preference over (new-style) mandatory/recommended policy. | 
|  | 76   bool has_device_policy() const { | 
|  | 77     return has_device_policy_; | 
|  | 78   } | 
|  | 79 | 
|  | 80  private: | 
|  | 81   friend class CloudPolicyCacheTest; | 
|  | 82   friend class DeviceManagementPolicyCacheDecodeTest; | 
|  | 83 | 
|  | 84   // Decodes a CloudPolicyResponse into two (ConfigurationPolicyType -> Value*) | 
|  | 85   // maps and a timestamp. Also performs verification, returns NULL if any | 
|  | 86   // check fails. | 
|  | 87   static bool DecodePolicyResponse( | 
|  | 88       const em::CloudPolicyResponse& policy_response, | 
|  | 89       PolicyMapType* mandatory, | 
|  | 90       PolicyMapType* recommended, | 
|  | 91       base::Time* timestamp); | 
|  | 92 | 
|  | 93   // Returns true if |certificate_chain| is trusted and a |signature| created | 
|  | 94   // from it matches |data|. | 
|  | 95   static bool VerifySignature( | 
|  | 96       const std::string& signature, | 
|  | 97       const std::string& data, | 
|  | 98       const RepeatedPtrField<std::string>& certificate_chain); | 
|  | 99 | 
|  | 100   // Returns true if |a| equals |b|. | 
|  | 101   static bool Equals(const PolicyMapType& a, const PolicyMapType& b); | 
|  | 102   // Helper function for the above. | 
|  | 103   static bool MapEntryEquals(const PolicyMapType::value_type& a, | 
|  | 104                              const PolicyMapType::value_type& b); | 
|  | 105 | 
|  | 106   // Decodes an int64 value. Checks whether the passed value fits the numeric | 
|  | 107   // limits of the value representation. Returns a value (ownership is | 
|  | 108   // transferred to the caller) on success, NULL on failure. | 
|  | 109   static Value* DecodeIntegerValue(google::protobuf::int64 value); | 
|  | 110 | 
|  | 111   // Decode a GenericValue message to the Value representation used internally. | 
|  | 112   // Returns NULL if |value| is invalid (i.e. contains no actual value). | 
|  | 113   static Value* DecodeValue(const em::GenericValue& value); | 
|  | 114 | 
|  | 115   // Decodes a policy message and returns it in Value representation. Ownership | 
|  | 116   // of the returned dictionary is transferred to the caller. | 
|  | 117   static DictionaryValue* DecodeDevicePolicy( | 
|  | 118       const em::DevicePolicyResponse& response); | 
|  | 119 | 
|  | 120   // The file in which we store a cached version of the policy information. | 
|  | 121   const FilePath backing_file_path_; | 
|  | 122 | 
|  | 123   // Protects both |mandatory_policy_| and |recommended_policy_| as well as | 
|  | 124   // |device_policy_|. | 
|  | 125   base::Lock lock_; | 
|  | 126 | 
|  | 127   // Policy key-value information. | 
|  | 128   PolicyMapType mandatory_policy_; | 
|  | 129   PolicyMapType recommended_policy_; | 
|  | 130   scoped_ptr<DictionaryValue> device_policy_; | 
|  | 131 | 
|  | 132   // Tracks whether the store received a SetPolicy() call, which overrides any | 
|  | 133   // information loaded from the file. | 
|  | 134   bool fresh_policy_; | 
|  | 135 | 
|  | 136   bool is_unmanaged_; | 
|  | 137 | 
|  | 138   // Tracks whether the cache currently stores |device_policy_| that should be | 
|  | 139   // given preference over |mandatory_policy_| and |recommended_policy_|. | 
|  | 140   bool has_device_policy_; | 
|  | 141 | 
|  | 142   // The time at which the policy was last refreshed. | 
|  | 143   base::Time last_policy_refresh_time_; | 
|  | 144 }; | 
|  | 145 | 
|  | 146 }  // namespace policy | 
|  | 147 | 
|  | 148 #endif  // CHROME_BROWSER_POLICY_CLOUD_POLICY_CACHE_H_ | 
| OLD | NEW | 
|---|