• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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.
4 
5 #include "libcef/browser/prefs/pref_store.h"
6 
7 #include <memory>
8 #include <utility>
9 
10 #include "base/memory/ptr_util.h"
11 #include "base/values.h"
12 
CefPrefStore()13 CefPrefStore::CefPrefStore()
14     : read_only_(true),
15       read_success_(true),
16       read_error_(PersistentPrefStore::PREF_READ_ERROR_NONE),
17       block_async_read_(false),
18       pending_async_read_(false),
19       init_complete_(false),
20       committed_(true) {}
21 
GetValue(const std::string & key,const base::Value ** value) const22 bool CefPrefStore::GetValue(const std::string& key,
23                             const base::Value** value) const {
24   return prefs_.GetValue(key, value);
25 }
26 
GetValues() const27 std::unique_ptr<base::DictionaryValue> CefPrefStore::GetValues() const {
28   return prefs_.AsDictionaryValue();
29 }
30 
GetMutableValue(const std::string & key,base::Value ** value)31 bool CefPrefStore::GetMutableValue(const std::string& key,
32                                    base::Value** value) {
33   return prefs_.GetValue(key, value);
34 }
35 
AddObserver(PrefStore::Observer * observer)36 void CefPrefStore::AddObserver(PrefStore::Observer* observer) {
37   observers_.AddObserver(observer);
38 }
39 
RemoveObserver(PrefStore::Observer * observer)40 void CefPrefStore::RemoveObserver(PrefStore::Observer* observer) {
41   observers_.RemoveObserver(observer);
42 }
43 
HasObservers() const44 bool CefPrefStore::HasObservers() const {
45   return !observers_.empty();
46 }
47 
IsInitializationComplete() const48 bool CefPrefStore::IsInitializationComplete() const {
49   return init_complete_;
50 }
51 
SetValue(const std::string & key,std::unique_ptr<base::Value> value,uint32_t flags)52 void CefPrefStore::SetValue(const std::string& key,
53                             std::unique_ptr<base::Value> value,
54                             uint32_t flags) {
55   if (prefs_.SetValue(key, base::Value::FromUniquePtrValue(std::move(value)))) {
56     committed_ = false;
57     NotifyPrefValueChanged(key);
58   }
59 }
60 
SetValueSilently(const std::string & key,std::unique_ptr<base::Value> value,uint32_t flags)61 void CefPrefStore::SetValueSilently(const std::string& key,
62                                     std::unique_ptr<base::Value> value,
63                                     uint32_t flags) {
64   if (prefs_.SetValue(key, base::Value::FromUniquePtrValue(std::move(value))))
65     committed_ = false;
66 }
67 
RemoveValuesByPrefixSilently(const std::string & prefix)68 void CefPrefStore::RemoveValuesByPrefixSilently(const std::string& prefix) {
69   prefs_.ClearWithPrefix(prefix);
70   committed_ = false;
71 }
72 
RemoveValue(const std::string & key,uint32_t flags)73 void CefPrefStore::RemoveValue(const std::string& key, uint32_t flags) {
74   if (prefs_.RemoveValue(key)) {
75     committed_ = false;
76     NotifyPrefValueChanged(key);
77   }
78 }
79 
ReadOnly() const80 bool CefPrefStore::ReadOnly() const {
81   return read_only_;
82 }
83 
GetReadError() const84 PersistentPrefStore::PrefReadError CefPrefStore::GetReadError() const {
85   return read_error_;
86 }
87 
ReadPrefs()88 PersistentPrefStore::PrefReadError CefPrefStore::ReadPrefs() {
89   NotifyInitializationCompleted();
90   return read_error_;
91 }
92 
ReadPrefsAsync(ReadErrorDelegate * error_delegate)93 void CefPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) {
94   DCHECK(!pending_async_read_);
95   error_delegate_.reset(error_delegate);
96   if (block_async_read_)
97     pending_async_read_ = true;
98   else
99     NotifyInitializationCompleted();
100 }
101 
CommitPendingWrite(base::OnceClosure done_callback,base::OnceClosure synchronous_done_callback)102 void CefPrefStore::CommitPendingWrite(
103     base::OnceClosure done_callback,
104     base::OnceClosure synchronous_done_callback) {
105   committed_ = true;
106   PersistentPrefStore::CommitPendingWrite(std::move(done_callback),
107                                           std::move(synchronous_done_callback));
108 }
109 
SchedulePendingLossyWrites()110 void CefPrefStore::SchedulePendingLossyWrites() {}
111 
ClearMutableValues()112 void CefPrefStore::ClearMutableValues() {
113   NOTIMPLEMENTED();
114 }
115 
OnStoreDeletionFromDisk()116 void CefPrefStore::OnStoreDeletionFromDisk() {}
117 
SetInitializationCompleted()118 void CefPrefStore::SetInitializationCompleted() {
119   NotifyInitializationCompleted();
120 }
121 
NotifyPrefValueChanged(const std::string & key)122 void CefPrefStore::NotifyPrefValueChanged(const std::string& key) {
123   for (Observer& observer : observers_)
124     observer.OnPrefValueChanged(key);
125 }
126 
NotifyInitializationCompleted()127 void CefPrefStore::NotifyInitializationCompleted() {
128   DCHECK(!init_complete_);
129   init_complete_ = true;
130   if (read_success_ && read_error_ != PREF_READ_ERROR_NONE && error_delegate_)
131     error_delegate_->OnError(read_error_);
132   for (Observer& observer : observers_)
133     observer.OnInitializationCompleted(read_success_);
134 }
135 
ReportValueChanged(const std::string & key,uint32_t flags)136 void CefPrefStore::ReportValueChanged(const std::string& key, uint32_t flags) {
137   for (Observer& observer : observers_)
138     observer.OnPrefValueChanged(key);
139 }
140 
SetString(const std::string & key,const std::string & value)141 void CefPrefStore::SetString(const std::string& key, const std::string& value) {
142   SetValue(key, std::make_unique<base::Value>(value), DEFAULT_PREF_WRITE_FLAGS);
143 }
144 
SetInteger(const std::string & key,int value)145 void CefPrefStore::SetInteger(const std::string& key, int value) {
146   SetValue(key, std::make_unique<base::Value>(value), DEFAULT_PREF_WRITE_FLAGS);
147 }
148 
SetBoolean(const std::string & key,bool value)149 void CefPrefStore::SetBoolean(const std::string& key, bool value) {
150   SetValue(key, std::make_unique<base::Value>(value), DEFAULT_PREF_WRITE_FLAGS);
151 }
152 
GetString(const std::string & key,std::string * value) const153 bool CefPrefStore::GetString(const std::string& key, std::string* value) const {
154   const base::Value* stored_value;
155   if (!prefs_.GetValue(key, &stored_value) || !stored_value)
156     return false;
157 
158   if (value && stored_value->is_string()) {
159     *value = stored_value->GetString();
160     return true;
161   }
162   return stored_value->is_string();
163 }
164 
GetInteger(const std::string & key,int * value) const165 bool CefPrefStore::GetInteger(const std::string& key, int* value) const {
166   const base::Value* stored_value;
167   if (!prefs_.GetValue(key, &stored_value) || !stored_value)
168     return false;
169 
170   if (value && stored_value->is_int()) {
171     *value = stored_value->GetInt();
172     return true;
173   }
174   return stored_value->is_int();
175 }
176 
GetBoolean(const std::string & key,bool * value) const177 bool CefPrefStore::GetBoolean(const std::string& key, bool* value) const {
178   const base::Value* stored_value;
179   if (!prefs_.GetValue(key, &stored_value) || !stored_value)
180     return false;
181 
182   if (value && stored_value->is_bool()) {
183     *value = stored_value->GetBool();
184     return true;
185   }
186   return stored_value->is_bool();
187 }
188 
SetBlockAsyncRead(bool block_async_read)189 void CefPrefStore::SetBlockAsyncRead(bool block_async_read) {
190   DCHECK(!init_complete_);
191   block_async_read_ = block_async_read;
192   if (pending_async_read_ && !block_async_read_)
193     NotifyInitializationCompleted();
194 }
195 
set_read_only(bool read_only)196 void CefPrefStore::set_read_only(bool read_only) {
197   read_only_ = read_only;
198 }
199 
set_read_success(bool read_success)200 void CefPrefStore::set_read_success(bool read_success) {
201   DCHECK(!init_complete_);
202   read_success_ = read_success;
203 }
204 
set_read_error(PersistentPrefStore::PrefReadError read_error)205 void CefPrefStore::set_read_error(
206     PersistentPrefStore::PrefReadError read_error) {
207   DCHECK(!init_complete_);
208   read_error_ = read_error;
209 }
210 
~CefPrefStore()211 CefPrefStore::~CefPrefStore() {}
212