Index: chrome/browser/chrome_metrics_helper.cc |
diff --git a/chrome/browser/chrome_metrics_helper.cc b/chrome/browser/chrome_metrics_helper.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b6132f7e3cead4e703bc00b73ad70d19bb9f2a0c |
--- /dev/null |
+++ b/chrome/browser/chrome_metrics_helper.cc |
@@ -0,0 +1,163 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/chrome_metrics_helper.h" |
+ |
+#include "base/base64.h" |
+#include "base/memory/singleton.h" |
+#include "chrome/browser/google/google_util.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/profiles/profile_io_data.h" |
+#include "chrome/common/metrics/proto/chrome_experiments.pb.h" |
+#include "chrome/common/metrics/variations/variations_util.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "net/http/http_request_headers.h" |
+#include "net/url_request/url_fetcher.h" |
+#include "net/url_request/url_request.h" |
+ |
+using base::AutoLock; |
+using content::BrowserThread; |
Peter Kasting
2012/12/11 21:08:48
Nit: Normally we avoid using directives unless the
Bart N.
2012/12/11 23:18:56
Done for AutoLock, however the existing BrowserThr
|
+ |
+ChromeMetricsHelper* ChromeMetricsHelper::GetInstance() { |
+ return Singleton<ChromeMetricsHelper>::get(); |
+} |
+ |
+ChromeMetricsHelper::ChromeMetricsHelper() |
Peter Kasting
2012/12/11 21:08:48
Nit: Function definition order must match declarat
Bart N.
2012/12/11 23:18:56
Done.
|
+ : variation_ids_cache_initialized_(false) { |
+} |
+ |
+ChromeMetricsHelper::~ChromeMetricsHelper() { |
+} |
+ |
+void ChromeMetricsHelper::AppendHeadersToRequest( |
Peter Kasting
2012/12/11 21:08:48
Nit: Based on the implementations of these two fun
Bart N.
2012/12/11 23:18:56
I think it's a good suggestion. I made this class
SteveT
2012/12/13 16:24:08
+1 to this class not knowing about Fetches vs Requ
Bart N.
2012/12/14 18:21:42
Done.
|
+ net::URLRequest* request, |
+ content::ResourceContext* resource_context) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
Peter Kasting
2012/12/11 21:08:48
You may need to add a call to IsWellKnownThread()
Bart N.
2012/12/11 23:18:56
Good to know. Anyway, this the old code.
Done.
|
+ |
+ // Don't attempt to append headers to requests that have already started. |
+ // TODO(stevet): Remove this once the request ordering issues are resolved |
+ // in crbug.com/128048. |
+ if (request->is_pending()) |
+ return; |
+ |
+ net::HttpRequestHeaders headers; |
+ headers.CopyFrom(request->extra_request_headers()); |
+ ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context); |
+ AppendHeaders(request->url(), io_data->is_incognito(), |
+ io_data->GetMetricsEnabledStateOnIOThread(), &headers); |
+ request->SetExtraRequestHeaders(headers); |
+} |
+ |
+void ChromeMetricsHelper::AppendHeadersToFetcher(net::URLFetcher* fetcher, |
+ const Profile* profile) { |
+ net::HttpRequestHeaders headers; |
+ AppendHeaders(fetcher->GetOriginalURL(), profile->IsOffTheRecord(), false, |
+ &headers); |
+ fetcher->SetExtraRequestHeaders(headers.ToString()); |
+} |
+ |
+void ChromeMetricsHelper::AppendHeaders( |
+ const GURL& url, |
+ bool incognito, |
+ bool uma_enabled, |
+ net::HttpRequestHeaders* headers) { |
+ |
+ // Note the criteria for attaching Chrome experiment headers: |
+ // 1. We only transmit to *.google.<TLD> domains. NOTE that this use of |
+ // google_util helpers to check this does not guarantee that the URL is |
+ // Google-owned, only that it is of the form *.google.<TLD>. In the future |
+ // we may choose to reinforce this check. |
+ // 2. Only transmit for non-Incognito profiles. |
+ // 3. For the X-Chrome-UMA-Enabled bit, only set it if UMA is in fact enabled |
+ // for this install of Chrome. |
+ // 4. For the X-Chrome-Variations, only include non-empty variation IDs. |
+ if (incognito || |
+ !google_util::IsGoogleDomainUrl(url.spec(), |
+ google_util::ALLOW_SUBDOMAIN, |
+ google_util::ALLOW_NON_STANDARD_PORTS)) |
+ return; |
+ |
+ if (uma_enabled) |
+ headers->SetHeaderIfMissing("X-Chrome-UMA-Enabled", "1"); |
+ |
+ // Lazily initialize the header, if not already done, before attempting to |
+ // transmit it. |
+ InitVariationIDsCacheIfNeeded(); |
+ AutoLock scoped_lock(lock_); |
+ if (!variation_ids_header_.empty()) |
+ headers->SetHeaderIfMissing("X-Chrome-Variations", variation_ids_header_); |
+} |
+ |
+void ChromeMetricsHelper::OnFieldTrialGroupFinalized( |
Peter Kasting
2012/12/11 21:08:48
Is this supposed to be called exclusively on one p
Bart N.
2012/12/11 23:18:56
I think it can be called only from the IO thread (
SteveT
2012/12/13 16:24:08
So this method can be called on this class after t
Bart N.
2012/12/14 18:21:42
I think so. However, I've realized that the existi
SteveT
2012/12/14 18:35:41
This seems like a general issue with the pattern,
Bart N.
2012/12/16 00:09:09
No idea. In our case, both IO/UI have it.
Done.
|
+ const std::string& trial_name, |
+ const std::string& group_name) { |
+ chrome_variations::VariationID new_id = |
+ chrome_variations::GetGoogleVariationID( |
+ chrome_variations::GOOGLE_WEB_PROPERTIES, trial_name, group_name); |
+ if (new_id == chrome_variations::kEmptyID) |
+ return; |
+ AutoLock scoped_lock(lock_); |
+ variation_ids_set_.insert(new_id); |
+ UpdateVariationIDsHeaderValue(); |
+} |
+ |
+void ChromeMetricsHelper::InitVariationIDsCacheIfNeeded() { |
+ AutoLock scoped_lock(lock_); |
+ if (variation_ids_cache_initialized_) |
+ return; |
+ |
+ // Register for additional cache updates. This is done first to avoid a race |
+ // that could cause registered FieldTrials to be missed. |
+ base::FieldTrialList::AddObserver(this); |
+ |
+ base::FieldTrial::ActiveGroups initial_groups; |
+ base::FieldTrialList::GetActiveFieldTrialGroups(&initial_groups); |
+ for (base::FieldTrial::ActiveGroups::const_iterator it = |
+ initial_groups.begin(); it != initial_groups.end(); ++it) { |
+ const chrome_variations::VariationID id = |
+ chrome_variations::GetGoogleVariationID( |
+ chrome_variations::GOOGLE_WEB_PROPERTIES, it->trial_name, |
+ it->group_name); |
+ if (id != chrome_variations::kEmptyID) |
+ variation_ids_set_.insert(id); |
+ } |
+ UpdateVariationIDsHeaderValue(); |
+ |
+ variation_ids_cache_initialized_ = true; |
+} |
+ |
+void ChromeMetricsHelper::UpdateVariationIDsHeaderValue() { |
Peter Kasting
2012/12/11 21:08:48
Nit: While it's technically not necessary since bo
Bart N.
2012/12/11 23:18:56
Doesn't it incur extra CPU cost? This method is pr
Peter Kasting
2012/12/12 00:01:06
I don't know about the cost, but I can't imagine i
Bart N.
2012/12/12 02:06:17
OK. Two things:
(1) The cost of acquiring a lock i
|
+ // The header value is a serialized protobuffer of Variation IDs which is |
+ // base64 encoded before transmitting as a string. |
+ if (variation_ids_set_.empty()) |
+ return; |
+ |
+ // This is the bottleneck for the creation of the header, so validate the size |
+ // here. Force a hard maximum on the ID count in case the Variations server |
+ // returns too many IDs and DOSs receiving servers with large requests. |
+ DCHECK_LE(variation_ids_set_.size(), 10U); |
+ if (variation_ids_set_.size() > 20) { |
Peter Kasting
2012/12/11 21:08:48
You cannot handle DCHECK failure. A DCHECK means
Bart N.
2012/12/11 23:18:56
I'll leave it up to Steve to answer this question;
SteveT
2012/12/13 16:24:08
Yeah so this was added as a double safety:
* The
|
+ variation_ids_header_.clear(); |
+ return; |
+ } |
+ |
+ metrics::ChromeVariations proto; |
+ for (std::set<chrome_variations::VariationID>::const_iterator it = |
+ variation_ids_set_.begin(); it != variation_ids_set_.end(); ++it) |
+ proto.add_variation_id(*it); |
+ |
+ std::string serialized; |
+ proto.SerializeToString(&serialized); |
+ |
+ std::string hashed; |
+ if (base::Base64Encode(serialized, &hashed)) { |
+ // If successful, swap the header value with the new one. |
+ // Note that the list of IDs and the header could be temporarily out of sync |
+ // if IDs are added as the header is recreated. The receiving servers are OK |
+ // with such descrepancies. |
+ variation_ids_header_ = hashed; |
+ } else { |
+ DVLOG(1) << "Failed to base64 encode Variation IDs value: " << serialized; |
Peter Kasting
2012/12/11 21:08:48
Is logging really what you want here? Note that i
Bart N.
2012/12/11 23:18:56
Steve?
SteveT
2012/12/13 16:24:08
Hmm, yeah, I believe we were just trying to follow
|
+ } |
+} |