Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / prefs / pref_hash_store_impl.h
1 // Copyright 2013 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_PREFS_PREF_HASH_STORE_IMPL_H_
6 #define CHROME_BROWSER_PREFS_PREF_HASH_STORE_IMPL_H_
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/prefs/scoped_user_pref_update.h"
13 #include "chrome/browser/prefs/pref_hash_calculator.h"
14 #include "chrome/browser/prefs/pref_hash_store.h"
15
16 class PrefHashStoreTransaction;
17 class PrefRegistrySimple;
18 class PrefService;
19
20 namespace base {
21 class DictionaryValue;
22 class Value;
23 }
24
25 namespace internals {
26
27 // Hash of hashes for each profile, used to validate the existing hashes when
28 // debating whether an unknown value is to be trusted, will be stored as a
29 // string under
30 // |kProfilePreferenceHashes|.|kHashOfHashesDict|.|hash_stored_id_|.
31 extern const char kHashOfHashesDict[];
32
33 // Versions for each PrefHashStore are stored in this dictionary under
34 // |hash_store_id_|.
35 extern const char kStoreVersionsDict[];
36
37 }  // namespace internals
38
39 // Implements PrefHashStoreImpl by storing preference hashes in a PrefService.
40 class PrefHashStoreImpl : public PrefHashStore {
41  public:
42   enum StoreVersion {
43     // No hashes have been stored in this PrefHashStore yet.
44     VERSION_UNINITIALIZED = 0,
45     // The hashes in this PrefHashStore were stored before the introduction
46     // of a version number and should be re-initialized.
47     VERSION_PRE_MIGRATION = 1,
48     // The hashes in this PrefHashStore were stored using the latest algorithm.
49     VERSION_LATEST = 2,
50   };
51
52   // Constructs a PrefHashStoreImpl that calculates hashes using
53   // |seed| and |device_id| and stores them in |local_state|. Multiple hash
54   // stores can use the same |local_state| with distinct |hash_store_id|s.
55   //
56   // The same |seed|, |device_id|, and |hash_store_id| must be used to load and
57   // validate previously stored hashes in |local_state|.
58   //
59   // |local_state| must have previously been passed to |RegisterPrefs|.
60   PrefHashStoreImpl(const std::string& hash_store_id,
61                     const std::string& seed,
62                     const std::string& device_id,
63                     PrefService* local_state);
64
65   // Registers required local state preferences.
66   static void RegisterPrefs(PrefRegistrySimple* registry);
67
68   // Clears the contents of this PrefHashStore. |IsInitialized()| will return
69   // false after this call.
70   void Reset();
71
72   // PrefHashStore implementation.
73   virtual scoped_ptr<PrefHashStoreTransaction> BeginTransaction() OVERRIDE;
74
75   // Returns the current version of this hash store.
76   StoreVersion GetCurrentVersion() const;
77
78  private:
79   class PrefHashStoreTransactionImpl;
80
81   // Returns true if the dictionary of hashes stored for |hash_store_id_| is
82   // trusted (which implies unknown values can be trusted as newly tracked
83   // values).
84   bool IsHashDictionaryTrusted() const;
85
86   const std::string hash_store_id_;
87   const PrefHashCalculator pref_hash_calculator_;
88   PrefService* local_state_;
89   // Must come after |local_state_| and |pref_hash_calculator_| in the
90   // initialization list as it depends on them to compute its value via
91   // IsHashDictionaryTrusted().
92   const bool initial_hashes_dictionary_trusted_;
93
94   DISALLOW_COPY_AND_ASSIGN(PrefHashStoreImpl);
95 };
96
97 #endif  // CHROME_BROWSER_PREFS_PREF_HASH_STORE_IMPL_H_