| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADER_PROVIDER_H_ | |
| 6 #define COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADER_PROVIDER_H_ | |
| 7 | |
| 8 #include <set> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/gtest_prod_util.h" | |
| 14 #include "base/metrics/field_trial.h" | |
| 15 #include "base/synchronization/lock.h" | |
| 16 #include "components/metrics/metrics_service.h" | |
| 17 #include "components/variations/variations_associated_data.h" | |
| 18 | |
| 19 namespace content { | |
| 20 class ResourceContext; | |
| 21 } | |
| 22 | |
| 23 namespace net { | |
| 24 class HttpRequestHeaders; | |
| 25 } | |
| 26 | |
| 27 class GURL; | |
| 28 | |
| 29 namespace base { | |
| 30 template <typename T> struct DefaultSingletonTraits; | |
| 31 } | |
| 32 | |
| 33 namespace variations { | |
| 34 | |
| 35 // A helper class for maintaining client experiments and metrics state | |
| 36 // transmitted in custom HTTP request headers. | |
| 37 // This class is a thread-safe singleton. | |
| 38 class VariationsHttpHeaderProvider : public base::FieldTrialList::Observer, | |
| 39 public metrics::SyntheticTrialObserver { | |
| 40 public: | |
| 41 static VariationsHttpHeaderProvider* GetInstance(); | |
| 42 | |
| 43 // Adds Chrome experiment and metrics state as custom headers to |headers|. | |
| 44 // Some headers may not be set given the |incognito| mode or whether | |
| 45 // the user has |uma_enabled|. Also, we never transmit headers to non-Google | |
| 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 | |
| 52 // 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 | |
| 54 // force a server side experiment id. |variation_ids| should be a | |
| 55 // comma-separated string of numeric experiment ids. If an id is prefixed | |
| 56 // with "t" it will be treated as a trigger experiment id. | |
| 57 bool SetDefaultVariationIds(const std::string& variation_ids); | |
| 58 | |
| 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. | |
| 63 void ResetForTesting(); | |
| 64 | |
| 65 private: | |
| 66 friend struct base::DefaultSingletonTraits<VariationsHttpHeaderProvider>; | |
| 67 | |
| 68 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | |
| 69 ShouldAppendHeaders); | |
| 70 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | |
| 71 SetDefaultVariationIds_Valid); | |
| 72 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | |
| 73 SetDefaultVariationIds_Invalid); | |
| 74 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | |
| 75 OnFieldTrialGroupFinalized); | |
| 76 | |
| 77 VariationsHttpHeaderProvider(); | |
| 78 ~VariationsHttpHeaderProvider() override; | |
| 79 | |
| 80 // base::FieldTrialList::Observer: | |
| 81 // This will add the variation ID associated with |trial_name| and | |
| 82 // |group_name| to the variation ID cache. | |
| 83 void OnFieldTrialGroupFinalized(const std::string& trial_name, | |
| 84 const std::string& group_name) override; | |
| 85 | |
| 86 // metrics::SyntheticTrialObserver: | |
| 87 void OnSyntheticTrialsChanged( | |
| 88 const std::vector<metrics::SyntheticTrialGroup>& groups) override; | |
| 89 | |
| 90 // Prepares the variation IDs cache with initial values if not already done. | |
| 91 // This method also registers the caller with the FieldTrialList to receive | |
| 92 // new variation IDs. | |
| 93 void InitVariationIDsCacheIfNeeded(); | |
| 94 | |
| 95 // Takes whatever is currently in |variation_ids_set_| and recreates | |
| 96 // |variation_ids_header_| with it. Assumes the the |lock_| is currently | |
| 97 // held. | |
| 98 void UpdateVariationIDsHeaderValue(); | |
| 99 | |
| 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 | |
| 105 // |variation_ids_header_|. | |
| 106 base::Lock lock_; | |
| 107 | |
| 108 // Whether or not we've initialized the cache. | |
| 109 bool variation_ids_cache_initialized_; | |
| 110 | |
| 111 // 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. | |
| 113 std::set<VariationID> variation_ids_set_; | |
| 114 std::set<VariationID> variation_trigger_ids_set_; | |
| 115 | |
| 116 // Provides the google experiment ids forced from command line. | |
| 117 std::set<VariationID> default_variation_ids_set_; | |
| 118 std::set<VariationID> default_trigger_id_set_; | |
| 119 | |
| 120 // Variations ids from synthetic field trials. | |
| 121 std::set<VariationID> synthetic_variation_ids_set_; | |
| 122 | |
| 123 std::string variation_ids_header_; | |
| 124 | |
| 125 DISALLOW_COPY_AND_ASSIGN(VariationsHttpHeaderProvider); | |
| 126 }; | |
| 127 | |
| 128 } // namespace variations | |
| 129 | |
| 130 #endif // COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADER_PROVIDER_H_ | |
| OLD | NEW |