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 "net/base/request_priority.h" 22 #include "webkit/common/resource_type.h" 23 24 struct ResourceMsg_RequestCompleteData; 25 26 namespace blink { 27 class WebThreadedDataReceiver; 28 } 29 30 namespace webkit_glue { 31 class ResourceLoaderBridge; 32 } 33 34 namespace content { 35 class RequestPeer; 36 class ResourceDispatcherDelegate; 37 class ThreadedDataProvider; 38 struct ResourceResponseInfo; 39 struct RequestInfo; 40 struct ResourceResponseHead; 41 struct SiteIsolationResponseMetaData; 42 43 // This class serves as a communication interface between the 44 // ResourceDispatcherHost in the browser process and the ResourceLoaderBridge in 45 // the child process. It can be used from any child process. 46 class CONTENT_EXPORT ResourceDispatcher : public IPC::Listener { 47 public: 48 explicit ResourceDispatcher(IPC::Sender* sender); 49 virtual ~ResourceDispatcher(); 50 51 // IPC::Listener implementation. 52 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 53 54 // Creates a ResourceLoaderBridge for this type of dispatcher, this is so 55 // this can be tested regardless of the ResourceLoaderBridge::Create 56 // implementation. 57 webkit_glue::ResourceLoaderBridge* CreateBridge( 58 const RequestInfo& request_info); 59 60 // Adds a request from the |pending_requests_| list, returning the new 61 // requests' ID. 62 int AddPendingRequest(RequestPeer* callback, 63 ResourceType::Type resource_type, 64 int origin_pid, 65 const GURL& frame_origin, 66 const GURL& request_url, 67 bool download_to_file); 68 69 // Removes a request from the |pending_requests_| list, returning true if the 70 // request was found and removed. 71 bool RemovePendingRequest(int request_id); 72 73 // Cancels a request in the |pending_requests_| list. The request will be 74 // removed from the dispatcher as well. 75 void CancelPendingRequest(int request_id); 76 77 // Toggles the is_deferred attribute for the specified request. 78 void SetDefersLoading(int request_id, bool value); 79 80 // Indicates the priority of the specified request changed. 81 void DidChangePriority(int request_id, 82 net::RequestPriority new_priority, 83 int intra_priority_value); 84 85 // The provided data receiver will receive incoming resource data rather 86 // than the resource bridge. 87 bool AttachThreadedDataReceiver( 88 int request_id, blink::WebThreadedDataReceiver* threaded_data_receiver); 89 90 IPC::Sender* message_sender() const { return message_sender_; } 91 92 // This does not take ownership of the delegate. It is expected that the 93 // delegate have a longer lifetime than the ResourceDispatcher. 94 void set_delegate(ResourceDispatcherDelegate* delegate) { 95 delegate_ = delegate; 96 } 97 98 // Remembers IO thread timestamp for next resource message. 99 void set_io_timestamp(base::TimeTicks io_timestamp) { 100 io_timestamp_ = io_timestamp; 101 } 102 103 private: 104 friend class ResourceDispatcherTest; 105 106 typedef std::deque<IPC::Message*> MessageQueue; 107 struct PendingRequestInfo { 108 PendingRequestInfo(); 109 110 PendingRequestInfo(RequestPeer* peer, 111 ResourceType::Type resource_type, 112 int origin_pid, 113 const GURL& frame_origin, 114 const GURL& request_url, 115 bool download_to_file); 116 117 ~PendingRequestInfo(); 118 119 RequestPeer* peer; 120 ThreadedDataProvider* threaded_data_provider; 121 ResourceType::Type resource_type; 122 // The PID of the original process which issued this request. This gets 123 // non-zero only for a request proxied by another renderer, particularly 124 // requests from plugins. 125 int origin_pid; 126 MessageQueue deferred_message_queue; 127 bool is_deferred; 128 // Original requested url. 129 GURL url; 130 // The security origin of the frame that initiates this request. 131 GURL frame_origin; 132 // The url of the latest response even in case of redirection. 133 GURL response_url; 134 bool download_to_file; 135 linked_ptr<IPC::Message> pending_redirect_message; 136 base::TimeTicks request_start; 137 base::TimeTicks response_start; 138 base::TimeTicks completion_time; 139 linked_ptr<base::SharedMemory> buffer; 140 linked_ptr<SiteIsolationResponseMetaData> site_isolation_metadata; 141 bool blocked_response; 142 int buffer_size; 143 }; 144 typedef base::hash_map<int, PendingRequestInfo> PendingRequestList; 145 146 // Helper to lookup the info based on the request_id. 147 // May return NULL if the request as been canceled from the client side. 148 PendingRequestInfo* GetPendingRequestInfo(int request_id); 149 150 // Follows redirect, if any, for the given request. 151 void FollowPendingRedirect(int request_id, PendingRequestInfo& request_info); 152 153 // Message response handlers, called by the message handler for this process. 154 void OnUploadProgress(int request_id, int64 position, int64 size); 155 void OnReceivedResponse(int request_id, const ResourceResponseHead&); 156 void OnReceivedCachedMetadata(int request_id, const std::vector<char>& data); 157 void OnReceivedRedirect(int request_id, 158 const GURL& new_url, 159 const GURL& new_first_party_for_cookies, 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 base::WeakPtrFactory<ResourceDispatcher> weak_factory_; 214 215 ResourceDispatcherDelegate* delegate_; 216 217 // IO thread timestamp for ongoing IPC message. 218 base::TimeTicks io_timestamp_; 219 220 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcher); 221 }; 222 223 } // namespace content 224 225 #endif // CONTENT_CHILD_RESOURCE_DISPATCHER_H_ 226