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 int origin_pid, 49 const GURL& frame_origin, 50 const GURL& request_url); 51 52 // Removes a request from the pending_requests_ list, returning true if the 53 // request was found and removed. 54 bool RemovePendingRequest(int request_id); 55 56 // Cancels a request in the pending_requests_ list. 57 void CancelPendingRequest(int request_id); 58 59 IPC::Sender* message_sender() const { 60 return message_sender_; 61 } 62 63 // Toggles the is_deferred attribute for the specified request. 64 void SetDefersLoading(int request_id, bool value); 65 66 // Indicates the priority of the specified request changed. 67 void DidChangePriority(int routing_id, int request_id, 68 net::RequestPriority new_priority); 69 70 // This does not take ownership of the delegate. It is expected that the 71 // delegate have a longer lifetime than the ResourceDispatcher. 72 void set_delegate(ResourceDispatcherDelegate* delegate) { 73 delegate_ = delegate; 74 } 75 76 // Remembers IO thread timestamp for next resource message. 77 void set_io_timestamp(base::TimeTicks io_timestamp) { 78 io_timestamp_ = io_timestamp; 79 } 80 81 private: 82 friend class ResourceDispatcherTest; 83 84 typedef std::deque<IPC::Message*> MessageQueue; 85 struct PendingRequestInfo { 86 PendingRequestInfo(); 87 88 PendingRequestInfo(webkit_glue::ResourceLoaderBridge::Peer* peer, 89 ResourceType::Type resource_type, 90 int origin_pid, 91 const GURL& frame_origin, 92 const GURL& request_url); 93 94 ~PendingRequestInfo(); 95 96 webkit_glue::ResourceLoaderBridge::Peer* peer; 97 ResourceType::Type resource_type; 98 // The PID of the original process which issued this request. This gets 99 // non-zero only for a request proxied by another renderer, particularly 100 // requests from plugins. 101 int origin_pid; 102 MessageQueue deferred_message_queue; 103 bool is_deferred; 104 // Original requested url. 105 GURL url; 106 // The security origin of the frame that initiates this request. 107 GURL frame_origin; 108 // The url of the latest response even in case of redirection. 109 GURL response_url; 110 linked_ptr<IPC::Message> pending_redirect_message; 111 base::TimeTicks request_start; 112 base::TimeTicks response_start; 113 base::TimeTicks completion_time; 114 linked_ptr<base::SharedMemory> buffer; 115 int buffer_size; 116 }; 117 typedef base::hash_map<int, PendingRequestInfo> PendingRequestList; 118 119 // Helper to lookup the info based on the request_id. 120 // May return NULL if the request as been canceled from the client side. 121 PendingRequestInfo* GetPendingRequestInfo(int request_id); 122 123 // Follows redirect, if any, for the given request. 124 void FollowPendingRedirect(int request_id, PendingRequestInfo& request_info); 125 126 // Message response handlers, called by the message handler for this process. 127 void OnUploadProgress( 128 int request_id, 129 int64 position, 130 int64 size); 131 void OnReceivedResponse(int request_id, const ResourceResponseHead&); 132 void OnReceivedCachedMetadata(int request_id, const std::vector<char>& data); 133 void OnReceivedRedirect( 134 int request_id, 135 const GURL& new_url, 136 const ResourceResponseHead& response_head); 137 void OnSetDataBuffer( 138 int request_id, 139 base::SharedMemoryHandle shm_handle, 140 int shm_size, 141 base::ProcessId renderer_pid); 142 void OnReceivedData( 143 int request_id, 144 int data_offset, 145 int data_length, 146 int encoded_data_length); 147 void OnDownloadedData( 148 int request_id, 149 int data_len, 150 int encoded_data_length); 151 void OnRequestComplete( 152 int request_id, 153 int error_code, 154 bool was_ignored_by_handler, 155 const std::string& security_info, 156 const base::TimeTicks& completion_time); 157 158 // Dispatch the message to one of the message response handlers. 159 void DispatchMessage(const IPC::Message& message); 160 161 // Dispatch any deferred messages for the given request, provided it is not 162 // again in the deferred state. 163 void FlushDeferredMessages(int request_id); 164 165 void ToResourceResponseInfo( 166 const PendingRequestInfo& request_info, 167 const ResourceResponseHead& browser_info, 168 webkit_glue::ResourceResponseInfo* renderer_info) const; 169 170 base::TimeTicks ToRendererCompletionTime( 171 const PendingRequestInfo& request_info, 172 const base::TimeTicks& browser_completion_time) const; 173 174 // Returns timestamp provided by IO thread. If no timestamp is supplied, 175 // then current time is returned. Saved timestamp is reset, so following 176 // invocations will return current time until set_io_timestamp is called. 177 base::TimeTicks ConsumeIOTimestamp(); 178 179 // Returns true if the message passed in is a resource related message. 180 static bool IsResourceDispatcherMessage(const IPC::Message& message); 181 182 // ViewHostMsg_Resource_DataReceived is not POD, it has a shared memory 183 // handle in it that we should cleanup it up nicely. This method accepts any 184 // message and determine whether the message is 185 // ViewHostMsg_Resource_DataReceived and clean up the shared memory handle. 186 static void ReleaseResourcesInDataMessage(const IPC::Message& message); 187 188 // Iterate through a message queue and clean up the messages by calling 189 // ReleaseResourcesInDataMessage and removing them from the queue. Intended 190 // for use on deferred message queues that are no longer needed. 191 static void ReleaseResourcesInMessageQueue(MessageQueue* queue); 192 193 IPC::Sender* message_sender_; 194 195 // All pending requests issued to the host 196 PendingRequestList pending_requests_; 197 198 base::WeakPtrFactory<ResourceDispatcher> weak_factory_; 199 200 ResourceDispatcherDelegate* delegate_; 201 202 // IO thread timestamp for ongoing IPC message. 203 base::TimeTicks io_timestamp_; 204 205 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcher); 206 }; 207 208 } // namespace content 209 210 #endif // CONTENT_CHILD_RESOURCE_DISPATCHER_H_ 211