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 (i.e. GOOGLE_WEB_PROPERTIES_SIGNED_IN entries) |
| 38 // will not be included. |
| 39 std::string GetClientDataHeader(bool is_signed_in); |
37 | 40 |
38 // Returns a space-separated string containing the list of current active | 41 // Returns a space-separated string containing the list of current active |
39 // variations (as would be reported in the |variation_id| repeated field of | 42 // variations (as would be reported in the |variation_id| repeated field of |
40 // the ClientVariations proto). The returned string is guaranteed to have a | 43 // the ClientVariations proto). Does not include variation ids that should be |
| 44 // 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 ". | 45 // a leading and trailing space, e.g. " 123 234 345 ". |
42 std::string GetVariationsString(); | 46 std::string GetVariationsString(); |
43 | 47 |
44 // Forces the list of |variation_ids| (which will be modified by adding the | 48 // 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 | 49 // comma-separated |command_line_variation_ids|). This is a wrapper function |
46 // around SetDefaultVariationIds. | 50 // around SetDefaultVariationIds. |
47 bool ForceVariationIds( | 51 bool ForceVariationIds( |
48 const std::string& command_line_variation_ids, | 52 const std::string& command_line_variation_ids, |
49 std::vector<std::string>* variation_ids); | 53 std::vector<std::string>* variation_ids); |
50 | 54 |
51 // Sets *additional* variation ids and trigger variation ids to be encoded in | 55 // 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 | 56 // 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 | 57 // 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 | 58 // strings of numeric experiment ids. If an id is prefixed with "t" it will |
55 // be treated as a trigger experiment id. | 59 // be treated as a trigger experiment id. |
56 bool SetDefaultVariationIds(const std::vector<std::string>& variation_ids); | 60 bool SetDefaultVariationIds(const std::vector<std::string>& variation_ids); |
57 | 61 |
58 // Resets any cached state for tests. | 62 // Resets any cached state for tests. |
59 void ResetForTesting(); | 63 void ResetForTesting(); |
60 | 64 |
61 private: | 65 private: |
62 friend struct base::DefaultSingletonTraits<VariationsHttpHeaderProvider>; | 66 friend struct base::DefaultSingletonTraits<VariationsHttpHeaderProvider>; |
63 | 67 |
| 68 typedef std::pair<VariationID, IDCollectionKey> VariationIDEntry; |
| 69 |
64 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | 70 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
65 SetDefaultVariationIds_Valid); | 71 SetDefaultVariationIds_Valid); |
66 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | 72 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
67 SetDefaultVariationIds_Invalid); | 73 SetDefaultVariationIds_Invalid); |
68 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | 74 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
69 OnFieldTrialGroupFinalized); | 75 OnFieldTrialGroupFinalized); |
70 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | 76 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
71 GetVariationsString); | 77 GetVariationsString); |
72 | 78 |
73 VariationsHttpHeaderProvider(); | 79 VariationsHttpHeaderProvider(); |
74 ~VariationsHttpHeaderProvider() override; | 80 ~VariationsHttpHeaderProvider() override; |
75 | 81 |
76 // base::FieldTrialList::Observer: | 82 // base::FieldTrialList::Observer: |
77 // This will add the variation ID associated with |trial_name| and | 83 // This will add the variation ID associated with |trial_name| and |
78 // |group_name| to the variation ID cache. | 84 // |group_name| to the variation ID cache. |
79 void OnFieldTrialGroupFinalized(const std::string& trial_name, | 85 void OnFieldTrialGroupFinalized(const std::string& trial_name, |
80 const std::string& group_name) override; | 86 const std::string& group_name) override; |
81 | 87 |
82 // metrics::SyntheticTrialObserver: | 88 // metrics::SyntheticTrialObserver: |
83 void OnSyntheticTrialsChanged( | 89 void OnSyntheticTrialsChanged( |
84 const std::vector<SyntheticTrialGroup>& groups) override; | 90 const std::vector<SyntheticTrialGroup>& groups) override; |
85 | 91 |
86 // Prepares the variation IDs cache with initial values if not already done. | 92 // Prepares the variation IDs cache with initial values if not already done. |
87 // This method also registers the caller with the FieldTrialList to receive | 93 // This method also registers the caller with the FieldTrialList to receive |
88 // new variation IDs. | 94 // new variation IDs. |
89 void InitVariationIDsCacheIfNeeded(); | 95 void InitVariationIDsCacheIfNeeded(); |
90 | 96 |
| 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 |
91 // Takes whatever is currently in |variation_ids_set_| and recreates | 103 // Takes whatever is currently in |variation_ids_set_| and recreates |
92 // |variation_ids_header_| with it. Assumes the the |lock_| is currently | 104 // |variation_ids_header_| with it. Assumes the the |lock_| is currently |
93 // held. | 105 // held. |
94 void UpdateVariationIDsHeaderValue(); | 106 void UpdateVariationIDsHeaderValue(); |
95 | 107 |
| 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 |
96 // Returns the currently active set of variation ids, which includes any | 112 // Returns the currently active set of variation ids, which includes any |
97 // default values, synthetic variations and actual field trial variations. | 113 // default values, synthetic variations and actual field trial variations. |
98 std::set<VariationID> GetAllVariationIds(); | 114 std::set<VariationIDEntry> GetAllVariationIds(); |
99 | 115 |
100 // Guards |variation_ids_cache_initialized_|, |variation_ids_set_| and | 116 // Guards access to variables below. |
101 // |variation_ids_header_|. | |
102 base::Lock lock_; | 117 base::Lock lock_; |
103 | 118 |
104 // Whether or not we've initialized the cache. | 119 // Whether or not we've initialized the caches. |
105 bool variation_ids_cache_initialized_; | 120 bool variation_ids_cache_initialized_; |
106 | 121 |
107 // Keep a cache of variation IDs that are transmitted in headers to Google. | 122 // 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. | 123 // This consists of a list of valid IDs, and the actual transmitted header. |
109 std::set<VariationID> variation_ids_set_; | 124 std::set<VariationIDEntry> variation_ids_set_; |
110 std::set<VariationID> variation_trigger_ids_set_; | |
111 | 125 |
112 // Provides the google experiment ids forced from command line. | 126 // Provides the google experiment ids forced from command line. |
113 std::set<VariationID> default_variation_ids_set_; | 127 std::set<VariationIDEntry> default_variation_ids_set_; |
114 std::set<VariationID> default_trigger_id_set_; | |
115 | 128 |
116 // Variations ids from synthetic field trials. | 129 // Variations ids from synthetic field trials. |
117 std::set<VariationID> synthetic_variation_ids_set_; | 130 std::set<VariationIDEntry> synthetic_variation_ids_set_; |
118 | 131 |
119 std::string variation_ids_header_; | 132 std::string cached_variation_ids_header_; |
| 133 std::string cached_variation_ids_header_signed_in_; |
120 | 134 |
121 DISALLOW_COPY_AND_ASSIGN(VariationsHttpHeaderProvider); | 135 DISALLOW_COPY_AND_ASSIGN(VariationsHttpHeaderProvider); |
122 }; | 136 }; |
123 | 137 |
124 } // namespace variations | 138 } // namespace variations |
125 | 139 |
126 #endif // COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_ | 140 #endif // COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_ |
OLD | NEW |