zoom_level_delegate.cc 6.5 KB

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