Commit 1b1dc993 authored by Marc Treib's avatar Marc Treib Committed by Commit Bot

Cleanup: Merge c/c/search/search_urls.h/cc into c/b/search/search.cc

The functions there are only used within search.cc. They're also weird
and shouldn't be used by anyone, so as a first step, let's make them as
invisible as possible.

Bug: 604771
Change-Id: I23e373437b0e7882fbcc1de5a83321f2f26903d2
Reviewed-on: https://chromium-review.googlesource.com/800551Reviewed-by: default avatarChris Pickel <sfiera@chromium.org>
Commit-Queue: Marc Treib <treib@chromium.org>
Cr-Commit-Position: refs/heads/master@{#520915}
parent d23ef9a2
......@@ -17,7 +17,6 @@
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/search/search_urls.h"
#include "chrome/common/url_constants.h"
#include "components/google/core/browser/google_util.h"
#include "components/prefs/pref_service.h"
......@@ -28,6 +27,7 @@
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "url/gurl.h"
#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
#include "chrome/browser/supervised_user/supervised_user_service.h"
......@@ -48,6 +48,32 @@ namespace search {
namespace {
const char kServiceWorkerFileName[] = "newtab-serviceworker.js";
bool MatchesOrigin(const GURL& my_url, const GURL& other_url) {
return my_url.host_piece() == other_url.host_piece() &&
my_url.port() == other_url.port() &&
(my_url.scheme_piece() == other_url.scheme_piece() ||
(my_url.SchemeIs(url::kHttpsScheme) &&
other_url.SchemeIs(url::kHttpScheme)));
}
} // namespace
// Returns true if |my_url| matches |other_url| in terms of origin (i.e. host,
// port, and scheme) and path. As a special case, this also matches if |my_url|
// is "https://" and |other_url| is "http://" (and host, port, and path match).
// This is so that "https://" URLs will be considered "Instant URLs" even if the
// default search provider URL is "http://".
// Defined outside of the anonymous namespace so that it's accessible to unit
// tests.
bool MatchesOriginAndPath(const GURL& my_url, const GURL& other_url) {
return MatchesOrigin(my_url, other_url) &&
my_url.path_piece() == other_url.path_piece();
}
namespace {
// Status of the New Tab URL for the default Search provider. NOTE: Used in a
// UMA histogram so values should only be added at the end and not reordered.
enum NewTabURLState {
......@@ -86,6 +112,28 @@ const TemplateURL* GetDefaultSearchProviderTemplateURL(Profile* profile) {
return nullptr;
}
bool IsMatchingServiceWorker(const GURL& my_url, const GURL& document_url) {
// The origin should match.
if (!MatchesOrigin(my_url, document_url))
return false;
// The url filename should be the new tab page ServiceWorker.
std::string my_filename = my_url.ExtractFileName();
if (my_filename != kServiceWorkerFileName)
return false;
// The paths up to the filenames should be the same.
std::string my_path_without_filename = my_url.path();
my_path_without_filename = my_path_without_filename.substr(
0, my_path_without_filename.length() - my_filename.length());
std::string document_filename = document_url.ExtractFileName();
std::string document_path_without_filename = document_url.path();
document_path_without_filename = document_path_without_filename.substr(
0, document_path_without_filename.length() - document_filename.length());
return my_path_without_filename == document_path_without_filename;
}
// Returns true if |url| matches the NTP URL or the URL of the NTP's associated
// service worker.
bool IsNTPOrServiceWorkerURL(const GURL& url, Profile* profile) {
......
......@@ -40,6 +40,27 @@
namespace search {
bool MatchesOriginAndPath(const GURL& my_url, const GURL& other_url);
TEST(SearchURLsTest, MatchesOriginAndPath) {
EXPECT_TRUE(MatchesOriginAndPath(GURL("http://example.com/path"),
GURL("http://example.com/path?param")));
EXPECT_FALSE(MatchesOriginAndPath(GURL("http://not.example.com/path"),
GURL("http://example.com/path")));
EXPECT_TRUE(MatchesOriginAndPath(GURL("http://example.com:80/path"),
GURL("http://example.com/path")));
EXPECT_FALSE(MatchesOriginAndPath(GURL("http://example.com:8080/path"),
GURL("http://example.com/path")));
EXPECT_FALSE(MatchesOriginAndPath(GURL("ftp://example.com/path"),
GURL("http://example.com/path")));
EXPECT_FALSE(MatchesOriginAndPath(GURL("http://example.com/path"),
GURL("https://example.com/path")));
EXPECT_TRUE(MatchesOriginAndPath(GURL("https://example.com/path"),
GURL("http://example.com/path")));
EXPECT_FALSE(MatchesOriginAndPath(GURL("http://example.com/path"),
GURL("http://example.com/another-path")));
}
class SearchTest : public BrowserWithTestWindowTest {
protected:
void SetUp() override {
......
......@@ -13,7 +13,6 @@
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/common/search/search_urls.h"
#include "chrome/common/url_constants.h"
#include "components/browser_sync/profile_sync_service.h"
#include "components/ntp_tiles/metrics.h"
......
......@@ -171,8 +171,6 @@ static_library("common") {
"search/instant_types.cc",
"search/instant_types.h",
"search/ntp_logging_events.h",
"search/search_urls.cc",
"search/search_urls.h",
"secure_origin_whitelist.cc",
"secure_origin_whitelist.h",
"ssl_insecure_content.cc",
......
// Copyright 2013 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/common/search/search_urls.h"
#include "content/public/common/url_constants.h"
#include "url/gurl.h"
namespace search {
namespace {
const char kServiceWorkerFileName[] = "newtab-serviceworker.js";
bool MatchesOrigin(const GURL& my_url, const GURL& other_url) {
return my_url.host_piece() == other_url.host_piece() &&
my_url.port() == other_url.port() &&
(my_url.scheme_piece() == other_url.scheme_piece() ||
(my_url.SchemeIs(url::kHttpsScheme) &&
other_url.SchemeIs(url::kHttpScheme)));
}
} // namespace
bool MatchesOriginAndPath(const GURL& my_url, const GURL& other_url) {
return MatchesOrigin(my_url, other_url) &&
my_url.path_piece() == other_url.path_piece();
}
bool IsMatchingServiceWorker(const GURL& my_url, const GURL& document_url) {
// The origin should match.
if (!MatchesOrigin(my_url, document_url))
return false;
// The url filename should be the new tab page ServiceWorker.
std::string my_filename = my_url.ExtractFileName();
if (my_filename != kServiceWorkerFileName)
return false;
// The paths up to the filenames should be the same.
std::string my_path_without_filename = my_url.path();
my_path_without_filename = my_path_without_filename.substr(
0, my_path_without_filename.length() - my_filename.length());
std::string document_filename = document_url.ExtractFileName();
std::string document_path_without_filename = document_url.path();
document_path_without_filename = document_path_without_filename.substr(
0, document_path_without_filename.length() - document_filename.length());
return my_path_without_filename == document_path_without_filename;
}
} // namespace search
// Copyright 2013 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_COMMON_SEARCH_SEARCH_URLS_H_
#define CHROME_COMMON_SEARCH_SEARCH_URLS_H_
class GURL;
namespace search {
// Returns true if |my_url| matches |other_url| in terms of origin (i.e. host,
// port, and scheme) and path. As a special case, this also matches if |my_url|
// is "https://" and |other_url| is "http://" (and host, port, and path match).
// This is so that "https://" URLs will be considered "Instant URLs" even if the
// default search provider URL is "http://".
bool MatchesOriginAndPath(const GURL& my_url, const GURL& other_url);
bool IsMatchingServiceWorker(const GURL& my_url, const GURL& document_url);
} // namespace search
#endif // CHROME_COMMON_SEARCH_SEARCH_URLS_H_
// Copyright 2013 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/common/search/search_urls.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace search {
typedef testing::Test SearchURLsTest;
TEST_F(SearchURLsTest, MatchesOriginAndPath) {
EXPECT_TRUE(MatchesOriginAndPath(
GURL("http://example.com/path"),
GURL("http://example.com/path?param")));
EXPECT_FALSE(MatchesOriginAndPath(
GURL("http://not.example.com/path"),
GURL("http://example.com/path")));
EXPECT_TRUE(MatchesOriginAndPath(
GURL("http://example.com:80/path"),
GURL("http://example.com/path")));
EXPECT_FALSE(MatchesOriginAndPath(
GURL("http://example.com:8080/path"),
GURL("http://example.com/path")));
EXPECT_FALSE(MatchesOriginAndPath(
GURL("ftp://example.com/path"),
GURL("http://example.com/path")));
EXPECT_FALSE(MatchesOriginAndPath(
GURL("http://example.com/path"),
GURL("https://example.com/path")));
EXPECT_TRUE(MatchesOriginAndPath(
GURL("https://example.com/path"),
GURL("http://example.com/path")));
EXPECT_FALSE(MatchesOriginAndPath(
GURL("http://example.com/path"),
GURL("http://example.com/another-path")));
}
} // namespace search
......@@ -2494,7 +2494,6 @@ test("unit_tests") {
"../common/page_load_metrics/test/weak_mock_timer.h",
"../common/partial_circular_buffer_unittest.cc",
"../common/pref_names_util_unittest.cc",
"../common/search/search_urls_unittest.cc",
"../common/secure_origin_whitelist_unittest.cc",
"../renderer/app_categorizer_unittest.cc",
"../renderer/chrome_content_renderer_client_unittest.cc",
......
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