Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / components / variations / net / variations_http_header_provider.h
blob83fa5469550776645dd5b5e9596e72a2e44cb9ff
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_
8 #include <set>
9 #include <string>
10 #include <vector>
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"
19 namespace content {
20 class ResourceContext;
23 namespace net {
24 class HttpRequestHeaders;
27 class GURL;
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 {
38 public:
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,
46 bool incognito,
47 bool uma_enabled,
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 private:
61 friend struct DefaultSingletonTraits<VariationsHttpHeaderProvider>;
63 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest,
64 ShouldAppendHeaders);
65 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest,
66 SetDefaultVariationIds_Valid);
67 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest,
68 SetDefaultVariationIds_Invalid);
69 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest,
70 OnFieldTrialGroupFinalized);
72 VariationsHttpHeaderProvider();
73 ~VariationsHttpHeaderProvider() override;
75 // base::FieldTrialList::Observer:
76 // This will add the variation ID associated with |trial_name| and
77 // |group_name| to the variation ID cache.
78 void OnFieldTrialGroupFinalized(const std::string& trial_name,
79 const std::string& group_name) override;
81 // metrics::SyntheticTrialObserver:
82 void OnSyntheticTrialsChanged(
83 const std::vector<metrics::SyntheticTrialGroup>& groups) override;
85 // Prepares the variation IDs cache with initial values if not already done.
86 // This method also registers the caller with the FieldTrialList to receive
87 // new variation IDs.
88 void InitVariationIDsCacheIfNeeded();
90 // Takes whatever is currently in |variation_ids_set_| and recreates
91 // |variation_ids_header_| with it. Assumes the the |lock_| is currently
92 // held.
93 void UpdateVariationIDsHeaderValue();
95 // Checks whether variation headers should be appended to requests to the
96 // specified |url|. Returns true for google.<TLD> and youtube.<TLD> URLs.
97 static bool ShouldAppendHeaders(const GURL& url);
99 // Guards |variation_ids_cache_initialized_|, |variation_ids_set_| and
100 // |variation_ids_header_|.
101 base::Lock lock_;
103 // Whether or not we've initialized the cache.
104 bool variation_ids_cache_initialized_;
106 // Keep a cache of variation IDs that are transmitted in headers to Google.
107 // This consists of a list of valid IDs, and the actual transmitted header.
108 std::set<VariationID> variation_ids_set_;
109 std::set<VariationID> variation_trigger_ids_set_;
111 // Provides the google experiment ids forced from command line.
112 std::set<VariationID> default_variation_ids_set_;
113 std::set<VariationID> default_trigger_id_set_;
115 // Variations ids from synthetic field trials.
116 std::set<VariationID> synthetic_variation_ids_set_;
118 std::string variation_ids_header_;
120 DISALLOW_COPY_AND_ASSIGN(VariationsHttpHeaderProvider);
123 } // namespace variations
125 #endif // COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADER_PROVIDER_H_