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" | |
15 #include "base/memory/shared_memory.h" | 16 #include "base/memory/shared_memory.h" |
16 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
19 #include "ipc/ipc_listener.h" | 20 #include "ipc/ipc_listener.h" |
20 #include "ipc/ipc_sender.h" | 21 #include "ipc/ipc_sender.h" |
21 #include "net/base/request_priority.h" | 22 #include "net/base/request_priority.h" |
22 #include "webkit/common/resource_type.h" | 23 #include "webkit/common/resource_type.h" |
23 | 24 |
25 struct ResourceHostMsg_Request; | |
24 struct ResourceMsg_RequestCompleteData; | 26 struct ResourceMsg_RequestCompleteData; |
25 | 27 |
26 namespace webkit_glue { | 28 namespace webkit_glue { |
27 class ResourceLoaderBridge; | 29 class ResourceLoaderBridge; |
28 struct ResourceResponseInfo; | 30 struct ResourceResponseInfo; |
29 } | 31 } |
30 | 32 |
31 namespace content { | 33 namespace content { |
32 class RequestPeer; | 34 class RequestPeer; |
33 class ResourceDispatcherDelegate; | 35 class ResourceDispatcherDelegate; |
36 class ResourceRequestBody; | |
34 struct RequestInfo; | 37 struct RequestInfo; |
35 struct ResourceResponseHead; | 38 struct ResourceResponseHead; |
36 struct SiteIsolationResponseMetaData; | 39 struct SiteIsolationResponseMetaData; |
40 struct SyncLoadResponse; | |
37 | 41 |
38 // This class serves as a communication interface between the | 42 // This class serves as a communication interface between the |
39 // ResourceDispatcherHost in the browser process and the ResourceLoaderBridge in | 43 // ResourceDispatcherHost in the browser process and the ResourceLoaderBridge in |
40 // the child process. It can be used from any child process. | 44 // the child process. It can be used from any child process. |
41 class CONTENT_EXPORT ResourceDispatcher : public IPC::Listener { | 45 class CONTENT_EXPORT ResourceDispatcher : public IPC::Listener { |
42 public: | 46 public: |
43 explicit ResourceDispatcher(IPC::Sender* sender); | 47 explicit ResourceDispatcher(IPC::Sender* sender); |
44 virtual ~ResourceDispatcher(); | 48 virtual ~ResourceDispatcher(); |
45 | 49 |
46 // IPC::Listener implementation. | 50 // IPC::Listener implementation. |
47 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 51 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
48 | 52 |
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); | |
54 | 53 |
55 // Adds a request from the pending_requests_ list, returning the new | 54 // Call this method to load the resource synchronously (i.e., in one shot). |
56 // requests' ID | 55 // This is an alternative to the StartAsync method. Be warned that this method |
57 int AddPendingRequest(RequestPeer* callback, | 56 // will block the calling thread until the resource is fully downloaded or an |
58 ResourceType::Type resource_type, | 57 // error occurs. It could block the calling thread for a long time, so only |
59 int origin_pid, | 58 // use this if you really need it! There is also no way for the caller to |
60 const GURL& frame_origin, | 59 // interrupt this method. Errors are reported via the status field of the |
61 const GURL& request_url); | 60 // response parameter. |
61 void StartSync(const RequestInfo& request_info, | |
62 ResourceRequestBody* request_body, | |
63 SyncLoadResponse* response); | |
62 | 64 |
63 // Removes a request from the pending_requests_ list, returning true if the | 65 // Call this method to initiate the request. If this method succeeds, then |
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 | |
64 // request was found and removed. | 73 // request was found and removed. |
65 bool RemovePendingRequest(int request_id); | 74 bool RemovePendingRequest(int request_id); |
66 | 75 |
67 // Cancels a request in the pending_requests_ list. | 76 // Cancels a request in the |pending_requests_| list. |
68 void CancelPendingRequest(int request_id); | 77 void Cancel(int request_id); |
69 | |
70 IPC::Sender* message_sender() const { | |
71 return message_sender_; | |
72 } | |
73 | 78 |
74 // Toggles the is_deferred attribute for the specified request. | 79 // Toggles the is_deferred attribute for the specified request. |
75 void SetDefersLoading(int request_id, bool value); | 80 void SetDefersLoading(int request_id, bool value); |
76 | 81 |
77 // Indicates the priority of the specified request changed. | 82 // Indicates the priority of the specified request changed. |
78 void DidChangePriority(int routing_id, int request_id, | 83 void DidChangePriority(int request_id, |
79 net::RequestPriority new_priority, | 84 net::RequestPriority new_priority, |
80 int intra_priority_value); | 85 int intra_priority_value); |
81 | 86 |
82 // This does not take ownership of the delegate. It is expected that the | 87 // This does not take ownership of the delegate. It is expected that the |
83 // delegate have a longer lifetime than the ResourceDispatcher. | 88 // delegate have a longer lifetime than the ResourceDispatcher. |
84 void set_delegate(ResourceDispatcherDelegate* delegate) { | 89 void set_delegate(ResourceDispatcherDelegate* delegate) { |
85 delegate_ = delegate; | 90 delegate_ = delegate; |
86 } | 91 } |
87 | 92 |
88 // Remembers IO thread timestamp for next resource message. | 93 // Remembers IO thread timestamp for next resource message. |
89 void set_io_timestamp(base::TimeTicks io_timestamp) { | 94 void set_io_timestamp(base::TimeTicks io_timestamp) { |
90 io_timestamp_ = io_timestamp; | 95 io_timestamp_ = io_timestamp; |
91 } | 96 } |
92 | 97 |
98 IPC::Sender* message_sender() const { return message_sender_; } | |
jam
2014/04/28 15:26:13
nit: hide this once you do my other comment (sendi
| |
99 | |
93 private: | 100 private: |
94 friend class ResourceDispatcherTest; | 101 friend class ResourceDispatcherTest; |
95 | 102 |
96 typedef std::deque<IPC::Message*> MessageQueue; | 103 typedef std::deque<IPC::Message*> MessageQueue; |
97 struct PendingRequestInfo { | 104 struct PendingRequestInfo { |
98 PendingRequestInfo(); | 105 PendingRequestInfo(); |
99 | 106 |
100 PendingRequestInfo(RequestPeer* peer, | 107 PendingRequestInfo(RequestPeer* peer, |
101 ResourceType::Type resource_type, | 108 ResourceType::Type resource_type, |
102 int origin_pid, | 109 int origin_pid, |
(...skipping 28 matching lines...) Expand all Loading... | |
131 typedef base::hash_map<int, PendingRequestInfo> PendingRequestList; | 138 typedef base::hash_map<int, PendingRequestInfo> PendingRequestList; |
132 | 139 |
133 // Helper to lookup the info based on the request_id. | 140 // Helper to lookup the info based on the request_id. |
134 // May return NULL if the request as been canceled from the client side. | 141 // May return NULL if the request as been canceled from the client side. |
135 PendingRequestInfo* GetPendingRequestInfo(int request_id); | 142 PendingRequestInfo* GetPendingRequestInfo(int request_id); |
136 | 143 |
137 // Follows redirect, if any, for the given request. | 144 // Follows redirect, if any, for the given request. |
138 void FollowPendingRedirect(int request_id, PendingRequestInfo& request_info); | 145 void FollowPendingRedirect(int request_id, PendingRequestInfo& request_info); |
139 | 146 |
140 // Message response handlers, called by the message handler for this process. | 147 // Message response handlers, called by the message handler for this process. |
141 void OnUploadProgress( | 148 void OnUploadProgress(int request_id, int64 position, int64 size); |
142 int request_id, | |
143 int64 position, | |
144 int64 size); | |
145 void OnReceivedResponse(int request_id, const ResourceResponseHead&); | 149 void OnReceivedResponse(int request_id, const ResourceResponseHead&); |
146 void OnReceivedCachedMetadata(int request_id, const std::vector<char>& data); | 150 void OnReceivedCachedMetadata(int request_id, const std::vector<char>& data); |
147 void OnReceivedRedirect( | 151 void OnReceivedRedirect(int request_id, |
148 int request_id, | 152 const GURL& new_url, |
149 const GURL& new_url, | 153 const ResourceResponseHead& response_head); |
150 const ResourceResponseHead& response_head); | 154 void OnSetDataBuffer(int request_id, |
151 void OnSetDataBuffer( | 155 base::SharedMemoryHandle shm_handle, |
152 int request_id, | 156 int shm_size, |
153 base::SharedMemoryHandle shm_handle, | 157 base::ProcessId renderer_pid); |
154 int shm_size, | 158 void OnReceivedData(int request_id, |
155 base::ProcessId renderer_pid); | 159 int data_offset, |
156 void OnReceivedData( | 160 int data_length, |
157 int request_id, | 161 int encoded_data_length); |
158 int data_offset, | 162 void OnDownloadedData(int request_id, int data_len, int encoded_data_length); |
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); | |
165 void OnRequestComplete( | 163 void OnRequestComplete( |
166 int request_id, | 164 int request_id, |
167 const ResourceMsg_RequestCompleteData &request_complete_data); | 165 const ResourceMsg_RequestCompleteData& request_complete_data); |
168 | 166 |
169 // Dispatch the message to one of the message response handlers. | 167 // Dispatch the message to one of the message response handlers. |
170 void DispatchMessage(const IPC::Message& message); | 168 void DispatchMessage(const IPC::Message& message); |
171 | 169 |
172 // Dispatch any deferred messages for the given request, provided it is not | 170 // Dispatch any deferred messages for the given request, provided it is not |
173 // again in the deferred state. | 171 // again in the deferred state. |
174 void FlushDeferredMessages(int request_id); | 172 void FlushDeferredMessages(int request_id); |
175 | 173 |
176 void ToResourceResponseInfo( | 174 void ToResourceResponseInfo( |
177 const PendingRequestInfo& request_info, | 175 const PendingRequestInfo& request_info, |
(...skipping 16 matching lines...) Expand all Loading... | |
194 // handle in it that we should cleanup it up nicely. This method accepts any | 192 // handle in it that we should cleanup it up nicely. This method accepts any |
195 // message and determine whether the message is | 193 // message and determine whether the message is |
196 // ViewHostMsg_Resource_DataReceived and clean up the shared memory handle. | 194 // ViewHostMsg_Resource_DataReceived and clean up the shared memory handle. |
197 static void ReleaseResourcesInDataMessage(const IPC::Message& message); | 195 static void ReleaseResourcesInDataMessage(const IPC::Message& message); |
198 | 196 |
199 // Iterate through a message queue and clean up the messages by calling | 197 // Iterate through a message queue and clean up the messages by calling |
200 // ReleaseResourcesInDataMessage and removing them from the queue. Intended | 198 // ReleaseResourcesInDataMessage and removing them from the queue. Intended |
201 // for use on deferred message queues that are no longer needed. | 199 // for use on deferred message queues that are no longer needed. |
202 static void ReleaseResourcesInMessageQueue(MessageQueue* queue); | 200 static void ReleaseResourcesInMessageQueue(MessageQueue* queue); |
203 | 201 |
202 scoped_ptr<ResourceHostMsg_Request> CreateRequest( | |
203 const RequestInfo& request_info, | |
204 ResourceRequestBody* request_body, | |
205 GURL* frame_origin); | |
206 | |
204 IPC::Sender* message_sender_; | 207 IPC::Sender* message_sender_; |
205 | 208 |
206 // All pending requests issued to the host | 209 // All pending requests issued to the host |
207 PendingRequestList pending_requests_; | 210 PendingRequestList pending_requests_; |
208 | 211 |
209 base::WeakPtrFactory<ResourceDispatcher> weak_factory_; | 212 base::WeakPtrFactory<ResourceDispatcher> weak_factory_; |
210 | 213 |
211 ResourceDispatcherDelegate* delegate_; | 214 ResourceDispatcherDelegate* delegate_; |
212 | 215 |
213 // IO thread timestamp for ongoing IPC message. | 216 // IO thread timestamp for ongoing IPC message. |
214 base::TimeTicks io_timestamp_; | 217 base::TimeTicks io_timestamp_; |
215 | 218 |
216 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcher); | 219 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcher); |
217 }; | 220 }; |
218 | 221 |
219 } // namespace content | 222 } // namespace content |
220 | 223 |
221 #endif // CONTENT_CHILD_RESOURCE_DISPATCHER_H_ | 224 #endif // CONTENT_CHILD_RESOURCE_DISPATCHER_H_ |
OLD | NEW |