#include "content/browser/host_zoom_map_impl.h"
#include <algorithm>
#include <cmath>
#include <memory>
#include <utility>
#include "base/containers/contains.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/default_clock.h"
#include "base/values.h"
#include "build/build_config.h"
#include "content/browser/renderer_host/navigation_entry_impl.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/host_zoom_map.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/resource_context.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/common/url_constants.h"
#include "net/base/url_util.h"
#include "third_party/blink/public/common/page/page_zoom.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/jni_string.h"
#include "content/public/browser/android/browser_context_handle.h"
#include "content/public/android/content_jni_headers/HostZoomMapImpl_jni.h"
#endif
namespace content {
namespace {
std::string GetHostFromProcessFrame(RenderFrameHostImpl* rfh) { … }
}
GURL HostZoomMap::GetURLFromEntry(NavigationEntry* entry) { … }
HostZoomMap* HostZoomMap::GetDefaultForBrowserContext(BrowserContext* context) { … }
HostZoomMap* HostZoomMap::Get(SiteInstance* instance) { … }
HostZoomMap* HostZoomMap::GetForWebContents(WebContents* contents) { … }
HostZoomMap* HostZoomMap::GetForStoragePartition(
StoragePartition* storage_partition) { … }
double HostZoomMap::GetZoomLevel(WebContents* web_contents) { … }
void HostZoomMap::SetZoomLevel(WebContents* web_contents, double level) { … }
void HostZoomMap::SendErrorPageZoomLevelRefresh(WebContents* web_contents) { … }
HostZoomMapImpl::HostZoomMapImpl()
: … { … }
void HostZoomMapImpl::CopyFrom(HostZoomMap* copy_interface) { … }
double HostZoomMapImpl::GetZoomLevelForHost(const std::string& host) const { … }
bool HostZoomMapImpl::HasZoomLevel(const std::string& scheme,
const std::string& host) { … }
double HostZoomMapImpl::GetZoomLevelForHostAndScheme(const std::string& scheme,
const std::string& host) { … }
#if BUILDFLAG(IS_ANDROID)
double HostZoomMapImpl::GetZoomLevelForHostAndSchemeAndroid(
const std::string& scheme,
const std::string& host) {
double zoom_level = GetZoomLevelForHostAndScheme(scheme, host);
JNIEnv* env = jni_zero::AttachCurrentThread();
double adjusted_zoom_level =
Java_HostZoomMapImpl_getAdjustedZoomLevel(env, zoom_level);
return adjusted_zoom_level;
}
#endif
HostZoomMap::ZoomLevelVector HostZoomMapImpl::GetAllZoomLevels() { … }
void HostZoomMapImpl::SetZoomLevelForHost(const std::string& host,
double level) { … }
void HostZoomMapImpl::InitializeZoomLevelForHost(const std::string& host,
double level,
base::Time last_modified) { … }
void HostZoomMapImpl::SetZoomLevelForHostInternal(const std::string& host,
double level,
base::Time last_modified) { … }
void HostZoomMapImpl::SetZoomLevelForHostAndScheme(const std::string& scheme,
const std::string& host,
double level) { … }
double HostZoomMapImpl::GetDefaultZoomLevel() { … }
void HostZoomMapImpl::SetDefaultZoomLevel(double level) { … }
base::CallbackListSubscription HostZoomMapImpl::AddZoomLevelChangedCallback(
ZoomLevelChangedCallback callback) { … }
double HostZoomMapImpl::GetZoomLevelForWebContents(
WebContentsImpl* web_contents_impl) { … }
void HostZoomMapImpl::SetZoomLevelForWebContents(
WebContentsImpl* web_contents_impl,
double level) { … }
bool HostZoomMapImpl::UsesTemporaryZoomLevel(
const GlobalRenderFrameHostId& rfh_id) { … }
void HostZoomMapImpl::SetNoLongerUsesTemporaryZoomLevel(
const GlobalRenderFrameHostId& rfh_id) { … }
double HostZoomMapImpl::GetTemporaryZoomLevel(
const GlobalRenderFrameHostId& rfh_id) const { … }
void HostZoomMapImpl::SetTemporaryZoomLevel(
const GlobalRenderFrameHostId& rfh_id,
double level) { … }
void HostZoomMapImpl::ClearZoomLevels(base::Time delete_begin,
base::Time delete_end) { … }
void HostZoomMapImpl::ClearTemporaryZoomLevel(
const GlobalRenderFrameHostId& rfh_id) { … }
void HostZoomMapImpl::SendZoomLevelChange(const std::string& scheme,
const std::string& host) { … }
void HostZoomMapImpl::SendErrorPageZoomLevelRefresh() { … }
HostZoomMapImpl::~HostZoomMapImpl() { … }
void HostZoomMapImpl::SetClockForTesting(base::Clock* clock) { … }
#if BUILDFLAG(IS_ANDROID)
void HostZoomMapImpl::SetSystemFontScaleForTesting(float scale) {
JNIEnv* env = jni_zero::AttachCurrentThread();
Java_HostZoomMapImpl_setSystemFontScaleForTesting(env, scale);
}
void HostZoomMapImpl::SetDefaultZoomLevelPrefCallback(
HostZoomMap::DefaultZoomChangedCallback callback) {
default_zoom_level_pref_callback_ = std::move(callback);
}
HostZoomMap::DefaultZoomChangedCallback*
HostZoomMapImpl::GetDefaultZoomLevelPrefCallback() {
return &default_zoom_level_pref_callback_;
}
void JNI_HostZoomMapImpl_SetZoomLevel(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_web_contents,
jdouble new_zoom_level,
jdouble adjusted_zoom_level) {
WebContents* web_contents = WebContents::FromJavaWebContents(j_web_contents);
DCHECK(web_contents);
GlobalRenderFrameHostId rfh_id =
web_contents->GetPrimaryMainFrame()->GetGlobalId();
HostZoomMap::SetZoomLevel(web_contents, new_zoom_level);
HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>(
HostZoomMap::GetForWebContents(web_contents));
host_zoom_map->SetTemporaryZoomLevel(rfh_id, adjusted_zoom_level);
host_zoom_map->SetNoLongerUsesTemporaryZoomLevel(rfh_id);
}
void JNI_HostZoomMapImpl_SetZoomLevelForHost(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_context,
const base::android::JavaParamRef<jstring>& j_host,
jdouble level) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserContext* context = BrowserContextFromJavaHandle(j_context);
if (!context) {
return;
}
HostZoomMap* host_zoom_map =
HostZoomMap::GetDefaultForBrowserContext(context);
std::string host(base::android::ConvertJavaStringToUTF8(env, j_host));
host_zoom_map->SetZoomLevelForHost(host, level);
}
jdouble JNI_HostZoomMapImpl_GetZoomLevel(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_web_contents) {
WebContents* web_contents = WebContents::FromJavaWebContents(j_web_contents);
DCHECK(web_contents);
return HostZoomMap::GetZoomLevel(web_contents);
}
void JNI_HostZoomMapImpl_SetDefaultZoomLevel(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_context,
jdouble new_default_zoom_level) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserContext* context = BrowserContextFromJavaHandle(j_context);
if (!context)
return;
HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>(
HostZoomMap::GetDefaultForBrowserContext(context));
if (host_zoom_map->GetDefaultZoomLevelPrefCallback()) {
host_zoom_map->GetDefaultZoomLevelPrefCallback()->Run(
new_default_zoom_level);
}
host_zoom_map->SetDefaultZoomLevel(new_default_zoom_level);
}
jdouble JNI_HostZoomMapImpl_GetDefaultZoomLevel(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_context) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserContext* context = BrowserContextFromJavaHandle(j_context);
if (!context)
return 0.0;
HostZoomMapImpl* host_zoom_map = static_cast<HostZoomMapImpl*>(
HostZoomMap::GetDefaultForBrowserContext(context));
return host_zoom_map->GetDefaultZoomLevel();
}
std::vector<jni_zero::ScopedJavaLocalRef<jobject>>
JNI_HostZoomMapImpl_GetAllHostZoomLevels(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_context) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
std::vector<jni_zero::ScopedJavaLocalRef<jobject>> ret;
BrowserContext* context = BrowserContextFromJavaHandle(j_context);
if (!context) {
return ret;
}
HostZoomMap* host_zoom_map =
HostZoomMap::GetDefaultForBrowserContext(context);
for (const auto& entry : host_zoom_map->GetAllZoomLevels()) {
switch (entry.mode) {
case HostZoomMap::ZOOM_CHANGED_FOR_HOST: {
ret.push_back(Java_HostZoomMapImpl_buildSiteZoomInfo(env, entry.host,
entry.zoom_level));
break;
}
case HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST:
NOTREACHED_IN_MIGRATION();
break;
case HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM:
NOTREACHED_IN_MIGRATION();
}
}
return ret;
}
#endif
double HostZoomMapImpl::GetZoomLevelForPreviewAndHost(const std::string& host) { … }
void HostZoomMapImpl::SetZoomLevelForPreviewAndHost(const std::string& host,
double level) { … }
}