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/callback_forward.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/weak_ptr.h" 11 #include "content/common/content_export.h" 12 #include "content/public/browser/browser_message_filter.h" 13 #include "webkit/common/resource_type.h" 14 15 struct ResourceHostMsg_Request; 16 17 namespace fileapi { 18 class FileSystemContext; 19 } // namespace fileapi 20 21 namespace net { 22 class URLRequestContext; 23 } // namespace net 24 25 26 namespace content { 27 class ChromeAppCacheService; 28 class ChromeBlobStorageContext; 29 class ResourceContext; 30 31 // This class filters out incoming IPC messages for network requests and 32 // processes them on the IPC thread. As a result, network requests are not 33 // delayed by costly UI processing that may be occuring on the main thread of 34 // the browser. It also means that any hangs in starting a network request 35 // will not interfere with browser UI. 36 class CONTENT_EXPORT ResourceMessageFilter : public BrowserMessageFilter { 37 public: 38 typedef base::Callback<void(const ResourceHostMsg_Request&, 39 ResourceContext**, 40 net::URLRequestContext**)> GetContextsCallback; 41 42 // |appcache_service|, |blob_storage_context|, |file_system_context| may be 43 // NULL in unittests or for requests from the (NPAPI) plugin process. 44 ResourceMessageFilter( 45 int child_id, 46 int process_type, 47 ChromeAppCacheService* appcache_service, 48 ChromeBlobStorageContext* blob_storage_context, 49 fileapi::FileSystemContext* file_system_context, 50 const GetContextsCallback& get_contexts_callback); 51 52 // BrowserMessageFilter implementation. 53 virtual void OnChannelClosing() OVERRIDE; 54 virtual bool OnMessageReceived(const IPC::Message& message, 55 bool* message_was_ok) OVERRIDE; 56 57 void GetContexts(const ResourceHostMsg_Request& request, 58 ResourceContext** resource_context, 59 net::URLRequestContext** request_context); 60 61 // Returns the net::URLRequestContext for the given request. 62 net::URLRequestContext* GetURLRequestContext( 63 ResourceType::Type request_type); 64 appcache_service()65 ChromeAppCacheService* appcache_service() const { 66 return appcache_service_.get(); 67 } 68 blob_storage_context()69 ChromeBlobStorageContext* blob_storage_context() const { 70 return blob_storage_context_.get(); 71 } 72 file_system_context()73 fileapi::FileSystemContext* file_system_context() const { 74 return file_system_context_.get(); 75 } 76 child_id()77 int child_id() const { return child_id_; } process_type()78 int process_type() const { return process_type_; } 79 80 base::WeakPtr<ResourceMessageFilter> GetWeakPtr(); 81 82 protected: 83 // Protected destructor so that we can be overriden in tests. 84 virtual ~ResourceMessageFilter(); 85 86 private: 87 // The ID of the child process. 88 int child_id_; 89 90 int process_type_; 91 92 scoped_refptr<ChromeAppCacheService> appcache_service_; 93 scoped_refptr<ChromeBlobStorageContext> blob_storage_context_; 94 scoped_refptr<fileapi::FileSystemContext> file_system_context_; 95 96 GetContextsCallback get_contexts_callback_; 97 98 // This must come last to make sure weak pointers are invalidated first. 99 base::WeakPtrFactory<ResourceMessageFilter> weak_ptr_factory_; 100 101 DISALLOW_IMPLICIT_CONSTRUCTORS(ResourceMessageFilter); 102 }; 103 104 } // namespace content 105 106 #endif // CONTENT_BROWSER_LOADER_RESOURCE_MESSAGE_FILTER_H_ 107