Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / safe_browsing / incident_reporting / state_store.h
blobc2226170f4c7e751442a41f501a9cb4f851732c0
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 <string>
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/prefs/scoped_user_pref_update.h"
14 class Profile;
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.
22 class StateStore {
23 public:
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.
31 class Transaction {
32 public:
33 explicit Transaction(StateStore* store);
34 ~Transaction();
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.
45 void ClearAll();
47 private:
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.
60 StateStore* store_;
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);
70 ~StateStore();
72 // Returns true if the described incident has already been reported.
73 bool HasBeenReported(IncidentType type,
74 const std::string& key,
75 IncidentDigest digest);
77 private:
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.
82 Profile* profile_;
84 // A read-only view on the profile's incidents_sent preference.
85 const base::DictionaryValue* incidents_sent_;
87 #if DCHECK_IS_ON()
88 // True when a Transaction instance is outstanding.
89 bool has_transaction_;
90 #endif
92 DISALLOW_COPY_AND_ASSIGN(StateStore);
95 } // namespace safe_browsing
97 #endif // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_STATE_STORE_H_