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

#include "chrome/browser/chromeos/tablet_mode/chrome_content_browser_client_tablet_mode_part.h"

#include <string_view>

#include "chrome/browser/ash/browser_delegate/browser_controller.h"
#include "chrome/browser/ash/browser_delegate/browser_delegate.h"
#include "chrome/browser/ash/browser_delegate/browser_type.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/ui/webui/ash/system_web_dialog/system_web_dialog_delegate.h"
#include "chrome/common/webui_url_constants.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "extensions/common/constants.h"
#include "third_party/blink/public/common/web_preferences/web_preferences.h"
#include "ui/display/screen.h"

namespace {

GURL GetURL(content::WebContents* contents) {
  content::NavigationEntry* entry = contents->GetController().GetVisibleEntry();
  return entry ? entry->GetURL() : GURL();
}

// Returns true if |contents| is of an internal pages (such as
// chrome://settings, chrome://extensions, ... etc).
bool IsInternalPage(content::WebContents* contents) {
  DCHECK(contents);

  GURL url = GetURL(contents);
  if (url.is_empty()) {
    // No entry has been committed. We don't know anything about this page, so
    // exclude it and let it have the default web prefs.
    return true;
  }

  Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
  if (profile && search::IsNTPOrRelatedURL(url, profile))
    return false;

  return url.SchemeIs(content::kChromeUIScheme);
}

void OverrideWebPreferencesForTabletMode(
    content::WebContents* contents,
    blink::web_pref::WebPreferences* web_prefs) {
  // Enable some mobile-like behaviors when in tablet mode on Chrome OS.
  if (!display::Screen::Get()->HasScreen() ||
      !display::Screen::Get()->InTabletMode()) {
    return;
  }

  // Do this only for webcontents displayed in browsers and are not of hosted
  // apps.
  ash::BrowserDelegate* browser =
      ash::BrowserController::GetInstance()->GetBrowserForTab(contents);
  if (!browser || browser->GetType() == ash::BrowserType::kApp ||
      browser->GetType() == ash::BrowserType::kAppPopup) {
    return;
  }

  if (IsInternalPage(contents)) {
    return;
  }

  web_prefs->double_tap_to_zoom_enabled = false;
  web_prefs->shrinks_viewport_contents_to_fit = true;
  web_prefs->main_frame_resizes_are_orientation_changes = true;
  web_prefs->default_minimum_page_scale_factor = 0.25f;
  web_prefs->default_maximum_page_scale_factor = 5.0;
}

// Returns true if the WebUI at |url| is considered "system UI" and should use
// the system font size (the default) instead of the browser font size.
// Takes a URL because the WebContents may not yet be associated with a window,
// SettingsWindowManager, etc.
bool UseDefaultFontSize(const GURL& url) {
  if (ash::SystemWebDialogDelegate::HasInstance(url))
    return true;

  if (url.SchemeIs(content::kChromeUIScheme))
    return chrome::IsSystemWebUIHost(url.host());

  if (url.SchemeIs(extensions::kExtensionScheme)) {
    std::string_view extension_id = url.host();
    return extension_misc::IsSystemUIApp(extension_id);
  }
  return false;
}

void OverrideFontSize(content::WebContents* contents,
                      blink::web_pref::WebPreferences* web_prefs) {
  DCHECK(contents);
  // Check the URL because |contents| may not yet be associated with a window,
  // SettingsWindowManager, etc.
  GURL url = GetURL(contents);
  if (!url.is_empty() && UseDefaultFontSize(url)) {
    // System dialogs are considered native UI, so they do not follow the
    // browser's web-page font sizes. Reset fonts to the base sizes.
    blink::web_pref::WebPreferences base_prefs;
    web_prefs->default_font_size = base_prefs.default_font_size;
    web_prefs->default_fixed_font_size = base_prefs.default_fixed_font_size;
  }
}

}  // namespace

ChromeContentBrowserClientTabletModePart::
    ChromeContentBrowserClientTabletModePart() = default;

ChromeContentBrowserClientTabletModePart::
    ~ChromeContentBrowserClientTabletModePart() = default;

void ChromeContentBrowserClientTabletModePart::OverrideWebPreferences(
    content::WebContents* web_contents,
    content::SiteInstance& main_frame_site,
    blink::web_pref::WebPreferences* web_prefs) {
  // A webcontents may not be the delegate of the render view host such as in
  // the case of interstitial pages.
  if (!web_contents)
    return;

  OverrideWebPreferencesForTabletMode(web_contents, web_prefs);
  OverrideFontSize(web_contents, web_prefs);
}

// static
bool ChromeContentBrowserClientTabletModePart::UseDefaultFontSizeForTest(
    const GURL& url) {
  return UseDefaultFontSize(url);
}
