OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/prefs/pref_hash_store_impl.h" | 5 #include "chrome/browser/prefs/pref_hash_store_impl.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "chrome/browser/prefs/pref_hash_store_transaction.h" | 10 #include "chrome/browser/prefs/pref_hash_store_transaction.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 bool GetSplitMacs(const std::string& path, | 52 bool GetSplitMacs(const std::string& path, |
53 std::map<std::string, std::string>* split_macs) const; | 53 std::map<std::string, std::string>* split_macs) const; |
54 PrefHashStoreImpl* outer_; | 54 PrefHashStoreImpl* outer_; |
55 bool has_changed_; | 55 bool has_changed_; |
56 | 56 |
57 DISALLOW_COPY_AND_ASSIGN(PrefHashStoreTransactionImpl); | 57 DISALLOW_COPY_AND_ASSIGN(PrefHashStoreTransactionImpl); |
58 }; | 58 }; |
59 | 59 |
60 PrefHashStoreImpl::PrefHashStoreImpl(const std::string& seed, | 60 PrefHashStoreImpl::PrefHashStoreImpl(const std::string& seed, |
61 const std::string& device_id, | 61 const std::string& device_id, |
62 scoped_ptr<HashStoreContents> contents) | 62 scoped_ptr<HashStoreContents> contents, |
| 63 bool use_super_mac) |
63 : pref_hash_calculator_(seed, device_id), | 64 : pref_hash_calculator_(seed, device_id), |
64 contents_(contents.Pass()), | 65 contents_(contents.Pass()), |
65 initial_hashes_dictionary_trusted_( | 66 initial_hashes_dictionary_trusted_( |
66 IsHashDictionaryTrusted(pref_hash_calculator_, *contents_)), | 67 use_super_mac |
| 68 ? IsHashDictionaryTrusted(pref_hash_calculator_, *contents_) |
| 69 : false), |
| 70 use_super_mac_(use_super_mac), |
67 has_pending_write_(false) { | 71 has_pending_write_(false) { |
68 DCHECK(contents_); | 72 DCHECK(contents_); |
69 UMA_HISTOGRAM_BOOLEAN("Settings.HashesDictionaryTrusted", | 73 UMA_HISTOGRAM_BOOLEAN("Settings.HashesDictionaryTrusted", |
70 initial_hashes_dictionary_trusted_); | 74 initial_hashes_dictionary_trusted_); |
71 } | 75 } |
72 | 76 |
73 PrefHashStoreImpl::~PrefHashStoreImpl() {} | 77 PrefHashStoreImpl::~PrefHashStoreImpl() {} |
74 | 78 |
75 void PrefHashStoreImpl::Reset() { | 79 void PrefHashStoreImpl::Reset() { |
76 contents_->Reset(); | 80 contents_->Reset(); |
77 } | 81 } |
78 | 82 |
79 scoped_ptr<PrefHashStoreTransaction> PrefHashStoreImpl::BeginTransaction() { | 83 scoped_ptr<PrefHashStoreTransaction> PrefHashStoreImpl::BeginTransaction() { |
80 return scoped_ptr<PrefHashStoreTransaction>( | 84 return scoped_ptr<PrefHashStoreTransaction>( |
81 new PrefHashStoreTransactionImpl(this)); | 85 new PrefHashStoreTransactionImpl(this)); |
82 } | 86 } |
83 | 87 |
84 PrefHashStoreImpl::StoreVersion PrefHashStoreImpl::GetCurrentVersion() const { | |
85 if (!contents_->IsInitialized()) | |
86 return VERSION_UNINITIALIZED; | |
87 | |
88 int current_version; | |
89 if (!contents_->GetVersion(¤t_version)) { | |
90 return VERSION_PRE_MIGRATION; | |
91 } | |
92 | |
93 DCHECK_GT(current_version, VERSION_PRE_MIGRATION); | |
94 return static_cast<StoreVersion>(current_version); | |
95 } | |
96 | |
97 void PrefHashStoreImpl::CommitPendingWrite() { | 88 void PrefHashStoreImpl::CommitPendingWrite() { |
98 if (has_pending_write_) { | 89 if (has_pending_write_) { |
99 contents_->CommitPendingWrite(); | 90 contents_->CommitPendingWrite(); |
100 has_pending_write_ = false; | 91 has_pending_write_ = false; |
101 } | 92 } |
102 } | 93 } |
103 | 94 |
104 PrefHashStoreImpl::PrefHashStoreTransactionImpl::PrefHashStoreTransactionImpl( | 95 PrefHashStoreImpl::PrefHashStoreTransactionImpl::PrefHashStoreTransactionImpl( |
105 PrefHashStoreImpl* outer) : outer_(outer), has_changed_(false) { | 96 PrefHashStoreImpl* outer) : outer_(outer), has_changed_(false) { |
106 } | 97 } |
107 | 98 |
108 PrefHashStoreImpl::PrefHashStoreTransactionImpl:: | 99 PrefHashStoreImpl::PrefHashStoreTransactionImpl:: |
109 ~PrefHashStoreTransactionImpl() { | 100 ~PrefHashStoreTransactionImpl() { |
110 // Update the super MAC if and only if the hashes dictionary has been | 101 // Update the super MAC if and only if the hashes dictionary has been |
111 // modified in this transaction. | 102 // modified in this transaction. |
112 if (has_changed_) { | 103 if (has_changed_) { |
113 // Get the dictionary of hashes (or NULL if it doesn't exist). | 104 if (outer_->use_super_mac_) { |
114 const base::DictionaryValue* hashes_dict = outer_->contents_->GetContents(); | 105 // Get the dictionary of hashes (or NULL if it doesn't exist). |
115 outer_->contents_->SetSuperMac(outer_->pref_hash_calculator_.Calculate( | 106 const base::DictionaryValue* hashes_dict = |
116 outer_->contents_->hash_store_id(), hashes_dict)); | 107 outer_->contents_->GetContents(); |
117 | 108 outer_->contents_->SetSuperMac(outer_->pref_hash_calculator_.Calculate( |
| 109 outer_->contents_->hash_store_id(), hashes_dict)); |
| 110 } |
118 outer_->has_pending_write_ = true; | 111 outer_->has_pending_write_ = true; |
119 } | 112 } |
120 | 113 |
121 // Mark this hash store has having been updated to the latest version (in | |
122 // practice only initialization transactions will actually do this, but | |
123 // since they always occur before minor update transaction it's okay | |
124 // to unconditionally do this here). Only do this if this store's version | |
125 // isn't already at VERSION_LATEST (to avoid scheduling a write when | |
126 // unecessary). Note, this is outside of |if (has_changed)| to also seed | |
127 // version number of otherwise unchanged profiles. | |
128 int current_version; | |
129 if (!outer_->contents_->GetVersion(¤t_version) || | |
130 current_version != VERSION_LATEST) { | |
131 outer_->contents_->SetVersion(VERSION_LATEST); | |
132 outer_->has_pending_write_ = true; | |
133 } | |
134 } | 114 } |
135 | 115 |
136 PrefHashStoreTransaction::ValueState | 116 PrefHashStoreTransaction::ValueState |
137 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue( | 117 PrefHashStoreImpl::PrefHashStoreTransactionImpl::CheckValue( |
138 const std::string& path, const base::Value* initial_value) const { | 118 const std::string& path, const base::Value* initial_value) const { |
139 const base::DictionaryValue* hashed_prefs = outer_->contents_->GetContents(); | 119 const base::DictionaryValue* hashed_prefs = outer_->contents_->GetContents(); |
140 | 120 |
141 std::string last_hash; | 121 std::string last_hash; |
142 if (hashed_prefs) | 122 if (hashed_prefs) |
143 hashed_prefs->GetString(path, &last_hash); | 123 hashed_prefs->GetString(path, &last_hash); |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 it.Advance()) { | 268 it.Advance()) { |
289 std::string mac_string; | 269 std::string mac_string; |
290 if (!it.value().GetAsString(&mac_string)) { | 270 if (!it.value().GetAsString(&mac_string)) { |
291 NOTREACHED(); | 271 NOTREACHED(); |
292 continue; | 272 continue; |
293 } | 273 } |
294 split_macs->insert(make_pair(it.key(), mac_string)); | 274 split_macs->insert(make_pair(it.key(), mac_string)); |
295 } | 275 } |
296 return true; | 276 return true; |
297 } | 277 } |
OLD | NEW |