Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(219)

Unified Diff: chrome/browser/precache/precache_util.cc

Issue 2061303003: Precache should cancel when there is user traffic (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comment Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/precache/precache_util.cc
diff --git a/chrome/browser/precache/precache_util.cc b/chrome/browser/precache/precache_util.cc
new file mode 100644
index 0000000000000000000000000000000000000000..97326b70a1e94a20b865fd02a9611f1ee9fad72a
--- /dev/null
+++ b/chrome/browser/precache/precache_util.cc
@@ -0,0 +1,66 @@
+// Copyright (c) 2016 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/precache/precache_util.h"
+
+#include "chrome/browser/browser_process.h"
+#include "chrome/browser/precache/precache_manager_factory.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/profiles/profile_manager.h"
+#include "components/data_use_measurement/content/data_use_measurement.h"
+#include "components/precache/content/precache_manager.h"
+#include "content/public/browser/browser_thread.h"
+
+namespace {
+
+void UpdatePrecacheMetricsAndStateOnUIThread(const GURL& url,
+ const GURL& referrer,
+ base::TimeDelta latency,
+ const base::Time& fetch_time,
+ int64_t size,
+ bool was_cached,
+ bool is_user_traffic,
+ void* profile_id) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+
+ if (!g_browser_process->profile_manager()->IsValidProfile(profile_id))
+ return;
+ Profile* profile = reinterpret_cast<Profile*>(profile_id);
+
+ precache::PrecacheManager* precache_manager =
+ precache::PrecacheManagerFactory::GetForBrowserContext(profile);
+ // |precache_manager| could be NULL if the profile is off the record.
+ if (!precache_manager || !precache_manager->IsPrecachingAllowed())
+ return;
+
+ precache_manager->UpdatePrecacheMetricsAndState(
+ url, referrer, latency, fetch_time, size, was_cached, is_user_traffic);
+}
+
+} // namespace
+
+namespace precache {
+
+// TODO(rajendrant): Add unittests for this function.
+void UpdatePrecacheMetricsAndState(const net::URLRequest* request,
bengr 2016/07/06 17:04:27 #include url_request.h
Raj 2016/07/06 18:28:23 Done.
+ void* profile_id) {
bengr 2016/07/06 17:04:27 You should probably DCHECK_CURRENTLY_ON(content::B
Raj 2016/07/06 18:28:23 Done.
+ // For better accuracy, we use the actual bytes read instead of the length
+ // specified with the Content-Length header, which may be inaccurate,
+ // or missing, as is the case with chunked encoding.
+ int64_t received_content_length = request->received_response_content_length();
+ base::TimeDelta latency = base::TimeTicks::Now() - request->creation_time();
bengr 2016/07/06 17:04:27 #include time.h
Raj 2016/07/06 18:28:22 Done.
+
+ // Record precache metrics when a fetch is completed successfully, if
+ // precaching is allowed.
+ content::BrowserThread::PostTask(
+ content::BrowserThread::UI, FROM_HERE,
+ base::Bind(
+ &UpdatePrecacheMetricsAndStateOnUIThread, request->url(),
+ GURL(request->referrer()), latency, base::Time::Now(),
bengr 2016/07/06 17:04:27 #include gurl.h
Raj 2016/07/06 18:28:22 Done.
+ received_content_length, request->was_cached(),
+ data_use_measurement::DataUseMeasurement::IsUserTraffic(request),
+ profile_id));
+}
+
+} // namespace precache

Powered by Google App Engine
This is Rietveld 408576698