Commit 0491cdb4 authored by Tommy Martino's avatar Tommy Martino Committed by Chromium LUCI CQ

[SH iOS] Add component code for disabling per-site

This adds a helper function that platforms can use to determine if the
current site's URL should prevent the Link to Text feature from being
offered. This will be called in a follow-up by platform-specific code
that enables/disables the option. For the time being, we expect the
number of sites affected to be very small and will reevaluate if the
list gets longer than ~a dozen.

Eventually we'd like to make this more dynamic (possibly delivering the
URLs and regexes from a server), but this version is meant to get a very
quick spot fix out the door.

Bug: 1157981
Change-Id: I1785459b298d93528415d8735aaa01c4d7a7769e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2587432Reviewed-by: default avatarSebastien Lalancette <seblalancette@chromium.org>
Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Commit-Queue: Tommy Martino <tmartino@chromium.org>
Cr-Commit-Position: refs/heads/master@{#842655}
parent ed15df01
......@@ -4,6 +4,8 @@
static_library("common") {
sources = [
"disabled_sites.cc",
"disabled_sites.h",
"shared_highlighting_metrics.cc",
"shared_highlighting_metrics.h",
"text_fragment.cc",
......@@ -19,12 +21,14 @@ static_library("common") {
"//components/search_engines:search_engine_utils",
"//services/metrics/public/cpp:metrics_cpp",
"//services/metrics/public/cpp:ukm_builders",
"//third_party/re2",
]
}
source_set("unit_tests") {
testonly = true
sources = [
"disabled_sites_unittest.cc",
"shared_highlighting_metrics_unittest.cc",
"text_fragment_unittest.cc",
"text_fragments_utils_unittest.cc",
......
......@@ -4,5 +4,6 @@ include_rules = [
"+components/ukm/test_ukm_recorder.h",
"+net/base",
"+services/metrics/public",
"+third_party/re2",
"+url",
]
\ No newline at end of file
// 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 "components/shared_highlighting/core/common/disabled_sites.h"
#include "third_party/re2/src/re2/re2.h"
#include <map>
#include <utility>
namespace shared_highlighting {
bool ShouldOfferLinkToText(const GURL& url) {
// If a URL's host matches a key in this map, then the path will be tested
// against the RE stored in the value. For example, {"foo.com", ".*"} means
// any page on the foo.com domain.
const static std::map<std::string, std::string> kBlocklist = {
{"youtube.com", ".*"},
// TODO(crbug.com/1157981): special case this to cover other Google TLDs
{"google.com", "^\\/amp\\/.*"}};
const std::string domain =
url.host().compare(0, 4, "www.") == 0 ? url.host().substr(4) : url.host();
auto it = kBlocklist.find(domain);
if (it != kBlocklist.end()) {
return !re2::RE2::FullMatch(url.path(), it->second);
}
return true;
}
} // namespace shared_highlighting
// 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 COMPONENTS_SHARED_HIGHLIGHTING_CORE_COMMON_DISABLED_SITES_H_
#define COMPONENTS_SHARED_HIGHLIGHTING_CORE_COMMON_DISABLED_SITES_H_
#include "url/gurl.h"
namespace shared_highlighting {
// Returns true iff Link to Text menu options should be enabled on this page.
// Uses a blocklist to identify certain sites where personalized or dynamic
// content make it unlikely that a generated URL will actually work when
// shared.
bool ShouldOfferLinkToText(const GURL& url);
} // namespace shared_highlighting
#endif // COMPONENTS_SHARED_HIGHLIGHTING_CORE_COMMON_DISABLED_SITES_H_
// 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 "components/shared_highlighting/core/common/disabled_sites.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace shared_highlighting {
namespace {
TEST(DisabledSitesTest, AllPaths) {
EXPECT_FALSE(ShouldOfferLinkToText(GURL("https://www.youtube.com")));
EXPECT_FALSE(ShouldOfferLinkToText(GURL("https://www.youtube.com/somepage")));
EXPECT_FALSE(ShouldOfferLinkToText(GURL("https://youtube.com")));
EXPECT_FALSE(ShouldOfferLinkToText(GURL("https://youtube.com/somepage")));
}
TEST(DisabledSitesTest, SpecificPages) {
// Paths starting with /amp/ are disabled.
EXPECT_FALSE(ShouldOfferLinkToText(GURL("https://www.google.com/amp/")));
EXPECT_FALSE(ShouldOfferLinkToText(GURL("https://www.google.com/amp/foo")));
EXPECT_FALSE(ShouldOfferLinkToText(GURL("https://google.com/amp/")));
EXPECT_FALSE(ShouldOfferLinkToText(GURL("https://google.com/amp/foo")));
// Other paths are not.
EXPECT_TRUE(ShouldOfferLinkToText(GURL("https://www.google.com")));
EXPECT_TRUE(ShouldOfferLinkToText(GURL("https://www.google.com/somepage")));
EXPECT_TRUE(ShouldOfferLinkToText(GURL("https://google.com")));
EXPECT_TRUE(ShouldOfferLinkToText(GURL("https://google.com/somepage")));
// Paths with /amp/ later on are also not affected.
EXPECT_TRUE(ShouldOfferLinkToText(GURL("https://google.com/foo/amp/")));
EXPECT_TRUE(ShouldOfferLinkToText(GURL("https://google.com/foo/amp/bar")));
}
TEST(DisabledSitesTest, NonMatchingHost) {
EXPECT_TRUE(ShouldOfferLinkToText(GURL("https://www.example.com")));
}
} // namespace
} // namespace shared_highlighting
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