• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium Embedded Framework Authors.
2 // Portions copyright 2016 The Chromium Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 
6 #include "libcef/browser/extensions/value_store/cef_value_store_factory.h"
7 
8 #include "base/memory/ptr_util.h"
9 #include "extensions/browser/value_store/leveldb_value_store.h"
10 #include "libcef/browser/extensions/value_store/cef_value_store.h"
11 
12 namespace {
13 
14 const char kUMAClientName[] = "Cef";
15 
16 }  // namespace
17 
18 namespace extensions {
19 
20 using SettingsNamespace = settings_namespace::Namespace;
21 
22 CefValueStoreFactory::StorageHelper::StorageHelper() = default;
23 
24 CefValueStoreFactory::StorageHelper::~StorageHelper() = default;
25 
GetKnownExtensionIDs(ModelType model_type) const26 std::set<ExtensionId> CefValueStoreFactory::StorageHelper::GetKnownExtensionIDs(
27     ModelType model_type) const {
28   std::set<ExtensionId> ids;
29   switch (model_type) {
30     case ValueStoreFactory::ModelType::APP:
31       for (const auto& key : app_stores_)
32         ids.insert(key.first);
33       break;
34     case ValueStoreFactory::ModelType::EXTENSION:
35       for (const auto& key : extension_stores_)
36         ids.insert(key.first);
37       break;
38   }
39   return ids;
40 }
41 
Reset()42 void CefValueStoreFactory::StorageHelper::Reset() {
43   app_stores_.clear();
44   extension_stores_.clear();
45 }
46 
AddValueStore(const ExtensionId & extension_id,ValueStore * value_store,ModelType model_type)47 ValueStore* CefValueStoreFactory::StorageHelper::AddValueStore(
48     const ExtensionId& extension_id,
49     ValueStore* value_store,
50     ModelType model_type) {
51   if (model_type == ValueStoreFactory::ModelType::APP) {
52     DCHECK(app_stores_.find(extension_id) == app_stores_.end());
53     app_stores_[extension_id] = value_store;
54   } else {
55     DCHECK(extension_stores_.find(extension_id) == extension_stores_.end());
56     extension_stores_[extension_id] = value_store;
57   }
58   return value_store;
59 }
60 
DeleteSettings(const ExtensionId & extension_id,ModelType model_type)61 void CefValueStoreFactory::StorageHelper::DeleteSettings(
62     const ExtensionId& extension_id,
63     ModelType model_type) {
64   switch (model_type) {
65     case ValueStoreFactory::ModelType::APP:
66       app_stores_.erase(extension_id);
67       break;
68     case ValueStoreFactory::ModelType::EXTENSION:
69       extension_stores_.erase(extension_id);
70       break;
71   }
72 }
73 
HasSettings(const ExtensionId & extension_id,ModelType model_type) const74 bool CefValueStoreFactory::StorageHelper::HasSettings(
75     const ExtensionId& extension_id,
76     ModelType model_type) const {
77   switch (model_type) {
78     case ValueStoreFactory::ModelType::APP:
79       return app_stores_.find(extension_id) != app_stores_.end();
80     case ValueStoreFactory::ModelType::EXTENSION:
81       return extension_stores_.find(extension_id) != extension_stores_.end();
82   }
83   NOTREACHED();
84   return false;
85 }
86 
GetExisting(const ExtensionId & extension_id) const87 ValueStore* CefValueStoreFactory::StorageHelper::GetExisting(
88     const ExtensionId& extension_id) const {
89   auto it = app_stores_.find(extension_id);
90   if (it != app_stores_.end())
91     return it->second;
92   it = extension_stores_.find(extension_id);
93   if (it != extension_stores_.end())
94     return it->second;
95   return nullptr;
96 }
97 
98 CefValueStoreFactory::CefValueStoreFactory() = default;
99 
CefValueStoreFactory(const base::FilePath & db_path)100 CefValueStoreFactory::CefValueStoreFactory(const base::FilePath& db_path)
101     : db_path_(db_path) {}
102 
~CefValueStoreFactory()103 CefValueStoreFactory::~CefValueStoreFactory() {}
104 
CreateRulesStore()105 std::unique_ptr<ValueStore> CefValueStoreFactory::CreateRulesStore() {
106   if (db_path_.empty())
107     last_created_store_ = new CefValueStore();
108   else
109     last_created_store_ = new LeveldbValueStore(kUMAClientName, db_path_);
110   return base::WrapUnique(last_created_store_);
111 }
112 
CreateStateStore()113 std::unique_ptr<ValueStore> CefValueStoreFactory::CreateStateStore() {
114   return CreateRulesStore();
115 }
116 
GetStorageHelper(SettingsNamespace settings_namespace)117 CefValueStoreFactory::StorageHelper& CefValueStoreFactory::GetStorageHelper(
118     SettingsNamespace settings_namespace) {
119   switch (settings_namespace) {
120     case settings_namespace::LOCAL:
121       return local_helper_;
122     case settings_namespace::SYNC:
123       return sync_helper_;
124     case settings_namespace::MANAGED:
125       return managed_helper_;
126     case settings_namespace::INVALID:
127       break;
128   }
129   NOTREACHED();
130   return local_helper_;
131 }
132 
CreateSettingsStore(SettingsNamespace settings_namespace,ModelType model_type,const ExtensionId & extension_id)133 std::unique_ptr<ValueStore> CefValueStoreFactory::CreateSettingsStore(
134     SettingsNamespace settings_namespace,
135     ModelType model_type,
136     const ExtensionId& extension_id) {
137   std::unique_ptr<ValueStore> settings_store(CreateRulesStore());
138   // Note: This factory is purposely keeping the raw pointers to each ValueStore
139   //       created. Tests using CefValueStoreFactory must be careful to keep
140   //       those ValueStore's alive for the duration of their test.
141   GetStorageHelper(settings_namespace)
142       .AddValueStore(extension_id, settings_store.get(), model_type);
143   return settings_store;
144 }
145 
LastCreatedStore() const146 ValueStore* CefValueStoreFactory::LastCreatedStore() const {
147   return last_created_store_;
148 }
149 
DeleteSettings(SettingsNamespace settings_namespace,ModelType model_type,const ExtensionId & extension_id)150 void CefValueStoreFactory::DeleteSettings(SettingsNamespace settings_namespace,
151                                           ModelType model_type,
152                                           const ExtensionId& extension_id) {
153   GetStorageHelper(settings_namespace).DeleteSettings(extension_id, model_type);
154 }
155 
HasSettings(SettingsNamespace settings_namespace,ModelType model_type,const ExtensionId & extension_id)156 bool CefValueStoreFactory::HasSettings(SettingsNamespace settings_namespace,
157                                        ModelType model_type,
158                                        const ExtensionId& extension_id) {
159   return GetStorageHelper(settings_namespace)
160       .HasSettings(extension_id, model_type);
161 }
162 
GetKnownExtensionIDs(SettingsNamespace settings_namespace,ModelType model_type) const163 std::set<ExtensionId> CefValueStoreFactory::GetKnownExtensionIDs(
164     SettingsNamespace settings_namespace,
165     ModelType model_type) const {
166   return const_cast<CefValueStoreFactory*>(this)
167       ->GetStorageHelper(settings_namespace)
168       .GetKnownExtensionIDs(model_type);
169 }
170 
GetExisting(const ExtensionId & extension_id) const171 ValueStore* CefValueStoreFactory::GetExisting(
172     const ExtensionId& extension_id) const {
173   ValueStore* existing_store = local_helper_.GetExisting(extension_id);
174   if (existing_store)
175     return existing_store;
176   existing_store = sync_helper_.GetExisting(extension_id);
177   if (existing_store)
178     return existing_store;
179   existing_store = managed_helper_.GetExisting(extension_id);
180   DCHECK(existing_store != nullptr);
181   return existing_store;
182 }
183 
Reset()184 void CefValueStoreFactory::Reset() {
185   last_created_store_ = nullptr;
186   local_helper_.Reset();
187   sync_helper_.Reset();
188   managed_helper_.Reset();
189 }
190 
191 }  // namespace extensions
192