Commit 233d3725 authored by rajendrant's avatar rajendrant Committed by Commit Bot

Refactor subresource redirect observer to use utility functions

This moves the common API used in subresource redirect web contents
observer to a separate utility file. These common function will be used
in the upcoming robots rules fetching and checking logic.

No change in behavior is expected.

Bug: 1147565
Change-Id: I46c13ab38a8290f24a8e7142f7a7354f08a8ba93
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2530391
Commit-Queue: rajendrant <rajendrant@chromium.org>
Reviewed-by: default avatarRobert Ogden <robertogden@chromium.org>
Cr-Commit-Position: refs/heads/master@{#826178}
parent 97f84cda
......@@ -1733,6 +1733,8 @@ static_library("browser") {
"subresource_redirect/https_image_compression_infobar_decider.h",
"subresource_redirect/subresource_redirect_observer.cc",
"subresource_redirect/subresource_redirect_observer.h",
"subresource_redirect/subresource_redirect_util.cc",
"subresource_redirect/subresource_redirect_util.h",
"sync/bookmark_sync_service_factory.cc",
"sync/bookmark_sync_service_factory.h",
"sync/chrome_sync_client.cc",
......
......@@ -4,14 +4,10 @@
#include "chrome/browser/subresource_redirect/subresource_redirect_observer.h"
#include "build/build_config.h"
#include "chrome/browser/data_reduction_proxy/data_reduction_proxy_chrome_settings.h"
#include "chrome/browser/data_reduction_proxy/data_reduction_proxy_chrome_settings_factory.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/subresource_redirect/https_image_compression_bypass_decider.h"
#include "chrome/browser/subresource_redirect/https_image_compression_infobar_decider.h"
#include "chrome/browser/subresource_redirect/subresource_redirect_util.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
#include "components/optimization_guide/proto/performance_hints_metadata.pb.h"
#include "content/public/browser/navigation_handle.h"
......@@ -20,14 +16,9 @@
#include "content/public/browser/web_contents.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/loader/previews_resource_loading_hints.mojom.h"
#include "url/gurl.h"
#if defined(OS_ANDROID)
#include "chrome/browser/previews/android/previews_android_bridge.h"
#endif
namespace subresource_redirect {
namespace {
......@@ -36,7 +27,7 @@ namespace {
// redirect feature are enabled.
optimization_guide::OptimizationGuideDecider*
GetOptimizationGuideDeciderFromWebContents(content::WebContents* web_contents) {
DCHECK(base::FeatureList::IsEnabled(blink::features::kSubresourceRedirect));
DCHECK(ShouldEnablePublicImageHintsBasedCompression());
if (!web_contents)
return nullptr;
......@@ -51,15 +42,6 @@ GetOptimizationGuideDeciderFromWebContents(content::WebContents* web_contents) {
return nullptr;
}
DataReductionProxyChromeSettings* GetDataReductionProxyChromeSettings(
content::WebContents* web_contents) {
DCHECK(base::FeatureList::IsEnabled(blink::features::kSubresourceRedirect));
if (!web_contents)
return nullptr;
return DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
web_contents->GetBrowserContext());
}
// Pass down the |images_hints| to |render_frame_host|.
void SetResourceLoadingImageHints(
content::RenderFrameHost* render_frame_host,
......@@ -74,48 +56,23 @@ void SetResourceLoadingImageHints(
}
}
// Should the subresource be redirected to its compressed version. This returns
// false if only coverage metrics need to be recorded and actual redirection
// should not happen.
bool ShouldCompressionServerRedirectSubresource() {
return base::FeatureList::IsEnabled(blink::features::kSubresourceRedirect) &&
base::GetFieldTrialParamByFeatureAsBool(
blink::features::kSubresourceRedirect,
"enable_subresource_server_redirect", true);
}
bool ShowInfoBarOnAndroid(content::WebContents* web_contents) {
#if defined(OS_ANDROID)
return PreviewsAndroidBridge::CreateHttpsImageCompressionInfoBar(
web_contents);
#endif
return true;
}
} // namespace
// static
void SubresourceRedirectObserver::MaybeCreateForWebContents(
content::WebContents* web_contents) {
if (!web_contents ||
!base::FeatureList::IsEnabled(blink::features::kSubresourceRedirect)) {
return;
if (ShouldEnablePublicImageHintsBasedCompression() &&
IsLiteModeEnabled(web_contents)) {
SubresourceRedirectObserver::CreateForWebContents(web_contents);
}
const auto* data_reduction_proxy_settings =
GetDataReductionProxyChromeSettings(web_contents);
if (!data_reduction_proxy_settings ||
!data_reduction_proxy_settings->IsDataReductionProxyEnabled()) {
return;
}
return SubresourceRedirectObserver::CreateForWebContents(web_contents);
}
// static
bool SubresourceRedirectObserver::IsHttpsImageCompressionApplied(
content::WebContents* web_contents) {
if (!ShouldCompressionServerRedirectSubresource()) {
if (!ShouldCompressRedirectSubresource())
return false;
}
SubresourceRedirectObserver* observer =
SubresourceRedirectObserver::FromWebContents(web_contents);
return observer && observer->is_https_image_compression_applied_;
......@@ -125,7 +82,7 @@ SubresourceRedirectObserver::SubresourceRedirectObserver(
content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
receivers_(web_contents, this) {
DCHECK(base::FeatureList::IsEnabled(blink::features::kSubresourceRedirect));
DCHECK(ShouldEnablePublicImageHintsBasedCompression());
auto* optimization_guide_decider =
GetOptimizationGuideDeciderFromWebContents(web_contents);
if (optimization_guide_decider) {
......@@ -139,41 +96,23 @@ SubresourceRedirectObserver::~SubresourceRedirectObserver() = default;
void SubresourceRedirectObserver::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
DCHECK(navigation_handle);
DCHECK(base::FeatureList::IsEnabled(blink::features::kSubresourceRedirect));
DCHECK(ShouldEnablePublicImageHintsBasedCompression());
if (!navigation_handle->IsInMainFrame() ||
!navigation_handle->HasCommitted() ||
navigation_handle->IsSameDocument()) {
return;
}
if (!GetDataReductionProxyChromeSettings(web_contents())
->IsDataReductionProxyEnabled()) {
return;
}
auto* https_image_compression_infobar_decider =
GetDataReductionProxyChromeSettings(web_contents())
->https_image_compression_infobar_decider();
if (!https_image_compression_infobar_decider ||
https_image_compression_infobar_decider->NeedToShowInfoBar()) {
if (https_image_compression_infobar_decider->CanShowInfoBar(
navigation_handle) &&
ShowInfoBarOnAndroid(web_contents())) {
https_image_compression_infobar_decider->SetUserHasSeenInfoBar();
}
// Do not enable image compression on this page.
if (!IsLiteModeEnabled(web_contents()))
return;
}
is_https_image_compression_applied_ = false;
if (!navigation_handle->GetURL().SchemeIsHTTPOrHTTPS())
return;
if (GetDataReductionProxyChromeSettings(web_contents())
->https_image_compression_bypass_decider()
->ShouldBypassNow()) {
if (!ShowInfoBarAndGetImageCompressionState(web_contents(),
navigation_handle))
return;
}
auto* optimization_guide_decider = GetOptimizationGuideDeciderFromWebContents(
navigation_handle->GetWebContents());
......@@ -200,8 +139,8 @@ void SubresourceRedirectObserver::OnResourceLoadingImageHintsReceived(
optimization_guide::OptimizationGuideDecision decision,
const optimization_guide::OptimizationMetadata& optimization_metadata) {
// Clear |is_https_image_compression_applied_| since it may be set to true
// when multiple navigations are starting and image hints is received for the
// first one.
// when multiple navigations are starting and image hints is received for
// the first one.
is_https_image_compression_applied_ = false;
content::RenderFrameHost* current_render_frame_host =
......@@ -234,9 +173,8 @@ void SubresourceRedirectObserver::OnResourceLoadingImageHintsReceived(
void SubresourceRedirectObserver::NotifyCompressedImageFetchFailed(
base::TimeDelta retry_after) {
GetDataReductionProxyChromeSettings(web_contents())
->https_image_compression_bypass_decider()
->NotifyCompressedImageFetchFailed(retry_after);
subresource_redirect::NotifyCompressedImageFetchFailed(web_contents(),
retry_after);
}
WEB_CONTENTS_USER_DATA_KEY_IMPL(SubresourceRedirectObserver)
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/subresource_redirect/subresource_redirect_util.h"
#include "build/build_config.h"
#include "chrome/browser/data_reduction_proxy/data_reduction_proxy_chrome_settings.h"
#include "chrome/browser/data_reduction_proxy/data_reduction_proxy_chrome_settings_factory.h"
#include "chrome/browser/subresource_redirect/https_image_compression_bypass_decider.h"
#include "chrome/browser/subresource_redirect/https_image_compression_infobar_decider.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
#include "content/public/browser/web_contents.h"
#include "third_party/blink/public/common/features.h"
#if defined(OS_ANDROID)
#include "chrome/browser/previews/android/previews_android_bridge.h"
#endif
namespace subresource_redirect {
namespace {
bool IsSubresourceRedirectEnabled() {
return base::FeatureList::IsEnabled(blink::features::kSubresourceRedirect);
}
DataReductionProxyChromeSettings* GetDataReductionProxyChromeSettings(
content::WebContents* web_contents) {
DCHECK(base::FeatureList::IsEnabled(blink::features::kSubresourceRedirect));
if (!web_contents)
return nullptr;
return DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
web_contents->GetBrowserContext());
}
bool ShowInfoBarOnAndroid(content::WebContents* web_contents) {
#if defined(OS_ANDROID)
return PreviewsAndroidBridge::CreateHttpsImageCompressionInfoBar(
web_contents);
#endif
return true;
}
} // namespace
bool IsLiteModeEnabled(content::WebContents* web_contents) {
if (!web_contents)
return false;
const auto* data_reduction_proxy_settings =
GetDataReductionProxyChromeSettings(web_contents);
return data_reduction_proxy_settings &&
data_reduction_proxy_settings->IsDataReductionProxyEnabled();
}
bool ShouldEnablePublicImageHintsBasedCompression() {
bool is_enabled = IsSubresourceRedirectEnabled() &&
base::GetFieldTrialParamByFeatureAsBool(
blink::features::kSubresourceRedirect,
"enable_public_image_hints_based_compression", true);
// Only one of the public image hints or login and robots based image
// compression should be active.
DCHECK(!is_enabled || !ShouldEnableLoginRobotsCheckedCompression());
return is_enabled;
}
bool ShouldEnableLoginRobotsCheckedCompression() {
bool is_enabled = IsSubresourceRedirectEnabled() &&
base::GetFieldTrialParamByFeatureAsBool(
blink::features::kSubresourceRedirect,
"enable_login_robots_based_compression", false);
// Only one of the public image hints or login and robots based image
// compression should be active.
DCHECK(!is_enabled || !ShouldEnablePublicImageHintsBasedCompression());
return is_enabled;
}
// Should the subresource be redirected to its compressed version. This returns
// false if only coverage metrics need to be recorded and actual redirection
// should not happen.
bool ShouldCompressRedirectSubresource() {
return base::FeatureList::IsEnabled(blink::features::kSubresourceRedirect) &&
base::GetFieldTrialParamByFeatureAsBool(
blink::features::kSubresourceRedirect,
"enable_subresource_server_redirect", true);
}
bool ShowInfoBarAndGetImageCompressionState(
content::WebContents* web_contents,
content::NavigationHandle* navigation_handle) {
auto* data_reduction_proxy_settings =
GetDataReductionProxyChromeSettings(web_contents);
if (!data_reduction_proxy_settings->IsDataReductionProxyEnabled()) {
return false;
}
if (data_reduction_proxy_settings->https_image_compression_bypass_decider()
->ShouldBypassNow()) {
return false;
}
auto* https_image_compression_infobar_decider =
data_reduction_proxy_settings->https_image_compression_infobar_decider();
if (!https_image_compression_infobar_decider ||
https_image_compression_infobar_decider->NeedToShowInfoBar()) {
if (https_image_compression_infobar_decider->CanShowInfoBar(
navigation_handle) &&
ShowInfoBarOnAndroid(web_contents)) {
https_image_compression_infobar_decider->SetUserHasSeenInfoBar();
}
// Do not enable image compression on this page.
return false;
}
return true;
}
void NotifyCompressedImageFetchFailed(content::WebContents* web_contents,
base::TimeDelta retry_after) {
GetDataReductionProxyChromeSettings(web_contents)
->https_image_compression_bypass_decider()
->NotifyCompressedImageFetchFailed(retry_after);
}
} // namespace subresource_redirect
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_SUBRESOURCE_REDIRECT_SUBRESOURCE_REDIRECT_UTIL_H_
#define CHROME_BROWSER_SUBRESOURCE_REDIRECT_SUBRESOURCE_REDIRECT_UTIL_H_
#include "base/macros.h"
#include "base/time/time.h"
namespace content {
class NavigationHandle;
class WebContents;
} // namespace content
namespace subresource_redirect {
// Returns whether LiteMode is enabled for the profile associated with the
// |web_contents|.
bool IsLiteModeEnabled(content::WebContents* web_contents);
// Returns if the public image hints based subresource compression is enabled.
bool ShouldEnablePublicImageHintsBasedCompression();
// Returns if the login and robots checks based subresource compression is
// enabled. This compresses non logged-in pages and subresources allowed by
// robots.txt rules.
bool ShouldEnableLoginRobotsCheckedCompression();
// Should the subresource be redirected to its compressed version. This returns
// false if only coverage metrics need to be recorded and actual redirection
// should not happen.
bool ShouldCompressRedirectSubresource();
// Returns whether image compression should be applied for this web_contents.
// Also shows an one-time InfoBar on Android if needed.
bool ShowInfoBarAndGetImageCompressionState(
content::WebContents* web_contents,
content::NavigationHandle* navigation_handle);
// Notifies to LiteMode that image compression fetch had failed.
void NotifyCompressedImageFetchFailed(content::WebContents* web_contents,
base::TimeDelta retry_after);
} // namespace subresource_redirect
#endif // CHROME_BROWSER_SUBRESOURCE_REDIRECT_SUBRESOURCE_REDIRECT_UTIL_H_
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