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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_STATE_STORE_H_
6 #define CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_STATE_STORE_H_
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/prefs/scoped_user_pref_update.h"
16 namespace safe_browsing
{
18 enum class IncidentType
: int32_t;
20 // The storage to track which incidents have been reported for a profile. Only
21 // usable on the UI thread.
24 using IncidentDigest
= uint32_t;
26 // An object through which modifications to a StateStore can be made. Changes
27 // are visible to the StateStore immediately and are written to persistent
28 // storage when the instance is destroyed (or shortly thereafter). Only one
29 // transaction may be live for a given StateStore at a given time. Instances
30 // are typically created on the stack for immediate use.
33 explicit Transaction(StateStore
* store
);
36 // Marks the described incident as having been reported.
37 void MarkAsReported(IncidentType type
,
38 const std::string
& key
,
39 IncidentDigest digest
);
41 // Clears all data associated with an incident type.
42 void ClearForType(IncidentType type
);
44 // Clears all data in the store.
48 friend class StateStore
;
50 // Returns a writable view on the incidents_sent preference. The act of
51 // obtaining this view will cause a serialize-and-write operation to be
52 // scheduled when the transaction terminates. Use the store's
53 // |incidents_sent_| member directly to simply query the preference.
54 base::DictionaryValue
* GetPrefDict();
56 // Replaces the contents of the underlying dictionary value.
57 void ReplacePrefDict(scoped_ptr
<base::DictionaryValue
> pref_dict
);
59 // The store corresponding to this transaction.
62 // A ScopedUserPrefUpdate through which changes to the incidents_sent
63 // preference are made.
64 scoped_ptr
<DictionaryPrefUpdate
> pref_update_
;
66 DISALLOW_COPY_AND_ASSIGN(Transaction
);
69 explicit StateStore(Profile
* profile
);
72 // Returns true if the described incident has already been reported.
73 bool HasBeenReported(IncidentType type
,
74 const std::string
& key
,
75 IncidentDigest digest
);
78 // Called on load to clear values that are no longer used.
79 void CleanLegacyValues(Transaction
* transaction
);
81 // The profile to which this state corresponds.
84 // A read-only view on the profile's incidents_sent preference.
85 const base::DictionaryValue
* incidents_sent_
;
88 // True when a Transaction instance is outstanding.
89 bool has_transaction_
;
92 DISALLOW_COPY_AND_ASSIGN(StateStore
);
95 } // namespace safe_browsing
97 #endif // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_STATE_STORE_H_