• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_PREFS_PERSISTENT_PREF_STORE_H_
6 #define COMPONENTS_PREFS_PERSISTENT_PREF_STORE_H_
7 
8 #include "base/functional/callback.h"
9 #include "components/prefs/prefs_export.h"
10 #include "components/prefs/writeable_pref_store.h"
11 
12 // This interface is complementary to the PrefStore interface, declaring
13 // additional functionality that adds support for setting values and persisting
14 // the data to some backing store.
15 class COMPONENTS_PREFS_EXPORT PersistentPrefStore : public WriteablePrefStore {
16  public:
17   // Unique integer code for each type of error so we can report them
18   // distinctly in a histogram.
19   // NOTE: Don't change the explicit values of the enums as it will change the
20   // server's meaning of the histogram.
21   enum PrefReadError {
22     PREF_READ_ERROR_NONE = 0,
23     PREF_READ_ERROR_JSON_PARSE = 1,
24     PREF_READ_ERROR_JSON_TYPE = 2,
25     PREF_READ_ERROR_ACCESS_DENIED = 3,
26     PREF_READ_ERROR_FILE_OTHER = 4,
27     PREF_READ_ERROR_FILE_LOCKED = 5,
28     PREF_READ_ERROR_NO_FILE = 6,
29     PREF_READ_ERROR_JSON_REPEAT = 7,
30     // PREF_READ_ERROR_OTHER = 8,  // Deprecated.
31     PREF_READ_ERROR_FILE_NOT_SPECIFIED = 9,
32     // Indicates that ReadPrefs() couldn't complete synchronously and is waiting
33     // for an asynchronous task to complete first.
34     PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE = 10,
35     PREF_READ_ERROR_MAX_ENUM
36   };
37 
38   class ReadErrorDelegate {
39    public:
~ReadErrorDelegate()40     virtual ~ReadErrorDelegate() {}
41 
42     virtual void OnError(PrefReadError error) = 0;
43   };
44 
45   // Whether the store is in a pseudo-read-only mode where changes are not
46   // actually persisted to disk.  This happens in some cases when there are
47   // read errors during startup.
48   virtual bool ReadOnly() const = 0;
49 
50   // Gets the read error. Only valid if IsInitializationComplete() returns true.
51   virtual PrefReadError GetReadError() const = 0;
52 
53   // Reads the preferences from disk. Notifies observers via
54   // "PrefStore::OnInitializationCompleted" when done.
55   virtual PrefReadError ReadPrefs() = 0;
56 
57   // Reads the preferences from disk asynchronously. Notifies observers via
58   // "PrefStore::OnInitializationCompleted" when done. Also it fires
59   // |error_delegate| if it is not NULL and reading error has occurred.
60   // Owns |error_delegate|.
61   virtual void ReadPrefsAsync(ReadErrorDelegate* error_delegate) = 0;
62 
63   // Lands pending writes to disk. |reply_callback| will be posted to the
64   // current sequence when changes have been written.
65   // |synchronous_done_callback| on the other hand will be invoked right away
66   // wherever the writes complete (could even be invoked synchronously if no
67   // writes need to occur); this is useful when the current thread cannot pump
68   // messages to observe the reply (e.g. nested loops banned on main thread
69   // during shutdown). |synchronous_done_callback| must be thread-safe.
70   virtual void CommitPendingWrite(
71       base::OnceClosure reply_callback = base::OnceClosure(),
72       base::OnceClosure synchronous_done_callback = base::OnceClosure());
73 
74   // Schedules a write if there is any lossy data pending. Unlike
75   // CommitPendingWrite() this does not immediately sync to disk, instead it
76   // triggers an eventual write if there is lossy data pending and if there
77   // isn't one scheduled already.
78   virtual void SchedulePendingLossyWrites() = 0;
79 
80   // Cleans preference data that may have been saved outside of the store.
81   virtual void OnStoreDeletionFromDisk() = 0;
82 
83   // TODO(crbug.com/942491) Remove this after fixing the bug.
84   virtual bool IsInMemoryPrefStore() const;
85 
86  protected:
~PersistentPrefStore()87   ~PersistentPrefStore() override {}
88 };
89 
90 #endif  // COMPONENTS_PREFS_PERSISTENT_PREF_STORE_H_
91