diff options
author | asvitkine <asvitkine@chromium.org> | 2015-12-17 18:35:50 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-12-18 02:36:44 +0000 |
commit | 9a2798399231b89c9becb3c9ca016da34550ebd2 (patch) | |
tree | 0eb51fa3d4becb0b56ba691e234795a946aaf30f /components/variations/net/variations_http_headers.cc | |
parent | aeb6648f175f95009e0eb10a07aca126ba81a878 (diff) | |
download | chromium_src-9a2798399231b89c9becb3c9ca016da34550ebd2.zip chromium_src-9a2798399231b89c9becb3c9ca016da34550ebd2.tar.gz chromium_src-9a2798399231b89c9becb3c9ca016da34550ebd2.tar.bz2 |
Refactor VariationsHttpHeaderProvider.
The goal is to move the bulk of its implementation to the main
variations component, so that it can be used by this CL from JNI:
https://codereview.chromium.org/1528543003/
Creates variations_http_headers.cc in net that still needs to depend
on net, which uses the http header provider internally. Updates
callers of the previous API to use the new simpler API that doesn't
require going through the singleton by clients.
Additionally, also adds variations/synthetic_trials.h and moves
the synthetic trials structs from metrics_service.h to the new
file. This works around an otherwise circular dependency between
metrics and variations, since variations_http_header_provider.cc
depends on synthetic trials.
TBRs below are for owners of downstream users of the API which
is being updated.
BUG=530223
TBR=caitkp@chromium.org,thestig@chromium.org
Review URL: https://codereview.chromium.org/1530133005
Cr-Commit-Position: refs/heads/master@{#365991}
Diffstat (limited to 'components/variations/net/variations_http_headers.cc')
-rw-r--r-- | components/variations/net/variations_http_headers.cc | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/components/variations/net/variations_http_headers.cc b/components/variations/net/variations_http_headers.cc new file mode 100644 index 0000000..632a0e0 --- /dev/null +++ b/components/variations/net/variations_http_headers.cc @@ -0,0 +1,98 @@ +// 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 "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", +}; + +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; + } + + return google_util::IsYoutubeDomainUrl(url, google_util::ALLOW_SUBDOMAIN, + google_util::ALLOW_NON_STANDARD_PORTS); +} + +} // namespace internal + +} // namespace variations |