Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / components / variations / net / variations_http_header_provider.h
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.
4
5 #ifndef COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADER_PROVIDER_H_
6 #define COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADER_PROVIDER_H_
7
8 #include <set>
9 #include <string>
10 #include <vector>
11
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"
18
19 namespace content {
20 class ResourceContext;
21 }
22
23 namespace net {
24 class HttpRequestHeaders;
25 }
26
27 class GURL;
28
29 template <typename T> struct DefaultSingletonTraits;
30
31 namespace variations {
32
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();
40
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
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);
56
57   // Returns the HTTP header names which are added in this class.
58   std::set<std::string> GetVariationHeaderNames() const;
59
60  private:
61   friend struct DefaultSingletonTraits<VariationsHttpHeaderProvider>;
62
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);
71
72   VariationsHttpHeaderProvider();
73   ~VariationsHttpHeaderProvider() override;
74
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;
80
81   // metrics::SyntheticTrialObserver:
82   void OnSyntheticTrialsChanged(
83       const std::vector<metrics::SyntheticTrialGroup>& groups) override;
84
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();
89
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();
94
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);
98
99   // Guards |variation_ids_cache_initialized_|, |variation_ids_set_| and
100   // |variation_ids_header_|.
101   base::Lock lock_;
102
103   // Whether or not we've initialized the cache.
104   bool variation_ids_cache_initialized_;
105
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_;
110
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_;
114
115   // Variations ids from synthetic field trials.
116   std::set<VariationID> synthetic_variation_ids_set_;
117
118   std::string variation_ids_header_;
119
120   DISALLOW_COPY_AND_ASSIGN(VariationsHttpHeaderProvider);
121 };
122
123 }  // namespace variations
124
125 #endif  // COMPONENTS_VARIATIONS_NET_VARIATIONS_HTTP_HEADER_PROVIDER_H_