| 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 <vector> | 10 #include <vector> |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 // This class is a thread-safe singleton. | 28 // This class is a thread-safe singleton. |
| 29 class VariationsHttpHeaderProvider : public base::FieldTrialList::Observer, | 29 class VariationsHttpHeaderProvider : public base::FieldTrialList::Observer, |
| 30 public SyntheticTrialObserver { | 30 public SyntheticTrialObserver { |
| 31 public: | 31 public: |
| 32 static VariationsHttpHeaderProvider* GetInstance(); | 32 static VariationsHttpHeaderProvider* GetInstance(); |
| 33 | 33 |
| 34 // 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 |
| 35 // necessary. | 35 // necessary. |
| 36 std::string GetClientDataHeader(); | 36 std::string GetClientDataHeader(); |
| 37 | 37 |
| 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 |
| 38 // Sets *additional* variation ids and trigger variation ids to be encoded in | 44 // Sets *additional* variation ids and trigger variation ids to be encoded in |
| 39 // the X-Client-Data request header. This is intended for development use to | 45 // the X-Client-Data request header. This is intended for development use to |
| 40 // force a server side experiment id. |variation_ids| should be a | 46 // force a server side experiment id. |variation_ids| should be a |
| 41 // comma-separated string of numeric experiment ids. If an id is prefixed | 47 // comma-separated string of numeric experiment ids. If an id is prefixed |
| 42 // with "t" it will be treated as a trigger experiment id. | 48 // with "t" it will be treated as a trigger experiment id. |
| 43 bool SetDefaultVariationIds(const std::string& variation_ids); | 49 bool SetDefaultVariationIds(const std::string& variation_ids); |
| 44 | 50 |
| 45 // Resets any cached state for tests. | 51 // Resets any cached state for tests. |
| 46 void ResetForTesting(); | 52 void ResetForTesting(); |
| 47 | 53 |
| 48 private: | 54 private: |
| 49 friend struct base::DefaultSingletonTraits<VariationsHttpHeaderProvider>; | 55 friend struct base::DefaultSingletonTraits<VariationsHttpHeaderProvider>; |
| 50 | 56 |
| 51 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | 57 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
| 52 SetDefaultVariationIds_Valid); | 58 SetDefaultVariationIds_Valid); |
| 53 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | 59 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
| 54 SetDefaultVariationIds_Invalid); | 60 SetDefaultVariationIds_Invalid); |
| 55 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | 61 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
| 56 OnFieldTrialGroupFinalized); | 62 OnFieldTrialGroupFinalized); |
| 63 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
| 64 GetVariationsString); |
| 57 | 65 |
| 58 VariationsHttpHeaderProvider(); | 66 VariationsHttpHeaderProvider(); |
| 59 ~VariationsHttpHeaderProvider() override; | 67 ~VariationsHttpHeaderProvider() override; |
| 60 | 68 |
| 61 // base::FieldTrialList::Observer: | 69 // base::FieldTrialList::Observer: |
| 62 // This will add the variation ID associated with |trial_name| and | 70 // This will add the variation ID associated with |trial_name| and |
| 63 // |group_name| to the variation ID cache. | 71 // |group_name| to the variation ID cache. |
| 64 void OnFieldTrialGroupFinalized(const std::string& trial_name, | 72 void OnFieldTrialGroupFinalized(const std::string& trial_name, |
| 65 const std::string& group_name) override; | 73 const std::string& group_name) override; |
| 66 | 74 |
| 67 // metrics::SyntheticTrialObserver: | 75 // metrics::SyntheticTrialObserver: |
| 68 void OnSyntheticTrialsChanged( | 76 void OnSyntheticTrialsChanged( |
| 69 const std::vector<SyntheticTrialGroup>& groups) override; | 77 const std::vector<SyntheticTrialGroup>& groups) override; |
| 70 | 78 |
| 71 // Prepares the variation IDs cache with initial values if not already done. | 79 // Prepares the variation IDs cache with initial values if not already done. |
| 72 // This method also registers the caller with the FieldTrialList to receive | 80 // This method also registers the caller with the FieldTrialList to receive |
| 73 // new variation IDs. | 81 // new variation IDs. |
| 74 void InitVariationIDsCacheIfNeeded(); | 82 void InitVariationIDsCacheIfNeeded(); |
| 75 | 83 |
| 76 // Takes whatever is currently in |variation_ids_set_| and recreates | 84 // Takes whatever is currently in |variation_ids_set_| and recreates |
| 77 // |variation_ids_header_| with it. Assumes the the |lock_| is currently | 85 // |variation_ids_header_| with it. Assumes the the |lock_| is currently |
| 78 // held. | 86 // held. |
| 79 void UpdateVariationIDsHeaderValue(); | 87 void UpdateVariationIDsHeaderValue(); |
| 80 | 88 |
| 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 |
| 81 // Guards |variation_ids_cache_initialized_|, |variation_ids_set_| and | 93 // Guards |variation_ids_cache_initialized_|, |variation_ids_set_| and |
| 82 // |variation_ids_header_|. | 94 // |variation_ids_header_|. |
| 83 base::Lock lock_; | 95 base::Lock lock_; |
| 84 | 96 |
| 85 // Whether or not we've initialized the cache. | 97 // Whether or not we've initialized the cache. |
| 86 bool variation_ids_cache_initialized_; | 98 bool variation_ids_cache_initialized_; |
| 87 | 99 |
| 88 // Keep a cache of variation IDs that are transmitted in headers to Google. | 100 // Keep a cache of variation IDs that are transmitted in headers to Google. |
| 89 // This consists of a list of valid IDs, and the actual transmitted header. | 101 // This consists of a list of valid IDs, and the actual transmitted header. |
| 90 std::set<VariationID> variation_ids_set_; | 102 std::set<VariationID> variation_ids_set_; |
| 91 std::set<VariationID> variation_trigger_ids_set_; | 103 std::set<VariationID> variation_trigger_ids_set_; |
| 92 | 104 |
| 93 // Provides the google experiment ids forced from command line. | 105 // Provides the google experiment ids forced from command line. |
| 94 std::set<VariationID> default_variation_ids_set_; | 106 std::set<VariationID> default_variation_ids_set_; |
| 95 std::set<VariationID> default_trigger_id_set_; | 107 std::set<VariationID> default_trigger_id_set_; |
| 96 | 108 |
| 97 // Variations ids from synthetic field trials. | 109 // Variations ids from synthetic field trials. |
| 98 std::set<VariationID> synthetic_variation_ids_set_; | 110 std::set<VariationID> synthetic_variation_ids_set_; |
| 99 | 111 |
| 100 std::string variation_ids_header_; | 112 std::string variation_ids_header_; |
| 101 | 113 |
| 102 DISALLOW_COPY_AND_ASSIGN(VariationsHttpHeaderProvider); | 114 DISALLOW_COPY_AND_ASSIGN(VariationsHttpHeaderProvider); |
| 103 }; | 115 }; |
| 104 | 116 |
| 105 } // namespace variations | 117 } // namespace variations |
| 106 | 118 |
| 107 #endif // COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_ | 119 #endif // COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_ |
| OLD | NEW |