summaryrefslogtreecommitdiffstats
path: root/chrome/browser/renderer_host/offline_resource_throttle.cc
blob: a7f370c504057a6d6cd687c739668fdab9210e74 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// 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/renderer_host/offline_resource_throttle.h"

#include <vector>

#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
#include "base/metrics/histogram.h"
#include "base/string_util.h"
#include "chrome/browser/chromeos/offline/offline_load_page.h"
#include "chrome/browser/net/chrome_url_request_context.h"
#include "chrome/common/url_constants.h"
#include "content/browser/renderer_host/resource_dispatcher_host.h"
#include "content/browser/renderer_host/resource_dispatcher_host_request_info.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_view_host_delegate.h"
#include "content/public/browser/resource_context.h"
#include "content/public/browser/resource_throttle_controller.h"
#include "net/base/net_errors.h"
#include "net/base/network_change_notifier.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_context.h"
#include "webkit/appcache/appcache_service.h"

using content::BrowserThread;
using content::ResourceContext;
using content::WebContents;

namespace {

void ShowOfflinePage(
    int render_process_id,
    int render_view_id,
    const GURL& url,
    const chromeos::OfflineLoadPage::CompletionCallback& callback) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  // Check again on UI thread and proceed if it's connected.
  if (!net::NetworkChangeNotifier::IsOffline()) {
    BrowserThread::PostTask(
        BrowserThread::IO, FROM_HERE, base::Bind(callback, true));
  } else {
    RenderViewHost* render_view_host =
        RenderViewHost::FromID(render_process_id, render_view_id);
    WebContents* web_contents = render_view_host ?
        render_view_host->GetDelegate()->GetAsWebContents() : NULL;
    // There is a chance that the tab closed after we decided to show
    // the offline page on the IO thread and before we actually show the
    // offline page here on the UI thread.
    if (web_contents)
      (new chromeos::OfflineLoadPage(web_contents, url, callback))->Show();
  }
}

}  // namespace

OfflineResourceThrottle::OfflineResourceThrottle(
    int render_process_id,
    int render_view_id,
    net::URLRequest* request,
    content::ResourceContext* resource_context)
    : render_process_id_(render_process_id),
      render_view_id_(render_view_id),
      request_(request),
      resource_context_(resource_context) {
  DCHECK(resource_context);
}

OfflineResourceThrottle::~OfflineResourceThrottle() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));

  if (!appcache_completion_callback_.IsCancelled())
    appcache_completion_callback_.Cancel();
}

void OfflineResourceThrottle::WillStartRequest(bool* defer) {
  if (!ShouldShowOfflinePage(request_->url()))
    return;

  DVLOG(1) << "WillStartRequest: this=" << this << ", url=" << request_->url();

  DCHECK(appcache_completion_callback_.IsCancelled());

  appcache_completion_callback_.Reset(
      base::Bind(&OfflineResourceThrottle::OnCanHandleOfflineComplete,
                 AsWeakPtr()));
  ResourceContext::GetAppCacheService(resource_context_)->
      CanHandleMainResourceOffline(
          request_->url(),
          request_->first_party_for_cookies(),
          appcache_completion_callback_.callback());

  *defer = true;
}

void OfflineResourceThrottle::OnBlockingPageComplete(bool proceed) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));

  if (proceed) {
    controller()->Resume();
  } else {
    controller()->Cancel();
  }
}

bool OfflineResourceThrottle::IsRemote(const GURL& url) const {
  return url.SchemeIs(chrome::kFtpScheme) ||
         url.SchemeIs(chrome::kHttpScheme) ||
         url.SchemeIs(chrome::kHttpsScheme);
}

bool OfflineResourceThrottle::ShouldShowOfflinePage(const GURL& url) const {
  // If the network is disconnected while loading other resources, we'll simply
  // show broken link/images.
  return IsRemote(url) && net::NetworkChangeNotifier::IsOffline();
}

void OfflineResourceThrottle::OnCanHandleOfflineComplete(int rv) {
  appcache_completion_callback_.Cancel();

  if (rv == net::OK) {
    controller()->Resume();
  } else {
    BrowserThread::PostTask(
        BrowserThread::UI,
        FROM_HERE,
        base::Bind(
            &ShowOfflinePage,
            render_process_id_,
            render_view_id_,
            request_->url(),
            base::Bind(
                &OfflineResourceThrottle::OnBlockingPageComplete,
                AsWeakPtr())));
  }
}