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 | |
Takashi Toyoshima | cd413b4 | 2019-04-10 20:33:46 | [diff] [blame] | 13 | #include "services/network/public/mojom/network_context.mojom.h" |
Lucas Furukawa Gadani | 962a30c | 2019-09-18 19:55:08 | [diff] [blame] | 14 | #include "services/network/public/mojom/url_response_head.mojom-forward.h" |
Takashi Toyoshima | cd413b4 | 2019-04-10 20:33:46 | [diff] [blame] | 15 | |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 16 | namespace net { |
Jun Cai | 6260d6c | 2018-06-26 23:29:08 | [diff] [blame] | 17 | struct NetworkTrafficAnnotationTag; |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame] | 18 | struct RedirectInfo; |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 19 | } |
| 20 | |
Jun Cai | 6260d6c | 2018-06-26 23:29:08 | [diff] [blame] | 21 | namespace network { |
| 22 | struct ResourceRequest; |
Jun Cai | 6260d6c | 2018-06-26 23:29:08 | [diff] [blame] | 23 | class SimpleURLLoader; |
| 24 | } // namespace network |
| 25 | |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 26 | class GURL; |
| 27 | |
| 28 | namespace variations { |
| 29 | |
Mark Pearson | cea91cf | 2017-12-13 20:45:58 | [diff] [blame] | 30 | enum class InIncognito { kNo, kYes }; |
| 31 | |
| 32 | enum class SignedIn { kNo, kYes }; |
| 33 | |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame] | 34 | // Adds Chrome experiment and metrics state as custom headers to |request|. |
Mark Pearson | cea91cf | 2017-12-13 20:45:58 | [diff] [blame] | 35 | // The content of the headers will depend on |incognito| and |signed_in| |
| 36 | // parameters. It is fine to pass SignedIn::NO if the state is not known to the |
| 37 | // caller. This will prevent addition of ids of type |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 38 | // GOOGLE_WEB_PROPERTIES_SIGNED_IN, which is not the case for any ids that come |
| 39 | // from the variations server. These headers are never transmitted to non-Google |
| 40 | // web sites, which is checked based on the destination |url|. |
Jun Cai | 6260d6c | 2018-06-26 23:29:08 | [diff] [blame] | 41 | // Returns true if custom headers are added. Returns false otherwise. |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame] | 42 | bool AppendVariationsHeader(const GURL& url, |
Mark Pearson | cea91cf | 2017-12-13 20:45:58 | [diff] [blame] | 43 | InIncognito incognito, |
| 44 | SignedIn signed_in, |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame] | 45 | network::ResourceRequest* request); |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 46 | |
Takashi Toyoshima | 03a7b3fa | 2019-06-14 10:10:10 | [diff] [blame] | 47 | // Similar to AppendVariationsHeader, but uses specified |variations_header| as |
| 48 | // the custom header value. You should not generally need to use this. |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame] | 49 | bool AppendVariationsHeaderWithCustomValue(const GURL& url, |
Wang Hui | 00fff44 | 2018-07-09 23:50:23 | [diff] [blame] | 50 | InIncognito incognito, |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame] | 51 | const std::string& variations_header, |
| 52 | network::ResourceRequest* request); |
Wang Hui | 00fff44 | 2018-07-09 23:50:23 | [diff] [blame] | 53 | |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame] | 54 | // Adds Chrome experiment and metrics state as a custom header to |request| |
| 55 | // when the signed-in state is not known to the caller; See above for details. |
| 56 | bool AppendVariationsHeaderUnknownSignedIn(const GURL& url, |
| 57 | InIncognito incognito, |
| 58 | network::ResourceRequest* request); |
| 59 | |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame] | 60 | // Removes the variations header for requests when a redirect to a non-Google |
| 61 | // URL occurs. |
| 62 | void RemoveVariationsHeaderIfNeeded( |
| 63 | const net::RedirectInfo& redirect_info, |
Lucas Furukawa Gadani | 962a30c | 2019-09-18 19:55:08 | [diff] [blame] | 64 | const network::mojom::URLResponseHead& response_head, |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame] | 65 | std::vector<std::string>* to_be_removed_headers); |
| 66 | |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame] | 67 | // Creates a SimpleURLLoader that will include the variations header for |
| 68 | // requests to Google and ensures they're removed if a redirect to a non-Google |
| 69 | // URL occurs. |
Jun Cai | 6260d6c | 2018-06-26 23:29:08 | [diff] [blame] | 70 | std::unique_ptr<network::SimpleURLLoader> |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame] | 71 | CreateSimpleURLLoaderWithVariationsHeader( |
Jun Cai | 6260d6c | 2018-06-26 23:29:08 | [diff] [blame] | 72 | std::unique_ptr<network::ResourceRequest> request, |
| 73 | InIncognito incognito, |
| 74 | SignedIn signed_in, |
| 75 | const net::NetworkTrafficAnnotationTag& annotation_tag); |
| 76 | |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame] | 77 | // Creates a SimpleURLLoader that will include the variations header for |
| 78 | // requests to Google when the signed-in state is unknown and ensures they're |
| 79 | // removed if a redirect to a non-Google URL occurs. |
Wang Hui | 00fff44 | 2018-07-09 23:50:23 | [diff] [blame] | 80 | std::unique_ptr<network::SimpleURLLoader> |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame] | 81 | CreateSimpleURLLoaderWithVariationsHeaderUnknownSignedIn( |
Wang Hui | 00fff44 | 2018-07-09 23:50:23 | [diff] [blame] | 82 | std::unique_ptr<network::ResourceRequest> request, |
| 83 | InIncognito incognito, |
| 84 | const net::NetworkTrafficAnnotationTag& annotation_tag); |
| 85 | |
Takashi Toyoshima | f3ceca9 | 2019-02-04 07:49:05 | [diff] [blame] | 86 | // Checks if |header_name| is one for the variations header. |
| 87 | bool IsVariationsHeader(const std::string& header_name); |
| 88 | |
| 89 | // Checks if |request| contains the variations header. |
| 90 | bool HasVariationsHeader(const network::ResourceRequest& request); |
| 91 | |
Takashi Toyoshima | 395c752 | 2019-02-07 10:19:44 | [diff] [blame] | 92 | // Calls the internal ShouldAppendVariationsHeader() for testing. |
| 93 | bool ShouldAppendVariationsHeaderForTesting(const GURL& url); |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 94 | |
Takashi Toyoshima | cd413b4 | 2019-04-10 20:33:46 | [diff] [blame] | 95 | // Updates |cors_exempt_header_list| field of the given |param| to register the |
| 96 | // variation headers. |
| 97 | void UpdateCorsExemptHeaderForVariations( |
| 98 | network::mojom::NetworkContextParams* params); |
| 99 | |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 100 | } // namespace variations |
| 101 | |
| 102 | #endif // COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADERS_H_ |