OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_ | 5 #ifndef COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_ |
6 #define COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_ | 6 #define COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | |
11 #include <vector> | 10 #include <vector> |
12 | 11 |
13 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
14 #include "base/macros.h" | 13 #include "base/macros.h" |
15 #include "base/metrics/field_trial.h" | 14 #include "base/metrics/field_trial.h" |
16 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
17 #include "components/variations/synthetic_trials.h" | 16 #include "components/variations/synthetic_trials.h" |
18 #include "components/variations/variations_associated_data.h" | 17 #include "components/variations/variations_associated_data.h" |
19 | 18 |
20 namespace base { | 19 namespace base { |
21 template <typename T> | 20 template <typename T> |
22 struct DefaultSingletonTraits; | 21 struct DefaultSingletonTraits; |
23 } | 22 } |
24 | 23 |
25 namespace variations { | 24 namespace variations { |
26 | 25 |
27 // A helper class for maintaining client experiments and metrics state | 26 // A helper class for maintaining client experiments and metrics state |
28 // transmitted in custom HTTP request headers. | 27 // transmitted in custom HTTP request headers. |
29 // This class is a thread-safe singleton. | 28 // This class is a thread-safe singleton. |
30 class VariationsHttpHeaderProvider : public base::FieldTrialList::Observer, | 29 class VariationsHttpHeaderProvider : public base::FieldTrialList::Observer, |
31 public SyntheticTrialObserver { | 30 public SyntheticTrialObserver { |
32 public: | 31 public: |
33 static VariationsHttpHeaderProvider* GetInstance(); | 32 static VariationsHttpHeaderProvider* GetInstance(); |
34 | 33 |
35 // Returns the value of the client data header, computing and caching it if | 34 // Returns the value of the client data header, computing and caching it if |
36 // necessary. If |is_signed_in| is false, variation ids that should only be | 35 // necessary. |
37 // sent for signed in users (i.e. GOOGLE_WEB_PROPERTIES_SIGNED_IN entries) | 36 std::string GetClientDataHeader(); |
38 // will not be included. | |
39 std::string GetClientDataHeader(bool is_signed_in); | |
40 | 37 |
41 // Returns a space-separated string containing the list of current active | 38 // Returns a space-separated string containing the list of current active |
42 // variations (as would be reported in the |variation_id| repeated field of | 39 // variations (as would be reported in the |variation_id| repeated field of |
43 // the ClientVariations proto). Does not include variation ids that should be | 40 // the ClientVariations proto). The returned string is guaranteed to have a |
44 // sent for signed-in users only. The returned string is guaranteed to have a | |
45 // a leading and trailing space, e.g. " 123 234 345 ". | 41 // a leading and trailing space, e.g. " 123 234 345 ". |
46 std::string GetVariationsString(); | 42 std::string GetVariationsString(); |
47 | 43 |
48 // Forces the list of |variation_ids| (which will be modified by adding the | 44 // Forces the list of |variation_ids| (which will be modified by adding the |
49 // comma-separated |command_line_variation_ids|). This is a wrapper function | 45 // comma-separated |command_line_variation_ids|). This is a wrapper function |
50 // around SetDefaultVariationIds. | 46 // around SetDefaultVariationIds. |
51 bool ForceVariationIds( | 47 bool ForceVariationIds( |
52 const std::string& command_line_variation_ids, | 48 const std::string& command_line_variation_ids, |
53 std::vector<std::string>* variation_ids); | 49 std::vector<std::string>* variation_ids); |
54 | 50 |
55 // Sets *additional* variation ids and trigger variation ids to be encoded in | 51 // Sets *additional* variation ids and trigger variation ids to be encoded in |
56 // the X-Client-Data request header. This is intended for development use to | 52 // the X-Client-Data request header. This is intended for development use to |
57 // force a server side experiment id. |variation_ids| should be a list of | 53 // force a server side experiment id. |variation_ids| should be a list of |
58 // strings of numeric experiment ids. If an id is prefixed with "t" it will | 54 // strings of numeric experiment ids. If an id is prefixed with "t" it will |
59 // be treated as a trigger experiment id. | 55 // be treated as a trigger experiment id. |
60 bool SetDefaultVariationIds(const std::vector<std::string>& variation_ids); | 56 bool SetDefaultVariationIds(const std::vector<std::string>& variation_ids); |
61 | 57 |
62 // Resets any cached state for tests. | 58 // Resets any cached state for tests. |
63 void ResetForTesting(); | 59 void ResetForTesting(); |
64 | 60 |
65 private: | 61 private: |
66 friend struct base::DefaultSingletonTraits<VariationsHttpHeaderProvider>; | 62 friend struct base::DefaultSingletonTraits<VariationsHttpHeaderProvider>; |
67 | 63 |
68 typedef std::pair<VariationID, IDCollectionKey> VariationIDEntry; | |
69 | |
70 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | 64 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
71 SetDefaultVariationIds_Valid); | 65 SetDefaultVariationIds_Valid); |
72 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | 66 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
73 SetDefaultVariationIds_Invalid); | 67 SetDefaultVariationIds_Invalid); |
74 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | 68 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
75 OnFieldTrialGroupFinalized); | 69 OnFieldTrialGroupFinalized); |
76 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | 70 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
77 GetVariationsString); | 71 GetVariationsString); |
78 | 72 |
79 VariationsHttpHeaderProvider(); | 73 VariationsHttpHeaderProvider(); |
80 ~VariationsHttpHeaderProvider() override; | 74 ~VariationsHttpHeaderProvider() override; |
81 | 75 |
82 // base::FieldTrialList::Observer: | 76 // base::FieldTrialList::Observer: |
83 // This will add the variation ID associated with |trial_name| and | 77 // This will add the variation ID associated with |trial_name| and |
84 // |group_name| to the variation ID cache. | 78 // |group_name| to the variation ID cache. |
85 void OnFieldTrialGroupFinalized(const std::string& trial_name, | 79 void OnFieldTrialGroupFinalized(const std::string& trial_name, |
86 const std::string& group_name) override; | 80 const std::string& group_name) override; |
87 | 81 |
88 // metrics::SyntheticTrialObserver: | 82 // metrics::SyntheticTrialObserver: |
89 void OnSyntheticTrialsChanged( | 83 void OnSyntheticTrialsChanged( |
90 const std::vector<SyntheticTrialGroup>& groups) override; | 84 const std::vector<SyntheticTrialGroup>& groups) override; |
91 | 85 |
92 // Prepares the variation IDs cache with initial values if not already done. | 86 // Prepares the variation IDs cache with initial values if not already done. |
93 // This method also registers the caller with the FieldTrialList to receive | 87 // This method also registers the caller with the FieldTrialList to receive |
94 // new variation IDs. | 88 // new variation IDs. |
95 void InitVariationIDsCacheIfNeeded(); | 89 void InitVariationIDsCacheIfNeeded(); |
96 | 90 |
97 // Looks up the associated id for the given trial/group and adds an entry for | |
98 // it to |variation_ids_set_| if found. | |
99 void CacheVariationsId(const std::string& trial_name, | |
100 const std::string& group_name, | |
101 IDCollectionKey key); | |
102 | |
103 // Takes whatever is currently in |variation_ids_set_| and recreates | 91 // Takes whatever is currently in |variation_ids_set_| and recreates |
104 // |variation_ids_header_| with it. Assumes the the |lock_| is currently | 92 // |variation_ids_header_| with it. Assumes the the |lock_| is currently |
105 // held. | 93 // held. |
106 void UpdateVariationIDsHeaderValue(); | 94 void UpdateVariationIDsHeaderValue(); |
107 | 95 |
108 // Generates a base64-encoded proto to be used as a header value for the given | |
109 // |is_signed_in| state. | |
110 std::string GenerateBase64EncodedProto(bool is_signed_in); | |
111 | |
112 // Returns the currently active set of variation ids, which includes any | 96 // Returns the currently active set of variation ids, which includes any |
113 // default values, synthetic variations and actual field trial variations. | 97 // default values, synthetic variations and actual field trial variations. |
114 std::set<VariationIDEntry> GetAllVariationIds(); | 98 std::set<VariationID> GetAllVariationIds(); |
115 | 99 |
116 // Guards access to variables below. | 100 // Guards |variation_ids_cache_initialized_|, |variation_ids_set_| and |
| 101 // |variation_ids_header_|. |
117 base::Lock lock_; | 102 base::Lock lock_; |
118 | 103 |
119 // Whether or not we've initialized the caches. | 104 // Whether or not we've initialized the cache. |
120 bool variation_ids_cache_initialized_; | 105 bool variation_ids_cache_initialized_; |
121 | 106 |
122 // Keep a cache of variation IDs that are transmitted in headers to Google. | 107 // Keep a cache of variation IDs that are transmitted in headers to Google. |
123 // This consists of a list of valid IDs, and the actual transmitted header. | 108 // This consists of a list of valid IDs, and the actual transmitted header. |
124 std::set<VariationIDEntry> variation_ids_set_; | 109 std::set<VariationID> variation_ids_set_; |
| 110 std::set<VariationID> variation_trigger_ids_set_; |
125 | 111 |
126 // Provides the google experiment ids forced from command line. | 112 // Provides the google experiment ids forced from command line. |
127 std::set<VariationIDEntry> default_variation_ids_set_; | 113 std::set<VariationID> default_variation_ids_set_; |
| 114 std::set<VariationID> default_trigger_id_set_; |
128 | 115 |
129 // Variations ids from synthetic field trials. | 116 // Variations ids from synthetic field trials. |
130 std::set<VariationIDEntry> synthetic_variation_ids_set_; | 117 std::set<VariationID> synthetic_variation_ids_set_; |
131 | 118 |
132 std::string cached_variation_ids_header_; | 119 std::string variation_ids_header_; |
133 std::string cached_variation_ids_header_signed_in_; | |
134 | 120 |
135 DISALLOW_COPY_AND_ASSIGN(VariationsHttpHeaderProvider); | 121 DISALLOW_COPY_AND_ASSIGN(VariationsHttpHeaderProvider); |
136 }; | 122 }; |
137 | 123 |
138 } // namespace variations | 124 } // namespace variations |
139 | 125 |
140 #endif // COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_ | 126 #endif // COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_ |
OLD | NEW |