chromium/content/browser/host_zoom_map_impl.cc

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "content/public/android/content_jni_headers/HostZoomMapImpl_jni.h"
#endif

namespace content {

namespace {

std::string GetHostFromProcessFrame(RenderFrameHostImpl* rfh) {}

}  // namespace

GURL HostZoomMap::GetURLFromEntry(NavigationEntry* entry) {}

// static
HostZoomMap* HostZoomMap::GetDefaultForBrowserContext(BrowserContext* context) {}

// static
HostZoomMap* HostZoomMap::Get(SiteInstance* instance) {}

// static
HostZoomMap* HostZoomMap::GetForWebContents(WebContents* contents) {}

// static
HostZoomMap* HostZoomMap::GetForStoragePartition(
    StoragePartition* storage_partition) {}

// Helper function for setting/getting zoom levels for WebContents without
// having to import HostZoomMapImpl everywhere.
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);

  // On Android, we will use a zoom level that considers the current OS-level
  // setting. For this we pass the given |level| through JNI to the Java-side
  // code, which can access the Android configuration and |fontScale|. This
  // method will return the adjusted zoom level considering OS settings. Note
  // that the OS |fontScale| will be factored in only when the Page Zoom feature
  // is enabled.
  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);  // IN-TEST
}

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();

  // We want to set and save the new zoom level, but we want to actually render
  // the adjusted level.
  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);

  // We must now remove this webcontents from the list of temporary zoom levels,
  // this is so that any future request will continue to update the underlying
  // host/scheme save, and will not be perceived as "temporary".
  // i.e. once temporary is set for a web_contents, the call to
  // SetZoomLevelForWebContents will keep updating what is rendered, but will no
  // longer call SetZoomLevelForHost, which saves the choice for that host.
  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 a callback has been set (e.g. by chrome_zoom_level_prefs to store an
  // updated value in Prefs), call this now with the chosen zoom level.
  if (host_zoom_map->GetDefaultZoomLevelPrefCallback()) {
    host_zoom_map->GetDefaultZoomLevelPrefCallback()->Run(
        new_default_zoom_level);
  }

  // Update the default zoom level for existing tabs. This must be done after
  // the Pref is updated due to guard clause in chrome_zoom_level_prefs.
  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;

  // Get instance of HostZoomMap.
  BrowserContext* context = BrowserContextFromJavaHandle(j_context);
  if (!context) {
    return ret;
  }

  HostZoomMap* host_zoom_map =
      HostZoomMap::GetDefaultForBrowserContext(context);

  // Convert C++ vector of structs to vector of objects.
  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) {}

}  // namespace content