Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / metrics / variations / variations_http_header_provider.h
blob744b84dc6cc17840273243ad16eb0081901c4867
1 // Copyright (c) 2012 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 CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_
6 #define CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_
8 #include <set>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/metrics/field_trial.h"
14 #include "base/synchronization/lock.h"
15 #include "components/variations/variations_associated_data.h"
17 namespace content {
18 class ResourceContext;
21 namespace net {
22 class HttpRequestHeaders;
25 class GURL;
26 class Profile;
27 class ProfileIOData;
29 template <typename T> struct DefaultSingletonTraits;
31 namespace chrome_variations {
33 // A helper class for maintaining Chrome experiments and metrics state
34 // transmitted in custom HTTP request headers.
35 // This class is a thread-safe singleton.
36 class VariationsHttpHeaderProvider : base::FieldTrialList::Observer {
37 public:
38 static VariationsHttpHeaderProvider* GetInstance();
40 // Adds Chrome experiment and metrics state as custom headers to |headers|.
41 // Some headers may not be set given the |incognito| mode or whether
42 // the user has |uma_enabled|. Also, we never transmit headers to non-Google
43 // sites, which is checked based on the destination |url|.
44 void AppendHeaders(const GURL& url,
45 bool incognito,
46 bool uma_enabled,
47 net::HttpRequestHeaders* headers);
49 // Sets *additional* variation ids to be encoded in the X-Client-Data
50 // request header. This is intended for development use to force a server
51 // side experiment id. |variation_ids| should be a comma-separated string of
52 // numeric experiment ids.
53 bool SetDefaultVariationIds(const std::string& variation_ids);
55 private:
56 friend struct DefaultSingletonTraits<VariationsHttpHeaderProvider>;
58 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest,
59 ShouldAppendHeaders);
60 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest,
61 SetDefaultVariationIds_Valid);
62 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest,
63 SetDefaultVariationIds_Invalid);
65 VariationsHttpHeaderProvider();
66 virtual ~VariationsHttpHeaderProvider();
68 // base::FieldTrialList::Observer implementation.
69 // This will add the variation ID associated with |trial_name| and
70 // |group_name| to the variation ID cache.
71 virtual void OnFieldTrialGroupFinalized(
72 const std::string& trial_name,
73 const std::string& group_name) OVERRIDE;
75 // Prepares the variation IDs cache with initial values if not already done.
76 // This method also registers the caller with the FieldTrialList to receive
77 // new variation IDs.
78 void InitVariationIDsCacheIfNeeded();
80 // Takes whatever is currently in |variation_ids_set_| and recreates
81 // |variation_ids_header_| with it. Assumes the the |lock_| is currently
82 // held.
83 void UpdateVariationIDsHeaderValue();
85 // Checks whether variation headers should be appended to requests to the
86 // specified |url|. Returns true for google.<TLD> and youtube.<TLD> URLs.
87 static bool ShouldAppendHeaders(const GURL& url);
89 // Guards |variation_ids_cache_initialized_|, |variation_ids_set_| and
90 // |variation_ids_header_|.
91 base::Lock lock_;
93 // Whether or not we've initialized the cache.
94 bool variation_ids_cache_initialized_;
96 // Keep a cache of variation IDs that are transmitted in headers to Google.
97 // This consists of a list of valid IDs, and the actual transmitted header.
98 std::set<chrome_variations::VariationID> variation_ids_set_;
100 // Provides the google experiment ids forced from command line.
101 std::set<chrome_variations::VariationID> default_variation_ids_set_;
102 std::string variation_ids_header_;
104 DISALLOW_COPY_AND_ASSIGN(VariationsHttpHeaderProvider);
107 } // namespace chrome_variations
109 #endif // CHROME_BROWSER_METRICS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_