Home | History | Annotate | Download | only in loader
      1 // Copyright (c) 2012 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_LOADER_RESOURCE_MESSAGE_FILTER_H_
      6 #define CONTENT_BROWSER_LOADER_RESOURCE_MESSAGE_FILTER_H_
      7 
      8 #include "base/memory/scoped_ptr.h"
      9 #include "content/common/content_export.h"
     10 #include "content/public/browser/browser_message_filter.h"
     11 #include "webkit/common/resource_type.h"
     12 
     13 namespace fileapi {
     14 class FileSystemContext;
     15 }  // namespace fileapi
     16 
     17 namespace net {
     18 class URLRequestContext;
     19 }  // namespace net
     20 
     21 
     22 namespace content {
     23 class ChromeAppCacheService;
     24 class ChromeBlobStorageContext;
     25 class ResourceContext;
     26 
     27 // This class filters out incoming IPC messages for network requests and
     28 // processes them on the IPC thread.  As a result, network requests are not
     29 // delayed by costly UI processing that may be occuring on the main thread of
     30 // the browser.  It also means that any hangs in starting a network request
     31 // will not interfere with browser UI.
     32 class CONTENT_EXPORT ResourceMessageFilter : public BrowserMessageFilter {
     33  public:
     34   // Allows selecting the net::URLRequestContext used to service requests.
     35   class URLRequestContextSelector {
     36    public:
     37     URLRequestContextSelector() {}
     38     virtual ~URLRequestContextSelector() {}
     39 
     40     virtual net::URLRequestContext* GetRequestContext(
     41         ResourceType::Type request_type) = 0;
     42 
     43    private:
     44     DISALLOW_COPY_AND_ASSIGN(URLRequestContextSelector);
     45   };
     46 
     47   ResourceMessageFilter(
     48       int child_id,
     49       int process_type,
     50       ResourceContext* resource_context,
     51       ChromeAppCacheService* appcache_service,
     52       ChromeBlobStorageContext* blob_storage_context,
     53       fileapi::FileSystemContext* file_system_context,
     54       URLRequestContextSelector* url_request_context_selector);
     55 
     56   // BrowserMessageFilter implementation.
     57   virtual void OnChannelClosing() OVERRIDE;
     58   virtual bool OnMessageReceived(const IPC::Message& message,
     59                                  bool* message_was_ok) OVERRIDE;
     60 
     61   ResourceContext* resource_context() const {
     62     return resource_context_;
     63   }
     64 
     65   ChromeAppCacheService* appcache_service() const {
     66     return appcache_service_.get();
     67   }
     68 
     69   ChromeBlobStorageContext* blob_storage_context() const {
     70     return blob_storage_context_.get();
     71   }
     72 
     73   fileapi::FileSystemContext* file_system_context() const {
     74     return file_system_context_.get();
     75   }
     76 
     77   // Returns the net::URLRequestContext for the given request.
     78   net::URLRequestContext* GetURLRequestContext(
     79       ResourceType::Type request_type);
     80 
     81   int child_id() const { return child_id_; }
     82   int process_type() const { return process_type_; }
     83 
     84  protected:
     85   // Protected destructor so that we can be overriden in tests.
     86   virtual ~ResourceMessageFilter();
     87 
     88  private:
     89   // The ID of the child process.
     90   int child_id_;
     91 
     92   int process_type_;
     93 
     94   // Owned by ProfileIOData* which is guaranteed to outlive us.
     95   ResourceContext* resource_context_;
     96 
     97   scoped_refptr<ChromeAppCacheService> appcache_service_;
     98   scoped_refptr<ChromeBlobStorageContext> blob_storage_context_;
     99   scoped_refptr<fileapi::FileSystemContext> file_system_context_;
    100 
    101   const scoped_ptr<URLRequestContextSelector> url_request_context_selector_;
    102 
    103   DISALLOW_IMPLICIT_CONSTRUCTORS(ResourceMessageFilter);
    104 };
    105 
    106 }  // namespace content
    107 
    108 #endif  // CONTENT_BROWSER_LOADER_RESOURCE_MESSAGE_FILTER_H_
    109