Commit 1e3e82ce authored by Sylvain Defresne's avatar Sylvain Defresne Committed by Commit Bot

Revert "Integrate Lite Page Server Previews with the Previews Stack"

This reverts commit 8bd3e513.

Reason for revert: identified as likely cause for flakyness of PreviewsLitePageServerBrowserTest.LitePagePreviewsNavigation by FindIt

Original change's description:
> Integrate Lite Page Server Previews with the Previews Stack
> 
> Adds Lite Page Server Previews to DetermineAllowedPreviews and
> DetermineCommitted. For now, the preview is always allowed if the
> feature is enabled and is committed if the URL matches.
> 
> This intentionally leaves an open question about whether more logic
> in the NavigationThrottle can/should be moved into the DeciderImpl.
> 
> The biggest part of this change is moving the URL checking logic to
> the previews component and adding additional helper functions there.
> 
> Bug: 898557
> Change-Id: Iccc0b672eeedbe23942f4d17f6afd214426372a9
> Reviewed-on: https://chromium-review.googlesource.com/c/1323800
> Commit-Queue: Robert Ogden <robertogden@chromium.org>
> Reviewed-by: Doug Arnett <dougarnett@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#607427}

TBR=dougarnett@chromium.org,robertogden@chromium.org

Change-Id: Iaa200e39f10086d95654bc7fb7485c66d3f53ed0
No-Presubmit: true
No-Tree-Checks: true
Bug: 898557,904830
Reviewed-on: https://chromium-review.googlesource.com/c/1333814Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Cr-Commit-Position: refs/heads/master@{#607613}
parent f3f15d41
......@@ -5126,6 +5126,13 @@ ChromeContentBrowserClient::DetermineCommittedPreviewsForURL(
data_reduction_proxy::ContentLoFiDecider::
DetermineCommittedServerPreviewsState(drp_data, initial_state);
// Check if the current URL indicates that we redirected to HTTPS server
// previews.
if (PreviewsLitePageNavigationThrottle::GetOriginalURL(
url, nullptr /* original_url */)) {
return previews_state & content::LITE_PAGE_REDIRECT_ON;
}
// Check the various other client previews types.
return previews::DetermineCommittedClientPreviewsState(
previews_user_data, url, previews_state, previews_decider);
......
......@@ -7,10 +7,10 @@
#include <memory>
#include "base/android/jni_android.h"
#include "chrome/browser/previews/previews_lite_page_navigation_throttle.h"
#include "chrome/browser/previews/previews_ui_tab_helper.h"
#include "components/previews/content/previews_user_data.h"
#include "components/previews/core/previews_experiments.h"
#include "components/previews/core/previews_lite_page_url_handler.h"
#include "content/public/browser/web_contents.h"
#include "jni/PreviewsAndroidBridge_jni.h"
......@@ -56,8 +56,8 @@ PreviewsAndroidBridge::GetLitePageRedirectOriginalURL(
const base::android::JavaParamRef<jstring>& j_visible_url) {
GURL visible_url(base::android::ConvertJavaStringToUTF16(env, j_visible_url));
std::string original_url;
if (previews::ExtractOriginalURLFromLitePageRedirectURL(visible_url,
&original_url)) {
if (PreviewsLitePageNavigationThrottle::GetOriginalURL(visible_url,
&original_url)) {
return base::android::ScopedJavaLocalRef<jstring>(
base::android::ConvertUTF8ToJavaString(env, original_url));
}
......
......@@ -26,23 +26,17 @@
#include "chrome/browser/previews/previews_lite_page_navigation_throttle.h"
#include "chrome/browser/previews/previews_service.h"
#include "chrome/browser/previews/previews_service_factory.h"
#include "chrome/browser/previews/previews_ui_tab_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config_service_client_test_utils.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"
#include "components/data_reduction_proxy/proto/data_store.pb.h"
#include "components/infobars/core/confirm_infobar_delegate.h"
#include "components/infobars/core/infobar.h"
#include "components/prefs/pref_service.h"
#include "components/previews/content/previews_user_data.h"
#include "components/previews/core/previews_experiments.h"
#include "components/previews/core/previews_features.h"
#include "components/previews/core/previews_lite_page_url_handler.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/page_type.h"
......@@ -53,7 +47,6 @@
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "services/network/test/test_network_quality_tracker.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"
class PreviewsLitePageServerBrowserTest : public InProcessBrowserTest {
......@@ -231,17 +224,6 @@ class PreviewsLitePageServerBrowserTest : public InProcessBrowserTest {
// this validation to flake. Waiting for the load stop on the page will
// ensure that the Virtual URL has been set.
content::WaitForLoadStop(GetWebContents());
base::RunLoop().RunUntilIdle();
PreviewsUITabHelper* ui_tab_helper =
PreviewsUITabHelper::FromWebContents(GetWebContents());
EXPECT_TRUE(ui_tab_helper->displayed_preview_ui());
previews::PreviewsUserData* previews_data =
ui_tab_helper->previews_user_data();
EXPECT_TRUE(previews_data->HasCommittedPreviewsType());
EXPECT_EQ(previews_data->committed_previews_type(),
previews::PreviewsType::LITE_PAGE_REDIRECT);
const GURL loaded_url = GetLoadedURL();
const GURL previews_host = previews_server();
......@@ -273,17 +255,6 @@ class PreviewsLitePageServerBrowserTest : public InProcessBrowserTest {
// this validation to flake. Waiting for the load stop on the page will
// ensure that the Virtual URL has been set.
content::WaitForLoadStop(GetWebContents());
base::RunLoop().RunUntilIdle();
PreviewsUITabHelper* ui_tab_helper =
PreviewsUITabHelper::FromWebContents(GetWebContents());
EXPECT_FALSE(ui_tab_helper->displayed_preview_ui());
previews::PreviewsUserData* previews_data =
ui_tab_helper->previews_user_data();
EXPECT_FALSE(previews_data->HasCommittedPreviewsType());
EXPECT_NE(previews_data->committed_previews_type(),
previews::PreviewsType::LITE_PAGE_REDIRECT);
const GURL loaded_url = GetLoadedURL();
const GURL previews_host = previews_server();
......@@ -441,7 +412,7 @@ class PreviewsLitePageServerBrowserTest : public InProcessBrowserTest {
std::string original_url_str;
// Ignore anything that's not a previews request with an unused status.
if (!previews::ExtractOriginalURLFromLitePageRedirectURL(
if (!PreviewsLitePageNavigationThrottle::GetOriginalURL(
request.GetURL(), &original_url_str)) {
response->set_code(net::HttpStatusCode::HTTP_BAD_REQUEST);
return response;
......@@ -1108,22 +1079,13 @@ IN_PROC_BROWSER_TEST_F(
VerifyPreviewNotLoaded();
ClearDeciderState();
ASSERT_EQ(1U, GetInfoBarService()->infobar_count());
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_LITE_PAGE_PREVIEWS_MESSAGE),
static_cast<ConfirmInfoBarDelegate*>(
GetInfoBarService()->infobar_at(0)->delegate())
->GetMessageText());
EXPECT_EQ(1U, GetInfoBarService()->infobar_count());
histogram_tester.ExpectBucketCount(
"Previews.ServerLitePage.IneligibleReasons",
PreviewsLitePageNavigationThrottle::IneligibleReason::kInfoBarNotSeen, 1);
ui_test_utils::NavigateToURL(browser(), HttpsLitePageURL(kSuccess));
// Expect the "Saved Data" InfoBar.
ASSERT_EQ(1U, GetInfoBarService()->infobar_count());
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_SAVED_DATA_TITLE),
static_cast<ConfirmInfoBarDelegate*>(
GetInfoBarService()->infobar_at(0)->delegate())
->GetMessageText());
EXPECT_EQ(0U, GetInfoBarService()->infobar_count());
VerifyPreviewLoaded();
}
......
......@@ -188,7 +188,7 @@ PreviewsLitePageDecider::MaybeCreateThrottleFor(
previews_service->previews_lite_page_decider();
DCHECK(decider);
// TODO(crbug/898557): Replace this logic with PreviewsState.
// TODO(crbug/842233): Replace this logic with PreviewsState.
bool drp_enabled = decider->drp_settings_->IsDataReductionProxyEnabled();
bool preview_enabled = previews::params::ArePreviewsAllowed() &&
previews::params::IsLitePageServerPreviewsEnabled();
......
......@@ -25,7 +25,6 @@
#include "components/base32/base32.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_headers.h"
#include "components/previews/core/previews_experiments.h"
#include "components/previews/core/previews_lite_page_url_handler.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
......@@ -52,6 +51,12 @@ constexpr char kChromeProxyHeader[] = "chrome-proxy";
const base::TimeDelta kBlacklistDuration = base::TimeDelta::FromDays(30);
bool IsPreviewsDomain(const GURL& url) {
GURL previews_host = previews::params::GetLitePagePreviewsDomainURL();
return url.DomainIs(previews_host.host()) &&
url.EffectiveIntPort() == previews_host.EffectiveIntPort();
}
bool IsPrivateDomain(const GURL& url) {
if (url.host().find(".") == base::StringPiece::npos)
return true;
......@@ -155,15 +160,14 @@ bool HandlePreviewsLitePageURLRewrite(
content::BrowserContext* browser_context) {
// Don't change the |url|, just register our interest in reversing it before
// it is displayed to the user in |HandlePreviewsLitePageURLRewriteReverse|.
return previews::IsLitePageRedirectPreviewURL(*url);
return !!PreviewsLitePageNavigationThrottle::GetOriginalURL(*url, nullptr);
}
bool HandlePreviewsLitePageURLRewriteReverse(
GURL* url,
content::BrowserContext* browser_context) {
std::string original_url;
if (previews::ExtractOriginalURLFromLitePageRedirectURL(*url,
&original_url)) {
if (PreviewsLitePageNavigationThrottle::GetOriginalURL(*url, &original_url)) {
*url = GURL(original_url);
return true;
}
......@@ -217,7 +221,7 @@ bool PreviewsLitePageNavigationThrottle::IsEligibleForPreview() const {
// Check dynamic blacklists.
std::vector<BlacklistReason> blacklist_reasons;
if (previews::IsLitePageRedirectPreviewDomain(url))
if (IsPreviewsDomain(url))
blacklist_reasons.push_back(BlacklistReason::kNavigationToPreviewsDomain);
if (IsPrivateDomain(url))
......@@ -254,6 +258,25 @@ bool PreviewsLitePageNavigationThrottle::IsEligibleForPreview() const {
return true;
}
// static
bool PreviewsLitePageNavigationThrottle::GetOriginalURL(
const GURL& url,
std::string* original_url) {
if (!url.is_valid())
return false;
if (!IsPreviewsDomain(url))
return false;
std::string original_url_query_param;
if (!net::GetValueForKeyInQuery(url, "u", &original_url_query_param))
return false;
if (original_url)
*original_url = original_url_query_param;
return true;
}
// static
GURL PreviewsLitePageNavigationThrottle::GetPreviewsURLForURL(
const GURL& original_url) {
......@@ -284,8 +307,7 @@ GURL PreviewsLitePageNavigationThrottle::GetPreviewsURLForURL(
}
GURL PreviewsLitePageNavigationThrottle::GetPreviewsURL() const {
DCHECK(!previews::IsLitePageRedirectPreviewDomain(
navigation_handle()->GetURL()));
DCHECK(!IsPreviewsDomain(navigation_handle()->GetURL()));
return GetPreviewsURLForURL(navigation_handle()->GetURL());
}
......@@ -395,8 +417,7 @@ PreviewsLitePageNavigationThrottle::MaybeNavigateToPreview() const {
std::string original_url;
if (navigation_handle()->GetReloadType() ==
content::ReloadType::ORIGINAL_REQUEST_URL &&
previews::ExtractOriginalURLFromLitePageRedirectURL(
navigation_handle()->GetURL(), &original_url)) {
GetOriginalURL(navigation_handle()->GetURL(), &original_url)) {
LoadAndBypass(navigation_handle()->GetWebContents(), manager_,
MakeOpenURLParams(navigation_handle(), GURL(original_url),
std::string()),
......@@ -434,8 +455,7 @@ PreviewsLitePageNavigationThrottle::WillRedirectRequest() {
// If we are redirecting on a preview, count some UMA and proceed.
std::string original_url;
if (previews::ExtractOriginalURLFromLitePageRedirectURL(previous_url,
&original_url)) {
if (GetOriginalURL(previous_url, &original_url)) {
// A redirect means one of two things: (1) there is no preview available for
// this page and we should redirect back to the original page. (2) the
// previews server is forwarding along a redirect from the origin. The
......@@ -481,10 +501,8 @@ PreviewsLitePageNavigationThrottle::WillRedirectRequest() {
content::NavigationThrottle::ThrottleCheckResult
PreviewsLitePageNavigationThrottle::WillFailRequest() {
std::string original_url;
if (!previews::ExtractOriginalURLFromLitePageRedirectURL(
navigation_handle()->GetURL(), &original_url)) {
if (!GetOriginalURL(navigation_handle()->GetURL(), &original_url))
return content::NavigationThrottle::PROCEED;
}
UMA_HISTOGRAM_ENUMERATION("Previews.ServerLitePage.ServerResponse",
ServerResponse::kFailed);
......@@ -502,8 +520,7 @@ PreviewsLitePageNavigationThrottle::WillFailRequest() {
content::NavigationThrottle::ThrottleCheckResult
PreviewsLitePageNavigationThrottle::WillProcessResponse() {
std::string original_url;
if (!previews::ExtractOriginalURLFromLitePageRedirectURL(
navigation_handle()->GetURL(), &original_url)) {
if (!GetOriginalURL(navigation_handle()->GetURL(), &original_url)) {
// Return early if this request was not for a Preview.
return content::NavigationThrottle::PROCEED;
}
......
......@@ -99,6 +99,11 @@ class PreviewsLitePageNavigationThrottle : public content::NavigationThrottle {
~PreviewsLitePageNavigationThrottle() override;
// Attempts to extract the original URL from the given Previews URL. Returns
// false if |url| is not a valid Preview URL. It is ok to pass nullptr for
// |original_url| if you only want the boolean return value.
static bool GetOriginalURL(const GURL& url, std::string* original_url);
// Returns the URL for a preview given by the url.
static GURL GetPreviewsURLForURL(const GURL& original_url);
......
......@@ -100,3 +100,44 @@ TEST(PreviewsLitePageNavigationThrottleTest, TestGetPreviewsURL) {
GURL(test_case.previews_url));
}
}
TEST(PreviewsLitePageNavigationThrottleTest, TestGetOriginalURL) {
struct TestCase {
std::string previews_host;
std::string original_url;
std::string previews_url;
bool want_ok;
};
const TestCase kTestCases[]{
// Use https://play.golang.org/p/HUM2HxmUTOW to compute |previews_url|.
{
"https://previews.host.com",
"https://original.host.com/path/path/path?query=yes",
"https://shta44dh4bi7rc6fnpjnkrtytwlabygjhk53v2trlot2wddylwua."
"previews.host.com/p?u="
"https%3A%2F%2Foriginal.host.com%2Fpath%2Fpath%2Fpath%3Fquery%3Dyes",
true,
},
{
"https://previews.host.com",
"http://original.host.com/path/path/path?query=yes",
"https://6p7dar4ju6r4ynz7x3pucmlcltuqsf7z5auhvckzln7voglkt56q."
"previews.host.com/p?u="
"http%3A%2F%2Foriginal.host.com%2Fpath%2Fpath%2Fpath%3Fquery%3Dyes",
true,
},
};
for (const TestCase& test_case : kTestCases) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeatureWithParameters(
previews::features::kLitePageServerPreviews,
{{"previews_host", test_case.previews_host}});
std::string original_url;
bool got_ok = PreviewsLitePageNavigationThrottle::GetOriginalURL(
GURL(test_case.previews_url), &original_url);
EXPECT_EQ(got_ok, test_case.want_ok);
EXPECT_EQ(original_url, test_case.original_url);
}
}
......@@ -123,7 +123,6 @@ void PreviewsUITabHelper::ShowUIElement(
#if defined(OS_ANDROID)
if (previews::params::IsPreviewsOmniboxUiEnabled()) {
displayed_preview_ui_ = true;
should_display_android_omnibox_badge_ = true;
return;
}
......
......@@ -174,7 +174,7 @@ TEST_F(PreviewsUITabHelperUnitTest, DidFinishNavigationDisplaysOmniboxBadge) {
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(ui_tab_helper->should_display_android_omnibox_badge());
EXPECT_TRUE(ui_tab_helper->displayed_preview_ui());
EXPECT_FALSE(ui_tab_helper->displayed_preview_ui());
EXPECT_EQ(0U, infobar_service()->infobar_count());
}
#endif
......
......@@ -6,7 +6,6 @@
#include "base/metrics/histogram_macros.h"
#include "components/previews/content/previews_user_data.h"
#include "components/previews/core/previews_lite_page_url_handler.h"
namespace previews {
......@@ -53,15 +52,6 @@ content::PreviewsState DetermineAllowedClientPreviewsState(
if (!is_data_saver_user)
return previews_state;
// Check for client-side previews in precedence order.
// Note: this is for the beginning of navigation so we should not
// check for https here (since an http request may redirect to https).
if (previews_decider->ShouldAllowPreviewAtNavigationStart(
previews_data, url, is_reload,
previews::PreviewsType::LITE_PAGE_REDIRECT)) {
previews_state |= content::LITE_PAGE_REDIRECT_ON;
}
if (previews_decider->ShouldAllowPreviewAtNavigationStart(
previews_data, url, is_reload,
previews::PreviewsType::RESOURCE_LOADING_HINTS)) {
......@@ -70,6 +60,9 @@ content::PreviewsState DetermineAllowedClientPreviewsState(
previews_decider->LoadResourceHints(url);
}
// Check for client-side previews in precedence order.
// Note: this is for the beginning of navigation so we should not
// check for https here (since an http request may redirect to https).
if (previews_decider->ShouldAllowPreviewAtNavigationStart(
previews_data, url, is_reload, previews::PreviewsType::NOSCRIPT)) {
previews_state |= content::NOSCRIPT_ON;
......@@ -122,14 +115,6 @@ content::PreviewsState DetermineCommittedClientPreviewsState(
return content::PREVIEWS_OFF;
}
// Check if a LITE_PAGE_REDIRECT preview was actually served.
if (previews_state & content::LITE_PAGE_REDIRECT_ON) {
if (IsLitePageRedirectPreviewURL(url))
return content::LITE_PAGE_REDIRECT_ON;
previews_state &= ~content::LITE_PAGE_REDIRECT_ON;
}
DCHECK(!IsLitePageRedirectPreviewURL(url));
// Make priority decision among allowed client preview types that can be
// decided at Commit time.
if (previews_state & content::RESOURCE_LOADING_HINTS_ON) {
......@@ -174,8 +159,6 @@ previews::PreviewsType GetMainFramePreviewsType(
// The order is important here.
if (previews_state & content::OFFLINE_PAGE_ON)
return previews::PreviewsType::OFFLINE;
if (previews_state & content::LITE_PAGE_REDIRECT_ON)
return previews::PreviewsType::LITE_PAGE_REDIRECT;
if (previews_state & content::SERVER_LITE_PAGE_ON)
return previews::PreviewsType::LITE_PAGE;
if (previews_state & content::SERVER_LOFI_ON)
......
......@@ -226,34 +226,6 @@ TEST_F(PreviewsContentUtilTest,
is_data_saver_user, enabled_previews_decider()));
}
TEST_F(PreviewsContentUtilTest,
DetermineAllowedClientPreviewsStateLitePageRedirect) {
// Enable both Client LoFi and NoScript.
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitFromCommandLine("Previews,LitePageServerPreviews",
std::string());
PreviewsUserData user_data(1);
bool is_reload = false;
bool is_redirect = false;
bool is_data_saver_user = true;
// Verify preview is enabled on HTTP and HTTPS.
EXPECT_TRUE(content::LITE_PAGE_REDIRECT_ON &
previews::DetermineAllowedClientPreviewsState(
&user_data, GURL("https://www.google.com"), is_reload,
is_redirect, is_data_saver_user, enabled_previews_decider()));
EXPECT_TRUE(content::LITE_PAGE_REDIRECT_ON &
previews::DetermineAllowedClientPreviewsState(
&user_data, GURL("http://www.google.com"), is_reload,
is_redirect, is_data_saver_user, enabled_previews_decider()));
// Verify non-HTTP[S] URL has no previews enabled.
EXPECT_EQ(content::PREVIEWS_UNSPECIFIED,
previews::DetermineAllowedClientPreviewsState(
&user_data, GURL("data://someblob"), is_reload, is_redirect,
is_data_saver_user, enabled_previews_decider()));
}
TEST_F(PreviewsContentUtilTest, DetermineCommittedClientPreviewsState) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitFromCommandLine(
......@@ -269,25 +241,6 @@ TEST_F(PreviewsContentUtilTest, DetermineCommittedClientPreviewsState) {
content::CLIENT_LOFI_ON | content::NOSCRIPT_ON,
enabled_previews_decider()));
content::PreviewsState lite_page_redirect_enabled =
content::CLIENT_LOFI_ON | content::NOSCRIPT_ON |
content::RESOURCE_LOADING_HINTS_ON | content::LITE_PAGE_REDIRECT_ON;
// LITE_PAGE_REDIRECT takes precedence over NoScript, Resource Loading Hints,
// and Client LoFi when the committed URL is for the lite page previews
// server.
EXPECT_EQ(
content::LITE_PAGE_REDIRECT_ON,
previews::DetermineCommittedClientPreviewsState(
&user_data, GURL("https://litepages.googlezip.net/?u=google.com"),
lite_page_redirect_enabled, enabled_previews_decider()));
// Verify LITE_PAGE_REDIRECT_ON not committed for non-lite-page-sever URL.
EXPECT_NE(content::LITE_PAGE_REDIRECT_ON,
previews::DetermineCommittedClientPreviewsState(
&user_data, GURL("https://www.google.com"),
lite_page_redirect_enabled, enabled_previews_decider()));
// NoScript has precedence over Client LoFi - kept for committed HTTPS:
EXPECT_EQ(content::NOSCRIPT_ON,
previews::DetermineCommittedClientPreviewsState(
......@@ -352,8 +305,6 @@ TEST_F(PreviewsContentUtilTest, GetMainFramePreviewsType) {
previews::GetMainFramePreviewsType(content::RESOURCE_LOADING_HINTS_ON));
EXPECT_EQ(previews::PreviewsType::LOFI,
previews::GetMainFramePreviewsType(content::CLIENT_LOFI_ON));
EXPECT_EQ(previews::PreviewsType::LITE_PAGE_REDIRECT,
previews::GetMainFramePreviewsType(content::LITE_PAGE_REDIRECT_ON));
// NONE cases:
EXPECT_EQ(previews::PreviewsType::NONE,
......@@ -380,11 +331,6 @@ TEST_F(PreviewsContentUtilTest, GetMainFramePreviewsType) {
previews::GetMainFramePreviewsType(
content::NOSCRIPT_ON | content::CLIENT_LOFI_ON |
content::RESOURCE_LOADING_HINTS_ON));
EXPECT_EQ(
previews::PreviewsType::LITE_PAGE_REDIRECT,
previews::GetMainFramePreviewsType(
content::NOSCRIPT_ON | content::CLIENT_LOFI_ON |
content::RESOURCE_LOADING_HINTS_ON | content::LITE_PAGE_REDIRECT_ON));
}
} // namespace
......
......@@ -15,8 +15,6 @@ static_library("core") {
"previews_experiments.h",
"previews_features.cc",
"previews_features.h",
"previews_lite_page_url_handler.cc",
"previews_lite_page_url_handler.h",
"previews_logger.cc",
"previews_logger.h",
"previews_logger_observer.h",
......@@ -60,7 +58,6 @@ source_set("unit_tests") {
"host_filter_unittest.cc",
"previews_black_list_unittest.cc",
"previews_experiments_unittest.cc",
"previews_lite_page_url_handler_unittest.cc",
"previews_logger_unittest.cc",
]
......
// Copyright 2018 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 "components/previews/core/previews_lite_page_url_handler.h"
#include "components/previews/core/previews_experiments.h"
#include "net/base/url_util.h"
namespace previews {
bool IsLitePageRedirectPreviewDomain(const GURL& url) {
if (!url.is_valid())
return false;
GURL previews_host = params::GetLitePagePreviewsDomainURL();
return url.DomainIs(previews_host.host()) &&
url.EffectiveIntPort() == previews_host.EffectiveIntPort();
}
bool IsLitePageRedirectPreviewURL(const GURL& url) {
return ExtractOriginalURLFromLitePageRedirectURL(url, nullptr);
}
bool ExtractOriginalURLFromLitePageRedirectURL(const GURL& url,
std::string* original_url) {
if (!IsLitePageRedirectPreviewDomain(url))
return false;
std::string original_url_query_param;
if (!net::GetValueForKeyInQuery(url, "u", &original_url_query_param))
return false;
if (original_url)
*original_url = original_url_query_param;
return true;
}
} // namespace previews
// Copyright 2018 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 COMPONENTS_PREVIEWS_CORE_PREVIEWS_LITE_PAGE_URL_HANDLER_H_
#define COMPONENTS_PREVIEWS_CORE_PREVIEWS_LITE_PAGE_URL_HANDLER_H_
#include <string>
#include "url/gurl.h"
namespace previews {
// Returns true if the given |url| has the same domain as the lite page previews
// server.
bool IsLitePageRedirectPreviewDomain(const GURL& url);
// Returns true if the given URL is a Lite Page Preview URL. This does more
// checking than |IsLitePageRedirectPreviewDomain| so be sure to use the right
// one.
bool IsLitePageRedirectPreviewURL(const GURL& url);
// Attempts to extract the original URL from the given Previews URL. Returns
// false if |url| is not a valid Preview URL.
bool ExtractOriginalURLFromLitePageRedirectURL(const GURL& url,
std::string* original_url);
} // namespace previews
#endif // COMPONENTS_PREVIEWS_CORE_PREVIEWS_LITE_PAGE_URL_HANDLER_H_
// Copyright 2018 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 "components/previews/core/previews_lite_page_url_handler.h"
#include <memory>
#include "base/test/scoped_feature_list.h"
#include "components/previews/core/previews_features.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace previews {
TEST(PreviewsLitePageURLHandlerTest,
TestExtractOriginalURLFromLitePageRedirectURL) {
struct TestCase {
std::string previews_host;
std::string original_url;
std::string previews_url;
bool want_ok;
};
const TestCase kTestCases[]{
// Use https://play.golang.org/p/HUM2HxmUTOW to compute |previews_url|.
{
"https://previews.host.com",
"https://original.host.com/path/path/path?query=yes",
"https://shta44dh4bi7rc6fnpjnkrtytwlabygjhk53v2trlot2wddylwua."
"previews.host.com/p?u="
"https%3A%2F%2Foriginal.host.com%2Fpath%2Fpath%2Fpath%3Fquery%3Dyes",
true,
},
{
"https://previews.host.com",
"http://original.host.com/path/path/path?query=yes",
"https://6p7dar4ju6r4ynz7x3pucmlcltuqsf7z5auhvckzln7voglkt56q."
"previews.host.com/p?u="
"http%3A%2F%2Foriginal.host.com%2Fpath%2Fpath%2Fpath%3Fquery%3Dyes",
true,
},
};
for (const TestCase& test_case : kTestCases) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeatureWithParameters(
features::kLitePageServerPreviews,
{{"previews_host", test_case.previews_host}});
std::string original_url;
bool got_ok = ExtractOriginalURLFromLitePageRedirectURL(
GURL(test_case.previews_url), &original_url);
EXPECT_EQ(got_ok, test_case.want_ok);
EXPECT_EQ(original_url, test_case.original_url);
}
}
} // namespace previews
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