blob: 2386a5f3fd12ea09d94d4fa2c00d0e8a8324790e [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
[email protected]71011c1682014-07-09 17:19:165#ifndef COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_
6#define COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_
[email protected]bd3b4712012-12-18 17:01:307
8#include <set>
9#include <string>
10
11#include "base/basictypes.h"
[email protected]7f8a9932013-07-26 20:43:3412#include "base/gtest_prod_util.h"
[email protected]bd3b4712012-12-18 17:01:3013#include "base/metrics/field_trial.h"
14#include "base/synchronization/lock.h"
[email protected]50ae9f12013-08-29 18:03:2215#include "components/variations/variations_associated_data.h"
[email protected]bd3b4712012-12-18 17:01:3016
17namespace content {
18class ResourceContext;
19}
20
21namespace net {
22class HttpRequestHeaders;
23}
24
25class GURL;
[email protected]bd3b4712012-12-18 17:01:3026
27template <typename T> struct DefaultSingletonTraits;
28
[email protected]71011c1682014-07-09 17:19:1629namespace variations {
[email protected]ab7780792013-01-10 01:26:0930
[email protected]ea15bd52014-07-14 22:42:5031// A helper class for maintaining client experiments and metrics state
[email protected]bd3b4712012-12-18 17:01:3032// transmitted in custom HTTP request headers.
33// This class is a thread-safe singleton.
[email protected]ab7780792013-01-10 01:26:0934class VariationsHttpHeaderProvider : base::FieldTrialList::Observer {
[email protected]bd3b4712012-12-18 17:01:3035 public:
[email protected]ab7780792013-01-10 01:26:0936 static VariationsHttpHeaderProvider* GetInstance();
[email protected]bd3b4712012-12-18 17:01:3037
38 // Adds Chrome experiment and metrics state as custom headers to |headers|.
39 // Some headers may not be set given the |incognito| mode or whether
40 // the user has |uma_enabled|. Also, we never transmit headers to non-Google
41 // sites, which is checked based on the destination |url|.
42 void AppendHeaders(const GURL& url,
43 bool incognito,
44 bool uma_enabled,
45 net::HttpRequestHeaders* headers);
46
[email protected]8c2c5442014-04-04 18:55:2947 // Sets *additional* variation ids and trigger variation ids to be encoded in
48 // the X-Client-Data request header. This is intended for development use to
49 // force a server side experiment id. |variation_ids| should be a
50 // comma-separated string of numeric experiment ids. If an id is prefixed
51 // with "t" it will be treated as a trigger experiment id.
[email protected]1bd918d2013-10-13 18:23:0952 bool SetDefaultVariationIds(const std::string& variation_ids);
53
[email protected]bd3b4712012-12-18 17:01:3054 private:
[email protected]ab7780792013-01-10 01:26:0955 friend struct DefaultSingletonTraits<VariationsHttpHeaderProvider>;
[email protected]bd3b4712012-12-18 17:01:3056
[email protected]7f8a9932013-07-26 20:43:3457 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest,
58 ShouldAppendHeaders);
[email protected]1bd918d2013-10-13 18:23:0959 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest,
60 SetDefaultVariationIds_Valid);
61 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest,
62 SetDefaultVariationIds_Invalid);
[email protected]e51dcb0c2014-05-06 16:56:1063 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest,
64 OnFieldTrialGroupFinalized);
[email protected]7f8a9932013-07-26 20:43:3465
[email protected]ab7780792013-01-10 01:26:0966 VariationsHttpHeaderProvider();
67 virtual ~VariationsHttpHeaderProvider();
[email protected]bd3b4712012-12-18 17:01:3068
69 // base::FieldTrialList::Observer implementation.
70 // This will add the variation ID associated with |trial_name| and
71 // |group_name| to the variation ID cache.
72 virtual void OnFieldTrialGroupFinalized(
73 const std::string& trial_name,
74 const std::string& group_name) OVERRIDE;
75
76 // Prepares the variation IDs cache with initial values if not already done.
77 // This method also registers the caller with the FieldTrialList to receive
78 // new variation IDs.
79 void InitVariationIDsCacheIfNeeded();
80
81 // Takes whatever is currently in |variation_ids_set_| and recreates
[email protected]ab7780792013-01-10 01:26:0982 // |variation_ids_header_| with it. Assumes the the |lock_| is currently
83 // held.
[email protected]bd3b4712012-12-18 17:01:3084 void UpdateVariationIDsHeaderValue();
85
[email protected]7f8a9932013-07-26 20:43:3486 // Checks whether variation headers should be appended to requests to the
87 // specified |url|. Returns true for google.<TLD> and youtube.<TLD> URLs.
88 static bool ShouldAppendHeaders(const GURL& url);
89
[email protected]bd3b4712012-12-18 17:01:3090 // Guards |variation_ids_cache_initialized_|, |variation_ids_set_| and
91 // |variation_ids_header_|.
92 base::Lock lock_;
93
94 // Whether or not we've initialized the cache.
95 bool variation_ids_cache_initialized_;
96
97 // Keep a cache of variation IDs that are transmitted in headers to Google.
98 // This consists of a list of valid IDs, and the actual transmitted header.
[email protected]59b6f672014-07-26 18:35:4799 std::set<VariationID> variation_ids_set_;
100 std::set<VariationID> variation_trigger_ids_set_;
[email protected]1bd918d2013-10-13 18:23:09101
102 // Provides the google experiment ids forced from command line.
[email protected]59b6f672014-07-26 18:35:47103 std::set<VariationID> default_variation_ids_set_;
104 std::set<VariationID> default_trigger_id_set_;
[email protected]8c2c5442014-04-04 18:55:29105
[email protected]bd3b4712012-12-18 17:01:30106 std::string variation_ids_header_;
107
[email protected]ab7780792013-01-10 01:26:09108 DISALLOW_COPY_AND_ASSIGN(VariationsHttpHeaderProvider);
[email protected]bd3b4712012-12-18 17:01:30109};
110
[email protected]71011c1682014-07-09 17:19:16111} // namespace variations
[email protected]ab7780792013-01-10 01:26:09112
[email protected]71011c1682014-07-09 17:19:16113#endif // COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_