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.
5 #ifndef COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADER_PROVIDER_H_
6 #define COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADER_PROVIDER_H_
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"
20 class ResourceContext
;
24 class HttpRequestHeaders
;
30 template <typename T
> struct DefaultSingletonTraits
;
33 namespace variations
{
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
{
41 static VariationsHttpHeaderProvider
* GetInstance();
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
,
50 net::HttpRequestHeaders
* headers
);
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
);
59 // Returns the HTTP header names which are added in this class.
60 std::set
<std::string
> GetVariationHeaderNames() const;
62 // Resets any cached state for tests.
63 void ResetForTesting();
66 friend struct base::DefaultSingletonTraits
<VariationsHttpHeaderProvider
>;
68 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest
,
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
);
77 VariationsHttpHeaderProvider();
78 ~VariationsHttpHeaderProvider() override
;
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
;
86 // metrics::SyntheticTrialObserver:
87 void OnSyntheticTrialsChanged(
88 const std::vector
<metrics::SyntheticTrialGroup
>& groups
) override
;
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
93 void InitVariationIDsCacheIfNeeded();
95 // Takes whatever is currently in |variation_ids_set_| and recreates
96 // |variation_ids_header_| with it. Assumes the the |lock_| is currently
98 void UpdateVariationIDsHeaderValue();
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
);
104 // Guards |variation_ids_cache_initialized_|, |variation_ids_set_| and
105 // |variation_ids_header_|.
108 // Whether or not we've initialized the cache.
109 bool variation_ids_cache_initialized_
;
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_
;
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_
;
120 // Variations ids from synthetic field trials.
121 std::set
<VariationID
> synthetic_variation_ids_set_
;
123 std::string variation_ids_header_
;
125 DISALLOW_COPY_AND_ASSIGN(VariationsHttpHeaderProvider
);
128 } // namespace variations
130 #endif // COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADER_PROVIDER_H_