| 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_NET_VARIATIONS_HTTP_HEADER_PROVIDER_H_ | 5 #ifndef COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_ |
| 6 #define COMPONENTS_VARIATIONS_NET_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> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/metrics/field_trial.h" | 14 #include "base/metrics/field_trial.h" |
| 15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
| 16 #include "components/metrics/metrics_service.h" | 16 #include "components/variations/synthetic_trials.h" |
| 17 #include "components/variations/variations_associated_data.h" | 17 #include "components/variations/variations_associated_data.h" |
| 18 | 18 |
| 19 namespace content { | |
| 20 class ResourceContext; | |
| 21 } | |
| 22 | |
| 23 namespace net { | |
| 24 class HttpRequestHeaders; | |
| 25 } | |
| 26 | |
| 27 class GURL; | |
| 28 | |
| 29 namespace base { | 19 namespace base { |
| 30 template <typename T> struct DefaultSingletonTraits; | 20 template <typename T> |
| 21 struct DefaultSingletonTraits; |
| 31 } | 22 } |
| 32 | 23 |
| 33 namespace variations { | 24 namespace variations { |
| 34 | 25 |
| 35 // A helper class for maintaining client experiments and metrics state | 26 // A helper class for maintaining client experiments and metrics state |
| 36 // transmitted in custom HTTP request headers. | 27 // transmitted in custom HTTP request headers. |
| 37 // This class is a thread-safe singleton. | 28 // This class is a thread-safe singleton. |
| 38 class VariationsHttpHeaderProvider : public base::FieldTrialList::Observer, | 29 class VariationsHttpHeaderProvider : public base::FieldTrialList::Observer, |
| 39 public metrics::SyntheticTrialObserver { | 30 public SyntheticTrialObserver { |
| 40 public: | 31 public: |
| 41 static VariationsHttpHeaderProvider* GetInstance(); | 32 static VariationsHttpHeaderProvider* GetInstance(); |
| 42 | 33 |
| 43 // Adds Chrome experiment and metrics state as custom headers to |headers|. | 34 // Returns the value of the client data header, computing and caching it if |
| 44 // Some headers may not be set given the |incognito| mode or whether | 35 // necessary. |
| 45 // the user has |uma_enabled|. Also, we never transmit headers to non-Google | 36 std::string GetClientDataHeader(); |
| 46 // sites, which is checked based on the destination |url|. | |
| 47 void AppendHeaders(const GURL& url, | |
| 48 bool incognito, | |
| 49 bool uma_enabled, | |
| 50 net::HttpRequestHeaders* headers); | |
| 51 | 37 |
| 52 // Sets *additional* variation ids and trigger variation ids to be encoded in | 38 // Sets *additional* variation ids and trigger variation ids to be encoded in |
| 53 // the X-Client-Data request header. This is intended for development use to | 39 // the X-Client-Data request header. This is intended for development use to |
| 54 // force a server side experiment id. |variation_ids| should be a | 40 // force a server side experiment id. |variation_ids| should be a |
| 55 // comma-separated string of numeric experiment ids. If an id is prefixed | 41 // comma-separated string of numeric experiment ids. If an id is prefixed |
| 56 // with "t" it will be treated as a trigger experiment id. | 42 // with "t" it will be treated as a trigger experiment id. |
| 57 bool SetDefaultVariationIds(const std::string& variation_ids); | 43 bool SetDefaultVariationIds(const std::string& variation_ids); |
| 58 | 44 |
| 59 // Returns the HTTP header names which are added in this class. | |
| 60 std::set<std::string> GetVariationHeaderNames() const; | |
| 61 | |
| 62 // Resets any cached state for tests. | 45 // Resets any cached state for tests. |
| 63 void ResetForTesting(); | 46 void ResetForTesting(); |
| 64 | 47 |
| 65 private: | 48 private: |
| 66 friend struct base::DefaultSingletonTraits<VariationsHttpHeaderProvider>; | 49 friend struct base::DefaultSingletonTraits<VariationsHttpHeaderProvider>; |
| 67 | 50 |
| 68 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | 51 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
| 69 ShouldAppendHeaders); | |
| 70 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | |
| 71 SetDefaultVariationIds_Valid); | 52 SetDefaultVariationIds_Valid); |
| 72 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | 53 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
| 73 SetDefaultVariationIds_Invalid); | 54 SetDefaultVariationIds_Invalid); |
| 74 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | 55 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, |
| 75 OnFieldTrialGroupFinalized); | 56 OnFieldTrialGroupFinalized); |
| 76 | 57 |
| 77 VariationsHttpHeaderProvider(); | 58 VariationsHttpHeaderProvider(); |
| 78 ~VariationsHttpHeaderProvider() override; | 59 ~VariationsHttpHeaderProvider() override; |
| 79 | 60 |
| 80 // base::FieldTrialList::Observer: | 61 // base::FieldTrialList::Observer: |
| 81 // This will add the variation ID associated with |trial_name| and | 62 // This will add the variation ID associated with |trial_name| and |
| 82 // |group_name| to the variation ID cache. | 63 // |group_name| to the variation ID cache. |
| 83 void OnFieldTrialGroupFinalized(const std::string& trial_name, | 64 void OnFieldTrialGroupFinalized(const std::string& trial_name, |
| 84 const std::string& group_name) override; | 65 const std::string& group_name) override; |
| 85 | 66 |
| 86 // metrics::SyntheticTrialObserver: | 67 // metrics::SyntheticTrialObserver: |
| 87 void OnSyntheticTrialsChanged( | 68 void OnSyntheticTrialsChanged( |
| 88 const std::vector<metrics::SyntheticTrialGroup>& groups) override; | 69 const std::vector<SyntheticTrialGroup>& groups) override; |
| 89 | 70 |
| 90 // Prepares the variation IDs cache with initial values if not already done. | 71 // Prepares the variation IDs cache with initial values if not already done. |
| 91 // This method also registers the caller with the FieldTrialList to receive | 72 // This method also registers the caller with the FieldTrialList to receive |
| 92 // new variation IDs. | 73 // new variation IDs. |
| 93 void InitVariationIDsCacheIfNeeded(); | 74 void InitVariationIDsCacheIfNeeded(); |
| 94 | 75 |
| 95 // Takes whatever is currently in |variation_ids_set_| and recreates | 76 // Takes whatever is currently in |variation_ids_set_| and recreates |
| 96 // |variation_ids_header_| with it. Assumes the the |lock_| is currently | 77 // |variation_ids_header_| with it. Assumes the the |lock_| is currently |
| 97 // held. | 78 // held. |
| 98 void UpdateVariationIDsHeaderValue(); | 79 void UpdateVariationIDsHeaderValue(); |
| 99 | 80 |
| 100 // Checks whether variation headers should be appended to requests to the | |
| 101 // specified |url|. Returns true for google.<TLD> and youtube.<TLD> URLs. | |
| 102 static bool ShouldAppendHeaders(const GURL& url); | |
| 103 | |
| 104 // Guards |variation_ids_cache_initialized_|, |variation_ids_set_| and | 81 // Guards |variation_ids_cache_initialized_|, |variation_ids_set_| and |
| 105 // |variation_ids_header_|. | 82 // |variation_ids_header_|. |
| 106 base::Lock lock_; | 83 base::Lock lock_; |
| 107 | 84 |
| 108 // Whether or not we've initialized the cache. | 85 // Whether or not we've initialized the cache. |
| 109 bool variation_ids_cache_initialized_; | 86 bool variation_ids_cache_initialized_; |
| 110 | 87 |
| 111 // Keep a cache of variation IDs that are transmitted in headers to Google. | 88 // Keep a cache of variation IDs that are transmitted in headers to Google. |
| 112 // This consists of a list of valid IDs, and the actual transmitted header. | 89 // This consists of a list of valid IDs, and the actual transmitted header. |
| 113 std::set<VariationID> variation_ids_set_; | 90 std::set<VariationID> variation_ids_set_; |
| 114 std::set<VariationID> variation_trigger_ids_set_; | 91 std::set<VariationID> variation_trigger_ids_set_; |
| 115 | 92 |
| 116 // Provides the google experiment ids forced from command line. | 93 // Provides the google experiment ids forced from command line. |
| 117 std::set<VariationID> default_variation_ids_set_; | 94 std::set<VariationID> default_variation_ids_set_; |
| 118 std::set<VariationID> default_trigger_id_set_; | 95 std::set<VariationID> default_trigger_id_set_; |
| 119 | 96 |
| 120 // Variations ids from synthetic field trials. | 97 // Variations ids from synthetic field trials. |
| 121 std::set<VariationID> synthetic_variation_ids_set_; | 98 std::set<VariationID> synthetic_variation_ids_set_; |
| 122 | 99 |
| 123 std::string variation_ids_header_; | 100 std::string variation_ids_header_; |
| 124 | 101 |
| 125 DISALLOW_COPY_AND_ASSIGN(VariationsHttpHeaderProvider); | 102 DISALLOW_COPY_AND_ASSIGN(VariationsHttpHeaderProvider); |
| 126 }; | 103 }; |
| 127 | 104 |
| 128 } // namespace variations | 105 } // namespace variations |
| 129 | 106 |
| 130 #endif // COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADER_PROVIDER_H_ | 107 #endif // COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_ |
| OLD | NEW |