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
;
29 template <typename T
> struct DefaultSingletonTraits
;
31 namespace variations
{
33 // A helper class for maintaining client experiments and metrics state
34 // transmitted in custom HTTP request headers.
35 // This class is a thread-safe singleton.
36 class VariationsHttpHeaderProvider
: public base::FieldTrialList::Observer
,
37 public metrics::SyntheticTrialObserver
{
39 static VariationsHttpHeaderProvider
* GetInstance();
41 // Adds Chrome experiment and metrics state as custom headers to |headers|.
42 // Some headers may not be set given the |incognito| mode or whether
43 // the user has |uma_enabled|. Also, we never transmit headers to non-Google
44 // sites, which is checked based on the destination |url|.
45 void AppendHeaders(const GURL
& url
,
48 net::HttpRequestHeaders
* headers
);
50 // Sets *additional* variation ids and trigger variation ids to be encoded in
51 // the X-Client-Data request header. This is intended for development use to
52 // force a server side experiment id. |variation_ids| should be a
53 // comma-separated string of numeric experiment ids. If an id is prefixed
54 // with "t" it will be treated as a trigger experiment id.
55 bool SetDefaultVariationIds(const std::string
& variation_ids
);
57 // Returns the HTTP header names which are added in this class.
58 std::set
<std::string
> GetVariationHeaderNames() const;
60 // Resets any cached state for tests.
61 void ResetForTesting();
64 friend struct DefaultSingletonTraits
<VariationsHttpHeaderProvider
>;
66 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest
,
68 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest
,
69 SetDefaultVariationIds_Valid
);
70 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest
,
71 SetDefaultVariationIds_Invalid
);
72 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest
,
73 OnFieldTrialGroupFinalized
);
75 VariationsHttpHeaderProvider();
76 ~VariationsHttpHeaderProvider() override
;
78 // base::FieldTrialList::Observer:
79 // This will add the variation ID associated with |trial_name| and
80 // |group_name| to the variation ID cache.
81 void OnFieldTrialGroupFinalized(const std::string
& trial_name
,
82 const std::string
& group_name
) override
;
84 // metrics::SyntheticTrialObserver:
85 void OnSyntheticTrialsChanged(
86 const std::vector
<metrics::SyntheticTrialGroup
>& groups
) override
;
88 // Prepares the variation IDs cache with initial values if not already done.
89 // This method also registers the caller with the FieldTrialList to receive
91 void InitVariationIDsCacheIfNeeded();
93 // Takes whatever is currently in |variation_ids_set_| and recreates
94 // |variation_ids_header_| with it. Assumes the the |lock_| is currently
96 void UpdateVariationIDsHeaderValue();
98 // Checks whether variation headers should be appended to requests to the
99 // specified |url|. Returns true for google.<TLD> and youtube.<TLD> URLs.
100 static bool ShouldAppendHeaders(const GURL
& url
);
102 // Guards |variation_ids_cache_initialized_|, |variation_ids_set_| and
103 // |variation_ids_header_|.
106 // Whether or not we've initialized the cache.
107 bool variation_ids_cache_initialized_
;
109 // Keep a cache of variation IDs that are transmitted in headers to Google.
110 // This consists of a list of valid IDs, and the actual transmitted header.
111 std::set
<VariationID
> variation_ids_set_
;
112 std::set
<VariationID
> variation_trigger_ids_set_
;
114 // Provides the google experiment ids forced from command line.
115 std::set
<VariationID
> default_variation_ids_set_
;
116 std::set
<VariationID
> default_trigger_id_set_
;
118 // Variations ids from synthetic field trials.
119 std::set
<VariationID
> synthetic_variation_ids_set_
;
121 std::string variation_ids_header_
;
123 DISALLOW_COPY_AND_ASSIGN(VariationsHttpHeaderProvider
);
126 } // namespace variations
128 #endif // COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADER_PROVIDER_H_