summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net/prerender_interceptor.cc
blob: c1e5698b40be31a1736d471c278dc451e33bd618 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// 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/net/prerender_interceptor.h"

#include <string>

#include "base/logging.h"
#include "chrome/browser/browser_thread.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/io_thread.h"
#include "googleurl/src/gurl.h"
#include "net/base/load_flags.h"

DISABLE_RUNNABLE_METHOD_REFCOUNT(chrome_browser_net::PrerenderInterceptor);

namespace chrome_browser_net {

PrerenderInterceptor::PrerenderInterceptor()
    : ALLOW_THIS_IN_INITIALIZER_LIST(
        callback_(NewCallback(this,
                              &PrerenderInterceptor::PrerenderDispatch))) {
  URLRequest::RegisterRequestInterceptor(this);
}

PrerenderInterceptor::PrerenderInterceptor(
    PrerenderInterceptorCallback* callback)
  : callback_(callback) {
  URLRequest::RegisterRequestInterceptor(this);
}

PrerenderInterceptor::~PrerenderInterceptor() {
  URLRequest::UnregisterRequestInterceptor(this);
}

net::URLRequestJob* PrerenderInterceptor::MaybeIntercept(
    net::URLRequest* request) {
  return NULL;
}

net::URLRequestJob* PrerenderInterceptor::MaybeInterceptResponse(
    net::URLRequest* request) {
  // TODO(gavinp): unfortunately, we can't figure out the origin
  // of this request here on systems where the referrer is blocked by
  // configuration.

  // TODO(gavinp): note that the response still might be intercepted
  // by a later interceptor.  Should we write an interposing delegate
  // and only prerender dispatch on requests that aren't intercepted?
  // Or is this a slippery slope?

  if (request->load_flags() & net::LOAD_PREFETCH) {
    std::string mime_type;
    request->GetMimeType(&mime_type);
    if (mime_type == "text/html")
      BrowserThread::PostTask(
          BrowserThread::UI,
          FROM_HERE,
          NewRunnableMethod(this,
                            &PrerenderInterceptor::RunCallbackFromUIThread,
                            request->url()));
  }
  return NULL;
}

void PrerenderInterceptor::RunCallbackFromUIThread(const GURL& url) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  callback_->Run(url);
}

void PrerenderInterceptor::PrerenderDispatch(
    const GURL& url) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  DVLOG(2) << "PrerenderDispatchOnUIThread: url=" << url;
}

}  // namespace chrome_browser_net