Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 5 | #include "content/browser/host_zoom_map_impl.h" |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 6 | |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | b9e7c479f | 2013-04-12 04:33:24 | [diff] [blame] | 8 | #include <cmath> |
Lukasz Anforowicz | c773bbd | 2017-10-02 19:25:29 | [diff] [blame] | 9 | #include <memory> |
| 10 | #include <utility> |
[email protected] | b9e7c479f | 2013-04-12 04:33:24 | [diff] [blame] | 11 | |
Lei Zhang | d4f2c7ad | 2021-05-13 20:10:12 | [diff] [blame] | 12 | #include "base/containers/contains.h" |
[email protected] | 74ebfb1 | 2013-06-07 20:48:00 | [diff] [blame] | 13 | #include "base/strings/utf_string_conversions.h" |
Christian Dullweber | cc736c1 | 2017-09-04 09:27:50 | [diff] [blame] | 14 | #include "base/time/default_clock.h" |
[email protected] | d407cc0 | 2011-09-13 16:01:46 | [diff] [blame] | 15 | #include "base/values.h" |
Mark Schillaci | 1363e4a | 2021-10-04 19:10:09 | [diff] [blame] | 16 | #include "build/build_config.h" |
danakj | e94b7c84 | 2020-09-16 18:47:43 | [diff] [blame] | 17 | #include "content/browser/renderer_host/navigation_entry_impl.h" |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 18 | #include "content/browser/renderer_host/render_frame_host_impl.h" |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 19 | #include "content/browser/renderer_host/render_process_host_impl.h" |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 20 | #include "content/browser/web_contents/web_contents_impl.h" |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 21 | #include "content/public/browser/browser_context.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 22 | #include "content/public/browser/browser_thread.h" |
W. James MacLean | f562b92 | 2025-03-13 15:36:19 | [diff] [blame] | 23 | #include "content/public/browser/content_browser_client.h" |
Mark Schillaci | cddfa17 | 2022-07-28 19:57:13 | [diff] [blame] | 24 | #include "content/public/browser/host_zoom_map.h" |
Lukasz Anforowicz | c773bbd | 2017-10-02 19:25:29 | [diff] [blame] | 25 | #include "content/public/browser/render_frame_host.h" |
[email protected] | 5fe3713a | 2012-02-22 08:31:56 | [diff] [blame] | 26 | #include "content/public/browser/resource_context.h" |
wjmaclean | caa7d6d | 2014-11-12 16:42:11 | [diff] [blame] | 27 | #include "content/public/browser/site_instance.h" |
| 28 | #include "content/public/browser/storage_partition.h" |
W. James MacLean | f562b92 | 2025-03-13 15:36:19 | [diff] [blame] | 29 | #include "content/public/common/content_client.h" |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame] | 30 | #include "content/public/common/url_constants.h" |
tfarina | 7a4a7fd | 2016-01-20 14:23:44 | [diff] [blame] | 31 | #include "net/base/url_util.h" |
danakj | 938b37a6 | 2019-09-24 18:35:54 | [diff] [blame] | 32 | #include "third_party/blink/public/common/page/page_zoom.h" |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 33 | |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 34 | #if BUILDFLAG(IS_ANDROID) |
Andrew Grieve | f46caa51 | 2024-05-23 16:59:36 | [diff] [blame] | 35 | #include "base/android/jni_string.h" |
Mark Schillaci | c28765d | 2022-06-03 21:38:44 | [diff] [blame] | 36 | #include "content/public/browser/android/browser_context_handle.h" |
Andrew Grieve | f46caa51 | 2024-05-23 16:59:36 | [diff] [blame] | 37 | |
| 38 | // Must come after all headers that specialize FromJniType() / ToJniType(). |
| 39 | #include "content/public/android/content_jni_headers/HostZoomMapImpl_jni.h" |
Mark Schillaci | 1363e4a | 2021-10-04 19:10:09 | [diff] [blame] | 40 | #endif |
| 41 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 42 | namespace content { |
| 43 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 44 | namespace { |
| 45 | |
W. James MacLean | 817a05e | 2025-02-18 15:51:15 | [diff] [blame] | 46 | GURL GetURLForRenderFrameHostPtr(const RenderFrameHost* rfh) { |
| 47 | if (!rfh) { |
| 48 | return GURL(); |
| 49 | } |
| 50 | |
| 51 | // If a user lands on an error page, and then modifies the zoom level, it |
| 52 | // should be attributed to the error-page host and not the page they were |
| 53 | // trying to reach. |
| 54 | return rfh->IsErrorDocument() ? GURL(kUnreachableWebDataURL) |
| 55 | : rfh->GetLastCommittedURL(); |
| 56 | } |
| 57 | |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 58 | std::string GetHostFromProcessFrame(RenderFrameHostImpl* rfh) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 59 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 60 | if (!rfh) |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 61 | return std::string(); |
| 62 | |
W. James MacLean | 817a05e | 2025-02-18 15:51:15 | [diff] [blame] | 63 | const GURL url = GetURLForRenderFrameHostPtr(rfh); |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 64 | |
W. James MacLean | 817a05e | 2025-02-18 15:51:15 | [diff] [blame] | 65 | return net::GetHostOrSpecFromURL(url); |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 66 | } |
| 67 | |
W. James MacLean | f562b92 | 2025-03-13 15:36:19 | [diff] [blame] | 68 | // Allows HostZoomMap to grant independent zoom to subframes. |
| 69 | BASE_FEATURE(kSubframeZoom, "SubframeZoom", base::FEATURE_ENABLED_BY_DEFAULT); |
| 70 | |
| 71 | // Returns true if local root subframes may have different zoom levels than |
| 72 | // the primary main frame. |
| 73 | bool IsIndependentSubframeZoomEnabled() { |
| 74 | // kSubframeZoom acts as a killswitch here. It is enabled by default, but |
| 75 | // only return true here if some feature that requires subframe zoom is also |
| 76 | // enabled. |
| 77 | return base::FeatureList::IsEnabled(kSubframeZoom) && |
| 78 | (base::FeatureList::IsEnabled(features::kGuestViewMPArch) || |
| 79 | GetContentClient()->browser()->ShouldEnableSubframeZoom()); |
| 80 | } |
| 81 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 82 | } // namespace |
| 83 | |
W. James MacLean | 817a05e | 2025-02-18 15:51:15 | [diff] [blame] | 84 | // static |
| 85 | GURL HostZoomMap::GetURLForRenderFrameHost(GlobalRenderFrameHostId rfh_id) { |
| 86 | return GetURLForRenderFrameHostPtr(RenderFrameHost::FromID(rfh_id)); |
| 87 | } |
| 88 | |
| 89 | // static |
| 90 | GURL HostZoomMap::GetURLForWebContents(WebContents* web_contents) { |
| 91 | return GetURLForRenderFrameHostPtr(web_contents->GetPrimaryMainFrame()); |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame] | 92 | } |
| 93 | |
Robbie McElrath | 3e9db50 | 2023-05-08 23:02:45 | [diff] [blame] | 94 | // static |
wjmaclean | f9b6ec8 | 2014-08-27 14:33:24 | [diff] [blame] | 95 | HostZoomMap* HostZoomMap::GetDefaultForBrowserContext(BrowserContext* context) { |
Robbie McElrath | 3e9db50 | 2023-05-08 23:02:45 | [diff] [blame] | 96 | return GetForStoragePartition(context->GetDefaultStoragePartition()); |
wjmaclean | caa7d6d | 2014-11-12 16:42:11 | [diff] [blame] | 97 | } |
| 98 | |
Robbie McElrath | 3e9db50 | 2023-05-08 23:02:45 | [diff] [blame] | 99 | // static |
wjmaclean | caa7d6d | 2014-11-12 16:42:11 | [diff] [blame] | 100 | HostZoomMap* HostZoomMap::Get(SiteInstance* instance) { |
Robbie McElrath | 3e9db50 | 2023-05-08 23:02:45 | [diff] [blame] | 101 | return GetForStoragePartition( |
| 102 | instance->GetBrowserContext()->GetStoragePartition(instance)); |
wjmaclean | caa7d6d | 2014-11-12 16:42:11 | [diff] [blame] | 103 | } |
| 104 | |
Robbie McElrath | 3e9db50 | 2023-05-08 23:02:45 | [diff] [blame] | 105 | // static |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 106 | HostZoomMap* HostZoomMap::GetForWebContents(WebContents* contents) { |
mcnee | 432e47d | 2015-11-09 19:37:46 | [diff] [blame] | 107 | // TODO(wjmaclean): Update this behaviour to work with OOPIF. |
| 108 | // See crbug.com/528407. |
Robbie McElrath | 3e9db50 | 2023-05-08 23:02:45 | [diff] [blame] | 109 | return Get(contents->GetSiteInstance()); |
| 110 | } |
| 111 | |
| 112 | // static |
| 113 | HostZoomMap* HostZoomMap::GetForStoragePartition( |
| 114 | StoragePartition* storage_partition) { |
| 115 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 116 | CHECK(storage_partition); |
| 117 | return storage_partition->GetHostZoomMap(); |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 120 | // Helper function for setting/getting zoom levels for WebContents without |
| 121 | // having to import HostZoomMapImpl everywhere. |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 122 | |
| 123 | // static |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 124 | double HostZoomMap::GetZoomLevel(WebContents* web_contents) { |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 125 | return GetZoomLevel(web_contents, |
| 126 | web_contents->GetPrimaryMainFrame()->GetGlobalId()); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 127 | } |
| 128 | |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 129 | // static |
| 130 | double HostZoomMap::GetZoomLevel(WebContents* web_contents, |
| 131 | GlobalRenderFrameHostId rfh_id) { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 132 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
wjmaclean | caa7d6d | 2014-11-12 16:42:11 | [diff] [blame] | 133 | HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 134 | HostZoomMap::Get(RenderFrameHost::FromID(rfh_id)->GetSiteInstance())); |
| 135 | return host_zoom_map->GetZoomLevelForWebContents( |
| 136 | static_cast<WebContentsImpl*>(web_contents), rfh_id); |
| 137 | } |
| 138 | |
| 139 | // static |
| 140 | void HostZoomMap::SetZoomLevel(WebContents* web_contents, double level) { |
| 141 | HostZoomMap::SetZoomLevel( |
| 142 | web_contents, web_contents->GetPrimaryMainFrame()->GetGlobalId(), level); |
| 143 | } |
| 144 | |
| 145 | // static |
| 146 | void HostZoomMap::SetZoomLevel(WebContents* web_contents, |
| 147 | GlobalRenderFrameHostId rfh_id, |
| 148 | double level) { |
| 149 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 150 | HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( |
| 151 | HostZoomMap::Get(RenderFrameHost::FromID(rfh_id)->GetSiteInstance())); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 152 | host_zoom_map->SetZoomLevelForWebContents( |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 153 | static_cast<WebContentsImpl*>(web_contents), rfh_id, level); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 154 | } |
| 155 | |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 156 | void HostZoomMap::SendErrorPageZoomLevelRefresh(WebContents* web_contents) { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 157 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame] | 158 | HostZoomMapImpl* host_zoom_map = |
| 159 | static_cast<HostZoomMapImpl*>(HostZoomMap::GetDefaultForBrowserContext( |
| 160 | web_contents->GetBrowserContext())); |
| 161 | host_zoom_map->SendErrorPageZoomLevelRefresh(); |
| 162 | } |
| 163 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 164 | HostZoomMapImpl::HostZoomMapImpl() |
Christian Dullweber | cc736c1 | 2017-09-04 09:27:50 | [diff] [blame] | 165 | : default_zoom_level_(0.0), |
tzik | 67025f67 | 2017-11-29 05:04:44 | [diff] [blame] | 166 | clock_(base::DefaultClock::GetInstance()) { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 167 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 168 | } |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 169 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 170 | void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 171 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 172 | HostZoomMapImpl* copy = static_cast<HostZoomMapImpl*>(copy_interface); |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 173 | host_zoom_levels_.insert(copy->host_zoom_levels_.begin(), |
| 174 | copy->host_zoom_levels_.end()); |
| 175 | for (const auto& it : copy->scheme_host_zoom_levels_) { |
| 176 | const std::string& host = it.first; |
| 177 | scheme_host_zoom_levels_[host] = HostZoomLevels(); |
| 178 | scheme_host_zoom_levels_[host].insert(it.second.begin(), it.second.end()); |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 179 | } |
[email protected] | 19d2669 | 2013-01-12 19:56:33 | [diff] [blame] | 180 | default_zoom_level_ = copy->default_zoom_level_; |
keno | d3fe1c22 | 2023-12-13 19:58:50 | [diff] [blame] | 181 | |
| 182 | host_zoom_levels_for_preview_.insert( |
| 183 | copy->host_zoom_levels_for_preview_.begin(), |
| 184 | copy->host_zoom_levels_for_preview_.end()); |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 185 | } |
| 186 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 187 | double HostZoomMapImpl::GetZoomLevelForHost(const std::string& host) const { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 188 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 189 | const auto it = host_zoom_levels_.find(host); |
| 190 | return it != host_zoom_levels_.end() ? it->second.level : default_zoom_level_; |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 191 | } |
| 192 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 193 | bool HostZoomMapImpl::HasZoomLevel(const std::string& scheme, |
Lucas Furukawa Gadani | 4b4eed0 | 2019-06-04 23:12:04 | [diff] [blame] | 194 | const std::string& host) { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 195 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
jdoerrie | 55ec69d | 2018-10-08 13:34:46 | [diff] [blame] | 196 | auto scheme_iterator(scheme_host_zoom_levels_.find(scheme)); |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 197 | |
| 198 | const HostZoomLevels& zoom_levels = |
| 199 | (scheme_iterator != scheme_host_zoom_levels_.end()) |
| 200 | ? scheme_iterator->second |
| 201 | : host_zoom_levels_; |
| 202 | |
Jan Wilken Dörrie | 77c581a | 2019-06-07 16:25:06 | [diff] [blame] | 203 | return base::Contains(zoom_levels, host); |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 204 | } |
| 205 | |
Lucas Furukawa Gadani | 4b4eed0 | 2019-06-04 23:12:04 | [diff] [blame] | 206 | double HostZoomMapImpl::GetZoomLevelForHostAndScheme(const std::string& scheme, |
| 207 | const std::string& host) { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 208 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
jdoerrie | 55ec69d | 2018-10-08 13:34:46 | [diff] [blame] | 209 | auto scheme_iterator(scheme_host_zoom_levels_.find(scheme)); |
wjmaclean | c6249049 | 2015-02-13 22:02:07 | [diff] [blame] | 210 | if (scheme_iterator != scheme_host_zoom_levels_.end()) { |
jdoerrie | 55ec69d | 2018-10-08 13:34:46 | [diff] [blame] | 211 | auto i(scheme_iterator->second.find(host)); |
wjmaclean | c6249049 | 2015-02-13 22:02:07 | [diff] [blame] | 212 | if (i != scheme_iterator->second.end()) |
Christian Dullweber | cc736c1 | 2017-09-04 09:27:50 | [diff] [blame] | 213 | return i->second.level; |
wjmaclean | c6249049 | 2015-02-13 22:02:07 | [diff] [blame] | 214 | } |
| 215 | |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 216 | return GetZoomLevelForHost(host); |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 217 | } |
| 218 | |
Mark Schillaci | 6fd675e | 2023-03-16 00:02:35 | [diff] [blame] | 219 | #if BUILDFLAG(IS_ANDROID) |
Aishwarya Rajesh | b56269c2 | 2024-01-25 18:34:08 | [diff] [blame] | 220 | double HostZoomMapImpl::GetZoomLevelForHostAndSchemeAndroid( |
Mark Schillaci | 6fd675e | 2023-03-16 00:02:35 | [diff] [blame] | 221 | const std::string& scheme, |
Aishwarya Rajesh | b56269c2 | 2024-01-25 18:34:08 | [diff] [blame] | 222 | const std::string& host) { |
Mark Schillaci | 6fd675e | 2023-03-16 00:02:35 | [diff] [blame] | 223 | double zoom_level = GetZoomLevelForHostAndScheme(scheme, host); |
| 224 | |
Mark Schillaci | 6fd675e | 2023-03-16 00:02:35 | [diff] [blame] | 225 | // On Android, we will use a zoom level that considers the current OS-level |
Aishwarya Rajesh | b56269c2 | 2024-01-25 18:34:08 | [diff] [blame] | 226 | // setting. For this we pass the given |level| through JNI to the Java-side |
| 227 | // code, which can access the Android configuration and |fontScale|. This |
| 228 | // method will return the adjusted zoom level considering OS settings. Note |
| 229 | // that the OS |fontScale| will be factored in only when the Page Zoom feature |
| 230 | // is enabled. |
Andrew Grieve | f46caa51 | 2024-05-23 16:59:36 | [diff] [blame] | 231 | JNIEnv* env = jni_zero::AttachCurrentThread(); |
Aishwarya Rajesh | b56269c2 | 2024-01-25 18:34:08 | [diff] [blame] | 232 | double adjusted_zoom_level = |
| 233 | Java_HostZoomMapImpl_getAdjustedZoomLevel(env, zoom_level); |
Mark Schillaci | 6fd675e | 2023-03-16 00:02:35 | [diff] [blame] | 234 | return adjusted_zoom_level; |
| 235 | } |
| 236 | #endif |
| 237 | |
Lucas Furukawa Gadani | 4b4eed0 | 2019-06-04 23:12:04 | [diff] [blame] | 238 | HostZoomMap::ZoomLevelVector HostZoomMapImpl::GetAllZoomLevels() { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 239 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 0f37405 | 2014-03-18 20:37:22 | [diff] [blame] | 240 | HostZoomMap::ZoomLevelVector result; |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 241 | result.reserve(host_zoom_levels_.size() + scheme_host_zoom_levels_.size()); |
| 242 | for (const auto& entry : host_zoom_levels_) { |
| 243 | ZoomLevelChange change = { |
| 244 | HostZoomMap::ZOOM_CHANGED_FOR_HOST, |
| 245 | entry.first, // host |
| 246 | std::string(), // scheme |
| 247 | entry.second.level, // zoom level |
| 248 | entry.second.last_modified // last modified |
| 249 | }; |
| 250 | result.push_back(change); |
| 251 | } |
| 252 | for (const auto& scheme_entry : scheme_host_zoom_levels_) { |
| 253 | const std::string& scheme = scheme_entry.first; |
| 254 | const HostZoomLevels& host_zoom_levels = scheme_entry.second; |
| 255 | for (const auto& entry : host_zoom_levels) { |
Christian Dullweber | cc736c1 | 2017-09-04 09:27:50 | [diff] [blame] | 256 | ZoomLevelChange change = { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 257 | HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST, |
Christian Dullweber | cc736c1 | 2017-09-04 09:27:50 | [diff] [blame] | 258 | entry.first, // host |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 259 | scheme, // scheme |
Christian Dullweber | cc736c1 | 2017-09-04 09:27:50 | [diff] [blame] | 260 | entry.second.level, // zoom level |
| 261 | entry.second.last_modified // last modified |
[email protected] | 0f37405 | 2014-03-18 20:37:22 | [diff] [blame] | 262 | }; |
| 263 | result.push_back(change); |
| 264 | } |
[email protected] | 0f37405 | 2014-03-18 20:37:22 | [diff] [blame] | 265 | } |
| 266 | return result; |
| 267 | } |
| 268 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 269 | void HostZoomMapImpl::SetZoomLevelForHost(const std::string& host, |
| 270 | double level) { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 271 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Christian Dullweber | b7c5948 | 2018-01-29 11:25:01 | [diff] [blame] | 272 | base::Time last_modified = clock_->Now(); |
Christian Dullweber | cc736c1 | 2017-09-04 09:27:50 | [diff] [blame] | 273 | SetZoomLevelForHostInternal(host, level, last_modified); |
| 274 | } |
| 275 | |
| 276 | void HostZoomMapImpl::InitializeZoomLevelForHost(const std::string& host, |
| 277 | double level, |
| 278 | base::Time last_modified) { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 279 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Christian Dullweber | cc736c1 | 2017-09-04 09:27:50 | [diff] [blame] | 280 | SetZoomLevelForHostInternal(host, level, last_modified); |
| 281 | } |
| 282 | |
| 283 | void HostZoomMapImpl::SetZoomLevelForHostInternal(const std::string& host, |
| 284 | double level, |
| 285 | base::Time last_modified) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 286 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 3ef4bc63 | 2010-04-09 04:25:31 | [diff] [blame] | 287 | |
Stefan Zager | 036a35c | 2024-06-13 20:53:34 | [diff] [blame] | 288 | if (blink::ZoomValuesEqual(level, default_zoom_level_)) { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 289 | host_zoom_levels_.erase(host); |
| 290 | } else { |
| 291 | ZoomLevel& zoomLevel = host_zoom_levels_[host]; |
| 292 | zoomLevel.level = level; |
| 293 | zoomLevel.last_modified = last_modified; |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 294 | } |
| 295 | |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 296 | // TODO(wjmaclean) Should we use a GURL here? crbug.com/384486 |
akaba | c6bd121 | 2018-06-25 20:10:48 | [diff] [blame] | 297 | SendZoomLevelChange(std::string(), host); |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 298 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 299 | HostZoomMap::ZoomLevelChange change; |
| 300 | change.mode = HostZoomMap::ZOOM_CHANGED_FOR_HOST; |
| 301 | change.host = host; |
| 302 | change.zoom_level = level; |
Christian Dullweber | cc736c1 | 2017-09-04 09:27:50 | [diff] [blame] | 303 | change.last_modified = last_modified; |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 304 | |
[email protected] | 11783281 | 2013-10-02 07:06:02 | [diff] [blame] | 305 | zoom_level_changed_callbacks_.Notify(change); |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | void HostZoomMapImpl::SetZoomLevelForHostAndScheme(const std::string& scheme, |
| 309 | const std::string& host, |
| 310 | double level) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 311 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 312 | // No last_modified timestamp for scheme and host because they are |
| 313 | // not persistet and are used for special cases only. |
| 314 | scheme_host_zoom_levels_[scheme][host].level = level; |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 315 | |
akaba | c6bd121 | 2018-06-25 20:10:48 | [diff] [blame] | 316 | SendZoomLevelChange(scheme, host); |
[email protected] | 4e2a25a | 2012-01-27 00:42:08 | [diff] [blame] | 317 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 318 | HostZoomMap::ZoomLevelChange change; |
| 319 | change.mode = HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST; |
| 320 | change.host = host; |
| 321 | change.scheme = scheme; |
| 322 | change.zoom_level = level; |
Christian Dullweber | cc736c1 | 2017-09-04 09:27:50 | [diff] [blame] | 323 | change.last_modified = base::Time(); |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 324 | |
[email protected] | 11783281 | 2013-10-02 07:06:02 | [diff] [blame] | 325 | zoom_level_changed_callbacks_.Notify(change); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 326 | } |
| 327 | |
Lucas Furukawa Gadani | 4b4eed0 | 2019-06-04 23:12:04 | [diff] [blame] | 328 | double HostZoomMapImpl::GetDefaultZoomLevel() { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 329 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 330 | return default_zoom_level_; |
| 331 | } |
| 332 | |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 333 | void HostZoomMapImpl::SetDefaultZoomLevelInternal(double level, |
| 334 | WebContentsImpl* web_contents, |
| 335 | RenderFrameHostImpl* rfh) { |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 336 | std::string host; |
| 337 | std::string scheme; |
| 338 | |
W. James MacLean | 817a05e | 2025-02-18 15:51:15 | [diff] [blame] | 339 | // Get the url from the RenderFrameHost directly, as calling |
| 340 | // WebContentsImpl::GetLastCommittedURL() may give us a virtual url that |
| 341 | // is different than the one stored in the map. |
| 342 | GURL url = GetURLForRenderFrameHostPtr(rfh); |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 343 | // It is possible for a WebContent's zoom level to be queried before |
W. James MacLean | 817a05e | 2025-02-18 15:51:15 | [diff] [blame] | 344 | // a navigation has occurred, in which case `url` will be empty. |
| 345 | scheme = url.scheme(); |
| 346 | host = net::GetHostOrSpecFromURL(url); |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 347 | |
| 348 | bool uses_default_zoom = !HasZoomLevel(scheme, host) && |
| 349 | !UsesTemporaryZoomLevel(rfh->GetGlobalId()); |
| 350 | |
| 351 | if (uses_default_zoom) { |
| 352 | web_contents->UpdateZoom(rfh->GetGlobalId()); |
| 353 | |
| 354 | HostZoomMap::ZoomLevelChange change; |
| 355 | change.mode = HostZoomMap::ZOOM_CHANGED_FOR_HOST; |
| 356 | change.host = host; |
| 357 | change.zoom_level = level; |
| 358 | |
| 359 | zoom_level_changed_callbacks_.Notify(change); |
| 360 | } |
| 361 | } |
| 362 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 363 | void HostZoomMapImpl::SetDefaultZoomLevel(double level) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 364 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
wjmaclean | 6495190 | 2016-04-29 20:59:12 | [diff] [blame] | 365 | |
Stefan Zager | 036a35c | 2024-06-13 20:53:34 | [diff] [blame] | 366 | if (blink::ZoomValuesEqual(level, default_zoom_level_)) { |
danakj | 938b37a6 | 2019-09-24 18:35:54 | [diff] [blame] | 367 | return; |
Stefan Zager | 036a35c | 2024-06-13 20:53:34 | [diff] [blame] | 368 | } |
wjmaclean | 6495190 | 2016-04-29 20:59:12 | [diff] [blame] | 369 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 370 | default_zoom_level_ = level; |
wjmaclean | 6495190 | 2016-04-29 20:59:12 | [diff] [blame] | 371 | |
| 372 | // First, remove all entries that match the new default zoom level. |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 373 | for (auto it = host_zoom_levels_.begin(); it != host_zoom_levels_.end();) { |
Stefan Zager | 036a35c | 2024-06-13 20:53:34 | [diff] [blame] | 374 | if (blink::ZoomValuesEqual(it->second.level, default_zoom_level_)) { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 375 | it = host_zoom_levels_.erase(it); |
Stefan Zager | 036a35c | 2024-06-13 20:53:34 | [diff] [blame] | 376 | } else { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 377 | it++; |
Stefan Zager | 036a35c | 2024-06-13 20:53:34 | [diff] [blame] | 378 | } |
wjmaclean | 6495190 | 2016-04-29 20:59:12 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | // Second, update zoom levels for all pages that do not have an overriding |
| 382 | // entry. |
vmpstr | 10e0d5f | 2016-07-21 23:46:09 | [diff] [blame] | 383 | for (auto* web_contents : WebContentsImpl::GetAllWebContents()) { |
wjmaclean | 6495190 | 2016-04-29 20:59:12 | [diff] [blame] | 384 | // Only change zoom for WebContents tied to the StoragePartition this |
| 385 | // HostZoomMap serves. |
| 386 | if (GetForWebContents(web_contents) != this) |
| 387 | continue; |
| 388 | |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 389 | SetDefaultZoomLevelInternal(level, web_contents, |
| 390 | web_contents->GetPrimaryMainFrame()); |
| 391 | } |
wjmaclean | 6495190 | 2016-04-29 20:59:12 | [diff] [blame] | 392 | |
W. James MacLean | f562b92 | 2025-03-13 15:36:19 | [diff] [blame] | 393 | // If independent subframe zoom is enabled, then update zoom levels for |
| 394 | // subframes that do not have an overriding entry. |
| 395 | if (!IsIndependentSubframeZoomEnabled()) { |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 396 | return; |
| 397 | } |
| 398 | for (auto ftn_id : independent_zoom_frame_tree_nodes_) { |
| 399 | auto* rfh = FrameTreeNode::GloballyFindByID(ftn_id)->current_frame_host(); |
| 400 | // `rfh` should be non-null here since, if it was deleted, then |
| 401 | // WebContentsObserver::FrameDeleted will have notified any ZoomControllers, |
| 402 | // and `independent_zoom_frame_tree_nodes_` will have been updated |
| 403 | // accordingly. |
| 404 | if (!UsesTemporaryZoomLevel(rfh->GetGlobalId())) { |
| 405 | auto* web_contents = WebContentsImpl::FromRenderFrameHostImpl(rfh); |
| 406 | if (web_contents && (rfh != web_contents->GetPrimaryMainFrame())) { |
| 407 | SetDefaultZoomLevelInternal(level, web_contents, rfh); |
| 408 | } |
wjmaclean | 6495190 | 2016-04-29 20:59:12 | [diff] [blame] | 409 | } |
| 410 | } |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 411 | } |
| 412 | |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 413 | base::CallbackListSubscription HostZoomMapImpl::AddZoomLevelChangedCallback( |
danakj | 6aa13ea | 2019-12-10 14:53:26 | [diff] [blame] | 414 | ZoomLevelChangedCallback callback) { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 415 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
danakj | 6aa13ea | 2019-12-10 14:53:26 | [diff] [blame] | 416 | return zoom_level_changed_callbacks_.Add(std::move(callback)); |
[email protected] | 89c9aca | 2013-02-07 15:08:42 | [diff] [blame] | 417 | } |
| 418 | |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 419 | double HostZoomMapImpl::GetZoomLevelForWebContents( |
Lucas Furukawa Gadani | 4b4eed0 | 2019-06-04 23:12:04 | [diff] [blame] | 420 | WebContentsImpl* web_contents_impl) { |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 421 | return GetZoomLevelForWebContents( |
| 422 | web_contents_impl, |
| 423 | web_contents_impl->GetPrimaryMainFrame()->GetGlobalId()); |
| 424 | } |
| 425 | |
| 426 | double HostZoomMapImpl::GetZoomLevelForWebContents( |
| 427 | WebContentsImpl* web_contents_impl, |
| 428 | GlobalRenderFrameHostId rfh_id) { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 429 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 430 | |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 431 | if (UsesTemporaryZoomLevel(rfh_id)) |
| 432 | return GetTemporaryZoomLevel(rfh_id); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 433 | |
W. James MacLean | 817a05e | 2025-02-18 15:51:15 | [diff] [blame] | 434 | GURL url = GetURLForRenderFrameHost(rfh_id); |
Mark Schillaci | cddfa17 | 2022-07-28 19:57:13 | [diff] [blame] | 435 | |
Mark Schillaci | cddfa17 | 2022-07-28 19:57:13 | [diff] [blame] | 436 | #if BUILDFLAG(IS_ANDROID) |
Aishwarya Rajesh | b56269c2 | 2024-01-25 18:34:08 | [diff] [blame] | 437 | return GetZoomLevelForHostAndSchemeAndroid(url.scheme(), |
| 438 | net::GetHostOrSpecFromURL(url)); |
Mark Schillaci | cddfa17 | 2022-07-28 19:57:13 | [diff] [blame] | 439 | #else |
Mark Schillaci | 6fd675e | 2023-03-16 00:02:35 | [diff] [blame] | 440 | return GetZoomLevelForHostAndScheme(url.scheme(), |
| 441 | net::GetHostOrSpecFromURL(url)); |
Mark Schillaci | cddfa17 | 2022-07-28 19:57:13 | [diff] [blame] | 442 | #endif |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | void HostZoomMapImpl::SetZoomLevelForWebContents( |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 446 | WebContentsImpl* web_contents_impl, |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 447 | GlobalRenderFrameHostId rfh_id, |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 448 | double level) { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 449 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 450 | |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 451 | if (UsesTemporaryZoomLevel(rfh_id)) { |
| 452 | SetTemporaryZoomLevel(rfh_id, level); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 453 | } else { |
W. James MacLean | 817a05e | 2025-02-18 15:51:15 | [diff] [blame] | 454 | GURL url = GetURLForRenderFrameHost(rfh_id); |
[email protected] | acda0ef3 | 2014-06-05 23:13:30 | [diff] [blame] | 455 | SetZoomLevelForHost(net::GetHostOrSpecFromURL(url), level); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 459 | bool HostZoomMapImpl::UsesTemporaryZoomLevel( |
| 460 | const GlobalRenderFrameHostId& rfh_id) { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 461 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 462 | return base::Contains(temporary_zoom_levels_, rfh_id); |
[email protected] | fce82322 | 2014-05-30 16:24:30 | [diff] [blame] | 463 | } |
| 464 | |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 465 | void HostZoomMapImpl::SetNoLongerUsesTemporaryZoomLevel( |
| 466 | const GlobalRenderFrameHostId& rfh_id) { |
Mark Schillaci | cddfa17 | 2022-07-28 19:57:13 | [diff] [blame] | 467 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 468 | temporary_zoom_levels_.erase(rfh_id); |
Mark Schillaci | cddfa17 | 2022-07-28 19:57:13 | [diff] [blame] | 469 | } |
| 470 | |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 471 | double HostZoomMapImpl::GetTemporaryZoomLevel( |
| 472 | const GlobalRenderFrameHostId& rfh_id) const { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 473 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 474 | const auto it = temporary_zoom_levels_.find(rfh_id); |
| 475 | |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 476 | return it != temporary_zoom_levels_.end() ? it->second : 0; |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 477 | } |
| 478 | |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 479 | void HostZoomMapImpl::SetTemporaryZoomLevel( |
| 480 | const GlobalRenderFrameHostId& rfh_id, |
| 481 | double level) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 482 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 483 | |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 484 | RenderFrameHostImpl* rfh = RenderFrameHostImpl::FromID(rfh_id); |
W. James MacLean | f562b92 | 2025-03-13 15:36:19 | [diff] [blame] | 485 | if (IsIndependentSubframeZoomEnabled()) { |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 486 | CHECK(rfh->is_local_root()); |
| 487 | } else { |
| 488 | DCHECK(rfh == rfh->GetOutermostMainFrame()); |
| 489 | } |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 490 | |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 491 | temporary_zoom_levels_[rfh_id] = level; |
| 492 | |
| 493 | WebContentsImpl* web_contents = WebContentsImpl::FromRenderFrameHostImpl(rfh); |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 494 | web_contents->UpdateZoom(rfh_id); |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 495 | |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 496 | HostZoomMap::ZoomLevelChange change; |
| 497 | change.mode = HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM; |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 498 | change.host = GetHostFromProcessFrame(rfh); |
[email protected] | 367c5c1d | 2013-03-11 18:59:02 | [diff] [blame] | 499 | change.zoom_level = level; |
| 500 | |
[email protected] | 11783281 | 2013-10-02 07:06:02 | [diff] [blame] | 501 | zoom_level_changed_callbacks_.Notify(change); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 502 | } |
| 503 | |
Christian Dullweber | cc736c1 | 2017-09-04 09:27:50 | [diff] [blame] | 504 | void HostZoomMapImpl::ClearZoomLevels(base::Time delete_begin, |
| 505 | base::Time delete_end) { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 506 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Christian Dullweber | cc736c1 | 2017-09-04 09:27:50 | [diff] [blame] | 507 | double default_zoom_level = GetDefaultZoomLevel(); |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 508 | for (const auto& zoom_level : GetAllZoomLevels()) { |
Christian Dullweber | cc736c1 | 2017-09-04 09:27:50 | [diff] [blame] | 509 | if (zoom_level.scheme.empty() && delete_begin <= zoom_level.last_modified && |
| 510 | (delete_end.is_null() || zoom_level.last_modified < delete_end)) { |
| 511 | SetZoomLevelForHost(zoom_level.host, default_zoom_level); |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 516 | void HostZoomMapImpl::ClearTemporaryZoomLevel( |
| 517 | const GlobalRenderFrameHostId& rfh_id) { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 518 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 519 | auto it = temporary_zoom_levels_.find(rfh_id); |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 520 | if (it == temporary_zoom_levels_.end()) |
| 521 | return; |
| 522 | |
| 523 | temporary_zoom_levels_.erase(it); |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 524 | WebContentsImpl* web_contents = WebContentsImpl::FromRenderFrameHostImpl( |
| 525 | RenderFrameHostImpl::FromID(rfh_id)); |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 526 | web_contents->UpdateZoom(rfh_id); |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme, |
akaba | c6bd121 | 2018-06-25 20:10:48 | [diff] [blame] | 530 | const std::string& host) { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 531 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
wjmaclean | 6495190 | 2016-04-29 20:59:12 | [diff] [blame] | 532 | // We'll only send to WebContents not using temporary zoom levels. The one |
| 533 | // other case of interest is where the renderer is hosting a plugin document; |
| 534 | // that should be reflected in our temporary zoom level map, but we will |
| 535 | // double check on the renderer side to avoid the possibility of any races. |
vmpstr | 10e0d5f | 2016-07-21 23:46:09 | [diff] [blame] | 536 | for (auto* web_contents : WebContentsImpl::GetAllWebContents()) { |
wjmaclean | 6495190 | 2016-04-29 20:59:12 | [diff] [blame] | 537 | // Only send zoom level changes to WebContents that are using this |
| 538 | // HostZoomMap. |
| 539 | if (GetForWebContents(web_contents) != this) |
| 540 | continue; |
| 541 | |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 542 | if (!UsesTemporaryZoomLevel( |
| 543 | web_contents->GetPrimaryMainFrame()->GetGlobalId())) { |
akaba | c6bd121 | 2018-06-25 20:10:48 | [diff] [blame] | 544 | web_contents->UpdateZoomIfNecessary(scheme, host); |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 545 | } |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 546 | } |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 547 | |
| 548 | // Also loop over the independently-zoomed FTNs that aren't primary |
W. James MacLean | f562b92 | 2025-03-13 15:36:19 | [diff] [blame] | 549 | // mainframes. If independent subframe zoom isn't enabled, then there will be |
| 550 | // no such frames, so early-out in that case. |
| 551 | if (!IsIndependentSubframeZoomEnabled()) { |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 552 | return; |
| 553 | } |
| 554 | for (auto ftn_id : independent_zoom_frame_tree_nodes_) { |
| 555 | auto* rfh = FrameTreeNode::GloballyFindByID(ftn_id)->current_frame_host(); |
| 556 | if (!UsesTemporaryZoomLevel(rfh->GetGlobalId())) { |
| 557 | auto* web_contents = WebContentsImpl::FromRenderFrameHostImpl(rfh); |
| 558 | if (web_contents && (rfh != web_contents->GetPrimaryMainFrame())) { |
| 559 | web_contents->UpdateZoomIfNecessary(scheme, host, rfh); |
| 560 | } |
| 561 | } |
| 562 | } |
[email protected] | d42bf47 | 2014-06-14 01:49:38 | [diff] [blame] | 563 | } |
| 564 | |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame] | 565 | void HostZoomMapImpl::SendErrorPageZoomLevelRefresh() { |
Lei Zhang | 9d40e790 | 2017-11-20 19:54:36 | [diff] [blame] | 566 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame] | 567 | GURL error_url(kUnreachableWebDataURL); |
| 568 | std::string host = net::GetHostOrSpecFromURL(error_url); |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame] | 569 | |
akaba | c6bd121 | 2018-06-25 20:10:48 | [diff] [blame] | 570 | SendZoomLevelChange(std::string(), host); |
wjmaclean | de29ed5 | 2014-10-28 21:09:06 | [diff] [blame] | 571 | } |
| 572 | |
[email protected] | 5c925087 | 2012-01-30 17:24:05 | [diff] [blame] | 573 | HostZoomMapImpl::~HostZoomMapImpl() { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 574 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 40bd658 | 2009-12-04 23:49:51 | [diff] [blame] | 575 | } |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 576 | |
tzik | 67025f67 | 2017-11-29 05:04:44 | [diff] [blame] | 577 | void HostZoomMapImpl::SetClockForTesting(base::Clock* clock) { |
| 578 | clock_ = clock; |
Christian Dullweber | cc736c1 | 2017-09-04 09:27:50 | [diff] [blame] | 579 | } |
| 580 | |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 581 | #if BUILDFLAG(IS_ANDROID) |
Mark Schillaci | 6fd675e | 2023-03-16 00:02:35 | [diff] [blame] | 582 | void HostZoomMapImpl::SetSystemFontScaleForTesting(float scale) { |
Andrew Grieve | f46caa51 | 2024-05-23 16:59:36 | [diff] [blame] | 583 | JNIEnv* env = jni_zero::AttachCurrentThread(); |
Mark Schillaci | 6fd675e | 2023-03-16 00:02:35 | [diff] [blame] | 584 | Java_HostZoomMapImpl_setSystemFontScaleForTesting(env, scale); // IN-TEST |
| 585 | } |
| 586 | |
Mark Schillaci | 7f43c669 | 2024-10-31 17:52:26 | [diff] [blame] | 587 | void HostZoomMapImpl::SetShouldAdjustForOSLevelForTesting( |
| 588 | bool shouldAdjustForOSLevel) { |
| 589 | JNIEnv* env = jni_zero::AttachCurrentThread(); |
| 590 | Java_HostZoomMapImpl_setShouldAdjustForOSLevelForTesting( |
| 591 | env, shouldAdjustForOSLevel); // IN-TEST |
| 592 | } |
| 593 | |
Mark Schillaci | 8ff9749 | 2022-07-25 23:25:08 | [diff] [blame] | 594 | void HostZoomMapImpl::SetDefaultZoomLevelPrefCallback( |
| 595 | HostZoomMap::DefaultZoomChangedCallback callback) { |
| 596 | default_zoom_level_pref_callback_ = std::move(callback); |
| 597 | } |
| 598 | |
| 599 | HostZoomMap::DefaultZoomChangedCallback* |
| 600 | HostZoomMapImpl::GetDefaultZoomLevelPrefCallback() { |
| 601 | return &default_zoom_level_pref_callback_; |
| 602 | } |
| 603 | |
Mark Schillaci | 1363e4a | 2021-10-04 19:10:09 | [diff] [blame] | 604 | void JNI_HostZoomMapImpl_SetZoomLevel( |
| 605 | JNIEnv* env, |
| 606 | const base::android::JavaParamRef<jobject>& j_web_contents, |
Mark Schillaci | cddfa17 | 2022-07-28 19:57:13 | [diff] [blame] | 607 | jdouble new_zoom_level, |
| 608 | jdouble adjusted_zoom_level) { |
Mark Schillaci | 1363e4a | 2021-10-04 19:10:09 | [diff] [blame] | 609 | WebContents* web_contents = WebContents::FromJavaWebContents(j_web_contents); |
| 610 | DCHECK(web_contents); |
| 611 | |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 612 | GlobalRenderFrameHostId rfh_id = |
| 613 | web_contents->GetPrimaryMainFrame()->GetGlobalId(); |
Mark Schillaci | cddfa17 | 2022-07-28 19:57:13 | [diff] [blame] | 614 | |
| 615 | // We want to set and save the new zoom level, but we want to actually render |
| 616 | // the adjusted level. |
Mark Schillaci | 1363e4a | 2021-10-04 19:10:09 | [diff] [blame] | 617 | HostZoomMap::SetZoomLevel(web_contents, new_zoom_level); |
Mark Schillaci | cddfa17 | 2022-07-28 19:57:13 | [diff] [blame] | 618 | |
| 619 | HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( |
| 620 | HostZoomMap::GetForWebContents(web_contents)); |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 621 | host_zoom_map->SetTemporaryZoomLevel(rfh_id, adjusted_zoom_level); |
Mark Schillaci | cddfa17 | 2022-07-28 19:57:13 | [diff] [blame] | 622 | |
| 623 | // We must now remove this webcontents from the list of temporary zoom levels, |
| 624 | // this is so that any future request will continue to update the underlying |
| 625 | // host/scheme save, and will not be perceived as "temporary". |
| 626 | // i.e. once temporary is set for a web_contents, the call to |
| 627 | // SetZoomLevelForWebContents will keep updating what is rendered, but will no |
| 628 | // longer call SetZoomLevelForHost, which saves the choice for that host. |
Miyoung Shin | dae0986 | 2022-11-02 01:27:37 | [diff] [blame] | 629 | host_zoom_map->SetNoLongerUsesTemporaryZoomLevel(rfh_id); |
Mark Schillaci | 1363e4a | 2021-10-04 19:10:09 | [diff] [blame] | 630 | } |
| 631 | |
Aashna Sheth | ebb96ad | 2023-08-25 21:13:50 | [diff] [blame] | 632 | void JNI_HostZoomMapImpl_SetZoomLevelForHost( |
| 633 | JNIEnv* env, |
| 634 | const base::android::JavaParamRef<jobject>& j_context, |
| 635 | const base::android::JavaParamRef<jstring>& j_host, |
| 636 | jdouble level) { |
| 637 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 638 | |
| 639 | BrowserContext* context = BrowserContextFromJavaHandle(j_context); |
| 640 | if (!context) { |
| 641 | return; |
| 642 | } |
| 643 | |
| 644 | HostZoomMap* host_zoom_map = |
| 645 | HostZoomMap::GetDefaultForBrowserContext(context); |
| 646 | |
| 647 | std::string host(base::android::ConvertJavaStringToUTF8(env, j_host)); |
| 648 | host_zoom_map->SetZoomLevelForHost(host, level); |
| 649 | } |
| 650 | |
Mark Schillaci | 1363e4a | 2021-10-04 19:10:09 | [diff] [blame] | 651 | jdouble JNI_HostZoomMapImpl_GetZoomLevel( |
| 652 | JNIEnv* env, |
| 653 | const base::android::JavaParamRef<jobject>& j_web_contents) { |
| 654 | WebContents* web_contents = WebContents::FromJavaWebContents(j_web_contents); |
| 655 | DCHECK(web_contents); |
| 656 | |
| 657 | return HostZoomMap::GetZoomLevel(web_contents); |
| 658 | } |
Mark Schillaci | c28765d | 2022-06-03 21:38:44 | [diff] [blame] | 659 | |
| 660 | void JNI_HostZoomMapImpl_SetDefaultZoomLevel( |
| 661 | JNIEnv* env, |
| 662 | const base::android::JavaParamRef<jobject>& j_context, |
| 663 | jdouble new_default_zoom_level) { |
| 664 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 665 | BrowserContext* context = BrowserContextFromJavaHandle(j_context); |
| 666 | if (!context) |
| 667 | return; |
| 668 | |
| 669 | HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( |
| 670 | HostZoomMap::GetDefaultForBrowserContext(context)); |
Mark Schillaci | 8ff9749 | 2022-07-25 23:25:08 | [diff] [blame] | 671 | |
| 672 | // If a callback has been set (e.g. by chrome_zoom_level_prefs to store an |
| 673 | // updated value in Prefs), call this now with the chosen zoom level. |
| 674 | if (host_zoom_map->GetDefaultZoomLevelPrefCallback()) { |
| 675 | host_zoom_map->GetDefaultZoomLevelPrefCallback()->Run( |
| 676 | new_default_zoom_level); |
| 677 | } |
| 678 | |
| 679 | // Update the default zoom level for existing tabs. This must be done after |
| 680 | // the Pref is updated due to guard clause in chrome_zoom_level_prefs. |
Mark Schillaci | c28765d | 2022-06-03 21:38:44 | [diff] [blame] | 681 | host_zoom_map->SetDefaultZoomLevel(new_default_zoom_level); |
| 682 | } |
| 683 | |
| 684 | jdouble JNI_HostZoomMapImpl_GetDefaultZoomLevel( |
| 685 | JNIEnv* env, |
| 686 | const base::android::JavaParamRef<jobject>& j_context) { |
| 687 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 688 | BrowserContext* context = BrowserContextFromJavaHandle(j_context); |
| 689 | if (!context) |
| 690 | return 0.0; |
| 691 | |
| 692 | HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>( |
| 693 | HostZoomMap::GetDefaultForBrowserContext(context)); |
| 694 | return host_zoom_map->GetDefaultZoomLevel(); |
| 695 | } |
Aishwarya Rajesh | 39ef1c6 | 2022-11-09 06:25:37 | [diff] [blame] | 696 | |
Andrew Grieve | f46caa51 | 2024-05-23 16:59:36 | [diff] [blame] | 697 | std::vector<jni_zero::ScopedJavaLocalRef<jobject>> |
Andrew Grieve | 6a51bed | 2024-03-11 17:37:48 | [diff] [blame] | 698 | JNI_HostZoomMapImpl_GetAllHostZoomLevels( |
Aashna Sheth | ebb96ad | 2023-08-25 21:13:50 | [diff] [blame] | 699 | JNIEnv* env, |
| 700 | const base::android::JavaParamRef<jobject>& j_context) { |
| 701 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Andrew Grieve | f46caa51 | 2024-05-23 16:59:36 | [diff] [blame] | 702 | std::vector<jni_zero::ScopedJavaLocalRef<jobject>> ret; |
Aashna Sheth | ebb96ad | 2023-08-25 21:13:50 | [diff] [blame] | 703 | |
| 704 | // Get instance of HostZoomMap. |
| 705 | BrowserContext* context = BrowserContextFromJavaHandle(j_context); |
| 706 | if (!context) { |
Andrew Grieve | 6a51bed | 2024-03-11 17:37:48 | [diff] [blame] | 707 | return ret; |
Aashna Sheth | ebb96ad | 2023-08-25 21:13:50 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | HostZoomMap* host_zoom_map = |
| 711 | HostZoomMap::GetDefaultForBrowserContext(context); |
| 712 | |
| 713 | // Convert C++ vector of structs to vector of objects. |
Aashna Sheth | ebb96ad | 2023-08-25 21:13:50 | [diff] [blame] | 714 | for (const auto& entry : host_zoom_map->GetAllZoomLevels()) { |
| 715 | switch (entry.mode) { |
| 716 | case HostZoomMap::ZOOM_CHANGED_FOR_HOST: { |
Andrew Grieve | 6a51bed | 2024-03-11 17:37:48 | [diff] [blame] | 717 | ret.push_back(Java_HostZoomMapImpl_buildSiteZoomInfo(env, entry.host, |
| 718 | entry.zoom_level)); |
Aashna Sheth | ebb96ad | 2023-08-25 21:13:50 | [diff] [blame] | 719 | break; |
| 720 | } |
| 721 | case HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST: |
Peter Boström | fc7ddc18 | 2024-10-31 19:37:21 | [diff] [blame] | 722 | NOTREACHED(); |
Aashna Sheth | ebb96ad | 2023-08-25 21:13:50 | [diff] [blame] | 723 | case HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM: |
Peter Boström | fc7ddc18 | 2024-10-31 19:37:21 | [diff] [blame] | 724 | NOTREACHED(); |
Aashna Sheth | ebb96ad | 2023-08-25 21:13:50 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
Andrew Grieve | 6a51bed | 2024-03-11 17:37:48 | [diff] [blame] | 728 | return ret; |
Aashna Sheth | ebb96ad | 2023-08-25 21:13:50 | [diff] [blame] | 729 | } |
Mark Schillaci | 1363e4a | 2021-10-04 19:10:09 | [diff] [blame] | 730 | #endif |
| 731 | |
keno | d3fe1c22 | 2023-12-13 19:58:50 | [diff] [blame] | 732 | double HostZoomMapImpl::GetZoomLevelForPreviewAndHost(const std::string& host) { |
| 733 | const auto it = host_zoom_levels_for_preview_.find(host); |
| 734 | return it != host_zoom_levels_for_preview_.end() ? it->second.level |
| 735 | : default_zoom_level_; |
| 736 | } |
| 737 | |
| 738 | void HostZoomMapImpl::SetZoomLevelForPreviewAndHost(const std::string& host, |
| 739 | double level) { |
Stefan Zager | 036a35c | 2024-06-13 20:53:34 | [diff] [blame] | 740 | if (blink::ZoomValuesEqual(level, default_zoom_level_)) { |
keno | d3fe1c22 | 2023-12-13 19:58:50 | [diff] [blame] | 741 | host_zoom_levels_for_preview_.erase(host); |
| 742 | } else { |
| 743 | ZoomLevel& zoomLevel = host_zoom_levels_for_preview_[host]; |
| 744 | zoomLevel.level = level; |
| 745 | zoomLevel.last_modified = clock_->Now(); |
| 746 | } |
| 747 | } |
| 748 | |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 749 | void HostZoomMapImpl::SetIndependentZoomForFrameTreeNode( |
| 750 | WebContents* web_contents, |
| 751 | FrameTreeNodeId ftn_id) { |
| 752 | CHECK(web_contents); |
W. James MacLean | f562b92 | 2025-03-13 15:36:19 | [diff] [blame] | 753 | auto* rfh = static_cast<RenderFrameHostImpl*>( |
| 754 | web_contents->UnsafeFindFrameByFrameTreeNodeId(ftn_id)); |
| 755 | CHECK(rfh->is_local_root()); |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 756 | independent_zoom_frame_tree_nodes_.insert(ftn_id); |
W. James MacLean | f562b92 | 2025-03-13 15:36:19 | [diff] [blame] | 757 | |
| 758 | // Force an update in case the `rfh` contains content with a different zoom. |
| 759 | static_cast<WebContentsImpl*>(web_contents)->UpdateZoom(rfh->GetGlobalId()); |
W. James MacLean | 37af605e | 2025-01-20 16:59:38 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | void HostZoomMapImpl::ClearIndependentZoomForFrameTreeNode( |
| 763 | FrameTreeNodeId ftn_id) { |
| 764 | independent_zoom_frame_tree_nodes_.erase(ftn_id); |
| 765 | } |
| 766 | |
W. James MacLean | f562b92 | 2025-03-13 15:36:19 | [diff] [blame] | 767 | bool HostZoomMapImpl::IsIndependentZoomFrameTreeNode( |
| 768 | FrameTreeNodeId ftn_id) const { |
| 769 | return independent_zoom_frame_tree_nodes_.contains(ftn_id); |
| 770 | } |
| 771 | |
Kevin McNee | 3183a779 | 2021-11-09 21:03:36 | [diff] [blame] | 772 | } // namespace content |