1 // Copyright (c) 2011 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_COMMON_RESOURCE_DISPATCHER_H_
8 #define CONTENT_COMMON_RESOURCE_DISPATCHER_H_
14 #include "base/hash_tables.h"
15 #include "base/memory/linked_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/shared_memory.h"
18 #include "base/time.h"
19 #include "content/common/content_export.h"
20 #include "ipc/ipc_channel.h"
21 #include "webkit/glue/resource_loader_bridge.h"
24 class ResourceDispatcherDelegate
;
25 struct ResourceResponseHead
;
28 // This class serves as a communication interface between the
29 // ResourceDispatcherHost in the browser process and the ResourceLoaderBridge in
30 // the child process. It can be used from any child process.
31 class CONTENT_EXPORT ResourceDispatcher
: public IPC::Channel::Listener
{
33 explicit ResourceDispatcher(IPC::Message::Sender
* sender
);
34 virtual ~ResourceDispatcher();
36 // IPC::Channel::Listener implementation.
37 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
39 // Creates a ResourceLoaderBridge for this type of dispatcher, this is so
40 // this can be tested regardless of the ResourceLoaderBridge::Create
42 webkit_glue::ResourceLoaderBridge
* CreateBridge(
43 const webkit_glue::ResourceLoaderBridge::RequestInfo
& request_info
);
45 // Adds a request from the pending_requests_ list, returning the new
47 int AddPendingRequest(webkit_glue::ResourceLoaderBridge::Peer
* callback
,
48 ResourceType::Type resource_type
,
49 const GURL
& request_url
);
51 // Removes a request from the pending_requests_ list, returning true if the
52 // request was found and removed.
53 bool RemovePendingRequest(int request_id
);
55 // Cancels a request in the pending_requests_ list.
56 void CancelPendingRequest(int routing_id
, int request_id
);
58 IPC::Message::Sender
* message_sender() const {
59 return message_sender_
;
62 // Toggles the is_deferred attribute for the specified request.
63 void SetDefersLoading(int request_id
, bool value
);
65 // This does not take ownership of the delegate. It is expected that the
66 // delegate have a longer lifetime than the ResourceDispatcher.
67 void set_delegate(content::ResourceDispatcherDelegate
* delegate
) {
72 friend class ResourceDispatcherTest
;
74 typedef std::deque
<IPC::Message
*> MessageQueue
;
75 struct PendingRequestInfo
{
76 PendingRequestInfo() { }
77 PendingRequestInfo(webkit_glue::ResourceLoaderBridge::Peer
* peer
,
78 ResourceType::Type resource_type
,
79 const GURL
& request_url
)
81 resource_type(resource_type
),
84 request_start(base::TimeTicks::Now()) {
86 ~PendingRequestInfo() { }
87 webkit_glue::ResourceLoaderBridge::Peer
* peer
;
88 ResourceType::Type resource_type
;
89 MessageQueue deferred_message_queue
;
92 linked_ptr
<IPC::Message
> pending_redirect_message
;
93 base::TimeTicks request_start
;
94 base::TimeTicks response_start
;
95 base::TimeTicks completion_time
;
97 typedef base::hash_map
<int, PendingRequestInfo
> PendingRequestList
;
99 // Helper to lookup the info based on the request_id.
100 // May return NULL if the request as been canceled from the client side.
101 PendingRequestInfo
* GetPendingRequestInfo(int request_id
);
103 // Follows redirect, if any, for the given request.
104 void FollowPendingRedirect(int request_id
, PendingRequestInfo
& request_info
);
106 // Message response handlers, called by the message handler for this process.
107 void OnUploadProgress(
108 const IPC::Message
& message
,
112 void OnReceivedResponse(int request_id
, const content::ResourceResponseHead
&);
113 void OnReceivedCachedMetadata(int request_id
, const std::vector
<char>& data
);
114 void OnReceivedRedirect(
115 const IPC::Message
& message
,
118 const content::ResourceResponseHead
& response_head
);
120 const IPC::Message
& message
,
122 base::SharedMemoryHandle data
,
124 int encoded_data_length
);
125 void OnDownloadedData(
126 const IPC::Message
& message
,
129 void OnRequestComplete(
131 const net::URLRequestStatus
& status
,
132 const std::string
& security_info
,
133 const base::TimeTicks
& completion_time
);
135 // Dispatch the message to one of the message response handlers.
136 void DispatchMessage(const IPC::Message
& message
);
138 // Dispatch any deferred messages for the given request, provided it is not
139 // again in the deferred state.
140 void FlushDeferredMessages(int request_id
);
142 void ToResourceResponseInfo(
143 const PendingRequestInfo
& request_info
,
144 const content::ResourceResponseHead
& browser_info
,
145 webkit_glue::ResourceResponseInfo
* renderer_info
) const;
147 base::TimeTicks
ToRendererCompletionTime(
148 const PendingRequestInfo
& request_info
,
149 const base::TimeTicks
& browser_completion_time
) const;
151 // Returns true if the message passed in is a resource related message.
152 static bool IsResourceDispatcherMessage(const IPC::Message
& message
);
154 // ViewHostMsg_Resource_DataReceived is not POD, it has a shared memory
155 // handle in it that we should cleanup it up nicely. This method accepts any
156 // message and determine whether the message is
157 // ViewHostMsg_Resource_DataReceived and clean up the shared memory handle.
158 static void ReleaseResourcesInDataMessage(const IPC::Message
& message
);
160 // Iterate through a message queue and clean up the messages by calling
161 // ReleaseResourcesInDataMessage and removing them from the queue. Intended
162 // for use on deferred message queues that are no longer needed.
163 static void ReleaseResourcesInMessageQueue(MessageQueue
* queue
);
165 IPC::Message::Sender
* message_sender_
;
167 // All pending requests issued to the host
168 PendingRequestList pending_requests_
;
170 base::WeakPtrFactory
<ResourceDispatcher
> weak_factory_
;
172 content::ResourceDispatcherDelegate
* delegate_
;
174 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcher
);
177 #endif // CONTENT_COMMON_RESOURCE_DISPATCHER_H_