asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 1 | // 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 Cai | 6260d6c | 2018-06-26 23:29:08 | [diff] [blame] | 8 | #include <memory> |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 9 | #include <set> |
| 10 | #include <string> |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame^] | 11 | #include <vector> |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 12 | |
| 13 | namespace net { |
Jun Cai | 6260d6c | 2018-06-26 23:29:08 | [diff] [blame] | 14 | struct NetworkTrafficAnnotationTag; |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame^] | 15 | struct RedirectInfo; |
Dominic Battre | 5e8a1cd | 2018-01-02 16:16:58 | [diff] [blame] | 16 | class URLRequest; |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 17 | } |
| 18 | |
Jun Cai | 6260d6c | 2018-06-26 23:29:08 | [diff] [blame] | 19 | namespace network { |
| 20 | struct ResourceRequest; |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame^] | 21 | struct ResourceResponseHead; |
Jun Cai | 6260d6c | 2018-06-26 23:29:08 | [diff] [blame] | 22 | class SimpleURLLoader; |
| 23 | } // namespace network |
| 24 | |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 25 | class GURL; |
| 26 | |
| 27 | namespace variations { |
| 28 | |
Mark Pearson | cea91cf | 2017-12-13 20:45:58 | [diff] [blame] | 29 | enum class InIncognito { kNo, kYes }; |
| 30 | |
| 31 | enum class SignedIn { kNo, kYes }; |
| 32 | |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame^] | 33 | // Adds Chrome experiment and metrics state as custom headers to |request|. |
Mark Pearson | cea91cf | 2017-12-13 20:45:58 | [diff] [blame] | 34 | // 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 |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 37 | // 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 Cai | 6260d6c | 2018-06-26 23:29:08 | [diff] [blame] | 40 | // Returns true if custom headers are added. Returns false otherwise. |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame^] | 41 | bool AppendVariationsHeader(const GURL& url, |
Mark Pearson | cea91cf | 2017-12-13 20:45:58 | [diff] [blame] | 42 | InIncognito incognito, |
| 43 | SignedIn signed_in, |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame^] | 44 | network::ResourceRequest* request); |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 45 | |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame^] | 46 | // 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. |
| 49 | bool 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. |
| 56 | bool AppendVariationsHeaderWithCustomValue(const GURL& url, |
Wang Hui | 00fff44 | 2018-07-09 23:50:23 | [diff] [blame] | 57 | InIncognito incognito, |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame^] | 58 | const std::string& variations_header, |
| 59 | network::ResourceRequest* request); |
Wang Hui | 00fff44 | 2018-07-09 23:50:23 | [diff] [blame] | 60 | |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame^] | 61 | // 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. |
| 63 | bool 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. |
| 70 | bool 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. |
| 76 | void 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 Battre | 5e8a1cd | 2018-01-02 16:16:58 | [diff] [blame] | 82 | // that should receive it. This is being called by the ChromeNetworkDelegate. |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame^] | 83 | // Components calling AppendVariationsHeader() don't need to take care of this. |
| 84 | void StripVariationsHeaderIfNeeded(const GURL& new_location, |
| 85 | net::URLRequest* request); |
Dominic Battre | 5e8a1cd | 2018-01-02 16:16:58 | [diff] [blame] | 86 | |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame^] | 87 | // 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 Cai | 6260d6c | 2018-06-26 23:29:08 | [diff] [blame] | 90 | std::unique_ptr<network::SimpleURLLoader> |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame^] | 91 | CreateSimpleURLLoaderWithVariationsHeader( |
Jun Cai | 6260d6c | 2018-06-26 23:29:08 | [diff] [blame] | 92 | std::unique_ptr<network::ResourceRequest> request, |
| 93 | InIncognito incognito, |
| 94 | SignedIn signed_in, |
| 95 | const net::NetworkTrafficAnnotationTag& annotation_tag); |
| 96 | |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame^] | 97 | // 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 Hui | 00fff44 | 2018-07-09 23:50:23 | [diff] [blame] | 100 | std::unique_ptr<network::SimpleURLLoader> |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame^] | 101 | CreateSimpleURLLoaderWithVariationsHeaderUnknownSignedIn( |
Wang Hui | 00fff44 | 2018-07-09 23:50:23 | [diff] [blame] | 102 | std::unique_ptr<network::ResourceRequest> request, |
| 103 | InIncognito incognito, |
| 104 | const net::NetworkTrafficAnnotationTag& annotation_tag); |
| 105 | |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame^] | 106 | // Checks if |header_name| is one for the variations header. |
| 107 | bool IsVariationsHeader(const std::string& header_name); |
| 108 | |
| 109 | // Checks if |request| contains the variations header. |
| 110 | bool HasVariationsHeader(const network::ResourceRequest& request); |
| 111 | |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 112 | // Checks whether variation headers should be appended to requests to the |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame^] | 113 | // specified |url|. Returns true for Google's hosts served over secure schemes. |
| 114 | bool ShouldAppendVariationsHeader(const GURL& url); |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 115 | |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 116 | } // namespace variations |
| 117 | |
| 118 | #endif // COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADERS_H_ |