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