| OLD | NEW |
| (Empty) |
| 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_POLICY_CLOUD_POLICY_STORE_H_ | |
| 6 #define CHROME_BROWSER_POLICY_CLOUD_POLICY_STORE_H_ | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/observer_list.h" | |
| 11 #include "chrome/browser/policy/cloud_policy_validator.h" | |
| 12 #include "chrome/browser/policy/policy_map.h" | |
| 13 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
| 14 | |
| 15 namespace policy { | |
| 16 | |
| 17 // Defines the low-level interface used by the cloud policy code to: | |
| 18 // 1. Validate policy blobs that should be applied locally | |
| 19 // 2. Persist policy blobs | |
| 20 // 3. Decode policy blobs to PolicyMap representation | |
| 21 class CloudPolicyStore { | |
| 22 public: | |
| 23 // Status codes. | |
| 24 enum Status { | |
| 25 // Everything is in good order. | |
| 26 STATUS_OK, | |
| 27 // Loading policy from the underlying data store failed. | |
| 28 STATUS_LOAD_ERROR, | |
| 29 // Failed to store policy to the data store. | |
| 30 STATUS_STORE_ERROR, | |
| 31 // Failed to parse the policy read from the data store. | |
| 32 STATUS_PARSE_ERROR, | |
| 33 // Failed to serialize policy for storage. | |
| 34 STATUS_SERIALIZE_ERROR, | |
| 35 // Validation error. | |
| 36 STATUS_VALIDATION_ERROR, | |
| 37 // Store cannot accept policy (e.g. non-enterprise device). | |
| 38 STATUS_BAD_STATE, | |
| 39 }; | |
| 40 | |
| 41 // Callbacks for policy store events. Most importantly, policy updates. | |
| 42 class Observer { | |
| 43 public: | |
| 44 virtual ~Observer(); | |
| 45 | |
| 46 // Called on changes to store->policy() and/or store->policy_map(). | |
| 47 virtual void OnStoreLoaded(CloudPolicyStore* store) = 0; | |
| 48 | |
| 49 // Called upon encountering errors. | |
| 50 virtual void OnStoreError(CloudPolicyStore* store) = 0; | |
| 51 }; | |
| 52 | |
| 53 CloudPolicyStore(); | |
| 54 virtual ~CloudPolicyStore(); | |
| 55 | |
| 56 // Indicates whether the store has been fully initialized. This is | |
| 57 // accomplished by calling Load() after startup. | |
| 58 bool is_initialized() const { return is_initialized_; } | |
| 59 | |
| 60 const PolicyMap& policy_map() const { return policy_map_; } | |
| 61 bool has_policy() const { | |
| 62 return policy_.get() != NULL; | |
| 63 } | |
| 64 const enterprise_management::PolicyData* policy() const { | |
| 65 return policy_.get(); | |
| 66 } | |
| 67 bool is_managed() const { | |
| 68 return policy_.get() && | |
| 69 policy_->state() == enterprise_management::PolicyData::ACTIVE; | |
| 70 } | |
| 71 Status status() const { return status_; } | |
| 72 CloudPolicyValidatorBase::Status validation_status() const { | |
| 73 return validation_status_; | |
| 74 } | |
| 75 | |
| 76 // Store a new policy blob. Pending load/store operations will be canceled. | |
| 77 // The store operation may proceed asynchronously and observers are notified | |
| 78 // once the operation finishes. If successful, OnStoreLoaded() will be invoked | |
| 79 // on the observers and the updated policy can be read through policy(). | |
| 80 // Errors generate OnStoreError() notifications. | |
| 81 virtual void Store( | |
| 82 const enterprise_management::PolicyFetchResponse& policy) = 0; | |
| 83 | |
| 84 // Load the current policy blob from persistent storage. Pending load/store | |
| 85 // operations will be canceled. This may trigger asynchronous operations. | |
| 86 // Upon success, OnStoreLoaded() will be called on the registered observers. | |
| 87 // Otherwise, OnStoreError() reports the reason for failure. | |
| 88 virtual void Load() = 0; | |
| 89 | |
| 90 // Registers an observer to be notified when policy changes. | |
| 91 void AddObserver(Observer* observer); | |
| 92 | |
| 93 // Removes the specified observer. | |
| 94 void RemoveObserver(Observer* observer); | |
| 95 | |
| 96 protected: | |
| 97 // Invokes the corresponding callback on all registered observers. | |
| 98 void NotifyStoreLoaded(); | |
| 99 void NotifyStoreError(); | |
| 100 | |
| 101 // Decoded version of the currently effective policy. | |
| 102 PolicyMap policy_map_; | |
| 103 | |
| 104 // Currently effective policy. | |
| 105 scoped_ptr<enterprise_management::PolicyData> policy_; | |
| 106 | |
| 107 // Latest status code. | |
| 108 Status status_; | |
| 109 | |
| 110 // Latest validation status. | |
| 111 CloudPolicyValidatorBase::Status validation_status_; | |
| 112 | |
| 113 private: | |
| 114 // Whether the store has completed asynchronous initialization, which is | |
| 115 // triggered by calling Load(). | |
| 116 bool is_initialized_; | |
| 117 | |
| 118 ObserverList<Observer, true> observers_; | |
| 119 | |
| 120 DISALLOW_COPY_AND_ASSIGN(CloudPolicyStore); | |
| 121 }; | |
| 122 | |
| 123 } // namespace policy | |
| 124 | |
| 125 #endif // CHROME_BROWSER_POLICY_CLOUD_POLICY_STORE_H_ | |
| OLD | NEW |