1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_RENDERER_HOST_OFFLINE_RESOURCE_HANDLER_H_
6#define CHROME_BROWSER_RENDERER_HOST_OFFLINE_RESOURCE_HANDLER_H_
7#pragma once
8
9#include <string>
10
11#include "base/memory/ref_counted.h"
12#include "chrome/browser/chromeos/offline/offline_load_page.h"
13#include "content/browser/renderer_host/resource_handler.h"
14#include "net/base/completion_callback.h"
15
16class MessageLoop;
17class ResourceDispatcherHost;
18
19namespace net {
20class URLRequest;
21}  // namespace net
22
23// Used to show an offline interstitial page when the network is not available.
24class OfflineResourceHandler : public ResourceHandler,
25                               public chromeos::OfflineLoadPage::Delegate {
26 public:
27  OfflineResourceHandler(ResourceHandler* handler,
28                         int host_id,
29                         int render_view_id,
30                         ResourceDispatcherHost* rdh,
31                         net::URLRequest* request);
32  virtual ~OfflineResourceHandler();
33
34  // ResourceHandler implementation:
35  virtual bool OnUploadProgress(int request_id, uint64 position, uint64 size);
36  virtual bool OnRequestRedirected(int request_id, const GURL& new_url,
37                           ResourceResponse* response, bool* defer);
38  virtual bool OnResponseStarted(int request_id, ResourceResponse* response);
39  virtual bool OnWillStart(int request_id, const GURL& url, bool* defer);
40  virtual bool OnWillRead(int request_id, net::IOBuffer** buf, int* buf_size,
41                          int min_size);
42  virtual bool OnReadCompleted(int request_id, int* bytes_read);
43  virtual bool OnResponseCompleted(int request_id,
44                                   const net::URLRequestStatus& status,
45                                   const std::string& security_info);
46  virtual void OnRequestClosed();
47
48  // chromeos::OfflineLoadPage::Delegate
49  virtual void OnBlockingPageComplete(bool proceed);
50
51 private:
52  // Erease the state assocaited with a deferred load request.
53  void ClearRequestInfo();
54  bool IsRemote(const GURL& url) const;
55
56  // Resume the deferred load request.
57  void Resume();
58
59  // Tells if chrome should show the offline page.
60  bool ShouldShowOfflinePage(const GURL& url) const;
61
62  // Shows the offline interstitinal page in UI thread.
63  void ShowOfflinePage();
64
65  // A callback to tell if an appcache exists.
66  void OnCanHandleOfflineComplete(int rv);
67
68  scoped_refptr<ResourceHandler> next_handler_;
69
70  int process_host_id_;
71  int render_view_id_;
72  ResourceDispatcherHost* rdh_;
73  net::URLRequest* request_;
74
75  // The state for deferred load quest.
76  int deferred_request_id_;
77  GURL deferred_url_;
78
79  scoped_refptr<net::CancelableCompletionCallback<OfflineResourceHandler> >
80      appcache_completion_callback_;
81
82  DISALLOW_COPY_AND_ASSIGN(OfflineResourceHandler);
83};
84
85#endif  // CHROME_BROWSER_RENDERER_HOST_OFFLINE_RESOURCE_HANDLER_H_
86