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