zoom_level_delegate.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright 2014 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. #include "shell/browser/zoom_level_delegate.h"
  5. #include <functional>
  6. #include <memory>
  7. #include <vector>
  8. #include "base/bind.h"
  9. #include "base/files/file_path.h"
  10. #include "base/strings/string_number_conversions.h"
  11. #include "base/values.h"
  12. #include "components/prefs/json_pref_store.h"
  13. #include "components/prefs/pref_filter.h"
  14. #include "components/prefs/pref_registry_simple.h"
  15. #include "components/prefs/pref_service_factory.h"
  16. #include "components/prefs/scoped_user_pref_update.h"
  17. #include "content/public/browser/browser_thread.h"
  18. #include "third_party/blink/public/common/page/page_zoom.h"
  19. namespace electron {
  20. namespace {
  21. // Double that indicates the default zoom level.
  22. const char kPartitionDefaultZoomLevel[] = "partition.default_zoom_level";
  23. // Dictionary that maps hostnames to zoom levels. Hosts not in this pref will
  24. // be displayed at the default zoom level.
  25. const char kPartitionPerHostZoomLevels[] = "partition.per_host_zoom_levels";
  26. std::string GetHash(const base::FilePath& partition_path) {
  27. size_t int_key = std::hash<base::FilePath>()(partition_path);
  28. return base::NumberToString(int_key);
  29. }
  30. } // namespace
  31. // static
  32. void ZoomLevelDelegate::RegisterPrefs(PrefRegistrySimple* registry) {
  33. registry->RegisterDictionaryPref(kPartitionDefaultZoomLevel);
  34. registry->RegisterDictionaryPref(kPartitionPerHostZoomLevels);
  35. }
  36. ZoomLevelDelegate::ZoomLevelDelegate(PrefService* pref_service,
  37. const base::FilePath& partition_path)
  38. : pref_service_(pref_service) {
  39. DCHECK(pref_service_);
  40. partition_key_ = GetHash(partition_path);
  41. }
  42. ZoomLevelDelegate::~ZoomLevelDelegate() = default;
  43. void ZoomLevelDelegate::SetDefaultZoomLevelPref(double level) {
  44. if (blink::PageZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel()))
  45. return;
  46. DictionaryPrefUpdate update(pref_service_, kPartitionDefaultZoomLevel);
  47. update->SetDoubleKey(partition_key_, level);
  48. host_zoom_map_->SetDefaultZoomLevel(level);
  49. }
  50. double ZoomLevelDelegate::GetDefaultZoomLevelPref() const {
  51. double default_zoom_level = 0.0;
  52. const base::Value* default_zoom_level_dictionary =
  53. pref_service_->GetDictionary(kPartitionDefaultZoomLevel);
  54. // If no default has been previously set, the default returned is the
  55. // value used to initialize default_zoom_level in this function.
  56. absl::optional<double> maybe_default_zoom_level =
  57. default_zoom_level_dictionary->FindDoubleKey(partition_key_);
  58. if (maybe_default_zoom_level.has_value())
  59. default_zoom_level = maybe_default_zoom_level.value();
  60. return default_zoom_level;
  61. }
  62. void ZoomLevelDelegate::OnZoomLevelChanged(
  63. const content::HostZoomMap::ZoomLevelChange& change) {
  64. if (change.mode != content::HostZoomMap::ZOOM_CHANGED_FOR_HOST)
  65. return;
  66. double level = change.zoom_level;
  67. DictionaryPrefUpdate update(pref_service_, kPartitionPerHostZoomLevels);
  68. base::Value* host_zoom_dictionaries = update.Get();
  69. DCHECK(host_zoom_dictionaries);
  70. bool modification_is_removal =
  71. blink::PageZoomValuesEqual(level, host_zoom_map_->GetDefaultZoomLevel());
  72. base::Value* host_zoom_dictionary =
  73. host_zoom_dictionaries->FindDictKey(partition_key_);
  74. if (!host_zoom_dictionary) {
  75. host_zoom_dictionaries->SetKey(partition_key_,
  76. base::Value(base::Value::Type::DICTIONARY));
  77. host_zoom_dictionary = host_zoom_dictionaries->FindDictKey(partition_key_);
  78. }
  79. if (modification_is_removal)
  80. host_zoom_dictionary->RemoveKey(change.host);
  81. else
  82. host_zoom_dictionary->SetKey(change.host, base::Value(level));
  83. }
  84. void ZoomLevelDelegate::ExtractPerHostZoomLevels(
  85. const base::DictionaryValue* host_zoom_dictionary) {
  86. std::vector<std::string> keys_to_remove;
  87. std::unique_ptr<base::DictionaryValue> host_zoom_dictionary_copy =
  88. host_zoom_dictionary->DeepCopyWithoutEmptyChildren();
  89. for (base::DictionaryValue::Iterator i(*host_zoom_dictionary_copy);
  90. !i.IsAtEnd(); i.Advance()) {
  91. const std::string& host(i.key());
  92. const absl::optional<double> zoom_level = i.value().GetIfDouble();
  93. // Filter out A) the empty host, B) zoom levels equal to the default; and
  94. // remember them, so that we can later erase them from Prefs.
  95. // Values of type B could further have been stored before the default zoom
  96. // level was set to its current value. In either case, SetZoomLevelForHost
  97. // will ignore type B values, thus, to have consistency with HostZoomMap's
  98. // internal state, these values must also be removed from Prefs.
  99. if (host.empty() || !zoom_level ||
  100. blink::PageZoomValuesEqual(*zoom_level,
  101. host_zoom_map_->GetDefaultZoomLevel())) {
  102. keys_to_remove.push_back(host);
  103. continue;
  104. }
  105. host_zoom_map_->SetZoomLevelForHost(host, *zoom_level);
  106. }
  107. // Sanitize prefs to remove entries that match the default zoom level and/or
  108. // have an empty host.
  109. {
  110. DictionaryPrefUpdate update(pref_service_, kPartitionPerHostZoomLevels);
  111. base::Value* host_zoom_dictionaries = update.Get();
  112. base::Value* sanitized_host_zoom_dictionary =
  113. host_zoom_dictionaries->FindDictKey(partition_key_);
  114. if (sanitized_host_zoom_dictionary) {
  115. for (const std::string& s : keys_to_remove)
  116. sanitized_host_zoom_dictionary->RemoveKey(s);
  117. }
  118. }
  119. }
  120. void ZoomLevelDelegate::InitHostZoomMap(content::HostZoomMap* host_zoom_map) {
  121. // This init function must be called only once.
  122. DCHECK(!host_zoom_map_);
  123. DCHECK(host_zoom_map);
  124. host_zoom_map_ = host_zoom_map;
  125. // Initialize the default zoom level.
  126. host_zoom_map_->SetDefaultZoomLevel(GetDefaultZoomLevelPref());
  127. // Initialize the HostZoomMap with per-host zoom levels from the persisted
  128. // zoom-level preference values.
  129. const base::Value* host_zoom_dictionaries =
  130. pref_service_->GetDictionary(kPartitionPerHostZoomLevels);
  131. const base::Value* host_zoom_dictionary =
  132. host_zoom_dictionaries->FindDictKey(partition_key_);
  133. if (host_zoom_dictionary) {
  134. // Since we're calling this before setting up zoom_subscription_ below we
  135. // don't need to worry that host_zoom_dictionary is indirectly affected
  136. // by calls to HostZoomMap::SExtractPerHostZoomLevelsetZoomLevelForHost().
  137. ExtractPerHostZoomLevels(
  138. &base::Value::AsDictionaryValue(*host_zoom_dictionary));
  139. }
  140. zoom_subscription_ =
  141. host_zoom_map_->AddZoomLevelChangedCallback(base::BindRepeating(
  142. &ZoomLevelDelegate::OnZoomLevelChanged, base::Unretained(this)));
  143. }
  144. } // namespace electron