blob: 457aaabef6a9613fa0548716c32410bdb2e4a4aa [file] [log] [blame]
[email protected]71011c1682014-07-09 17:19:161// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]bd3b4712012-12-18 17:01:302// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
isherman3be67db2014-10-24 05:57:445#ifndef COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADER_PROVIDER_H_
6#define COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADER_PROVIDER_H_
[email protected]bd3b4712012-12-18 17:01:307
8#include <set>
9#include <string>
asvitkinee0dbdbe2014-10-31 21:59:5710#include <vector>
[email protected]bd3b4712012-12-18 17:01:3011
12#include "base/basictypes.h"
[email protected]7f8a9932013-07-26 20:43:3413#include "base/gtest_prod_util.h"
[email protected]bd3b4712012-12-18 17:01:3014#include "base/metrics/field_trial.h"
15#include "base/synchronization/lock.h"
asvitkinee0dbdbe2014-10-31 21:59:5716#include "components/metrics/metrics_service.h"
[email protected]50ae9f12013-08-29 18:03:2217#include "components/variations/variations_associated_data.h"
[email protected]bd3b4712012-12-18 17:01:3018
19namespace content {
20class ResourceContext;
21}
22
23namespace net {
24class HttpRequestHeaders;
25}
26
27class GURL;
[email protected]bd3b4712012-12-18 17:01:3028
29template <typename T> struct DefaultSingletonTraits;
30
[email protected]71011c1682014-07-09 17:19:1631namespace variations {
[email protected]ab7780792013-01-10 01:26:0932
[email protected]ea15bd52014-07-14 22:42:5033// A helper class for maintaining client experiments and metrics state
[email protected]bd3b4712012-12-18 17:01:3034// transmitted in custom HTTP request headers.
35// This class is a thread-safe singleton.
asvitkinee0dbdbe2014-10-31 21:59:5736class VariationsHttpHeaderProvider : public base::FieldTrialList::Observer,
37 public metrics::SyntheticTrialObserver {
[email protected]bd3b4712012-12-18 17:01:3038 public:
[email protected]ab7780792013-01-10 01:26:0939 static VariationsHttpHeaderProvider* GetInstance();
[email protected]bd3b4712012-12-18 17:01:3040
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);
49
[email protected]8c2c5442014-04-04 18:55:2950 // 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.
[email protected]1bd918d2013-10-13 18:23:0955 bool SetDefaultVariationIds(const std::string& variation_ids);
56
horoe09b6c82014-11-01 02:08:2857 // Returns the HTTP header names which are added in this class.
58 std::set<std::string> GetVariationHeaderNames() const;
59
asvitkineb4ed78682015-03-12 18:18:5460 // Resets any cached state for tests.
61 void ResetForTesting();
62
[email protected]bd3b4712012-12-18 17:01:3063 private:
[email protected]ab7780792013-01-10 01:26:0964 friend struct DefaultSingletonTraits<VariationsHttpHeaderProvider>;
[email protected]bd3b4712012-12-18 17:01:3065
[email protected]7f8a9932013-07-26 20:43:3466 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest,
67 ShouldAppendHeaders);
[email protected]1bd918d2013-10-13 18:23:0968 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest,
69 SetDefaultVariationIds_Valid);
70 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest,
71 SetDefaultVariationIds_Invalid);
[email protected]e51dcb0c2014-05-06 16:56:1072 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest,
73 OnFieldTrialGroupFinalized);
[email protected]7f8a9932013-07-26 20:43:3474
[email protected]ab7780792013-01-10 01:26:0975 VariationsHttpHeaderProvider();
dcheng00ea022b2014-10-21 11:24:5676 ~VariationsHttpHeaderProvider() override;
[email protected]bd3b4712012-12-18 17:01:3077
asvitkinee0dbdbe2014-10-31 21:59:5778 // base::FieldTrialList::Observer:
[email protected]bd3b4712012-12-18 17:01:3079 // This will add the variation ID associated with |trial_name| and
80 // |group_name| to the variation ID cache.
dcheng00ea022b2014-10-21 11:24:5681 void OnFieldTrialGroupFinalized(const std::string& trial_name,
82 const std::string& group_name) override;
[email protected]bd3b4712012-12-18 17:01:3083
asvitkinee0dbdbe2014-10-31 21:59:5784 // metrics::SyntheticTrialObserver:
85 void OnSyntheticTrialsChanged(
86 const std::vector<metrics::SyntheticTrialGroup>& groups) override;
87
[email protected]bd3b4712012-12-18 17:01:3088 // Prepares the variation IDs cache with initial values if not already done.
89 // This method also registers the caller with the FieldTrialList to receive
90 // new variation IDs.
91 void InitVariationIDsCacheIfNeeded();
92
93 // Takes whatever is currently in |variation_ids_set_| and recreates
[email protected]ab7780792013-01-10 01:26:0994 // |variation_ids_header_| with it. Assumes the the |lock_| is currently
95 // held.
[email protected]bd3b4712012-12-18 17:01:3096 void UpdateVariationIDsHeaderValue();
97
[email protected]7f8a9932013-07-26 20:43:3498 // 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);
101
[email protected]bd3b4712012-12-18 17:01:30102 // Guards |variation_ids_cache_initialized_|, |variation_ids_set_| and
103 // |variation_ids_header_|.
104 base::Lock lock_;
105
106 // Whether or not we've initialized the cache.
107 bool variation_ids_cache_initialized_;
108
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.
[email protected]59b6f672014-07-26 18:35:47111 std::set<VariationID> variation_ids_set_;
112 std::set<VariationID> variation_trigger_ids_set_;
[email protected]1bd918d2013-10-13 18:23:09113
114 // Provides the google experiment ids forced from command line.
[email protected]59b6f672014-07-26 18:35:47115 std::set<VariationID> default_variation_ids_set_;
116 std::set<VariationID> default_trigger_id_set_;
[email protected]8c2c5442014-04-04 18:55:29117
asvitkinee0dbdbe2014-10-31 21:59:57118 // Variations ids from synthetic field trials.
119 std::set<VariationID> synthetic_variation_ids_set_;
120
[email protected]bd3b4712012-12-18 17:01:30121 std::string variation_ids_header_;
122
[email protected]ab7780792013-01-10 01:26:09123 DISALLOW_COPY_AND_ASSIGN(VariationsHttpHeaderProvider);
[email protected]bd3b4712012-12-18 17:01:30124};
125
[email protected]71011c1682014-07-09 17:19:16126} // namespace variations
[email protected]ab7780792013-01-10 01:26:09127
isherman3be67db2014-10-24 05:57:44128#endif // COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADER_PROVIDER_H_