chore: refactor persisting permission granted to serial ports (#31181)

This commit is contained in:
John Kleinschmidt 2021-10-06 16:18:00 -04:00 committed by GitHub
parent 21c6b33ebe
commit d6de243837
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 155 additions and 131 deletions

View file

@ -8,8 +8,6 @@
#include <utility>
#include <vector>
#include "base/barrier_closure.h"
#include "base/base_paths.h"
#include "base/command_line.h"
@ -95,9 +93,6 @@ std::string MakePartitionName(const std::string& input) {
} // namespace
const char kSerialGrantedDevicesPref[] =
"profile.content_settings.exceptions.serial-chooser-data";
// static
ElectronBrowserContext::BrowserContextMap&
ElectronBrowserContext::browser_context_map() {
@ -194,7 +189,6 @@ void ElectronBrowserContext::InitPrefs() {
registry->RegisterFilePathPref(prefs::kDownloadDefaultDirectory,
download_dir);
registry->RegisterDictionaryPref(prefs::kDevToolsFileSystemPaths);
registry->RegisterDictionaryPref(kSerialGrantedDevicesPref);
InspectableWebContents::RegisterPrefs(registry.get());
MediaDeviceIDSalt::RegisterPrefs(registry.get());
ZoomLevelDelegate::RegisterPrefs(registry.get());
@ -420,54 +414,6 @@ void ElectronBrowserContext::SetSSLConfigClient(
ssl_config_client_ = std::move(client);
}
void ElectronBrowserContext::GrantObjectPermission(
const url::Origin& origin,
base::Value object,
const std::string& pref_key) {
std::string origin_string = origin.Serialize();
DictionaryPrefUpdate update(prefs(), pref_key);
base::Value* const current_objects = update.Get();
if (!current_objects || !current_objects->is_dict()) {
base::ListValue objects_for_origin;
objects_for_origin.Append(std::move(object));
base::DictionaryValue objects_by_origin;
objects_by_origin.SetPath(origin_string, std::move(objects_for_origin));
prefs()->Set(pref_key, std::move(objects_by_origin));
} else {
base::Value* const objects_mutable =
current_objects->FindListKey(origin_string);
if (objects_mutable) {
base::Value::ListStorage objects = std::move(*objects_mutable).TakeList();
objects.push_back(std::move(object));
*objects_mutable = base::Value(std::move(objects));
} else {
base::Value new_objects(base::Value::Type::LIST);
new_objects.Append(std::move(object));
current_objects->SetKey(origin_string, std::move(new_objects));
}
}
}
std::vector<std::unique_ptr<base::Value>>
ElectronBrowserContext::GetGrantedObjects(const url::Origin& origin,
const std::string& pref_key) {
auto* current_objects = prefs()->Get(pref_key);
if (!current_objects || !current_objects->is_dict()) {
return {};
}
const base::Value* objects_for_origin =
current_objects->FindPath(origin.Serialize());
if (!objects_for_origin)
return {};
std::vector<std::unique_ptr<base::Value>> results;
for (const auto& object : objects_for_origin->GetList())
results.push_back(std::make_unique<base::Value>(object.Clone()));
return results;
}
// static
ElectronBrowserContext* ElectronBrowserContext::From(
const std::string& partition,