Commit 5c28afb4 authored by Tarun Bansal's avatar Tarun Bansal Committed by Commit Bot

Include viewport-width client hint on main frame navigations on desktop

Bug: 821974
Change-Id: Icceeabb2e31c702341d898193f5ad6870b2d3f1e
Reviewed-on: https://chromium-review.googlesource.com/967095
Commit-Queue: Tarun Bansal <tbansal@chromium.org>
Reviewed-by: default avatarRyan Sturm <ryansturm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#543925}
parent f446ce58
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include <cmath>
#include "chrome/browser/client_hints/client_hints.h" #include "chrome/browser/client_hints/client_hints.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
...@@ -39,6 +41,27 @@ bool IsJavaScriptAllowed(Profile* profile, const GURL& url) { ...@@ -39,6 +41,27 @@ bool IsJavaScriptAllowed(Profile* profile, const GURL& url) {
std::string()) == CONTENT_SETTING_ALLOW; std::string()) == CONTENT_SETTING_ALLOW;
} }
// Returns the zoom factor for a given |url|.
double GetZoomFactor(content::BrowserContext* context, const GURL& url) {
// Android does not have the concept of zooming in like desktop.
#if defined(OS_ANDROID)
return 1.0;
#else
double zoom_level = content::HostZoomMap::GetDefaultForBrowserContext(context)
->GetZoomLevelForHostAndScheme(
url.scheme(), net::GetHostOrSpecFromURL(url));
if (zoom_level == 0.0) {
// Get default zoom level.
zoom_level = content::HostZoomMap::GetDefaultForBrowserContext(context)
->GetDefaultZoomLevel();
}
return content::ZoomLevelToZoomFactor(zoom_level);
#endif
}
} // namespace } // namespace
namespace client_hints { namespace client_hints {
...@@ -107,35 +130,35 @@ GetAdditionalNavigationRequestClientHintsHeaders( ...@@ -107,35 +130,35 @@ GetAdditionalNavigationRequestClientHintsHeaders(
->GetPrimaryDisplay() ->GetPrimaryDisplay()
.device_scale_factor(); .device_scale_factor();
} }
double zoom_factor = 1.0;
// Android does not have the concept of zooming in like desktop.
#if !defined(OS_ANDROID)
double zoom_level =
content::HostZoomMap::GetDefaultForBrowserContext(context)
->GetZoomLevelForHostAndScheme(url.scheme(),
net::GetHostOrSpecFromURL(url));
double default_zoom_level =
content::HostZoomMap::GetDefaultForBrowserContext(context)
->GetDefaultZoomLevel();
if (zoom_level <= 0.0)
zoom_level = default_zoom_level;
if (zoom_level > 0.0)
zoom_factor = content::ZoomLevelToZoomFactor(zoom_level);
#endif // !OS_ANDROID
DCHECK_LT(0.0, device_scale_factor); DCHECK_LT(0.0, device_scale_factor);
double zoom_factor = GetZoomFactor(context, url);
additional_headers->SetHeader( additional_headers->SetHeader(
blink::kClientHintsHeaderMapping[static_cast<int>( blink::kClientHintsHeaderMapping[static_cast<int>(
blink::mojom::WebClientHintsType::kDpr)], blink::mojom::WebClientHintsType::kDpr)],
base::NumberToString(device_scale_factor * zoom_factor)); base::NumberToString(device_scale_factor * zoom_factor));
} }
if (web_client_hints.IsEnabled(
blink::mojom::WebClientHintsType::kViewportWidth)) {
// TODO: https://crbug.com/821974: Viewport width client hint should be sent
// on non-Android main frame navigations as well.
#if !defined(OS_ANDROID)
double viewport_width = (display::Screen::GetScreen()
->GetPrimaryDisplay()
.GetSizeInPixel()
.width()) /
GetZoomFactor(context, url);
DCHECK_LT(0, viewport_width);
if (viewport_width > 0) {
additional_headers->SetHeader(
blink::kClientHintsHeaderMapping[static_cast<int>(
blink::mojom::WebClientHintsType::kViewportWidth)],
base::NumberToString(std::round(viewport_width)));
}
#endif // !OS_ANDROID
}
// Static assert that triggers if a new client hint header is added. If a new // Static assert that triggers if a new client hint header is added. If a new
// client hint header is added, the following assertion should be updated. // client hint header is added, the following assertion should be updated.
// If possible, logic should be added above so that the request headers for // If possible, logic should be added above so that the request headers for
......
HTTP/1.1 200 OK HTTP/1.1 200 OK
Accept-CH: dpr,device-memory Accept-CH: dpr,device-memory,viewport-width
Accept-CH-Lifetime: 3600 Accept-CH-Lifetime: 3600
\ No newline at end of file
HTTP/1.1 200 OK HTTP/1.1 200 OK
Accept-CH: dpr,device-memory Accept-CH: dpr,device-memory,viewport-width
\ No newline at end of file \ No newline at end of file
HTTP/1.1 200 OK HTTP/1.1 200 OK
Accept-CH: dpr,device-memory Accept-CH: dpr,device-memory,viewport-width
\ No newline at end of file \ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment