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

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: Fix unit tests. 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..7d1eccea9f02d3d9135318298017a722638460a3
--- /dev/null
+++ b/chrome/browser/prerender/prerender_resource_handler.cc
@@ -0,0 +1,126 @@
+// 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);
+ }
+
+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)) {
+ // DCHECK(url_.is_valid());
gavinp 2010/12/17 16:15:33 lose the comment.
cbentzel 2010/12/17 16:23:21 Done.
+ 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);
+}
+
+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(
gavinp 2010/12/17 16:15:33 anon namespace?
cbentzel 2010/12/17 16:23:21 Huh? These are static methods on PrerenderResource
+ 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