2017-01-30 11:55:46 +00:00
|
|
|
// Copyright 2014 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/zoom_level_delegate.h"
|
2017-01-30 11:55:46 +00:00
|
|
|
|
|
|
|
#include <functional>
|
2018-04-08 19:46:23 +00:00
|
|
|
#include <memory>
|
2022-09-07 07:46:37 +00:00
|
|
|
#include <utility>
|
2017-05-18 22:06:57 +00:00
|
|
|
#include <vector>
|
2017-01-30 11:55:46 +00:00
|
|
|
|
|
|
|
#include "base/bind.h"
|
2021-07-02 00:51:37 +00:00
|
|
|
#include "base/files/file_path.h"
|
2017-01-30 11:55:46 +00:00
|
|
|
#include "base/strings/string_number_conversions.h"
|
|
|
|
#include "components/prefs/json_pref_store.h"
|
|
|
|
#include "components/prefs/pref_filter.h"
|
|
|
|
#include "components/prefs/pref_registry_simple.h"
|
|
|
|
#include "components/prefs/pref_service_factory.h"
|
|
|
|
#include "components/prefs/scoped_user_pref_update.h"
|
|
|
|
#include "content/public/browser/browser_thread.h"
|
2019-10-18 19:57:34 +00:00
|
|
|
#include "third_party/blink/public/common/page/page_zoom.h"
|
2017-01-30 11:55:46 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2017-01-30 11:55:46 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Double that indicates the default zoom level.
|
|
|
|
const char kPartitionDefaultZoomLevel[] = "partition.default_zoom_level";
|
|
|
|
|
|
|
|
// Dictionary that maps hostnames to zoom levels. Hosts not in this pref will
|
|
|
|
// be displayed at the default zoom level.
|
|
|
|
const char kPartitionPerHostZoomLevels[] = "partition.per_host_zoom_levels";
|
|
|
|
|
|
|
|
std::string GetHash(const base::FilePath& partition_path) {
|
|
|
|
size_t int_key = std::hash<base::FilePath>()(partition_path);
|
2018-04-08 19:46:23 +00:00
|
|
|
return base::NumberToString(int_key);
|
2017-01-30 11:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// static
|
|
|
|
void ZoomLevelDelegate::RegisterPrefs(PrefRegistrySimple* registry) {
|
|
|
|
registry->RegisterDictionaryPref(kPartitionDefaultZoomLevel);
|
|
|
|
registry->RegisterDictionaryPref(kPartitionPerHostZoomLevels);
|
|
|
|
}
|
|
|
|
|
|
|
|
ZoomLevelDelegate::ZoomLevelDelegate(PrefService* pref_service,
|
|
|
|
const base::FilePath& partition_path)
|
2021-01-26 18:16:21 +00:00
|
|
|
: pref_service_(pref_service) {
|
2017-01-30 11:55:46 +00:00
|
|
|
DCHECK(pref_service_);
|
|
|
|
partition_key_ = GetHash(partition_path);
|
|
|
|
}
|
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
ZoomLevelDelegate::~ZoomLevelDelegate() = default;
|
2017-01-30 11:55:46 +00:00
|
|
|
|
|
|
|
void ZoomLevelDelegate::SetDefaultZoomLevelPref(double level) {
|
2019-10-18 19:57:34 +00:00
|
|
|
if (blink::PageZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel()))
|
2017-01-30 11:55:46 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
DictionaryPrefUpdate update(pref_service_, kPartitionDefaultZoomLevel);
|
2022-02-10 02:58:52 +00:00
|
|
|
update->SetDoubleKey(partition_key_, level);
|
2017-01-30 11:55:46 +00:00
|
|
|
host_zoom_map_->SetDefaultZoomLevel(level);
|
|
|
|
}
|
|
|
|
|
|
|
|
double ZoomLevelDelegate::GetDefaultZoomLevelPref() const {
|
2022-09-07 07:46:37 +00:00
|
|
|
const base::Value::Dict& default_zoom_level_dictionary =
|
|
|
|
pref_service_->GetValueDict(kPartitionDefaultZoomLevel);
|
2017-01-30 11:55:46 +00:00
|
|
|
// If no default has been previously set, the default returned is the
|
|
|
|
// value used to initialize default_zoom_level in this function.
|
2022-09-07 07:46:37 +00:00
|
|
|
return default_zoom_level_dictionary.FindDouble(partition_key_).value_or(0.0);
|
2017-01-30 11:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ZoomLevelDelegate::OnZoomLevelChanged(
|
|
|
|
const content::HostZoomMap::ZoomLevelChange& change) {
|
|
|
|
if (change.mode != content::HostZoomMap::ZOOM_CHANGED_FOR_HOST)
|
|
|
|
return;
|
|
|
|
|
|
|
|
double level = change.zoom_level;
|
|
|
|
DictionaryPrefUpdate update(pref_service_, kPartitionPerHostZoomLevels);
|
2022-09-07 07:46:37 +00:00
|
|
|
base::Value* host_zoom_update = update.Get();
|
|
|
|
DCHECK(host_zoom_update);
|
|
|
|
base::Value::Dict& host_zoom_dictionaries = host_zoom_update->GetDict();
|
2017-01-30 11:55:46 +00:00
|
|
|
|
|
|
|
bool modification_is_removal =
|
2019-10-18 19:57:34 +00:00
|
|
|
blink::PageZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel());
|
2017-01-30 11:55:46 +00:00
|
|
|
|
2022-09-07 07:46:37 +00:00
|
|
|
base::Value::Dict* host_zoom_dictionary =
|
|
|
|
host_zoom_dictionaries.FindDict(partition_key_);
|
2022-02-10 02:58:52 +00:00
|
|
|
if (!host_zoom_dictionary) {
|
2022-09-07 07:46:37 +00:00
|
|
|
base::Value::Dict dict;
|
|
|
|
host_zoom_dictionaries.Set(partition_key_, std::move(dict));
|
|
|
|
host_zoom_dictionary = host_zoom_dictionaries.FindDict(partition_key_);
|
2017-01-30 11:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (modification_is_removal)
|
2022-09-07 07:46:37 +00:00
|
|
|
host_zoom_dictionary->Remove(change.host);
|
2017-01-30 11:55:46 +00:00
|
|
|
else
|
2022-09-07 07:46:37 +00:00
|
|
|
host_zoom_dictionary->Set(change.host, base::Value(level));
|
2017-01-30 11:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ZoomLevelDelegate::ExtractPerHostZoomLevels(
|
2022-09-07 07:46:37 +00:00
|
|
|
const base::Value::Dict& host_zoom_dictionary) {
|
2017-01-30 11:55:46 +00:00
|
|
|
std::vector<std::string> keys_to_remove;
|
2022-09-07 07:46:37 +00:00
|
|
|
base::Value::Dict host_zoom_dictionary_copy = host_zoom_dictionary.Clone();
|
|
|
|
for (auto [host, value] : host_zoom_dictionary_copy) {
|
|
|
|
const absl::optional<double> zoom_level = value.GetIfDouble();
|
2017-01-30 11:55:46 +00:00
|
|
|
|
|
|
|
// Filter out A) the empty host, B) zoom levels equal to the default; and
|
|
|
|
// remember them, so that we can later erase them from Prefs.
|
|
|
|
// Values of type B could further have been stored before the default zoom
|
|
|
|
// level was set to its current value. In either case, SetZoomLevelForHost
|
|
|
|
// will ignore type B values, thus, to have consistency with HostZoomMap's
|
|
|
|
// internal state, these values must also be removed from Prefs.
|
2022-09-07 07:46:37 +00:00
|
|
|
if (host.empty() || !zoom_level.has_value() ||
|
|
|
|
blink::PageZoomValuesEqual(zoom_level.value(),
|
2019-10-18 19:57:34 +00:00
|
|
|
host_zoom_map_->GetDefaultZoomLevel())) {
|
2017-01-30 11:55:46 +00:00
|
|
|
keys_to_remove.push_back(host);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-07 07:46:37 +00:00
|
|
|
host_zoom_map_->SetZoomLevelForHost(host, zoom_level.value());
|
2017-01-30 11:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sanitize prefs to remove entries that match the default zoom level and/or
|
|
|
|
// have an empty host.
|
|
|
|
{
|
|
|
|
DictionaryPrefUpdate update(pref_service_, kPartitionPerHostZoomLevels);
|
2022-02-10 02:58:52 +00:00
|
|
|
base::Value* host_zoom_dictionaries = update.Get();
|
|
|
|
base::Value* sanitized_host_zoom_dictionary =
|
|
|
|
host_zoom_dictionaries->FindDictKey(partition_key_);
|
|
|
|
if (sanitized_host_zoom_dictionary) {
|
|
|
|
for (const std::string& s : keys_to_remove)
|
|
|
|
sanitized_host_zoom_dictionary->RemoveKey(s);
|
|
|
|
}
|
2017-01-30 11:55:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZoomLevelDelegate::InitHostZoomMap(content::HostZoomMap* host_zoom_map) {
|
|
|
|
// This init function must be called only once.
|
|
|
|
DCHECK(!host_zoom_map_);
|
|
|
|
DCHECK(host_zoom_map);
|
|
|
|
host_zoom_map_ = host_zoom_map;
|
|
|
|
|
|
|
|
// Initialize the default zoom level.
|
|
|
|
host_zoom_map_->SetDefaultZoomLevel(GetDefaultZoomLevelPref());
|
|
|
|
|
|
|
|
// Initialize the HostZoomMap with per-host zoom levels from the persisted
|
|
|
|
// zoom-level preference values.
|
2022-09-07 07:46:37 +00:00
|
|
|
const base::Value::Dict& host_zoom_dictionaries =
|
|
|
|
pref_service_->GetValueDict(kPartitionPerHostZoomLevels);
|
|
|
|
const base::Value::Dict* host_zoom_dictionary =
|
|
|
|
host_zoom_dictionaries.FindDict(partition_key_);
|
2022-02-10 02:58:52 +00:00
|
|
|
if (host_zoom_dictionary) {
|
2017-01-30 11:55:46 +00:00
|
|
|
// Since we're calling this before setting up zoom_subscription_ below we
|
|
|
|
// don't need to worry that host_zoom_dictionary is indirectly affected
|
2022-02-10 02:58:52 +00:00
|
|
|
// by calls to HostZoomMap::SExtractPerHostZoomLevelsetZoomLevelForHost().
|
2022-09-07 07:46:37 +00:00
|
|
|
ExtractPerHostZoomLevels(*host_zoom_dictionary);
|
2017-01-30 11:55:46 +00:00
|
|
|
}
|
2019-05-03 19:08:41 +00:00
|
|
|
zoom_subscription_ =
|
|
|
|
host_zoom_map_->AddZoomLevelChangedCallback(base::BindRepeating(
|
|
|
|
&ZoomLevelDelegate::OnZoomLevelChanged, base::Unretained(this)));
|
2017-01-30 11:55:46 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|