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 // This is the browser side of the resource dispatcher, it receives requests | 5 // This is the browser side of the resource dispatcher, it receives requests |
6 // from the child process (i.e. [Renderer, Plugin, Worker]ProcessHost), and | 6 // from the child process (i.e. [Renderer, Plugin, Worker]ProcessHost), and |
7 // dispatches them to URLRequests. It then forwards the messages from the | 7 // dispatches them to URLRequests. It then forwards the messages from the |
8 // URLRequests back to the correct process for handling. | 8 // URLRequests back to the correct process for handling. |
9 // | 9 // |
10 // See http://dev.chromium.org/developers/design-documents/multi-process-resourc
e-loading | 10 // See http://dev.chromium.org/developers/design-documents/multi-process-resourc
e-loading |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 // If it was, message_was_ok will be false iff the message was corrupt. | 78 // If it was, message_was_ok will be false iff the message was corrupt. |
79 bool OnMessageReceived(const IPC::Message& message, | 79 bool OnMessageReceived(const IPC::Message& message, |
80 ResourceMessageFilter* filter, | 80 ResourceMessageFilter* filter, |
81 bool* message_was_ok); | 81 bool* message_was_ok); |
82 | 82 |
83 // Initiates a download by explicit request of the renderer, e.g. due to | 83 // Initiates a download by explicit request of the renderer, e.g. due to |
84 // alt-clicking a link. If |request| is malformed or not permitted or the RDH | 84 // alt-clicking a link. If |request| is malformed or not permitted or the RDH |
85 // is shutting down, then |started_cb| will be called immediately. There is no | 85 // is shutting down, then |started_cb| will be called immediately. There is no |
86 // situation in which |started_cb| will never be called. | 86 // situation in which |started_cb| will never be called. |
87 void BeginDownload( | 87 void BeginDownload( |
88 net::URLRequest* request, // ownership is taken | 88 scoped_ptr<net::URLRequest> request, |
89 const DownloadSaveInfo& save_info, | 89 const DownloadSaveInfo& save_info, |
90 bool prompt_for_save_location, | 90 bool prompt_for_save_location, |
91 const DownloadResourceHandler::OnStartedCallback& started_cb, | 91 const DownloadResourceHandler::OnStartedCallback& started_cb, |
92 int child_id, | 92 int child_id, |
93 int route_id, | 93 int route_id, |
94 const content::ResourceContext& context); | 94 const content::ResourceContext& context); |
95 | 95 |
96 // Initiates a save file from the browser process (as opposed to a resource | 96 // Initiates a save file from the browser process (as opposed to a resource |
97 // request from the renderer or another child process). | 97 // request from the renderer or another child process). |
98 void BeginSaveFile(const GURL& url, | 98 void BeginSaveFile(const GURL& url, |
99 const GURL& referrer, | 99 const GURL& referrer, |
100 int process_unique_id, | 100 int child_id, |
101 int route_id, | 101 int route_id, |
102 const content::ResourceContext& context); | 102 const content::ResourceContext& context); |
103 | 103 |
104 // Cancels the given request if it still exists. We ignore cancels from the | 104 // Cancels the given request if it still exists. We ignore cancels from the |
105 // renderer in the event of a download. | 105 // renderer in the event of a download. |
106 void CancelRequest(int process_unique_id, | 106 void CancelRequest(int child_id, |
107 int request_id, | 107 int request_id, |
108 bool from_renderer); | 108 bool from_renderer); |
109 | 109 |
110 // Follows a deferred redirect for the given request. | 110 // Follows a deferred redirect for the given request. |
111 // new_first_party_for_cookies, if non-empty, is the new cookie policy URL | 111 // new_first_party_for_cookies, if non-empty, is the new cookie policy URL |
112 // for the redirected URL. If the cookie policy URL needs changing, pass | 112 // for the redirected URL. If the cookie policy URL needs changing, pass |
113 // true as has_new_first_party_for_cookies and the new cookie policy URL as | 113 // true as has_new_first_party_for_cookies and the new cookie policy URL as |
114 // new_first_party_for_cookies. Otherwise, pass false as | 114 // new_first_party_for_cookies. Otherwise, pass false as |
115 // has_new_first_party_for_cookies, and new_first_party_for_cookies will not | 115 // has_new_first_party_for_cookies, and new_first_party_for_cookies will not |
116 // be used. | 116 // be used. |
117 void FollowDeferredRedirect(int process_unique_id, | 117 void FollowDeferredRedirect(int child_id, |
118 int request_id, | 118 int request_id, |
119 bool has_new_first_party_for_cookies, | 119 bool has_new_first_party_for_cookies, |
120 const GURL& new_first_party_for_cookies); | 120 const GURL& new_first_party_for_cookies); |
121 | 121 |
122 // Starts a request that was deferred during ResourceHandler::OnWillStart(). | 122 // Starts a request that was deferred during ResourceHandler::OnWillStart(). |
123 void StartDeferredRequest(int process_unique_id, int request_id); | 123 void StartDeferredRequest(int child_id, int request_id); |
124 | 124 |
125 // Returns true if it's ok to send the data. If there are already too many | 125 // Returns true if it's ok to send the data. If there are already too many |
126 // data messages pending, it pauses the request and returns false. In this | 126 // data messages pending, it pauses the request and returns false. In this |
127 // case the caller should not send the data. | 127 // case the caller should not send the data. |
128 bool WillSendData(int process_unique_id, int request_id); | 128 bool WillSendData(int child_id, int request_id); |
129 | 129 |
130 // Pauses or resumes network activity for a particular request. | 130 // Pauses or resumes network activity for a particular request. |
131 void PauseRequest(int process_unique_id, int request_id, bool pause); | 131 void PauseRequest(int child_id, int request_id, bool pause); |
132 | 132 |
133 // Returns the number of pending requests. This is designed for the unittests | 133 // Returns the number of pending requests. This is designed for the unittests |
134 int pending_requests() const { | 134 int pending_requests() const { |
135 return static_cast<int>(pending_requests_.size()); | 135 return static_cast<int>(pending_requests_.size()); |
136 } | 136 } |
137 | 137 |
138 // Intended for unit-tests only. Returns the memory cost of all the | 138 // Intended for unit-tests only. Returns the memory cost of all the |
139 // outstanding requests (pending and blocked) for |process_unique_id|. | 139 // outstanding requests (pending and blocked) for |child_id|. |
140 int GetOutstandingRequestsMemoryCost(int process_unique_id) const; | 140 int GetOutstandingRequestsMemoryCost(int child_id) const; |
141 | 141 |
142 // Intended for unit-tests only. Overrides the outstanding requests bound. | 142 // Intended for unit-tests only. Overrides the outstanding requests bound. |
143 void set_max_outstanding_requests_cost_per_process(int limit) { | 143 void set_max_outstanding_requests_cost_per_process(int limit) { |
144 max_outstanding_requests_cost_per_process_ = limit; | 144 max_outstanding_requests_cost_per_process_ = limit; |
145 } | 145 } |
146 | 146 |
147 // The average private bytes increase of the browser for each new pending | 147 // The average private bytes increase of the browser for each new pending |
148 // request. Experimentally obtained. | 148 // request. Experimentally obtained. |
149 static const int kAvgBytesPerOutstandingRequest = 4400; | 149 static const int kAvgBytesPerOutstandingRequest = 4400; |
150 | 150 |
151 DownloadFileManager* download_file_manager() const { | 151 DownloadFileManager* download_file_manager() const { |
152 return download_file_manager_; | 152 return download_file_manager_; |
153 } | 153 } |
154 | 154 |
155 SaveFileManager* save_file_manager() const { | 155 SaveFileManager* save_file_manager() const { |
156 return save_file_manager_; | 156 return save_file_manager_; |
157 } | 157 } |
158 | 158 |
159 // Called when the unload handler for a cross-site request has finished. | 159 // Called when the unload handler for a cross-site request has finished. |
160 void OnSwapOutACK(const ViewMsg_SwapOut_Params& params); | 160 void OnSwapOutACK(const ViewMsg_SwapOut_Params& params); |
161 | 161 |
162 // Called when the renderer loads a resource from its internal cache. | 162 // Called when the renderer loads a resource from its internal cache. |
163 void OnDidLoadResourceFromMemoryCache(const GURL& url, | 163 void OnDidLoadResourceFromMemoryCache(const GURL& url, |
164 const std::string& security_info, | 164 const std::string& security_info, |
165 const std::string& http_method, | 165 const std::string& http_method, |
166 ResourceType::Type resource_type); | 166 ResourceType::Type resource_type); |
167 | 167 |
168 // Force cancels any pending requests for the given process. | 168 // Force cancels any pending requests for the given process. |
169 void CancelRequestsForProcess(int process_unique_id); | 169 void CancelRequestsForProcess(int child_id); |
170 | 170 |
171 // Force cancels any pending requests for the given route id. This method | 171 // Force cancels any pending requests for the given route id. This method |
172 // acts like CancelRequestsForProcess when route_id is -1. | 172 // acts like CancelRequestsForProcess when route_id is -1. |
173 void CancelRequestsForRoute(int process_unique_id, int route_id); | 173 void CancelRequestsForRoute(int child_id, int route_id); |
174 | 174 |
175 // Force cancels any pending requests for the given |context|. This is | 175 // Force cancels any pending requests for the given |context|. This is |
176 // necessary to ensure that before |context| goes away, all requests | 176 // necessary to ensure that before |context| goes away, all requests |
177 // for it are dead. | 177 // for it are dead. |
178 void CancelRequestsForContext(const content::ResourceContext* context); | 178 void CancelRequestsForContext(const content::ResourceContext* context); |
179 | 179 |
180 // net::URLRequest::Delegate | 180 // net::URLRequest::Delegate |
181 virtual void OnReceivedRedirect(net::URLRequest* request, | 181 virtual void OnReceivedRedirect(net::URLRequest* request, |
182 const GURL& new_url, | 182 const GURL& new_url, |
183 bool* defer_redirect) OVERRIDE; | 183 bool* defer_redirect) OVERRIDE; |
(...skipping 30 matching lines...) Expand all Loading... |
214 // such IDs associated with the request (such as non-page-related requests), | 214 // such IDs associated with the request (such as non-page-related requests), |
215 // this function will return false and both out params will be -1. | 215 // this function will return false and both out params will be -1. |
216 static bool RenderViewForRequest(const net::URLRequest* request, | 216 static bool RenderViewForRequest(const net::URLRequest* request, |
217 int* render_process_host_id, | 217 int* render_process_host_id, |
218 int* render_view_host_id); | 218 int* render_view_host_id); |
219 | 219 |
220 // Retrieves a net::URLRequest. Must be called from the IO thread. | 220 // Retrieves a net::URLRequest. Must be called from the IO thread. |
221 net::URLRequest* GetURLRequest( | 221 net::URLRequest* GetURLRequest( |
222 const content::GlobalRequestID& request_id) const; | 222 const content::GlobalRequestID& request_id) const; |
223 | 223 |
224 void RemovePendingRequest(int process_unique_id, int request_id); | 224 void RemovePendingRequest(int child_id, int request_id); |
225 | 225 |
226 // Causes all new requests for the route identified by | 226 // Causes all new requests for the route identified by |
227 // |process_unique_id| and |route_id| to be blocked (not being | 227 // |child_id| and |route_id| to be blocked (not being |
228 // started) until ResumeBlockedRequestsForRoute or | 228 // started) until ResumeBlockedRequestsForRoute or |
229 // CancelBlockedRequestsForRoute is called. | 229 // CancelBlockedRequestsForRoute is called. |
230 void BlockRequestsForRoute(int process_unique_id, int route_id); | 230 void BlockRequestsForRoute(int child_id, int route_id); |
231 | 231 |
232 // Resumes any blocked request for the specified route id. | 232 // Resumes any blocked request for the specified route id. |
233 void ResumeBlockedRequestsForRoute(int process_unique_id, int route_id); | 233 void ResumeBlockedRequestsForRoute(int child_id, int route_id); |
234 | 234 |
235 // Cancels any blocked request for the specified route id. | 235 // Cancels any blocked request for the specified route id. |
236 void CancelBlockedRequestsForRoute(int process_unique_id, int route_id); | 236 void CancelBlockedRequestsForRoute(int child_id, int route_id); |
237 | 237 |
238 // Decrements the pending_data_count for the request and resumes | 238 // Decrements the pending_data_count for the request and resumes |
239 // the request if it was paused due to too many pending data | 239 // the request if it was paused due to too many pending data |
240 // messages sent. | 240 // messages sent. |
241 void DataReceivedACK(int process_unique_id, int request_id); | 241 void DataReceivedACK(int child_id, int request_id); |
242 | 242 |
243 // Maintains a collection of temp files created in support of | 243 // Maintains a collection of temp files created in support of |
244 // the download_to_file capability. Used to grant access to the | 244 // the download_to_file capability. Used to grant access to the |
245 // child process and to defer deletion of the file until it's | 245 // child process and to defer deletion of the file until it's |
246 // no longer needed. | 246 // no longer needed. |
247 void RegisterDownloadedTempFile( | 247 void RegisterDownloadedTempFile( |
248 int child_id, int request_id, | 248 int child_id, int request_id, |
249 webkit_blob::DeletableFileReference* reference); | 249 webkit_blob::DeletableFileReference* reference); |
250 void UnregisterDownloadedTempFile(int child_id, int request_id); | 250 void UnregisterDownloadedTempFile(int child_id, int request_id); |
251 | 251 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 // Helper function that cancels |request|. Returns whether the | 326 // Helper function that cancels |request|. Returns whether the |
327 // request was actually cancelled. If a renderer cancels a request | 327 // request was actually cancelled. If a renderer cancels a request |
328 // for a download, we ignore the cancellation. | 328 // for a download, we ignore the cancellation. |
329 bool CancelRequestInternal(net::URLRequest* request, bool from_renderer); | 329 bool CancelRequestInternal(net::URLRequest* request, bool from_renderer); |
330 | 330 |
331 // Helper function that inserts |request| into the resource queue. | 331 // Helper function that inserts |request| into the resource queue. |
332 void InsertIntoResourceQueue( | 332 void InsertIntoResourceQueue( |
333 net::URLRequest* request, | 333 net::URLRequest* request, |
334 const ResourceDispatcherHostRequestInfo& request_info); | 334 const ResourceDispatcherHostRequestInfo& request_info); |
335 | 335 |
336 // Updates the "cost" of outstanding requests for |process_unique_id|. | 336 // Updates the "cost" of outstanding requests for |child_id|. |
337 // The "cost" approximates how many bytes are consumed by all the in-memory | 337 // The "cost" approximates how many bytes are consumed by all the in-memory |
338 // data structures supporting this request (net::URLRequest object, | 338 // data structures supporting this request (net::URLRequest object, |
339 // HttpNetworkTransaction, etc...). | 339 // HttpNetworkTransaction, etc...). |
340 // The value of |cost| is added to the running total, and the resulting | 340 // The value of |cost| is added to the running total, and the resulting |
341 // sum is returned. | 341 // sum is returned. |
342 int IncrementOutstandingRequestsMemoryCost(int cost, | 342 int IncrementOutstandingRequestsMemoryCost(int cost, |
343 int process_unique_id); | 343 int child_id); |
344 | 344 |
345 // Estimate how much heap space |request| will consume to run. | 345 // Estimate how much heap space |request| will consume to run. |
346 static int CalculateApproximateMemoryCost(net::URLRequest* request); | 346 static int CalculateApproximateMemoryCost(net::URLRequest* request); |
347 | 347 |
348 // The list of all requests that we have pending. This list is not really | 348 // The list of all requests that we have pending. This list is not really |
349 // optimized, and assumes that we have relatively few requests pending at once | 349 // optimized, and assumes that we have relatively few requests pending at once |
350 // since some operations require brute-force searching of the list. | 350 // since some operations require brute-force searching of the list. |
351 // | 351 // |
352 // It may be enhanced in the future to provide some kind of prioritization | 352 // It may be enhanced in the future to provide some kind of prioritization |
353 // mechanism. We should also consider a hashtable or binary tree if it turns | 353 // mechanism. We should also consider a hashtable or binary tree if it turns |
354 // out we have a lot of things here. | 354 // out we have a lot of things here. |
355 typedef std::map<content::GlobalRequestID, net::URLRequest*> | 355 typedef std::map<content::GlobalRequestID, net::URLRequest*> |
356 PendingRequestList; | 356 PendingRequestList; |
357 | 357 |
358 // Deletes the pending request identified by the iterator passed in. | 358 // Deletes the pending request identified by the iterator passed in. |
359 // This function will invalidate the iterator passed in. Callers should | 359 // This function will invalidate the iterator passed in. Callers should |
360 // not rely on this iterator being valid on return. | 360 // not rely on this iterator being valid on return. |
361 void RemovePendingRequest(const PendingRequestList::iterator& iter); | 361 void RemovePendingRequest(const PendingRequestList::iterator& iter); |
362 | 362 |
363 // Notify our observers that we started receiving a response for a request. | 363 // Notify our observers that we started receiving a response for a request. |
364 void NotifyResponseStarted(net::URLRequest* request, int process_unique_id); | 364 void NotifyResponseStarted(net::URLRequest* request, int child_id); |
365 | 365 |
366 // Notify our observers that a request has been redirected. | 366 // Notify our observers that a request has been redirected. |
367 void NotifyReceivedRedirect(net::URLRequest* request, | 367 void NotifyReceivedRedirect(net::URLRequest* request, |
368 int process_unique_id, | 368 int child_id, |
369 const GURL& new_url); | 369 const GURL& new_url); |
370 | 370 |
371 // Tries to handle the url with an external protocol. If the request is | 371 // Tries to handle the url with an external protocol. If the request is |
372 // handled, the function returns true. False otherwise. | 372 // handled, the function returns true. False otherwise. |
373 bool HandleExternalProtocol(int request_id, | 373 bool HandleExternalProtocol(int request_id, |
374 int process_unique_id, | 374 int child_id, |
375 int route_id, | 375 int route_id, |
376 const GURL& url, | 376 const GURL& url, |
377 ResourceType::Type resource_type, | 377 ResourceType::Type resource_type, |
378 const net::URLRequestJobFactory& job_factory, | 378 const net::URLRequestJobFactory& job_factory, |
379 ResourceHandler* handler); | 379 ResourceHandler* handler); |
380 | 380 |
381 // Checks all pending requests and updates the load states and upload | 381 // Checks all pending requests and updates the load states and upload |
382 // progress if necessary. | 382 // progress if necessary. |
383 void UpdateLoadStates(); | 383 void UpdateLoadStates(); |
384 | 384 |
385 // Checks the upload state and sends an update if one is necessary. | 385 // Checks the upload state and sends an update if one is necessary. |
386 void MaybeUpdateUploadProgress(ResourceDispatcherHostRequestInfo *info, | 386 void MaybeUpdateUploadProgress(ResourceDispatcherHostRequestInfo *info, |
387 net::URLRequest *request); | 387 net::URLRequest *request); |
388 | 388 |
389 // Resumes or cancels (if |cancel_requests| is true) any blocked requests. | 389 // Resumes or cancels (if |cancel_requests| is true) any blocked requests. |
390 void ProcessBlockedRequestsForRoute(int process_unique_id, | 390 void ProcessBlockedRequestsForRoute(int child_id, |
391 int route_id, | 391 int route_id, |
392 bool cancel_requests); | 392 bool cancel_requests); |
393 | 393 |
394 void OnRequestResource(const IPC::Message& msg, | 394 void OnRequestResource(const IPC::Message& msg, |
395 int request_id, | 395 int request_id, |
396 const ResourceHostMsg_Request& request_data); | 396 const ResourceHostMsg_Request& request_data); |
397 void OnSyncLoad(int request_id, | 397 void OnSyncLoad(int request_id, |
398 const ResourceHostMsg_Request& request_data, | 398 const ResourceHostMsg_Request& request_data, |
399 IPC::Message* sync_result); | 399 IPC::Message* sync_result); |
400 void BeginRequest(int request_id, | 400 void BeginRequest(int request_id, |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 base::WeakPtrFactory<ResourceDispatcherHost> weak_factory_; | 489 base::WeakPtrFactory<ResourceDispatcherHost> weak_factory_; |
490 | 490 |
491 // True if the resource dispatcher host has been shut down. | 491 // True if the resource dispatcher host has been shut down. |
492 bool is_shutdown_; | 492 bool is_shutdown_; |
493 | 493 |
494 typedef std::vector<net::URLRequest*> BlockedRequestsList; | 494 typedef std::vector<net::URLRequest*> BlockedRequestsList; |
495 typedef std::pair<int, int> ProcessRouteIDs; | 495 typedef std::pair<int, int> ProcessRouteIDs; |
496 typedef std::map<ProcessRouteIDs, BlockedRequestsList*> BlockedRequestMap; | 496 typedef std::map<ProcessRouteIDs, BlockedRequestsList*> BlockedRequestMap; |
497 BlockedRequestMap blocked_requests_map_; | 497 BlockedRequestMap blocked_requests_map_; |
498 | 498 |
499 // Maps the process_unique_ids to the approximate number of bytes | 499 // Maps the child_ids to the approximate number of bytes |
500 // being used to service its resource requests. No entry implies 0 cost. | 500 // being used to service its resource requests. No entry implies 0 cost. |
501 typedef std::map<int, int> OutstandingRequestsMemoryCostMap; | 501 typedef std::map<int, int> OutstandingRequestsMemoryCostMap; |
502 OutstandingRequestsMemoryCostMap outstanding_requests_memory_cost_map_; | 502 OutstandingRequestsMemoryCostMap outstanding_requests_memory_cost_map_; |
503 | 503 |
504 // |max_outstanding_requests_cost_per_process_| is the upper bound on how | 504 // |max_outstanding_requests_cost_per_process_| is the upper bound on how |
505 // many outstanding requests can be issued per child process host. | 505 // many outstanding requests can be issued per child process host. |
506 // The constraint is expressed in terms of bytes (where the cost of | 506 // The constraint is expressed in terms of bytes (where the cost of |
507 // individual requests is given by CalculateApproximateMemoryCost). | 507 // individual requests is given by CalculateApproximateMemoryCost). |
508 // The total number of outstanding requests is roughly: | 508 // The total number of outstanding requests is roughly: |
509 // (max_outstanding_requests_cost_per_process_ / | 509 // (max_outstanding_requests_cost_per_process_ / |
(...skipping 17 matching lines...) Expand all Loading... |
527 // Maps the request ID of request that is being transferred to a new RVH | 527 // Maps the request ID of request that is being transferred to a new RVH |
528 // to the respective request. | 528 // to the respective request. |
529 typedef std::map<content::GlobalRequestID, net::URLRequest*> | 529 typedef std::map<content::GlobalRequestID, net::URLRequest*> |
530 TransferredNavigations; | 530 TransferredNavigations; |
531 TransferredNavigations transferred_navigations_; | 531 TransferredNavigations transferred_navigations_; |
532 | 532 |
533 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcherHost); | 533 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcherHost); |
534 }; | 534 }; |
535 | 535 |
536 #endif // CONTENT_BROWSER_RENDERER_HOST_RESOURCE_DISPATCHER_HOST_H_ | 536 #endif // CONTENT_BROWSER_RENDERER_HOST_RESOURCE_DISPATCHER_HOST_H_ |
OLD | NEW |