diff options
Diffstat (limited to 'chromium/components/variations/net')
5 files changed, 296 insertions, 0 deletions
diff --git a/chromium/components/variations/net/BUILD.gn b/chromium/components/variations/net/BUILD.gn new file mode 100644 index 00000000000..c037d46fec3 --- /dev/null +++ b/chromium/components/variations/net/BUILD.gn @@ -0,0 +1,22 @@ +# Copyright 2014 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. + +source_set("net") { + sources = [ + "variations_http_headers.cc", + "variations_http_headers.h", + ] + + public_deps = [ + "//components/variations", + "//net", + "//url", + ] + deps = [ + "//base", + "//components/google/core/browser", + "//components/metrics", + "//components/variations/proto", + ] +} diff --git a/chromium/components/variations/net/DEPS b/chromium/components/variations/net/DEPS new file mode 100644 index 00000000000..1abc71b7882 --- /dev/null +++ b/chromium/components/variations/net/DEPS @@ -0,0 +1,5 @@ +include_rules = [ + "+components/google", + "+components/metrics", + "+net", +] diff --git a/chromium/components/variations/net/variations_http_headers.cc b/chromium/components/variations/net/variations_http_headers.cc new file mode 100644 index 00000000000..48e84eb190c --- /dev/null +++ b/chromium/components/variations/net/variations_http_headers.cc @@ -0,0 +1,110 @@ +// Copyright 2015 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/variations/net/variations_http_headers.h" + +#include <stddef.h> + +#include "base/macros.h" +#include "base/strings/string_util.h" +#include "components/google/core/browser/google_util.h" +#include "components/variations/variations_http_header_provider.h" +#include "net/http/http_request_headers.h" +#include "url/gurl.h" + +namespace variations { + +namespace { + +const char* kSuffixesToSetHeadersFor[] = { + ".android.com", + ".doubleclick.com", + ".doubleclick.net", + ".ggpht.com", + ".googleadservices.com", + ".googleapis.com", + ".googlesyndication.com", + ".googleusercontent.com", + ".googlevideo.com", + ".gstatic.com", + ".ytimg.com", +}; + +// Exact hostnames in lowercase to set headers for. +const char* kHostsToSetHeadersFor[] = { + "googleweblight.com", +}; + +const char kChromeUMAEnabled[] = "X-Chrome-UMA-Enabled"; +const char kClientData[] = "X-Client-Data"; + +} // namespace + +void AppendVariationHeaders(const GURL& url, + bool incognito, + bool uma_enabled, + net::HttpRequestHeaders* headers) { + // Note the criteria for attaching client experiment headers: + // 1. We only transmit to Google owned domains which can evaluate experiments. + // 1a. These include hosts which have a standard postfix such as: + // *.doubleclick.net or *.googlesyndication.com or + // exactly www.googleadservices.com or + // international TLD domains *.google.<TLD> or *.youtube.<TLD>. + // 2. Only transmit for non-Incognito profiles. + // 3. For the X-Chrome-UMA-Enabled bit, only set it if UMA is in fact enabled + // for this install of Chrome. + // 4. For the X-Client-Data header, only include non-empty variation IDs. + if (incognito || !internal::ShouldAppendVariationHeaders(url)) + return; + + if (uma_enabled) + headers->SetHeaderIfMissing(kChromeUMAEnabled, "1"); + + const std::string variation_ids_header = + VariationsHttpHeaderProvider::GetInstance()->GetClientDataHeader(); + if (!variation_ids_header.empty()) { + // Note that prior to M33 this header was named X-Chrome-Variations. + headers->SetHeaderIfMissing(kClientData, variation_ids_header); + } +} + +std::set<std::string> GetVariationHeaderNames() { + std::set<std::string> headers; + headers.insert(kChromeUMAEnabled); + headers.insert(kClientData); + return headers; +} + +namespace internal { + +// static +bool ShouldAppendVariationHeaders(const GURL& url) { + if (google_util::IsGoogleDomainUrl(url, google_util::ALLOW_SUBDOMAIN, + google_util::ALLOW_NON_STANDARD_PORTS)) { + return true; + } + + if (!url.is_valid() || !url.SchemeIsHTTPOrHTTPS()) + return false; + + // Some domains don't have international TLD extensions, so testing for them + // is very straight forward. + const std::string host = url.host(); + for (size_t i = 0; i < arraysize(kSuffixesToSetHeadersFor); ++i) { + if (base::EndsWith(host, kSuffixesToSetHeadersFor[i], + base::CompareCase::INSENSITIVE_ASCII)) + return true; + } + for (size_t i = 0; i < arraysize(kHostsToSetHeadersFor); ++i) { + if (base::LowerCaseEqualsASCII(host, kHostsToSetHeadersFor[i])) + return true; + } + + return google_util::IsYoutubeDomainUrl(url, google_util::ALLOW_SUBDOMAIN, + google_util::ALLOW_NON_STANDARD_PORTS); +} + +} // namespace internal + +} // namespace variations diff --git a/chromium/components/variations/net/variations_http_headers.h b/chromium/components/variations/net/variations_http_headers.h new file mode 100644 index 00000000000..d6280e56e8e --- /dev/null +++ b/chromium/components/variations/net/variations_http_headers.h @@ -0,0 +1,41 @@ +// Copyright 2015 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_VARIATIONS_NET_VARIATIONS_HTTP_HEADERS_H_ +#define COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADERS_H_ + +#include <set> +#include <string> + +namespace net { +class HttpRequestHeaders; +} + +class GURL; + +namespace variations { + +// Adds Chrome experiment and metrics state as custom headers to |headers|. +// Some headers may not be set given the |incognito| mode or whether +// the user has |uma_enabled|. Also, we never transmit headers to non-Google +// sites, which is checked based on the destination |url|. +void AppendVariationHeaders(const GURL& url, + bool incognito, + bool uma_enabled, + net::HttpRequestHeaders* headers); + +// Returns the HTTP header names which are added by AppendVariationHeaders(). +std::set<std::string> GetVariationHeaderNames(); + +namespace internal { + +// Checks whether variation headers should be appended to requests to the +// specified |url|. Returns true for google.<TLD> and youtube.<TLD> URLs. +bool ShouldAppendVariationHeaders(const GURL& url); + +} // namespace internal + +} // namespace variations + +#endif // COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADERS_H_ diff --git a/chromium/components/variations/net/variations_http_headers_unittest.cc b/chromium/components/variations/net/variations_http_headers_unittest.cc new file mode 100644 index 00000000000..6421cc78868 --- /dev/null +++ b/chromium/components/variations/net/variations_http_headers_unittest.cc @@ -0,0 +1,118 @@ +// Copyright 2014 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/variations/net/variations_http_headers.h" + +#include <stddef.h> + +#include "base/macros.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "url/gurl.h" + +namespace variations { + +TEST(VariationsHttpHeadersTest, ShouldAppendHeaders) { + struct { + const char* url; + bool should_append_headers; + } cases[] = { + {"http://google.com", true}, + {"http://www.google.com", true}, + {"http://m.google.com", true}, + {"http://google.ca", true}, + {"https://google.ca", true}, + {"http://google.co.uk", true}, + {"http://google.co.uk:8080/", true}, + {"http://www.google.co.uk:8080/", true}, + {"http://google", false}, + + {"http://youtube.com", true}, + {"http://www.youtube.com", true}, + {"http://www.youtube.ca", true}, + {"http://www.youtube.co.uk:8080/", true}, + {"https://www.youtube.com", true}, + {"http://youtube", false}, + + {"http://www.yahoo.com", false}, + + {"http://ad.doubleclick.net", true}, + {"https://a.b.c.doubleclick.net", true}, + {"https://a.b.c.doubleclick.net:8081", true}, + {"http://www.doubleclick.com", true}, + {"http://www.doubleclick.org", false}, + {"http://www.doubleclick.net.com", false}, + {"https://www.doubleclick.net.com", false}, + + {"http://ad.googlesyndication.com", true}, + {"https://a.b.c.googlesyndication.com", true}, + {"https://a.b.c.googlesyndication.com:8080", true}, + {"http://www.doubleclick.edu", false}, + {"http://www.googlesyndication.com.edu", false}, + {"https://www.googlesyndication.com.com", false}, + + {"http://www.googleadservices.com", true}, + {"http://www.googleadservices.com:8080", true}, + {"https://www.googleadservices.com", true}, + {"https://www.internal.googleadservices.com", true}, + {"https://www2.googleadservices.com", true}, + {"https://www.googleadservices.org", false}, + {"https://www.googleadservices.com.co.uk", false}, + + {"http://WWW.ANDROID.COM", true}, + {"http://www.android.com", true}, + {"http://www.doubleclick.com", true}, + {"http://www.doubleclick.net", true}, + {"http://www.ggpht.com", true}, + {"http://www.googleadservices.com", true}, + {"http://www.googleapis.com", true}, + {"http://www.googlesyndication.com", true}, + {"http://www.googleusercontent.com", true}, + {"http://www.googlevideo.com", true}, + {"http://ssl.gstatic.com", true}, + {"http://www.gstatic.com", true}, + {"http://www.ytimg.com", true}, + {"http://wwwytimg.com", false}, + {"http://ytimg.com", false}, + + {"http://www.android.org", false}, + {"http://www.doubleclick.org", false}, + {"http://www.doubleclick.net", true}, + {"http://www.ggpht.org", false}, + {"http://www.googleadservices.org", false}, + {"http://www.googleapis.org", false}, + {"http://www.googlesyndication.org", false}, + {"http://www.googleusercontent.org", false}, + {"http://www.googlevideo.org", false}, + {"http://ssl.gstatic.org", false}, + {"http://www.gstatic.org", false}, + {"http://www.ytimg.org", false}, + + {"http://a.b.android.com", true}, + {"http://a.b.doubleclick.com", true}, + {"http://a.b.doubleclick.net", true}, + {"http://a.b.ggpht.com", true}, + {"http://a.b.googleadservices.com", true}, + {"http://a.b.googleapis.com", true}, + {"http://a.b.googlesyndication.com", true}, + {"http://a.b.googleusercontent.com", true}, + {"http://a.b.googlevideo.com", true}, + {"http://ssl.gstatic.com", true}, + {"http://a.b.gstatic.com", true}, + {"http://a.b.ytimg.com", true}, + {"http://googleweblight.com", true}, + {"https://googleweblight.com", true}, + {"http://wwwgoogleweblight.com", false}, + {"http://www.googleweblight.com", false}, + {"http://a.b.googleweblight.com", false}, + }; + + for (size_t i = 0; i < arraysize(cases); ++i) { + const GURL url(cases[i].url); + EXPECT_EQ(cases[i].should_append_headers, + internal::ShouldAppendVariationHeaders(url)) + << url; + } +} + +} // namespace variations |