[email protected] | 71011c168 | 2014-07-09 17:19:16 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 71011c168 | 2014-07-09 17:19:16 | [diff] [blame] | 5 | #ifndef COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_ |
| 6 | #define COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_ |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 7 | |
| 8 | #include <set> |
| 9 | #include <string> |
| 10 | |
| 11 | #include "base/basictypes.h" |
[email protected] | 7f8a993 | 2013-07-26 20:43:34 | [diff] [blame] | 12 | #include "base/gtest_prod_util.h" |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 13 | #include "base/metrics/field_trial.h" |
| 14 | #include "base/synchronization/lock.h" |
[email protected] | 50ae9f1 | 2013-08-29 18:03:22 | [diff] [blame] | 15 | #include "components/variations/variations_associated_data.h" |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 16 | |
| 17 | namespace content { |
| 18 | class ResourceContext; |
| 19 | } |
| 20 | |
| 21 | namespace net { |
| 22 | class HttpRequestHeaders; |
| 23 | } |
| 24 | |
| 25 | class GURL; |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 26 | |
| 27 | template <typename T> struct DefaultSingletonTraits; |
| 28 | |
[email protected] | 71011c168 | 2014-07-09 17:19:16 | [diff] [blame] | 29 | namespace variations { |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 30 | |
[email protected] | ea15bd5 | 2014-07-14 22:42:50 | [diff] [blame] | 31 | // A helper class for maintaining client experiments and metrics state |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 32 | // transmitted in custom HTTP request headers. |
| 33 | // This class is a thread-safe singleton. |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 34 | class VariationsHttpHeaderProvider : base::FieldTrialList::Observer { |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 35 | public: |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 36 | static VariationsHttpHeaderProvider* GetInstance(); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 37 | |
| 38 | // Adds Chrome experiment and metrics state as custom headers to |headers|. |
| 39 | // Some headers may not be set given the |incognito| mode or whether |
| 40 | // the user has |uma_enabled|. Also, we never transmit headers to non-Google |
| 41 | // sites, which is checked based on the destination |url|. |
| 42 | void AppendHeaders(const GURL& url, |
| 43 | bool incognito, |
| 44 | bool uma_enabled, |
| 45 | net::HttpRequestHeaders* headers); |
| 46 | |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 47 | // Sets *additional* variation ids and trigger variation ids to be encoded in |
| 48 | // the X-Client-Data request header. This is intended for development use to |
| 49 | // force a server side experiment id. |variation_ids| should be a |
| 50 | // comma-separated string of numeric experiment ids. If an id is prefixed |
| 51 | // with "t" it will be treated as a trigger experiment id. |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 52 | bool SetDefaultVariationIds(const std::string& variation_ids); |
| 53 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 54 | private: |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 55 | friend struct DefaultSingletonTraits<VariationsHttpHeaderProvider>; |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 56 | |
[email protected] | 7f8a993 | 2013-07-26 20:43:34 | [diff] [blame] | 57 | FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
| 58 | ShouldAppendHeaders); |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 59 | FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
| 60 | SetDefaultVariationIds_Valid); |
| 61 | FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
| 62 | SetDefaultVariationIds_Invalid); |
[email protected] | e51dcb0c | 2014-05-06 16:56:10 | [diff] [blame] | 63 | FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
| 64 | OnFieldTrialGroupFinalized); |
[email protected] | 7f8a993 | 2013-07-26 20:43:34 | [diff] [blame] | 65 | |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 66 | VariationsHttpHeaderProvider(); |
| 67 | virtual ~VariationsHttpHeaderProvider(); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 68 | |
| 69 | // base::FieldTrialList::Observer implementation. |
| 70 | // This will add the variation ID associated with |trial_name| and |
| 71 | // |group_name| to the variation ID cache. |
| 72 | virtual void OnFieldTrialGroupFinalized( |
| 73 | const std::string& trial_name, |
| 74 | const std::string& group_name) OVERRIDE; |
| 75 | |
| 76 | // Prepares the variation IDs cache with initial values if not already done. |
| 77 | // This method also registers the caller with the FieldTrialList to receive |
| 78 | // new variation IDs. |
| 79 | void InitVariationIDsCacheIfNeeded(); |
| 80 | |
| 81 | // Takes whatever is currently in |variation_ids_set_| and recreates |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 82 | // |variation_ids_header_| with it. Assumes the the |lock_| is currently |
| 83 | // held. |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 84 | void UpdateVariationIDsHeaderValue(); |
| 85 | |
[email protected] | 7f8a993 | 2013-07-26 20:43:34 | [diff] [blame] | 86 | // Checks whether variation headers should be appended to requests to the |
| 87 | // specified |url|. Returns true for google.<TLD> and youtube.<TLD> URLs. |
| 88 | static bool ShouldAppendHeaders(const GURL& url); |
| 89 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 90 | // Guards |variation_ids_cache_initialized_|, |variation_ids_set_| and |
| 91 | // |variation_ids_header_|. |
| 92 | base::Lock lock_; |
| 93 | |
| 94 | // Whether or not we've initialized the cache. |
| 95 | bool variation_ids_cache_initialized_; |
| 96 | |
| 97 | // Keep a cache of variation IDs that are transmitted in headers to Google. |
| 98 | // This consists of a list of valid IDs, and the actual transmitted header. |
[email protected] | 59b6f67 | 2014-07-26 18:35:47 | [diff] [blame^] | 99 | std::set<VariationID> variation_ids_set_; |
| 100 | std::set<VariationID> variation_trigger_ids_set_; |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 101 | |
| 102 | // Provides the google experiment ids forced from command line. |
[email protected] | 59b6f67 | 2014-07-26 18:35:47 | [diff] [blame^] | 103 | std::set<VariationID> default_variation_ids_set_; |
| 104 | std::set<VariationID> default_trigger_id_set_; |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 105 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 106 | std::string variation_ids_header_; |
| 107 | |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 108 | DISALLOW_COPY_AND_ASSIGN(VariationsHttpHeaderProvider); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 109 | }; |
| 110 | |
[email protected] | 71011c168 | 2014-07-09 17:19:16 | [diff] [blame] | 111 | } // namespace variations |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 112 | |
[email protected] | 71011c168 | 2014-07-09 17:19:16 | [diff] [blame] | 113 | #endif // COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_ |