OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/safe_browsing/incident_reporting/state_store.h" | 5 #include "chrome/browser/safe_browsing/incident_reporting/state_store.h" |
6 | 6 |
7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/safe_browsing/incident_reporting/incident.h" | 11 #include "chrome/browser/safe_browsing/incident_reporting/incident.h" |
| 12 #include "chrome/browser/safe_browsing/incident_reporting/platform_state_store.h
" |
12 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
13 | 14 |
14 namespace safe_browsing { | 15 namespace safe_browsing { |
15 | 16 |
16 // StateStore::Transaction ----------------------------------------------------- | 17 // StateStore::Transaction ----------------------------------------------------- |
17 | 18 |
18 StateStore::Transaction::Transaction(StateStore* store) : store_(store) { | 19 StateStore::Transaction::Transaction(StateStore* store) : store_(store) { |
19 #if DCHECK_IS_ON() | 20 #if DCHECK_IS_ON() |
20 DCHECK(!store_->has_transaction_); | 21 DCHECK(!store_->has_transaction_); |
21 store_->has_transaction_ = true; | 22 store_->has_transaction_ = true; |
22 #endif | 23 #endif |
23 } | 24 } |
24 | 25 |
25 StateStore::Transaction::~Transaction() { | 26 StateStore::Transaction::~Transaction() { |
26 #if DCHECK_IS_ON() | 27 #if DCHECK_IS_ON() |
27 store_->has_transaction_ = false; | 28 store_->has_transaction_ = false; |
28 #endif | 29 #endif |
| 30 if (pref_update_) |
| 31 platform_state_store::Store(store_->profile_, store_->incidents_sent_); |
29 } | 32 } |
30 | 33 |
31 void StateStore::Transaction::MarkAsReported(IncidentType type, | 34 void StateStore::Transaction::MarkAsReported(IncidentType type, |
32 const std::string& key, | 35 const std::string& key, |
33 IncidentDigest digest) { | 36 IncidentDigest digest) { |
34 std::string type_string(base::IntToString(static_cast<int32_t>(type))); | 37 std::string type_string(base::IntToString(static_cast<int32_t>(type))); |
35 base::DictionaryValue* incidents_sent = GetPrefDict(); | 38 base::DictionaryValue* incidents_sent = GetPrefDict(); |
36 base::DictionaryValue* type_dict = nullptr; | 39 base::DictionaryValue* type_dict = nullptr; |
37 if (!incidents_sent->GetDictionaryWithoutPathExpansion(type_string, | 40 if (!incidents_sent->GetDictionaryWithoutPathExpansion(type_string, |
38 &type_dict)) { | 41 &type_dict)) { |
(...skipping 13 matching lines...) Expand all Loading... |
52 // will result in a full serialize-and-write operation on the preferences | 55 // will result in a full serialize-and-write operation on the preferences |
53 // store. | 56 // store. |
54 std::string type_string(base::IntToString(static_cast<int32_t>(type))); | 57 std::string type_string(base::IntToString(static_cast<int32_t>(type))); |
55 const base::DictionaryValue* type_dict = nullptr; | 58 const base::DictionaryValue* type_dict = nullptr; |
56 if (store_->incidents_sent_->GetDictionaryWithoutPathExpansion(type_string, | 59 if (store_->incidents_sent_->GetDictionaryWithoutPathExpansion(type_string, |
57 &type_dict)) { | 60 &type_dict)) { |
58 GetPrefDict()->RemoveWithoutPathExpansion(type_string, nullptr); | 61 GetPrefDict()->RemoveWithoutPathExpansion(type_string, nullptr); |
59 } | 62 } |
60 } | 63 } |
61 | 64 |
| 65 void StateStore::Transaction::ClearAll() { |
| 66 if (pref_update_) |
| 67 pref_update_.reset(); |
| 68 if (store_->incidents_sent_) { |
| 69 store_->incidents_sent_ = nullptr; |
| 70 store_->profile_->GetPrefs()->ClearPref(prefs::kSafeBrowsingIncidentsSent); |
| 71 } |
| 72 } |
| 73 |
62 base::DictionaryValue* StateStore::Transaction::GetPrefDict() { | 74 base::DictionaryValue* StateStore::Transaction::GetPrefDict() { |
63 if (!pref_update_) { | 75 if (!pref_update_) { |
64 pref_update_.reset(new DictionaryPrefUpdate( | 76 pref_update_.reset(new DictionaryPrefUpdate( |
65 store_->profile_->GetPrefs(), prefs::kSafeBrowsingIncidentsSent)); | 77 store_->profile_->GetPrefs(), prefs::kSafeBrowsingIncidentsSent)); |
66 // Getting the dict will cause it to be created if it doesn't exist. | 78 // Getting the dict will cause it to be created if it doesn't exist. |
67 // Unconditionally refresh the store's read-only view on the preference so | 79 // Unconditionally refresh the store's read-only view on the preference so |
68 // that it will always be correct. | 80 // that it will always be correct. |
69 store_->incidents_sent_ = pref_update_->Get(); | 81 store_->incidents_sent_ = pref_update_->Get(); |
70 } | 82 } |
71 return pref_update_->Get(); | 83 return pref_update_->Get(); |
72 } | 84 } |
73 | 85 |
| 86 void StateStore::Transaction::ReplacePrefDict( |
| 87 scoped_ptr<base::DictionaryValue> pref_dict) { |
| 88 GetPrefDict()->Swap(pref_dict.get()); |
| 89 } |
| 90 |
74 | 91 |
75 // StateStore ------------------------------------------------------------------ | 92 // StateStore ------------------------------------------------------------------ |
76 | 93 |
77 StateStore::StateStore(Profile* profile) | 94 StateStore::StateStore(Profile* profile) |
78 : profile_(profile), | 95 : profile_(profile), |
79 incidents_sent_(nullptr) | 96 incidents_sent_(nullptr) |
80 #if DCHECK_IS_ON() | 97 #if DCHECK_IS_ON() |
81 , | 98 , |
82 has_transaction_(false) | 99 has_transaction_(false) |
83 #endif | 100 #endif |
84 { | 101 { |
85 // Cache a read-only view of the preference. | 102 // Cache a read-only view of the preference. |
86 const base::Value* value = | 103 const base::Value* value = |
87 profile_->GetPrefs()->GetUserPrefValue(prefs::kSafeBrowsingIncidentsSent); | 104 profile_->GetPrefs()->GetUserPrefValue(prefs::kSafeBrowsingIncidentsSent); |
88 if (value) | 105 if (value) |
89 value->GetAsDictionary(&incidents_sent_); | 106 value->GetAsDictionary(&incidents_sent_); |
90 | 107 |
| 108 // Apply the platform data. |
91 Transaction transaction(this); | 109 Transaction transaction(this); |
92 CleanLegacyValues(&transaction); | 110 scoped_ptr<base::DictionaryValue> value_dict( |
| 111 platform_state_store::Load(profile_)); |
| 112 if (value_dict) { |
| 113 if (value_dict->empty()) |
| 114 transaction.ClearAll(); |
| 115 else if (!incidents_sent_ || !incidents_sent_->Equals(value_dict.get())) |
| 116 transaction.ReplacePrefDict(value_dict.Pass()); |
| 117 } |
| 118 |
| 119 if (incidents_sent_) |
| 120 CleanLegacyValues(&transaction); |
93 } | 121 } |
94 | 122 |
95 StateStore::~StateStore() { | 123 StateStore::~StateStore() { |
96 #if DCHECK_IS_ON() | 124 #if DCHECK_IS_ON() |
97 DCHECK(!has_transaction_); | 125 DCHECK(!has_transaction_); |
98 #endif | 126 #endif |
99 } | 127 } |
100 | 128 |
101 bool StateStore::HasBeenReported(IncidentType type, | 129 bool StateStore::HasBeenReported(IncidentType type, |
102 const std::string& key, | 130 const std::string& key, |
(...skipping 11 matching lines...) Expand all Loading... |
114 static const IncidentType kLegacyTypes[] = { | 142 static const IncidentType kLegacyTypes[] = { |
115 // TODO(grt): remove in M44 (crbug.com/451173). | 143 // TODO(grt): remove in M44 (crbug.com/451173). |
116 IncidentType::OMNIBOX_INTERACTION, | 144 IncidentType::OMNIBOX_INTERACTION, |
117 }; | 145 }; |
118 | 146 |
119 for (IncidentType type : kLegacyTypes) | 147 for (IncidentType type : kLegacyTypes) |
120 transaction->ClearForType(type); | 148 transaction->ClearForType(type); |
121 } | 149 } |
122 | 150 |
123 } // namespace safe_browsing | 151 } // namespace safe_browsing |
OLD | NEW |