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.
5 // See http://dev.chromium.org/developers/design-documents/multi-process-resource-loading
7 #ifndef CONTENT_CHILD_RESOURCE_DISPATCHER_H_
8 #define CONTENT_CHILD_RESOURCE_DISPATCHER_H_
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"
24 class ResourceDispatcherDelegate;
25 struct ResourceResponseHead;
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 {
32 explicit ResourceDispatcher(IPC::Sender* sender);
33 virtual ~ResourceDispatcher();
35 // IPC::Listener implementation.
36 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
38 // Creates a ResourceLoaderBridge for this type of dispatcher, this is so
39 // this can be tested regardless of the ResourceLoaderBridge::Create
41 webkit_glue::ResourceLoaderBridge* CreateBridge(
42 const webkit_glue::ResourceLoaderBridge::RequestInfo& request_info);
44 // Adds a request from the pending_requests_ list, returning the new
46 int AddPendingRequest(webkit_glue::ResourceLoaderBridge::Peer* callback,
47 ResourceType::Type resource_type,
49 const GURL& frame_origin,
50 const GURL& request_url);
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);
56 // Cancels a request in the pending_requests_ list.
57 void CancelPendingRequest(int request_id);
59 IPC::Sender* message_sender() const {
60 return message_sender_;
63 // Toggles the is_deferred attribute for the specified request.
64 void SetDefersLoading(int request_id, bool value);
66 // Indicates the priority of the specified request changed.
67 void DidChangePriority(int routing_id, int request_id,
68 net::RequestPriority new_priority);
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) {
76 // Remembers IO thread timestamp for next resource message.
77 void set_io_timestamp(base::TimeTicks io_timestamp) {
78 io_timestamp_ = io_timestamp;
82 friend class ResourceDispatcherTest;
84 typedef std::deque<IPC::Message*> MessageQueue;
85 struct PendingRequestInfo {
88 PendingRequestInfo(webkit_glue::ResourceLoaderBridge::Peer* peer,
89 ResourceType::Type resource_type,
91 const GURL& frame_origin,
92 const GURL& request_url);
94 ~PendingRequestInfo();
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.
102 MessageQueue deferred_message_queue;
104 // Original requested url.
106 // The security origin of the frame that initiates this request.
108 // The url of the latest response even in case of redirection.
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;
117 typedef base::hash_map<int, PendingRequestInfo> PendingRequestList;
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);
123 // Follows redirect, if any, for the given request.
124 void FollowPendingRedirect(int request_id, PendingRequestInfo& request_info);
126 // Message response handlers, called by the message handler for this process.
127 void OnUploadProgress(
131 void OnReceivedResponse(int request_id, const ResourceResponseHead&);
132 void OnReceivedCachedMetadata(int request_id, const std::vector<char>& data);
133 void OnReceivedRedirect(
136 const ResourceResponseHead& response_head);
137 void OnSetDataBuffer(
139 base::SharedMemoryHandle shm_handle,
141 base::ProcessId renderer_pid);
146 int encoded_data_length);
147 void OnDownloadedData(
150 int encoded_data_length);
151 void OnRequestComplete(
154 bool was_ignored_by_handler,
155 const std::string& security_info,
156 const base::TimeTicks& completion_time);
158 // Dispatch the message to one of the message response handlers.
159 void DispatchMessage(const IPC::Message& message);
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);
165 void ToResourceResponseInfo(
166 const PendingRequestInfo& request_info,
167 const ResourceResponseHead& browser_info,
168 webkit_glue::ResourceResponseInfo* renderer_info) const;
170 base::TimeTicks ToRendererCompletionTime(
171 const PendingRequestInfo& request_info,
172 const base::TimeTicks& browser_completion_time) const;
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();
179 // Returns true if the message passed in is a resource related message.
180 static bool IsResourceDispatcherMessage(const IPC::Message& message);
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);
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);
193 IPC::Sender* message_sender_;
195 // All pending requests issued to the host
196 PendingRequestList pending_requests_;
198 base::WeakPtrFactory<ResourceDispatcher> weak_factory_;
200 ResourceDispatcherDelegate* delegate_;
202 // IO thread timestamp for ongoing IPC message.
203 base::TimeTicks io_timestamp_;
205 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcher);
208 } // namespace content
210 #endif // CONTENT_CHILD_RESOURCE_DISPATCHER_H_