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