1// Copyright 2014 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 CONTENT_BROWSER_APPCACHE_APPCACHE_INTERCEPTOR_H_
6#define CONTENT_BROWSER_APPCACHE_APPCACHE_INTERCEPTOR_H_
7
8#include "base/memory/singleton.h"
9#include "content/common/content_export.h"
10#include "content/public/common/resource_type.h"
11#include "net/url_request/url_request.h"
12#include "url/gurl.h"
13
14namespace content {
15class AppCacheRequestHandler;
16class AppCacheServiceImpl;
17
18// An interceptor to hijack requests and potentially service them out of
19// the appcache.
20class CONTENT_EXPORT AppCacheInterceptor
21    : public net::URLRequest::Interceptor {
22 public:
23  // Registers a singleton instance with the net library.
24  // Should be called early in the IO thread prior to initiating requests.
25  static void EnsureRegistered() {
26    CHECK(GetInstance());
27  }
28
29  // Must be called to make a request eligible for retrieval from an appcache.
30  static void SetExtraRequestInfo(net::URLRequest* request,
31                                  AppCacheServiceImpl* service,
32                                  int process_id,
33                                  int host_id,
34                                  ResourceType resource_type);
35
36  // May be called after response headers are complete to retrieve extra
37  // info about the response.
38  static void GetExtraResponseInfo(net::URLRequest* request,
39                                   int64* cache_id,
40                                   GURL* manifest_url);
41
42  // Methods to support cross site navigations.
43  static void PrepareForCrossSiteTransfer(net::URLRequest* request,
44                                          int old_process_id);
45  static void CompleteCrossSiteTransfer(net::URLRequest* request,
46                                        int new_process_id,
47                                        int new_host_id);
48
49  static AppCacheInterceptor* GetInstance();
50
51 protected:
52  // Override from net::URLRequest::Interceptor:
53  virtual net::URLRequestJob* MaybeIntercept(
54      net::URLRequest* request,
55      net::NetworkDelegate* network_delegate) OVERRIDE;
56  virtual net::URLRequestJob* MaybeInterceptResponse(
57      net::URLRequest* request,
58      net::NetworkDelegate* network_delegate) OVERRIDE;
59  virtual net::URLRequestJob* MaybeInterceptRedirect(
60      net::URLRequest* request,
61      net::NetworkDelegate* network_delegate,
62      const GURL& location) OVERRIDE;
63
64 private:
65  friend struct DefaultSingletonTraits<AppCacheInterceptor>;
66
67  AppCacheInterceptor();
68  virtual ~AppCacheInterceptor();
69
70  static void SetHandler(net::URLRequest* request,
71                         AppCacheRequestHandler* handler);
72  static AppCacheRequestHandler* GetHandler(net::URLRequest* request);
73
74  DISALLOW_COPY_AND_ASSIGN(AppCacheInterceptor);
75};
76
77}  // namespace content
78
79#endif  // CONTENT_BROWSER_APPCACHE_APPCACHE_INTERCEPTOR_H_
80