Home | History | Annotate | Download | only in appcache
      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 
     14 namespace appcache {
     15 
     16 class AppCacheRequestHandler;
     17 class AppCacheService;
     18 
     19 // An interceptor to hijack requests and potentially service them out of
     20 // the appcache.
     21 class 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   static AppCacheInterceptor* GetInstance();
     44 
     45  protected:
     46   // Override from net::URLRequest::Interceptor:
     47   virtual net::URLRequestJob* MaybeIntercept(
     48       net::URLRequest* request,
     49       net::NetworkDelegate* network_delegate) OVERRIDE;
     50   virtual net::URLRequestJob* MaybeInterceptResponse(
     51       net::URLRequest* request,
     52       net::NetworkDelegate* network_delegate) OVERRIDE;
     53   virtual net::URLRequestJob* MaybeInterceptRedirect(
     54       net::URLRequest* request,
     55       net::NetworkDelegate* network_delegate,
     56       const GURL& location) OVERRIDE;
     57 
     58  private:
     59   friend struct DefaultSingletonTraits<AppCacheInterceptor>;
     60 
     61   AppCacheInterceptor();
     62   virtual ~AppCacheInterceptor();
     63 
     64   static void SetHandler(net::URLRequest* request,
     65                          AppCacheRequestHandler* handler);
     66   static AppCacheRequestHandler* GetHandler(net::URLRequest* request);
     67 
     68   DISALLOW_COPY_AND_ASSIGN(AppCacheInterceptor);
     69 };
     70 
     71 }  // namespace appcache
     72 
     73 #endif  // WEBKIT_BROWSER_APPCACHE_APPCACHE_INTERCEPTOR_H_
     74