zoom_level_delegate.cc 6.5 KB

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