[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 | |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [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> |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 10 | #include <vector> |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 11 | |
| 12 | #include "base/basictypes.h" |
[email protected] | 7f8a993 | 2013-07-26 20:43:34 | [diff] [blame] | 13 | #include "base/gtest_prod_util.h" |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 14 | #include "base/metrics/field_trial.h" |
| 15 | #include "base/synchronization/lock.h" |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 16 | #include "components/variations/synthetic_trials.h" |
[email protected] | 50ae9f1 | 2013-08-29 18:03:22 | [diff] [blame] | 17 | #include "components/variations/variations_associated_data.h" |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 18 | |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 19 | namespace base { |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 20 | template <typename T> |
| 21 | struct DefaultSingletonTraits; |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 22 | } |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 23 | |
[email protected] | 71011c168 | 2014-07-09 17:19:16 | [diff] [blame] | 24 | namespace variations { |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 25 | |
[email protected] | ea15bd5 | 2014-07-14 22:42:50 | [diff] [blame] | 26 | // A helper class for maintaining client experiments and metrics state |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 27 | // transmitted in custom HTTP request headers. |
| 28 | // This class is a thread-safe singleton. |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 29 | class VariationsHttpHeaderProvider : public base::FieldTrialList::Observer, |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 30 | public SyntheticTrialObserver { |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 31 | public: |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 32 | static VariationsHttpHeaderProvider* GetInstance(); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 33 | |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 34 | // Returns the value of the client data header, computing and caching it if |
| 35 | // necessary. |
| 36 | std::string GetClientDataHeader(); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 37 | |
asvitkine | 35ba647 | 2015-12-18 23:52:33 | [diff] [blame] | 38 | // Returns a space-separated string containing the list of current active |
| 39 | // variations (as would be reported in the |variation_id| repeated field of |
| 40 | // the ClientVariations proto). The returned string is guaranteed to have a |
| 41 | // a leading and trailing space, e.g. " 123 234 345 ". |
| 42 | std::string GetVariationsString(); |
| 43 | |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 44 | // Sets *additional* variation ids and trigger variation ids to be encoded in |
| 45 | // the X-Client-Data request header. This is intended for development use to |
| 46 | // force a server side experiment id. |variation_ids| should be a |
| 47 | // comma-separated string of numeric experiment ids. If an id is prefixed |
| 48 | // with "t" it will be treated as a trigger experiment id. |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 49 | bool SetDefaultVariationIds(const std::string& variation_ids); |
| 50 | |
asvitkine | b4ed7868 | 2015-03-12 18:18:54 | [diff] [blame] | 51 | // Resets any cached state for tests. |
| 52 | void ResetForTesting(); |
| 53 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 54 | private: |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 55 | friend struct base::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, |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 58 | SetDefaultVariationIds_Valid); |
| 59 | FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
| 60 | SetDefaultVariationIds_Invalid); |
[email protected] | e51dcb0c | 2014-05-06 16:56:10 | [diff] [blame] | 61 | FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
| 62 | OnFieldTrialGroupFinalized); |
asvitkine | 35ba647 | 2015-12-18 23:52:33 | [diff] [blame] | 63 | FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
| 64 | GetVariationsString); |
[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(); |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 67 | ~VariationsHttpHeaderProvider() override; |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 68 | |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 69 | // base::FieldTrialList::Observer: |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 70 | // This will add the variation ID associated with |trial_name| and |
| 71 | // |group_name| to the variation ID cache. |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 72 | void OnFieldTrialGroupFinalized(const std::string& trial_name, |
| 73 | const std::string& group_name) override; |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 74 | |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 75 | // metrics::SyntheticTrialObserver: |
| 76 | void OnSyntheticTrialsChanged( |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 77 | const std::vector<SyntheticTrialGroup>& groups) override; |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 78 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 79 | // Prepares the variation IDs cache with initial values if not already done. |
| 80 | // This method also registers the caller with the FieldTrialList to receive |
| 81 | // new variation IDs. |
| 82 | void InitVariationIDsCacheIfNeeded(); |
| 83 | |
| 84 | // Takes whatever is currently in |variation_ids_set_| and recreates |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 85 | // |variation_ids_header_| with it. Assumes the the |lock_| is currently |
| 86 | // held. |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 87 | void UpdateVariationIDsHeaderValue(); |
| 88 | |
asvitkine | 35ba647 | 2015-12-18 23:52:33 | [diff] [blame] | 89 | // Returns the currently active set of variation ids, which includes any |
| 90 | // default values, synthetic variations and actual field trial variations. |
| 91 | std::set<VariationID> GetAllVariationIds(); |
| 92 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 93 | // Guards |variation_ids_cache_initialized_|, |variation_ids_set_| and |
| 94 | // |variation_ids_header_|. |
| 95 | base::Lock lock_; |
| 96 | |
| 97 | // Whether or not we've initialized the cache. |
| 98 | bool variation_ids_cache_initialized_; |
| 99 | |
| 100 | // Keep a cache of variation IDs that are transmitted in headers to Google. |
| 101 | // This consists of a list of valid IDs, and the actual transmitted header. |
[email protected] | 59b6f67 | 2014-07-26 18:35:47 | [diff] [blame] | 102 | std::set<VariationID> variation_ids_set_; |
| 103 | std::set<VariationID> variation_trigger_ids_set_; |
[email protected] | 1bd918d | 2013-10-13 18:23:09 | [diff] [blame] | 104 | |
| 105 | // Provides the google experiment ids forced from command line. |
[email protected] | 59b6f67 | 2014-07-26 18:35:47 | [diff] [blame] | 106 | std::set<VariationID> default_variation_ids_set_; |
| 107 | std::set<VariationID> default_trigger_id_set_; |
[email protected] | 8c2c544 | 2014-04-04 18:55:29 | [diff] [blame] | 108 | |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 109 | // Variations ids from synthetic field trials. |
| 110 | std::set<VariationID> synthetic_variation_ids_set_; |
| 111 | |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 112 | std::string variation_ids_header_; |
| 113 | |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 114 | DISALLOW_COPY_AND_ASSIGN(VariationsHttpHeaderProvider); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 115 | }; |
| 116 | |
[email protected] | 71011c168 | 2014-07-09 17:19:16 | [diff] [blame] | 117 | } // namespace variations |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 118 | |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 119 | #endif // COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_ |