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>
|
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 "base/values.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);
|
|
|
|
update->SetDouble(partition_key_, level);
|
|
|
|
host_zoom_map_->SetDefaultZoomLevel(level);
|
|
|
|
}
|
|
|
|
|
|
|
|
double ZoomLevelDelegate::GetDefaultZoomLevelPref() const {
|
|
|
|
double default_zoom_level = 0.0;
|
|
|
|
|
|
|
|
const base::DictionaryValue* default_zoom_level_dictionary =
|
|
|
|
pref_service_->GetDictionary(kPartitionDefaultZoomLevel);
|
|
|
|
// If no default has been previously set, the default returned is the
|
|
|
|
// value used to initialize default_zoom_level in this function.
|
|
|
|
default_zoom_level_dictionary->GetDouble(partition_key_, &default_zoom_level);
|
|
|
|
return default_zoom_level;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
base::DictionaryValue* host_zoom_dictionaries = update.Get();
|
|
|
|
DCHECK(host_zoom_dictionaries);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
base::DictionaryValue* host_zoom_dictionary = nullptr;
|
|
|
|
if (!host_zoom_dictionaries->GetDictionary(partition_key_,
|
|
|
|
&host_zoom_dictionary)) {
|
2017-08-12 17:27:48 +00:00
|
|
|
host_zoom_dictionary = host_zoom_dictionaries->SetDictionary(
|
2018-04-12 12:48:32 +00:00
|
|
|
partition_key_, std::make_unique<base::DictionaryValue>());
|
2017-01-30 11:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (modification_is_removal)
|
2021-08-24 00:52:17 +00:00
|
|
|
host_zoom_dictionary->RemoveKey(change.host);
|
2017-01-30 11:55:46 +00:00
|
|
|
else
|
2017-11-27 06:45:01 +00:00
|
|
|
host_zoom_dictionary->SetKey(change.host, base::Value(level));
|
2017-01-30 11:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ZoomLevelDelegate::ExtractPerHostZoomLevels(
|
|
|
|
const base::DictionaryValue* host_zoom_dictionary) {
|
|
|
|
std::vector<std::string> keys_to_remove;
|
|
|
|
std::unique_ptr<base::DictionaryValue> host_zoom_dictionary_copy =
|
|
|
|
host_zoom_dictionary->DeepCopyWithoutEmptyChildren();
|
|
|
|
for (base::DictionaryValue::Iterator i(*host_zoom_dictionary_copy);
|
|
|
|
!i.IsAtEnd(); i.Advance()) {
|
|
|
|
const std::string& host(i.key());
|
2021-08-24 00:52:17 +00:00
|
|
|
const absl::optional<double> zoom_level = i.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.
|
2021-08-24 00:52:17 +00:00
|
|
|
if (host.empty() || !zoom_level ||
|
|
|
|
blink::PageZoomValuesEqual(*zoom_level,
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-24 00:52:17 +00:00
|
|
|
host_zoom_map_->SetZoomLevelForHost(host, *zoom_level);
|
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);
|
|
|
|
base::DictionaryValue* host_zoom_dictionaries = update.Get();
|
|
|
|
base::DictionaryValue* sanitized_host_zoom_dictionary = nullptr;
|
|
|
|
host_zoom_dictionaries->GetDictionary(partition_key_,
|
|
|
|
&sanitized_host_zoom_dictionary);
|
|
|
|
for (const std::string& s : keys_to_remove)
|
2021-08-24 00:52:17 +00:00
|
|
|
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.
|
|
|
|
const base::DictionaryValue* host_zoom_dictionaries =
|
|
|
|
pref_service_->GetDictionary(kPartitionPerHostZoomLevels);
|
|
|
|
const base::DictionaryValue* host_zoom_dictionary = nullptr;
|
|
|
|
if (host_zoom_dictionaries->GetDictionary(partition_key_,
|
|
|
|
&host_zoom_dictionary)) {
|
|
|
|
// Since we're calling this before setting up zoom_subscription_ below we
|
|
|
|
// don't need to worry that host_zoom_dictionary is indirectly affected
|
|
|
|
// by calls to HostZoomMap::SetZoomLevelForHost().
|
|
|
|
ExtractPerHostZoomLevels(host_zoom_dictionary);
|
|
|
|
}
|
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
|