OLD | NEW |
| (Empty) |
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_VARIATIONS_HTTP_HEADER_PROVIDER_H_ | |
6 #define COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_ | |
7 | |
8 #include <set> | |
9 #include <string> | |
10 | |
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" | |
16 | |
17 namespace content { | |
18 class ResourceContext; | |
19 } | |
20 | |
21 namespace net { | |
22 class HttpRequestHeaders; | |
23 } | |
24 | |
25 class GURL; | |
26 | |
27 template <typename T> struct DefaultSingletonTraits; | |
28 | |
29 namespace variations { | |
30 | |
31 // A helper class for maintaining client experiments and metrics state | |
32 // transmitted in custom HTTP request headers. | |
33 // This class is a thread-safe singleton. | |
34 class VariationsHttpHeaderProvider : base::FieldTrialList::Observer { | |
35 public: | |
36 static VariationsHttpHeaderProvider* GetInstance(); | |
37 | |
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 | |
47 // 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. | |
52 bool SetDefaultVariationIds(const std::string& variation_ids); | |
53 | |
54 private: | |
55 friend struct DefaultSingletonTraits<VariationsHttpHeaderProvider>; | |
56 | |
57 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | |
58 ShouldAppendHeaders); | |
59 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | |
60 SetDefaultVariationIds_Valid); | |
61 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | |
62 SetDefaultVariationIds_Invalid); | |
63 FRIEND_TEST_ALL_PREFIXES(VariationsHttpHeaderProviderTest, | |
64 OnFieldTrialGroupFinalized); | |
65 | |
66 VariationsHttpHeaderProvider(); | |
67 ~VariationsHttpHeaderProvider() override; | |
68 | |
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 void OnFieldTrialGroupFinalized(const std::string& trial_name, | |
73 const std::string& group_name) override; | |
74 | |
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(); | |
79 | |
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(); | |
84 | |
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); | |
88 | |
89 // Guards |variation_ids_cache_initialized_|, |variation_ids_set_| and | |
90 // |variation_ids_header_|. | |
91 base::Lock lock_; | |
92 | |
93 // Whether or not we've initialized the cache. | |
94 bool variation_ids_cache_initialized_; | |
95 | |
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<VariationID> variation_ids_set_; | |
99 std::set<VariationID> variation_trigger_ids_set_; | |
100 | |
101 // Provides the google experiment ids forced from command line. | |
102 std::set<VariationID> default_variation_ids_set_; | |
103 std::set<VariationID> default_trigger_id_set_; | |
104 | |
105 std::string variation_ids_header_; | |
106 | |
107 DISALLOW_COPY_AND_ASSIGN(VariationsHttpHeaderProvider); | |
108 }; | |
109 | |
110 } // namespace variations | |
111 | |
112 #endif // COMPONENTS_VARIATIONS_VARIATIONS_HTTP_HEADER_PROVIDER_H_ | |
OLD | NEW |