Home | History | Annotate | Download | only in cloud
      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 #include "chrome/browser/policy/cloud/cloud_policy_store.h"
      6 
      7 #include "base/hash.h"
      8 #include "base/logging.h"
      9 #include "chrome/browser/policy/cloud/cloud_external_data_manager.h"
     10 
     11 namespace policy {
     12 
     13 CloudPolicyStore::Observer::~Observer() {}
     14 
     15 CloudPolicyStore::CloudPolicyStore()
     16     : status_(STATUS_OK),
     17       validation_status_(CloudPolicyValidatorBase::VALIDATION_OK),
     18       invalidation_version_(0),
     19       is_initialized_(false),
     20       policy_changed_(false),
     21       hash_value_(0) {}
     22 
     23 CloudPolicyStore::~CloudPolicyStore() {}
     24 
     25 void CloudPolicyStore::Store(
     26     const enterprise_management::PolicyFetchResponse& policy,
     27     int64 invalidation_version) {
     28   invalidation_version_ = invalidation_version;
     29   Store(policy);
     30 }
     31 
     32 void CloudPolicyStore::AddObserver(CloudPolicyStore::Observer* observer) {
     33   observers_.AddObserver(observer);
     34 }
     35 
     36 void CloudPolicyStore::RemoveObserver(CloudPolicyStore::Observer* observer) {
     37   observers_.RemoveObserver(observer);
     38 }
     39 
     40 void CloudPolicyStore::NotifyStoreLoaded() {
     41   // Determine if the policy changed by comparing the new policy's hash value
     42   // to the previous.
     43   uint32 new_hash_value = 0;
     44   if (policy_ && policy_->has_policy_value())
     45     new_hash_value = base::Hash(policy_->policy_value());
     46   policy_changed_ = new_hash_value != hash_value_;
     47   hash_value_ = new_hash_value;
     48 
     49   is_initialized_ = true;
     50   // The |external_data_manager_| must be notified first so that when other
     51   // observers are informed about the changed policies and try to fetch external
     52   // data referenced by these, the |external_data_manager_| has the required
     53   // metadata already.
     54   if (external_data_manager_)
     55     external_data_manager_->OnPolicyStoreLoaded();
     56   FOR_EACH_OBSERVER(Observer, observers_, OnStoreLoaded(this));
     57 }
     58 
     59 void CloudPolicyStore::NotifyStoreError() {
     60   is_initialized_ = true;
     61   FOR_EACH_OBSERVER(Observer, observers_, OnStoreError(this));
     62 }
     63 
     64 void CloudPolicyStore::SetExternalDataManager(
     65     base::WeakPtr<CloudExternalDataManager> external_data_manager) {
     66   DCHECK(!external_data_manager_);
     67   external_data_manager_ = external_data_manager;
     68   if (is_initialized_)
     69     external_data_manager_->OnPolicyStoreLoaded();
     70 }
     71 
     72 }  // namespace policy
     73