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 // The result of state store initialization. Values here are used for UMA so
27 // they must not be changed.
28 enum InitializationResult
{
29 PSS_NULL
= 0, // The platform state store was absent or not supported.
30 PSS_EMPTY
= 1, // The platform state store was empty and the pref wasn't.
31 PSS_DIFFERS
= 2, // The platform state store differed from the preference.
32 PSS_MATCHES
= 3, // The platform state store matched the preference.
33 NUM_INITIALIZATION_RESULTS
= 4,
36 // An object through which modifications to a StateStore can be made. Changes
37 // are visible to the StateStore immediately and are written to persistent
38 // storage when the instance is destroyed (or shortly thereafter). Only one
39 // transaction may be live for a given StateStore at a given time. Instances
40 // are typically created on the stack for immediate use.
43 explicit Transaction(StateStore
* store
);
46 // Marks the described incident as having been reported.
47 void MarkAsReported(IncidentType type
,
48 const std::string
& key
,
49 IncidentDigest digest
);
51 // Clears all data associated with an incident type.
52 void ClearForType(IncidentType type
);
54 // Clears all data in the store.
58 friend class StateStore
;
60 // Returns a writable view on the incidents_sent preference. The act of
61 // obtaining this view will cause a serialize-and-write operation to be
62 // scheduled when the transaction terminates. Use the store's
63 // |incidents_sent_| member directly to simply query the preference.
64 base::DictionaryValue
* GetPrefDict();
66 // Replaces the contents of the underlying dictionary value.
67 void ReplacePrefDict(scoped_ptr
<base::DictionaryValue
> pref_dict
);
69 // The store corresponding to this transaction.
72 // A ScopedUserPrefUpdate through which changes to the incidents_sent
73 // preference are made.
74 scoped_ptr
<DictionaryPrefUpdate
> pref_update_
;
76 DISALLOW_COPY_AND_ASSIGN(Transaction
);
79 explicit StateStore(Profile
* profile
);
82 // Returns true if the described incident has already been reported.
83 bool HasBeenReported(IncidentType type
,
84 const std::string
& key
,
85 IncidentDigest digest
);
88 // Called on load to clear values that are no longer used.
89 void CleanLegacyValues(Transaction
* transaction
);
91 // The profile to which this state corresponds.
94 // A read-only view on the profile's incidents_sent preference.
95 const base::DictionaryValue
* incidents_sent_
;
98 // True when a Transaction instance is outstanding.
99 bool has_transaction_
;
102 DISALLOW_COPY_AND_ASSIGN(StateStore
);
105 } // namespace safe_browsing
107 #endif // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_STATE_STORE_H_