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

Unified Diff: chrome/browser/prerender/prerender_resource_handler.cc

Issue 5912001: Add PrerenderResourceHandler and hook it into the ResourceDispatcherHost.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Remove extra comment. Created 10 years 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/prerender/prerender_resource_handler.cc
diff --git a/chrome/browser/prerender/prerender_resource_handler.cc b/chrome/browser/prerender/prerender_resource_handler.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c015fdc95937be6eb96d5c25659e690614d3e1ec
--- /dev/null
+++ b/chrome/browser/prerender/prerender_resource_handler.cc
@@ -0,0 +1,125 @@
+// Copyright (c) 2010 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/prerender/prerender_resource_handler.h"
+
+#include "chrome/common/resource_response.h"
+#include "net/base/load_flags.h"
+#include "net/http/http_response_headers.h"
+#include "net/url_request/url_request.h"
+
+PrerenderResourceHandler::PrerenderResourceHandler(
+ ResourceHandler* next_handler,
+ PrerenderManager* prerender_manager)
+ : next_handler_(next_handler),
+ prerender_manager_(prerender_manager),
+ ALLOW_THIS_IN_INITIALIZER_LIST(
+ prerender_callback_(NewCallback(
+ this, &PrerenderResourceHandler::StartPrerender))) {
+ DCHECK(next_handler);
+ DCHECK(prerender_manager);
+}
+
+// This constructor is only used from unit tests.
+PrerenderResourceHandler::PrerenderResourceHandler(
+ ResourceHandler* next_handler,
+ PrerenderCallback* callback)
+ : next_handler_(next_handler),
+ prerender_callback_(callback) {
+ DCHECK(next_handler);
+ }
willchan no longer on Chromium 2010/12/17 21:45:17 indentation
cbentzel 2010/12/19 00:40:56 Done.
+
+PrerenderResourceHandler::~PrerenderResourceHandler() {
+}
+
+bool PrerenderResourceHandler::OnUploadProgress(int request_id,
+ uint64 position,
+ uint64 size) {
+ return next_handler_->OnUploadProgress(request_id, position, size);
+}
+
+bool PrerenderResourceHandler::OnRequestRedirected(int request_id,
+ const GURL& url,
+ ResourceResponse* response,
+ bool* defer) {
+ bool will_redirect = next_handler_->OnRequestRedirected(
+ request_id, url, response, defer);
+ if (will_redirect)
+ url_ = url;
+ return will_redirect;
+}
+
+bool PrerenderResourceHandler::OnResponseStarted(int request_id,
+ ResourceResponse* response) {
+ if (ShouldPrerender(url_, response)) {
+ BrowserThread::PostTask(
+ BrowserThread::UI,
+ FROM_HERE,
+ NewRunnableMethod(
+ this,
+ &PrerenderResourceHandler::RunCallbackFromUIThread,
+ url_));
+ }
+ return next_handler_->OnResponseStarted(request_id, response);
+}
+
+bool PrerenderResourceHandler::OnWillStart(int request_id,
+ const GURL& url,
+ bool* defer) {
+ bool will_start = next_handler_->OnWillStart(request_id, url, defer);
+ if (will_start)
+ url_ = url;
+ return will_start;
+}
+
+bool PrerenderResourceHandler::OnWillRead(int request_id,
+ net::IOBuffer** buf,
+ int* buf_size,
+ int min_size) {
+ return next_handler_->OnWillRead(request_id, buf, buf_size, min_size);
+}
+
+bool PrerenderResourceHandler::OnReadCompleted(int request_id,
+ int* bytes_read) {
+ return next_handler_->OnReadCompleted(request_id, bytes_read);
+}
+
+bool PrerenderResourceHandler::OnResponseCompleted(
+ int request_id,
+ const URLRequestStatus& status,
+ const std::string& security_info) {
+ return next_handler_->OnResponseCompleted(request_id, status, security_info);
+}
+
+void PrerenderResourceHandler::OnRequestClosed() {
+ next_handler_->OnRequestClosed();
+}
+
+void PrerenderResourceHandler::RunCallbackFromUIThread(const GURL& url) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ prerender_callback_->Run(url);
darin (slow to review) 2010/12/17 19:58:21 there seems to be the assumption here that GET req
cbentzel 2010/12/17 20:03:51 I can add that to ShouldPrerender - this is called
cbentzel 2010/12/19 00:40:56 Now, prerendering only happens if the top-level re
+}
+
+void PrerenderResourceHandler::StartPrerender(const GURL& url) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ prerender_manager_->AddPreload(url);
+}
+
+// static
+bool PrerenderResourceHandler::CouldPrerender(const net::URLRequest* request) {
+ return ((request->load_flags() & net::LOAD_PREFETCH) &&
+ request->method() == "GET");
+}
+
+// static
+bool PrerenderResourceHandler::ShouldPrerender(
willchan no longer on Chromium 2010/12/17 21:45:17 I'm not sure why this is a private static function
cbentzel 2010/12/19 00:40:56 Gavin had the same concern. I changed it.
+ const GURL& url,
+ const ResourceResponse* response) {
+ return (url.is_valid() &&
+ (url.SchemeIs("http") || url.SchemeIs("https")) &&
+ response->response_head.headers &&
+ response->response_head.headers->response_code() == 200 &&
+ response->response_head.mime_type == "text/html");
+}
+

Powered by Google App Engine
This is Rietveld 408576698