Introduce abstraction for prune state storage.
[chromium-blink-merge.git] / chrome / browser / safe_browsing / incident_reporting / state_store.h
blob91b56ad215bc24178ee0e06003f87c290787f077
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_
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/prefs/scoped_user_pref_update.h"
12 class Profile;
14 namespace safe_browsing {
16 enum class IncidentType : int32_t;
18 // The storage to track which incidents have been reported for a profile. Only
19 // usable on the UI thread.
20 class StateStore {
21 public:
22 using IncidentDigest = uint32_t;
24 // An object through which modifications to a StateStore can be made. Changes
25 // are visible to the StateStore immediately and are written to persistent
26 // storage when the instance is destroyed (or shortly thereafter). Only one
27 // transaction may be live for a given StateStore at a given time. Instances
28 // are typically created on the stack for immediate use.
29 class Transaction {
30 public:
31 explicit Transaction(StateStore* store);
32 ~Transaction();
34 // Marks the described incident as having been reported.
35 void MarkAsReported(IncidentType type,
36 const std::string& key,
37 IncidentDigest digest);
39 // Clears all data associated with an incident type.
40 void ClearForType(IncidentType type);
42 private:
43 // Returns a writable view on the incidents_sent preference. The act of
44 // obtaining this view will cause a serialize-and-write operation to be
45 // scheduled when the transaction terminates. Use the store's
46 // |incidents_sent_| member directly to simply query the preference.
47 base::DictionaryValue* GetPrefDict();
49 // The store corresponding to this transaction.
50 StateStore* store_;
52 // A ScopedUserPrefUpdate through which changes to the incidents_sent
53 // preference are made.
54 scoped_ptr<DictionaryPrefUpdate> pref_update_;
56 DISALLOW_COPY_AND_ASSIGN(Transaction);
59 explicit StateStore(Profile* profile);
60 ~StateStore();
62 // Returns true if the described incident has already been reported.
63 bool HasBeenReported(IncidentType type,
64 const std::string& key,
65 IncidentDigest digest);
67 private:
68 // Called on load to clear values that are no longer used.
69 void CleanLegacyValues(Transaction* transaction);
71 // The profile to which this state corresponds.
72 Profile* profile_;
74 // A read-only view on the profile's incidents_sent preference.
75 const base::DictionaryValue* incidents_sent_;
77 #if DCHECK_IS_ON()
78 // True when a Transaction instance is outstanding.
79 bool has_transaction_;
80 #endif
82 DISALLOW_COPY_AND_ASSIGN(StateStore);
85 } // namespace safe_browsing
87 #endif // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_STATE_STORE_H_