OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // See http://dev.chromium.org/developers/design-documents/multi-process-resourc
e-loading | 5 // See http://dev.chromium.org/developers/design-documents/multi-process-resourc
e-loading |
6 | 6 |
7 #ifndef CONTENT_CHILD_RESOURCE_DISPATCHER_H_ | 7 #ifndef CONTENT_CHILD_RESOURCE_DISPATCHER_H_ |
8 #define CONTENT_CHILD_RESOURCE_DISPATCHER_H_ | 8 #define CONTENT_CHILD_RESOURCE_DISPATCHER_H_ |
9 | 9 |
10 #include <deque> | 10 #include <deque> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/containers/hash_tables.h" | 13 #include "base/containers/hash_tables.h" |
14 #include "base/memory/linked_ptr.h" | 14 #include "base/memory/linked_ptr.h" |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/memory/shared_memory.h" | 15 #include "base/memory/shared_memory.h" |
17 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
18 #include "base/time/time.h" | 17 #include "base/time/time.h" |
19 #include "content/common/content_export.h" | 18 #include "content/common/content_export.h" |
20 #include "ipc/ipc_listener.h" | 19 #include "ipc/ipc_listener.h" |
21 #include "ipc/ipc_sender.h" | 20 #include "ipc/ipc_sender.h" |
22 #include "net/base/request_priority.h" | 21 #include "net/base/request_priority.h" |
23 #include "webkit/common/resource_type.h" | 22 #include "webkit/common/resource_type.h" |
24 | 23 |
25 struct ResourceHostMsg_Request; | |
26 struct ResourceMsg_RequestCompleteData; | 24 struct ResourceMsg_RequestCompleteData; |
27 | 25 |
28 namespace webkit_glue { | 26 namespace webkit_glue { |
29 class ResourceLoaderBridge; | 27 class ResourceLoaderBridge; |
30 struct ResourceResponseInfo; | 28 struct ResourceResponseInfo; |
31 } | 29 } |
32 | 30 |
33 namespace content { | 31 namespace content { |
34 class RequestPeer; | 32 class RequestPeer; |
35 class ResourceDispatcherDelegate; | 33 class ResourceDispatcherDelegate; |
36 class ResourceRequestBody; | |
37 struct RequestInfo; | 34 struct RequestInfo; |
38 struct ResourceResponseHead; | 35 struct ResourceResponseHead; |
39 struct SiteIsolationResponseMetaData; | 36 struct SiteIsolationResponseMetaData; |
40 struct SyncLoadResponse; | |
41 | 37 |
42 // This class serves as a communication interface between the | 38 // This class serves as a communication interface between the |
43 // ResourceDispatcherHost in the browser process and the ResourceLoaderBridge in | 39 // ResourceDispatcherHost in the browser process and the ResourceLoaderBridge in |
44 // the child process. It can be used from any child process. | 40 // the child process. It can be used from any child process. |
45 class CONTENT_EXPORT ResourceDispatcher : public IPC::Listener { | 41 class CONTENT_EXPORT ResourceDispatcher : public IPC::Listener { |
46 public: | 42 public: |
47 explicit ResourceDispatcher(IPC::Sender* sender); | 43 explicit ResourceDispatcher(IPC::Sender* sender); |
48 virtual ~ResourceDispatcher(); | 44 virtual ~ResourceDispatcher(); |
49 | 45 |
50 // IPC::Listener implementation. | 46 // IPC::Listener implementation. |
51 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 47 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
52 | 48 |
| 49 // Creates a ResourceLoaderBridge for this type of dispatcher, this is so |
| 50 // this can be tested regardless of the ResourceLoaderBridge::Create |
| 51 // implementation. |
| 52 webkit_glue::ResourceLoaderBridge* CreateBridge( |
| 53 const RequestInfo& request_info); |
53 | 54 |
54 // Call this method to load the resource synchronously (i.e., in one shot). | 55 // Adds a request from the pending_requests_ list, returning the new |
55 // This is an alternative to the StartAsync method. Be warned that this method | 56 // requests' ID |
56 // will block the calling thread until the resource is fully downloaded or an | 57 int AddPendingRequest(RequestPeer* callback, |
57 // error occurs. It could block the calling thread for a long time, so only | 58 ResourceType::Type resource_type, |
58 // use this if you really need it! There is also no way for the caller to | 59 int origin_pid, |
59 // interrupt this method. Errors are reported via the status field of the | 60 const GURL& frame_origin, |
60 // response parameter. | 61 const GURL& request_url); |
61 void StartSync(const RequestInfo& request_info, | |
62 ResourceRequestBody* request_body, | |
63 SyncLoadResponse* response); | |
64 | 62 |
65 // Call this method to initiate the request. If this method succeeds, then | 63 // Removes a request from the pending_requests_ list, returning true if the |
66 // the peer's methods will be called asynchronously to report various events. | |
67 // Returns the request id. | |
68 int StartAsync(const RequestInfo& request_info, | |
69 ResourceRequestBody* request_body, | |
70 RequestPeer* peer); | |
71 | |
72 // Removes a request from the |pending_requests_| list, returning true if the | |
73 // request was found and removed. | 64 // request was found and removed. |
74 bool RemovePendingRequest(int request_id); | 65 bool RemovePendingRequest(int request_id); |
75 | 66 |
76 // Cancels a request in the |pending_requests_| list. | 67 // Cancels a request in the pending_requests_ list. |
77 void Cancel(int request_id); | 68 void CancelPendingRequest(int request_id); |
| 69 |
| 70 IPC::Sender* message_sender() const { |
| 71 return message_sender_; |
| 72 } |
78 | 73 |
79 // Toggles the is_deferred attribute for the specified request. | 74 // Toggles the is_deferred attribute for the specified request. |
80 void SetDefersLoading(int request_id, bool value); | 75 void SetDefersLoading(int request_id, bool value); |
81 | 76 |
82 // Indicates the priority of the specified request changed. | 77 // Indicates the priority of the specified request changed. |
83 void DidChangePriority(int request_id, | 78 void DidChangePriority(int routing_id, int request_id, |
84 net::RequestPriority new_priority, | 79 net::RequestPriority new_priority, |
85 int intra_priority_value); | 80 int intra_priority_value); |
86 | 81 |
87 // This does not take ownership of the delegate. It is expected that the | 82 // This does not take ownership of the delegate. It is expected that the |
88 // delegate have a longer lifetime than the ResourceDispatcher. | 83 // delegate have a longer lifetime than the ResourceDispatcher. |
89 void set_delegate(ResourceDispatcherDelegate* delegate) { | 84 void set_delegate(ResourceDispatcherDelegate* delegate) { |
90 delegate_ = delegate; | 85 delegate_ = delegate; |
91 } | 86 } |
92 | 87 |
93 // Remembers IO thread timestamp for next resource message. | 88 // Remembers IO thread timestamp for next resource message. |
94 void set_io_timestamp(base::TimeTicks io_timestamp) { | 89 void set_io_timestamp(base::TimeTicks io_timestamp) { |
95 io_timestamp_ = io_timestamp; | 90 io_timestamp_ = io_timestamp; |
96 } | 91 } |
97 | 92 |
98 private: | 93 private: |
99 friend class ResourceDispatcherTest; | 94 friend class ResourceDispatcherTest; |
100 | 95 |
101 typedef std::deque<IPC::Message*> MessageQueue; | 96 typedef std::deque<IPC::Message*> MessageQueue; |
102 struct PendingRequestInfo { | 97 struct PendingRequestInfo { |
103 PendingRequestInfo(); | 98 PendingRequestInfo(); |
104 | 99 |
105 PendingRequestInfo(RequestPeer* peer, | 100 PendingRequestInfo(RequestPeer* peer, |
106 ResourceType::Type resource_type, | 101 ResourceType::Type resource_type, |
107 int origin_pid, | 102 int origin_pid, |
108 const GURL& frame_origin, | 103 const GURL& frame_origin, |
109 const GURL& request_url, | 104 const GURL& request_url); |
110 bool download_to_file); | |
111 | 105 |
112 ~PendingRequestInfo(); | 106 ~PendingRequestInfo(); |
113 | 107 |
114 RequestPeer* peer; | 108 RequestPeer* peer; |
115 ResourceType::Type resource_type; | 109 ResourceType::Type resource_type; |
116 // The PID of the original process which issued this request. This gets | 110 // The PID of the original process which issued this request. This gets |
117 // non-zero only for a request proxied by another renderer, particularly | 111 // non-zero only for a request proxied by another renderer, particularly |
118 // requests from plugins. | 112 // requests from plugins. |
119 int origin_pid; | 113 int origin_pid; |
120 MessageQueue deferred_message_queue; | 114 MessageQueue deferred_message_queue; |
121 bool is_deferred; | 115 bool is_deferred; |
122 // Original requested url. | 116 // Original requested url. |
123 GURL url; | 117 GURL url; |
124 // The security origin of the frame that initiates this request. | 118 // The security origin of the frame that initiates this request. |
125 GURL frame_origin; | 119 GURL frame_origin; |
126 // The url of the latest response even in case of redirection. | 120 // The url of the latest response even in case of redirection. |
127 GURL response_url; | 121 GURL response_url; |
128 bool download_to_file; | |
129 linked_ptr<IPC::Message> pending_redirect_message; | 122 linked_ptr<IPC::Message> pending_redirect_message; |
130 base::TimeTicks request_start; | 123 base::TimeTicks request_start; |
131 base::TimeTicks response_start; | 124 base::TimeTicks response_start; |
132 base::TimeTicks completion_time; | 125 base::TimeTicks completion_time; |
133 linked_ptr<base::SharedMemory> buffer; | 126 linked_ptr<base::SharedMemory> buffer; |
134 linked_ptr<SiteIsolationResponseMetaData> site_isolation_metadata; | 127 linked_ptr<SiteIsolationResponseMetaData> site_isolation_metadata; |
135 bool blocked_response; | 128 bool blocked_response; |
136 int buffer_size; | 129 int buffer_size; |
137 }; | 130 }; |
138 typedef base::hash_map<int, PendingRequestInfo> PendingRequestList; | 131 typedef base::hash_map<int, PendingRequestInfo> PendingRequestList; |
139 | 132 |
140 // Helper to lookup the info based on the request_id. | 133 // Helper to lookup the info based on the request_id. |
141 // May return NULL if the request as been canceled from the client side. | 134 // May return NULL if the request as been canceled from the client side. |
142 PendingRequestInfo* GetPendingRequestInfo(int request_id); | 135 PendingRequestInfo* GetPendingRequestInfo(int request_id); |
143 | 136 |
144 // Follows redirect, if any, for the given request. | 137 // Follows redirect, if any, for the given request. |
145 void FollowPendingRedirect(int request_id, PendingRequestInfo& request_info); | 138 void FollowPendingRedirect(int request_id, PendingRequestInfo& request_info); |
146 | 139 |
147 // Message response handlers, called by the message handler for this process. | 140 // Message response handlers, called by the message handler for this process. |
148 void OnUploadProgress(int request_id, int64 position, int64 size); | 141 void OnUploadProgress( |
| 142 int request_id, |
| 143 int64 position, |
| 144 int64 size); |
149 void OnReceivedResponse(int request_id, const ResourceResponseHead&); | 145 void OnReceivedResponse(int request_id, const ResourceResponseHead&); |
150 void OnReceivedCachedMetadata(int request_id, const std::vector<char>& data); | 146 void OnReceivedCachedMetadata(int request_id, const std::vector<char>& data); |
151 void OnReceivedRedirect(int request_id, | 147 void OnReceivedRedirect( |
152 const GURL& new_url, | 148 int request_id, |
153 const ResourceResponseHead& response_head); | 149 const GURL& new_url, |
154 void OnSetDataBuffer(int request_id, | 150 const ResourceResponseHead& response_head); |
155 base::SharedMemoryHandle shm_handle, | 151 void OnSetDataBuffer( |
156 int shm_size, | 152 int request_id, |
157 base::ProcessId renderer_pid); | 153 base::SharedMemoryHandle shm_handle, |
158 void OnReceivedData(int request_id, | 154 int shm_size, |
159 int data_offset, | 155 base::ProcessId renderer_pid); |
160 int data_length, | 156 void OnReceivedData( |
161 int encoded_data_length); | 157 int request_id, |
162 void OnDownloadedData(int request_id, int data_len, int encoded_data_length); | 158 int data_offset, |
| 159 int data_length, |
| 160 int encoded_data_length); |
| 161 void OnDownloadedData( |
| 162 int request_id, |
| 163 int data_len, |
| 164 int encoded_data_length); |
163 void OnRequestComplete( | 165 void OnRequestComplete( |
164 int request_id, | 166 int request_id, |
165 const ResourceMsg_RequestCompleteData& request_complete_data); | 167 const ResourceMsg_RequestCompleteData &request_complete_data); |
166 | 168 |
167 // Dispatch the message to one of the message response handlers. | 169 // Dispatch the message to one of the message response handlers. |
168 void DispatchMessage(const IPC::Message& message); | 170 void DispatchMessage(const IPC::Message& message); |
169 | 171 |
170 // Dispatch any deferred messages for the given request, provided it is not | 172 // Dispatch any deferred messages for the given request, provided it is not |
171 // again in the deferred state. | 173 // again in the deferred state. |
172 void FlushDeferredMessages(int request_id); | 174 void FlushDeferredMessages(int request_id); |
173 | 175 |
174 void ToResourceResponseInfo( | 176 void ToResourceResponseInfo( |
175 const PendingRequestInfo& request_info, | 177 const PendingRequestInfo& request_info, |
(...skipping 16 matching lines...) Expand all Loading... |
192 // handle in it that we should cleanup it up nicely. This method accepts any | 194 // handle in it that we should cleanup it up nicely. This method accepts any |
193 // message and determine whether the message is | 195 // message and determine whether the message is |
194 // ViewHostMsg_Resource_DataReceived and clean up the shared memory handle. | 196 // ViewHostMsg_Resource_DataReceived and clean up the shared memory handle. |
195 static void ReleaseResourcesInDataMessage(const IPC::Message& message); | 197 static void ReleaseResourcesInDataMessage(const IPC::Message& message); |
196 | 198 |
197 // Iterate through a message queue and clean up the messages by calling | 199 // Iterate through a message queue and clean up the messages by calling |
198 // ReleaseResourcesInDataMessage and removing them from the queue. Intended | 200 // ReleaseResourcesInDataMessage and removing them from the queue. Intended |
199 // for use on deferred message queues that are no longer needed. | 201 // for use on deferred message queues that are no longer needed. |
200 static void ReleaseResourcesInMessageQueue(MessageQueue* queue); | 202 static void ReleaseResourcesInMessageQueue(MessageQueue* queue); |
201 | 203 |
202 scoped_ptr<ResourceHostMsg_Request> CreateRequest( | |
203 const RequestInfo& request_info, | |
204 ResourceRequestBody* request_body, | |
205 GURL* frame_origin); | |
206 | |
207 IPC::Sender* message_sender_; | 204 IPC::Sender* message_sender_; |
208 | 205 |
209 // All pending requests issued to the host | 206 // All pending requests issued to the host |
210 PendingRequestList pending_requests_; | 207 PendingRequestList pending_requests_; |
211 | 208 |
212 base::WeakPtrFactory<ResourceDispatcher> weak_factory_; | 209 base::WeakPtrFactory<ResourceDispatcher> weak_factory_; |
213 | 210 |
214 ResourceDispatcherDelegate* delegate_; | 211 ResourceDispatcherDelegate* delegate_; |
215 | 212 |
216 // IO thread timestamp for ongoing IPC message. | 213 // IO thread timestamp for ongoing IPC message. |
217 base::TimeTicks io_timestamp_; | 214 base::TimeTicks io_timestamp_; |
218 | 215 |
219 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcher); | 216 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcher); |
220 }; | 217 }; |
221 | 218 |
222 } // namespace content | 219 } // namespace content |
223 | 220 |
224 #endif // CONTENT_CHILD_RESOURCE_DISPATCHER_H_ | 221 #endif // CONTENT_CHILD_RESOURCE_DISPATCHER_H_ |
OLD | NEW |