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 // See http://dev.chromium.org/developers/design-documents/multi-process-resource-loading 6 7 #ifndef CONTENT_CHILD_RESOURCE_DISPATCHER_H_ 8 #define CONTENT_CHILD_RESOURCE_DISPATCHER_H_ 9 10 #include <deque> 11 #include <string> 12 13 #include "base/containers/hash_tables.h" 14 #include "base/memory/linked_ptr.h" 15 #include "base/memory/shared_memory.h" 16 #include "base/memory/weak_ptr.h" 17 #include "base/time/time.h" 18 #include "content/common/content_export.h" 19 #include "ipc/ipc_listener.h" 20 #include "ipc/ipc_sender.h" 21 #include "webkit/child/resource_loader_bridge.h" 22 23 namespace content { 24 class ResourceDispatcherDelegate; 25 struct ResourceResponseHead; 26 27 // This class serves as a communication interface between the 28 // ResourceDispatcherHost in the browser process and the ResourceLoaderBridge in 29 // the child process. It can be used from any child process. 30 class CONTENT_EXPORT ResourceDispatcher : public IPC::Listener { 31 public: 32 explicit ResourceDispatcher(IPC::Sender* sender); 33 virtual ~ResourceDispatcher(); 34 35 // IPC::Listener implementation. 36 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 37 38 // Creates a ResourceLoaderBridge for this type of dispatcher, this is so 39 // this can be tested regardless of the ResourceLoaderBridge::Create 40 // implementation. 41 webkit_glue::ResourceLoaderBridge* CreateBridge( 42 const webkit_glue::ResourceLoaderBridge::RequestInfo& request_info); 43 44 // Adds a request from the pending_requests_ list, returning the new 45 // requests' ID 46 int AddPendingRequest(webkit_glue::ResourceLoaderBridge::Peer* callback, 47 ResourceType::Type resource_type, 48 const GURL& request_url); 49 50 // Removes a request from the pending_requests_ list, returning true if the 51 // request was found and removed. 52 bool RemovePendingRequest(int request_id); 53 54 // Cancels a request in the pending_requests_ list. 55 void CancelPendingRequest(int routing_id, int request_id); 56 57 IPC::Sender* message_sender() const { 58 return message_sender_; 59 } 60 61 // Toggles the is_deferred attribute for the specified request. 62 void SetDefersLoading(int request_id, bool value); 63 64 // Indicates the priority of the specified request changed. 65 void DidChangePriority(int routing_id, int request_id, 66 net::RequestPriority new_priority); 67 68 // This does not take ownership of the delegate. It is expected that the 69 // delegate have a longer lifetime than the ResourceDispatcher. 70 void set_delegate(ResourceDispatcherDelegate* delegate) { 71 delegate_ = delegate; 72 } 73 74 // Remembers IO thread timestamp for next resource message. 75 void set_io_timestamp(base::TimeTicks io_timestamp) { 76 io_timestamp_ = io_timestamp; 77 } 78 79 private: 80 friend class ResourceDispatcherTest; 81 82 typedef std::deque<IPC::Message*> MessageQueue; 83 struct PendingRequestInfo { 84 PendingRequestInfo(); 85 86 PendingRequestInfo(webkit_glue::ResourceLoaderBridge::Peer* peer, 87 ResourceType::Type resource_type, 88 const GURL& request_url); 89 90 ~PendingRequestInfo(); 91 92 webkit_glue::ResourceLoaderBridge::Peer* peer; 93 ResourceType::Type resource_type; 94 MessageQueue deferred_message_queue; 95 bool is_deferred; 96 GURL url; 97 linked_ptr<IPC::Message> pending_redirect_message; 98 base::TimeTicks request_start; 99 base::TimeTicks response_start; 100 base::TimeTicks completion_time; 101 linked_ptr<base::SharedMemory> buffer; 102 int buffer_size; 103 }; 104 typedef base::hash_map<int, PendingRequestInfo> PendingRequestList; 105 106 // Helper to lookup the info based on the request_id. 107 // May return NULL if the request as been canceled from the client side. 108 PendingRequestInfo* GetPendingRequestInfo(int request_id); 109 110 // Follows redirect, if any, for the given request. 111 void FollowPendingRedirect(int request_id, PendingRequestInfo& request_info); 112 113 // Message response handlers, called by the message handler for this process. 114 void OnUploadProgress( 115 const IPC::Message& message, 116 int request_id, 117 int64 position, 118 int64 size); 119 void OnReceivedResponse(int request_id, const ResourceResponseHead&); 120 void OnReceivedCachedMetadata(int request_id, const std::vector<char>& data); 121 void OnReceivedRedirect( 122 const IPC::Message& message, 123 int request_id, 124 const GURL& new_url, 125 const ResourceResponseHead& response_head); 126 void OnSetDataBuffer( 127 const IPC::Message& message, 128 int request_id, 129 base::SharedMemoryHandle shm_handle, 130 int shm_size, 131 base::ProcessId renderer_pid); 132 void OnReceivedData( 133 const IPC::Message& message, 134 int request_id, 135 int data_offset, 136 int data_length, 137 int encoded_data_length); 138 void OnDownloadedData( 139 const IPC::Message& message, 140 int request_id, 141 int data_len); 142 void OnRequestComplete( 143 int request_id, 144 int error_code, 145 bool was_ignored_by_handler, 146 const std::string& security_info, 147 const base::TimeTicks& completion_time); 148 149 // Dispatch the message to one of the message response handlers. 150 void DispatchMessage(const IPC::Message& message); 151 152 // Dispatch any deferred messages for the given request, provided it is not 153 // again in the deferred state. 154 void FlushDeferredMessages(int request_id); 155 156 void ToResourceResponseInfo( 157 const PendingRequestInfo& request_info, 158 const ResourceResponseHead& browser_info, 159 webkit_glue::ResourceResponseInfo* renderer_info) const; 160 161 base::TimeTicks ToRendererCompletionTime( 162 const PendingRequestInfo& request_info, 163 const base::TimeTicks& browser_completion_time) const; 164 165 // Returns timestamp provided by IO thread. If no timestamp is supplied, 166 // then current time is returned. Saved timestamp is reset, so following 167 // invocations will return current time until set_io_timestamp is called. 168 base::TimeTicks ConsumeIOTimestamp(); 169 170 // Returns true if the message passed in is a resource related message. 171 static bool IsResourceDispatcherMessage(const IPC::Message& message); 172 173 // ViewHostMsg_Resource_DataReceived is not POD, it has a shared memory 174 // handle in it that we should cleanup it up nicely. This method accepts any 175 // message and determine whether the message is 176 // ViewHostMsg_Resource_DataReceived and clean up the shared memory handle. 177 static void ReleaseResourcesInDataMessage(const IPC::Message& message); 178 179 // Iterate through a message queue and clean up the messages by calling 180 // ReleaseResourcesInDataMessage and removing them from the queue. Intended 181 // for use on deferred message queues that are no longer needed. 182 static void ReleaseResourcesInMessageQueue(MessageQueue* queue); 183 184 IPC::Sender* message_sender_; 185 186 // All pending requests issued to the host 187 PendingRequestList pending_requests_; 188 189 base::WeakPtrFactory<ResourceDispatcher> weak_factory_; 190 191 ResourceDispatcherDelegate* delegate_; 192 193 // IO thread timestamp for ongoing IPC message. 194 base::TimeTicks io_timestamp_; 195 196 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcher); 197 }; 198 199 } // namespace content 200 201 #endif // CONTENT_CHILD_RESOURCE_DISPATCHER_H_ 202