blob: ab62563e13cc5b57c7940732f6321a6ac99f3f57 [file] [log] [blame]
asvitkine9a279832015-12-18 02:35:501// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADERS_H_
6#define COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADERS_H_
7
Jun Cai6260d6c2018-06-26 23:29:088#include <memory>
asvitkine9a279832015-12-18 02:35:509#include <set>
10#include <string>
Takashi Toyoshimaf3ceca92019-02-04 07:49:0511#include <vector>
asvitkine9a279832015-12-18 02:35:5012
13namespace net {
Jun Cai6260d6c2018-06-26 23:29:0814struct NetworkTrafficAnnotationTag;
Takashi Toyoshimaf3ceca92019-02-04 07:49:0515struct RedirectInfo;
Dominic Battre5e8a1cd2018-01-02 16:16:5816class URLRequest;
asvitkine9a279832015-12-18 02:35:5017}
18
Jun Cai6260d6c2018-06-26 23:29:0819namespace network {
20struct ResourceRequest;
Takashi Toyoshimaf3ceca92019-02-04 07:49:0521struct ResourceResponseHead;
Jun Cai6260d6c2018-06-26 23:29:0822class SimpleURLLoader;
23} // namespace network
24
asvitkine9a279832015-12-18 02:35:5025class GURL;
26
27namespace variations {
28
Mark Pearsoncea91cf2017-12-13 20:45:5829enum class InIncognito { kNo, kYes };
30
31enum class SignedIn { kNo, kYes };
32
Takashi Toyoshimaf3ceca92019-02-04 07:49:0533// Adds Chrome experiment and metrics state as custom headers to |request|.
Mark Pearsoncea91cf2017-12-13 20:45:5834// The content of the headers will depend on |incognito| and |signed_in|
35// parameters. It is fine to pass SignedIn::NO if the state is not known to the
36// caller. This will prevent addition of ids of type
yutak3305f49d2016-12-13 10:32:3137// GOOGLE_WEB_PROPERTIES_SIGNED_IN, which is not the case for any ids that come
38// from the variations server. These headers are never transmitted to non-Google
39// web sites, which is checked based on the destination |url|.
Jun Cai6260d6c2018-06-26 23:29:0840// Returns true if custom headers are added. Returns false otherwise.
Takashi Toyoshimaf3ceca92019-02-04 07:49:0541bool AppendVariationsHeader(const GURL& url,
Mark Pearsoncea91cf2017-12-13 20:45:5842 InIncognito incognito,
43 SignedIn signed_in,
Takashi Toyoshimaf3ceca92019-02-04 07:49:0544 network::ResourceRequest* request);
asvitkine9a279832015-12-18 02:35:5045
Takashi Toyoshimaf3ceca92019-02-04 07:49:0546// TODO(toyoshim): Remove this deprecated API that takes net::URLRequest* once
47// all callers are removed after NetworkService being fully enabled, or migrated
48// to use SimpleURLLoader. See, crbug.com/773295.
49bool AppendVariationsHeader(const GURL& url,
50 InIncognito incognito,
51 SignedIn signed_in,
52 net::URLRequest* request);
53
54// Similar to functions above, but uses specified |variations_header| as the
55// custom header value. You should not generally need to use this.
56bool AppendVariationsHeaderWithCustomValue(const GURL& url,
Wang Hui00fff442018-07-09 23:50:2357 InIncognito incognito,
Takashi Toyoshimaf3ceca92019-02-04 07:49:0558 const std::string& variations_header,
59 network::ResourceRequest* request);
Wang Hui00fff442018-07-09 23:50:2360
Takashi Toyoshimaf3ceca92019-02-04 07:49:0561// Adds Chrome experiment and metrics state as a custom header to |request|
62// when the signed-in state is not known to the caller; See above for details.
63bool AppendVariationsHeaderUnknownSignedIn(const GURL& url,
64 InIncognito incognito,
65 network::ResourceRequest* request);
66
67// TODO(toyoshim): Remove this deprecated API that takes net::URLRequest* once
68// all callers are removed after NetworkService being fully enabled, or migrated
69// to use SimpleURLLoader. See, crbug.com/773295.
70bool AppendVariationsHeaderUnknownSignedIn(const GURL& url,
71 InIncognito incognito,
72 net::URLRequest* request);
73
74// Removes the variations header for requests when a redirect to a non-Google
75// URL occurs.
76void RemoveVariationsHeaderIfNeeded(
77 const net::RedirectInfo& redirect_info,
78 const network::ResourceResponseHead& response_head,
79 std::vector<std::string>* to_be_removed_headers);
80
81// Strips the variations header if |new_location| does not point to a location
Dominic Battre5e8a1cd2018-01-02 16:16:5882// that should receive it. This is being called by the ChromeNetworkDelegate.
Takashi Toyoshimaf3ceca92019-02-04 07:49:0583// Components calling AppendVariationsHeader() don't need to take care of this.
84void StripVariationsHeaderIfNeeded(const GURL& new_location,
85 net::URLRequest* request);
Dominic Battre5e8a1cd2018-01-02 16:16:5886
Takashi Toyoshimaf3ceca92019-02-04 07:49:0587// Creates a SimpleURLLoader that will include the variations header for
88// requests to Google and ensures they're removed if a redirect to a non-Google
89// URL occurs.
Jun Cai6260d6c2018-06-26 23:29:0890std::unique_ptr<network::SimpleURLLoader>
Takashi Toyoshimaf3ceca92019-02-04 07:49:0591CreateSimpleURLLoaderWithVariationsHeader(
Jun Cai6260d6c2018-06-26 23:29:0892 std::unique_ptr<network::ResourceRequest> request,
93 InIncognito incognito,
94 SignedIn signed_in,
95 const net::NetworkTrafficAnnotationTag& annotation_tag);
96
Takashi Toyoshimaf3ceca92019-02-04 07:49:0597// Creates a SimpleURLLoader that will include the variations header for
98// requests to Google when the signed-in state is unknown and ensures they're
99// removed if a redirect to a non-Google URL occurs.
Wang Hui00fff442018-07-09 23:50:23100std::unique_ptr<network::SimpleURLLoader>
Takashi Toyoshimaf3ceca92019-02-04 07:49:05101CreateSimpleURLLoaderWithVariationsHeaderUnknownSignedIn(
Wang Hui00fff442018-07-09 23:50:23102 std::unique_ptr<network::ResourceRequest> request,
103 InIncognito incognito,
104 const net::NetworkTrafficAnnotationTag& annotation_tag);
105
Takashi Toyoshimaf3ceca92019-02-04 07:49:05106// Checks if |header_name| is one for the variations header.
107bool IsVariationsHeader(const std::string& header_name);
108
109// Checks if |request| contains the variations header.
110bool HasVariationsHeader(const network::ResourceRequest& request);
111
asvitkine9a279832015-12-18 02:35:50112// Checks whether variation headers should be appended to requests to the
Takashi Toyoshimaf3ceca92019-02-04 07:49:05113// specified |url|. Returns true for Google's hosts served over secure schemes.
114bool ShouldAppendVariationsHeader(const GURL& url);
asvitkine9a279832015-12-18 02:35:50115
asvitkine9a279832015-12-18 02:35:50116} // namespace variations
117
118#endif // COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADERS_H_