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/weak_ptr.h"
16 #include "base/shared_memory.h"
17 #include "base/time.h"
18 #include "content/common/content_export.h"
19 #include "ipc/ipc_listener.h"
20 #include "ipc/ipc_sender.h"
21 #include "webkit/glue/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
,
48 const GURL
& request_url
);
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
);
54 // Cancels a request in the pending_requests_ list.
55 void CancelPendingRequest(int routing_id
, int request_id
);
57 IPC::Sender
* message_sender() const {
58 return message_sender_
;
61 // Toggles the is_deferred attribute for the specified request.
62 void SetDefersLoading(int request_id
, bool value
);
64 // Indicates the priority of the specified request changed.
65 void DidChangePriority(int routing_id
, int request_id
,
66 net::RequestPriority new_priority
);
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
) {
74 // Remembers IO thread timestamp for next resource message.
75 void set_io_timestamp(base::TimeTicks io_timestamp
) {
76 io_timestamp_
= io_timestamp
;
80 friend class ResourceDispatcherTest
;
82 typedef std::deque
<IPC::Message
*> MessageQueue
;
83 struct PendingRequestInfo
{
86 PendingRequestInfo(webkit_glue::ResourceLoaderBridge::Peer
* peer
,
87 ResourceType::Type resource_type
,
88 const GURL
& request_url
);
90 ~PendingRequestInfo();
92 webkit_glue::ResourceLoaderBridge::Peer
* peer
;
93 ResourceType::Type resource_type
;
94 MessageQueue deferred_message_queue
;
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
;
104 typedef base::hash_map
<int, PendingRequestInfo
> PendingRequestList
;
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
);
110 // Follows redirect, if any, for the given request.
111 void FollowPendingRedirect(int request_id
, PendingRequestInfo
& request_info
);
113 // Message response handlers, called by the message handler for this process.
114 void OnUploadProgress(
115 const IPC::Message
& message
,
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
,
125 const ResourceResponseHead
& response_head
);
126 void OnSetDataBuffer(
127 const IPC::Message
& message
,
129 base::SharedMemoryHandle shm_handle
,
131 base::ProcessId renderer_pid
);
133 const IPC::Message
& message
,
137 int encoded_data_length
);
138 void OnDownloadedData(
139 const IPC::Message
& message
,
142 void OnRequestComplete(
145 bool was_ignored_by_handler
,
146 const std::string
& security_info
,
147 const base::TimeTicks
& completion_time
);
149 // Dispatch the message to one of the message response handlers.
150 void DispatchMessage(const IPC::Message
& message
);
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
);
156 void ToResourceResponseInfo(
157 const PendingRequestInfo
& request_info
,
158 const ResourceResponseHead
& browser_info
,
159 webkit_glue::ResourceResponseInfo
* renderer_info
) const;
161 base::TimeTicks
ToRendererCompletionTime(
162 const PendingRequestInfo
& request_info
,
163 const base::TimeTicks
& browser_completion_time
) const;
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();
170 // Returns true if the message passed in is a resource related message.
171 static bool IsResourceDispatcherMessage(const IPC::Message
& message
);
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
);
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
);
184 IPC::Sender
* message_sender_
;
186 // All pending requests issued to the host
187 PendingRequestList pending_requests_
;
189 base::WeakPtrFactory
<ResourceDispatcher
> weak_factory_
;
191 ResourceDispatcherDelegate
* delegate_
;
193 // IO thread timestamp for ongoing IPC message.
194 base::TimeTicks io_timestamp_
;
196 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcher
);
199 } // namespace content
201 #endif // CONTENT_CHILD_RESOURCE_DISPATCHER_H_