15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/prefs/tracked/tracked_atomic_preference.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/values.h"
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/prefs/pref_hash_store_transaction.h"
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/prefs/tracked/tracked_preference_validation_delegate.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TrackedAtomicPreference::TrackedAtomicPreference(
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::string& pref_path,
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    size_t reporting_id,
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    size_t reporting_ids_count,
15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    PrefHashFilter::EnforcementLevel enforcement_level,
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    TrackedPreferenceValidationDelegate* delegate)
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : pref_path_(pref_path),
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      helper_(pref_path, reporting_id, reporting_ids_count, enforcement_level),
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      delegate_(delegate) {
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void TrackedAtomicPreference::OnNewValue(
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::Value* value,
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashStoreTransaction* transaction) const {
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  transaction->StoreHash(pref_path_, value);
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool TrackedAtomicPreference::EnforceAndReport(
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::DictionaryValue* pref_store_contents,
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PrefHashStoreTransaction* transaction) const {
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const base::Value* value = NULL;
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  pref_store_contents->Get(pref_path_, &value);
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PrefHashStoreTransaction::ValueState value_state =
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      transaction->CheckValue(pref_path_, value);
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  helper_.ReportValidationResult(value_state);
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TrackedPreferenceHelper::ResetAction reset_action =
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      helper_.GetAction(value_state);
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (delegate_) {
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    delegate_->OnAtomicPreferenceValidation(
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        pref_path_, value, value_state, reset_action);
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  helper_.ReportAction(reset_action);
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool was_reset = false;
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (reset_action == TrackedPreferenceHelper::DO_RESET) {
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    pref_store_contents->RemovePath(pref_path_, NULL);
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    was_reset = true;
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (value_state != PrefHashStoreTransaction::UNCHANGED) {
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Store the hash for the new value (whether it was reset or not).
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::Value* new_value = NULL;
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    pref_store_contents->Get(pref_path_, &new_value);
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    transaction->StoreHash(pref_path_, new_value);
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return was_reset;
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
61