Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: content/child/resource_dispatcher.cc

Issue 226273005: Remove webkit's ResourceLoaderBridge interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: put back AddRef() Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include "content/child/resource_dispatcher.h" 7 #include "content/child/resource_dispatcher.h"
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 10 matching lines...) Expand all
21 #include "content/child/sync_load_response.h" 21 #include "content/child/sync_load_response.h"
22 #include "content/common/inter_process_time_ticks_converter.h" 22 #include "content/common/inter_process_time_ticks_converter.h"
23 #include "content/common/resource_messages.h" 23 #include "content/common/resource_messages.h"
24 #include "content/public/child/request_peer.h" 24 #include "content/public/child/request_peer.h"
25 #include "content/public/child/resource_dispatcher_delegate.h" 25 #include "content/public/child/resource_dispatcher_delegate.h"
26 #include "content/public/common/resource_response.h" 26 #include "content/public/common/resource_response.h"
27 #include "net/base/net_errors.h" 27 #include "net/base/net_errors.h"
28 #include "net/base/net_util.h" 28 #include "net/base/net_util.h"
29 #include "net/base/request_priority.h" 29 #include "net/base/request_priority.h"
30 #include "net/http/http_response_headers.h" 30 #include "net/http/http_response_headers.h"
31 #include "webkit/child/resource_loader_bridge.h"
32 #include "webkit/common/resource_type.h" 31 #include "webkit/common/resource_type.h"
33 32
34 using webkit_glue::ResourceLoaderBridge; 33 using webkit_glue::ResourceLoaderBridge;
35 using webkit_glue::ResourceResponseInfo; 34 using webkit_glue::ResourceResponseInfo;
36 35
37 namespace content { 36 namespace content {
38 37
39 namespace { 38 namespace {
40 39
41 // Converts |time| from a remote to local TimeTicks, overwriting the original 40 // Converts |time| from a remote to local TimeTicks, overwriting the original
42 // value. 41 // value.
43 void RemoteToLocalTimeTicks( 42 void RemoteToLocalTimeTicks(
44 const InterProcessTimeTicksConverter& converter, 43 const InterProcessTimeTicksConverter& converter,
45 base::TimeTicks* time) { 44 base::TimeTicks* time) {
46 RemoteTimeTicks remote_time = RemoteTimeTicks::FromTimeTicks(*time); 45 RemoteTimeTicks remote_time = RemoteTimeTicks::FromTimeTicks(*time);
47 *time = converter.ToLocalTimeTicks(remote_time).ToTimeTicks(); 46 *time = converter.ToLocalTimeTicks(remote_time).ToTimeTicks();
48 } 47 }
49 48
50 49 void CrashOnMapFailure() {
51 } // namespace
52
53 static void CrashOnMapFailure() {
54 #if defined(OS_WIN) 50 #if defined(OS_WIN)
55 DWORD last_err = GetLastError(); 51 DWORD last_err = GetLastError();
56 base::debug::Alias(&last_err); 52 base::debug::Alias(&last_err);
57 #endif 53 #endif
58 CHECK(false); 54 CHECK(false);
59 } 55 }
60 56
61 // Each resource request is assigned an ID scoped to this process. 57 // Each resource request is assigned an ID scoped to this process.
62 static int MakeRequestID() { 58 int MakeRequestID() {
63 // NOTE: The resource_dispatcher_host also needs probably unique 59 // NOTE: The resource_dispatcher_host also needs probably unique
64 // request_ids, so they count down from -2 (-1 is a special we're 60 // request_ids, so they count down from -2 (-1 is a special we're
65 // screwed value), while the renderer process counts up. 61 // screwed value), while the renderer process counts up.
66 static int next_request_id = 0; 62 static int next_request_id = 0;
67 return next_request_id++; 63 return next_request_id++;
68 } 64 }
69 65
70 // ResourceLoaderBridge implementation ---------------------------------------- 66 } // namespace
71
72 class IPCResourceLoaderBridge : public ResourceLoaderBridge {
73 public:
74 IPCResourceLoaderBridge(ResourceDispatcher* dispatcher,
75 const RequestInfo& request_info);
76 virtual ~IPCResourceLoaderBridge();
77
78 // ResourceLoaderBridge
79 virtual void SetRequestBody(ResourceRequestBody* request_body) OVERRIDE;
80 virtual bool Start(RequestPeer* peer) OVERRIDE;
81 virtual void Cancel() OVERRIDE;
82 virtual void SetDefersLoading(bool value) OVERRIDE;
83 virtual void DidChangePriority(net::RequestPriority new_priority,
84 int intra_priority_value) OVERRIDE;
85 virtual void SyncLoad(SyncLoadResponse* response) OVERRIDE;
86
87 private:
88 RequestPeer* peer_;
89
90 // The resource dispatcher for this loader. The bridge doesn't own it, but
91 // it's guaranteed to outlive the bridge.
92 ResourceDispatcher* dispatcher_;
93
94 // The request to send, created on initialization for modification and
95 // appending data.
96 ResourceHostMsg_Request request_;
97
98 // ID for the request, valid once Start()ed, -1 if not valid yet.
99 int request_id_;
100
101 // The routing id used when sending IPC messages.
102 int routing_id_;
103
104 // The security origin of the frame that initiates this request.
105 GURL frame_origin_;
106
107 bool is_synchronous_request_;
108 };
109
110 IPCResourceLoaderBridge::IPCResourceLoaderBridge(
111 ResourceDispatcher* dispatcher,
112 const RequestInfo& request_info)
113 : peer_(NULL),
114 dispatcher_(dispatcher),
115 request_id_(-1),
116 routing_id_(request_info.routing_id),
117 is_synchronous_request_(false) {
118 DCHECK(dispatcher_) << "no resource dispatcher";
119 request_.method = request_info.method;
120 request_.url = request_info.url;
121 request_.first_party_for_cookies = request_info.first_party_for_cookies;
122 request_.referrer = request_info.referrer;
123 request_.referrer_policy = request_info.referrer_policy;
124 request_.headers = request_info.headers;
125 request_.load_flags = request_info.load_flags;
126 request_.origin_pid = request_info.requestor_pid;
127 request_.resource_type = request_info.request_type;
128 request_.priority = request_info.priority;
129 request_.request_context = request_info.request_context;
130 request_.appcache_host_id = request_info.appcache_host_id;
131 request_.download_to_file = request_info.download_to_file;
132 request_.has_user_gesture = request_info.has_user_gesture;
133
134 const RequestExtraData kEmptyData;
135 const RequestExtraData* extra_data;
136 if (request_info.extra_data)
137 extra_data = static_cast<RequestExtraData*>(request_info.extra_data);
138 else
139 extra_data = &kEmptyData;
140 request_.visiblity_state = extra_data->visibility_state();
141 request_.render_frame_id = extra_data->render_frame_id();
142 request_.is_main_frame = extra_data->is_main_frame();
143 request_.parent_is_main_frame = extra_data->parent_is_main_frame();
144 request_.parent_render_frame_id = extra_data->parent_render_frame_id();
145 request_.allow_download = extra_data->allow_download();
146 request_.transition_type = extra_data->transition_type();
147 request_.should_replace_current_entry =
148 extra_data->should_replace_current_entry();
149 request_.transferred_request_child_id =
150 extra_data->transferred_request_child_id();
151 request_.transferred_request_request_id =
152 extra_data->transferred_request_request_id();
153 request_.service_worker_provider_id =
154 extra_data->service_worker_provider_id();
155 frame_origin_ = extra_data->frame_origin();
156 }
157
158 IPCResourceLoaderBridge::~IPCResourceLoaderBridge() {
159 // we remove our hook for the resource dispatcher only when going away, since
160 // it doesn't keep track of whether we've force terminated the request
161 if (request_id_ >= 0) {
162 // this operation may fail, as the dispatcher will have preemptively
163 // removed us when the renderer sends the ReceivedAllData message.
164 dispatcher_->RemovePendingRequest(request_id_);
165
166 if (request_.download_to_file) {
167 dispatcher_->message_sender()->Send(
168 new ResourceHostMsg_ReleaseDownloadedFile(request_id_));
jam 2014/04/24 16:14:08 hmm, this isn't called either, it'll have to be ca
tfarina 2014/04/26 04:23:50 Done.
169 }
170 }
171 }
172
173 void IPCResourceLoaderBridge::SetRequestBody(
174 ResourceRequestBody* request_body) {
175 DCHECK(request_id_ == -1) << "request already started";
176 request_.request_body = request_body;
177 }
178
179 // Writes a footer on the message and sends it
180 bool IPCResourceLoaderBridge::Start(RequestPeer* peer) {
181 if (request_id_ != -1) {
182 NOTREACHED() << "Starting a request twice";
183 return false;
184 }
185
186 peer_ = peer;
187
188 // generate the request ID, and append it to the message
189 request_id_ = dispatcher_->AddPendingRequest(peer_,
190 request_.resource_type,
191 request_.origin_pid,
192 frame_origin_,
193 request_.url);
194
195 return dispatcher_->message_sender()->Send(
196 new ResourceHostMsg_RequestResource(routing_id_, request_id_, request_));
197 }
198
199 void IPCResourceLoaderBridge::Cancel() {
200 if (request_id_ < 0) {
201 NOTREACHED() << "Trying to cancel an unstarted request";
202 return;
203 }
204
205 if (!is_synchronous_request_)
206 dispatcher_->CancelPendingRequest(request_id_);
207
208 // We can't remove the request ID from the resource dispatcher because more
209 // data might be pending. Sending the cancel message may cause more data
210 // to be flushed, and will then cause a complete message to be sent.
211 }
212
213 void IPCResourceLoaderBridge::SetDefersLoading(bool value) {
214 if (request_id_ < 0) {
215 NOTREACHED() << "Trying to (un)defer an unstarted request";
216 return;
217 }
218
219 dispatcher_->SetDefersLoading(request_id_, value);
220 }
221
222 void IPCResourceLoaderBridge::DidChangePriority(
223 net::RequestPriority new_priority, int intra_priority_value) {
224 if (request_id_ < 0) {
225 NOTREACHED() << "Trying to change priority of an unstarted request";
226 return;
227 }
228
229 dispatcher_->DidChangePriority(routing_id_, request_id_, new_priority,
230 intra_priority_value);
231 }
232
233 void IPCResourceLoaderBridge::SyncLoad(SyncLoadResponse* response) {
234 if (request_id_ != -1) {
235 NOTREACHED() << "Starting a request twice";
236 response->error_code = net::ERR_FAILED;
237 return;
238 }
239
240 request_id_ = MakeRequestID();
241 is_synchronous_request_ = true;
242
243 SyncLoadResult result;
244 IPC::SyncMessage* msg = new ResourceHostMsg_SyncLoad(routing_id_, request_id_,
245 request_, &result);
246 // NOTE: This may pump events (see RenderThread::Send).
247 if (!dispatcher_->message_sender()->Send(msg)) {
248 response->error_code = net::ERR_FAILED;
249 return;
250 }
251
252 response->error_code = result.error_code;
253 response->url = result.final_url;
254 response->headers = result.headers;
255 response->mime_type = result.mime_type;
256 response->charset = result.charset;
257 response->request_time = result.request_time;
258 response->response_time = result.response_time;
259 response->encoded_data_length = result.encoded_data_length;
260 response->load_timing = result.load_timing;
261 response->devtools_info = result.devtools_info;
262 response->data.swap(result.data);
263 response->download_file_path = result.download_file_path;
264 }
265
266 // ResourceDispatcher ---------------------------------------------------------
267 67
268 ResourceDispatcher::ResourceDispatcher(IPC::Sender* sender) 68 ResourceDispatcher::ResourceDispatcher(IPC::Sender* sender)
269 : message_sender_(sender), 69 : message_sender_(sender),
270 weak_factory_(this), 70 weak_factory_(this),
271 delegate_(NULL), 71 delegate_(NULL),
272 io_timestamp_(base::TimeTicks()) { 72 io_timestamp_(base::TimeTicks()) {
273 } 73 }
274 74
275 ResourceDispatcher::~ResourceDispatcher() { 75 ResourceDispatcher::~ResourceDispatcher() {
276 } 76 }
277 77
278 // ResourceDispatcher implementation ------------------------------------------
279
280 bool ResourceDispatcher::OnMessageReceived(const IPC::Message& message) { 78 bool ResourceDispatcher::OnMessageReceived(const IPC::Message& message) {
281 if (!IsResourceDispatcherMessage(message)) { 79 if (!IsResourceDispatcherMessage(message)) {
282 return false; 80 return false;
283 } 81 }
284 82
285 int request_id; 83 int request_id;
286 84
287 PickleIterator iter(message); 85 PickleIterator iter(message);
288 if (!message.ReadInt(&iter, &request_id)) { 86 if (!message.ReadInt(&iter, &request_id)) {
289 NOTREACHED() << "malformed resource message"; 87 NOTREACHED() << "malformed resource message";
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
550 // Normally, dispatching this message causes the reference-counted request to 348 // Normally, dispatching this message causes the reference-counted request to
551 // die immediately. 349 // die immediately.
552 peer->OnCompletedRequest(request_complete_data.error_code, 350 peer->OnCompletedRequest(request_complete_data.error_code,
553 request_complete_data.was_ignored_by_handler, 351 request_complete_data.was_ignored_by_handler,
554 request_complete_data.exists_in_cache, 352 request_complete_data.exists_in_cache,
555 request_complete_data.security_info, 353 request_complete_data.security_info,
556 renderer_completion_time, 354 renderer_completion_time,
557 request_complete_data.encoded_data_length); 355 request_complete_data.encoded_data_length);
558 } 356 }
559 357
560 int ResourceDispatcher::AddPendingRequest(RequestPeer* callback, 358 int ResourceDispatcher::AddPendingRequest(RequestPeer* callback,
jam 2014/04/24 16:14:08 remove this method from the header and just inline
tfarina 2014/04/26 04:23:50 Done.
561 ResourceType::Type resource_type, 359 ResourceType::Type resource_type,
562 int origin_pid, 360 int origin_pid,
563 const GURL& frame_origin, 361 const GURL& frame_origin,
564 const GURL& request_url) { 362 const GURL& request_url) {
565 // Compute a unique request_id for this renderer process. 363 // Compute a unique request_id for this renderer process.
566 int id = MakeRequestID(); 364 int id = MakeRequestID();
567 pending_requests_[id] = PendingRequestInfo( 365 pending_requests_[id] = PendingRequestInfo(
568 callback, resource_type, origin_pid, frame_origin, request_url); 366 callback, resource_type, origin_pid, frame_origin, request_url);
569 return id; 367 return id;
570 } 368 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
608 FollowPendingRedirect(request_id, request_info); 406 FollowPendingRedirect(request_id, request_info);
609 407
610 base::MessageLoop::current()->PostTask( 408 base::MessageLoop::current()->PostTask(
611 FROM_HERE, 409 FROM_HERE,
612 base::Bind(&ResourceDispatcher::FlushDeferredMessages, 410 base::Bind(&ResourceDispatcher::FlushDeferredMessages,
613 weak_factory_.GetWeakPtr(), 411 weak_factory_.GetWeakPtr(),
614 request_id)); 412 request_id));
615 } 413 }
616 } 414 }
617 415
618 void ResourceDispatcher::DidChangePriority( 416 void ResourceDispatcher::DidChangePriority(int routing_id,
619 int routing_id, int request_id, net::RequestPriority new_priority, 417 int request_id,
620 int intra_priority_value) { 418 net::RequestPriority new_priority,
419 int intra_priority_value) {
621 DCHECK(ContainsKey(pending_requests_, request_id)); 420 DCHECK(ContainsKey(pending_requests_, request_id));
622 message_sender()->Send(new ResourceHostMsg_DidChangePriority( 421 message_sender()->Send(new ResourceHostMsg_DidChangePriority(
623 request_id, new_priority, intra_priority_value)); 422 request_id, new_priority, intra_priority_value));
624 } 423 }
625 424
626 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo() 425 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo()
627 : peer(NULL), 426 : peer(NULL),
628 resource_type(ResourceType::SUB_RESOURCE), 427 resource_type(ResourceType::SUB_RESOURCE),
629 is_deferred(false), 428 is_deferred(false),
630 blocked_response(false), 429 blocked_response(false),
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
687 if (index != pending_requests_.end()) { 486 if (index != pending_requests_.end()) {
688 PendingRequestInfo& pending_request = index->second; 487 PendingRequestInfo& pending_request = index->second;
689 if (pending_request.is_deferred) { 488 if (pending_request.is_deferred) {
690 pending_request.deferred_message_queue.swap(q); 489 pending_request.deferred_message_queue.swap(q);
691 return; 490 return;
692 } 491 }
693 } 492 }
694 } 493 }
695 } 494 }
696 495
697 ResourceLoaderBridge* ResourceDispatcher::CreateBridge( 496 void ResourceDispatcher::StartSync(const RequestInfo& request_info,
698 const RequestInfo& request_info) { 497 ResourceRequestBody* request_body,
699 return new IPCResourceLoaderBridge(this, request_info); 498 SyncLoadResponse* response) {
499 scoped_ptr<ResourceHostMsg_Request> request =
500 CreateRequest(request_info, request_body, NULL);
501
502 SyncLoadResult result;
503 IPC::SyncMessage* msg = new ResourceHostMsg_SyncLoad(
504 request_info.routing_id, MakeRequestID(), *request, &result);
505
506 // NOTE: This may pump events (see RenderThread::Send).
507 if (!message_sender_->Send(msg)) {
508 response->error_code = net::ERR_FAILED;
509 return;
510 }
511
512 response->error_code = result.error_code;
513 response->url = result.final_url;
514 response->headers = result.headers;
515 response->mime_type = result.mime_type;
516 response->charset = result.charset;
517 response->request_time = result.request_time;
518 response->response_time = result.response_time;
519 response->encoded_data_length = result.encoded_data_length;
520 response->load_timing = result.load_timing;
521 response->devtools_info = result.devtools_info;
522 response->data.swap(result.data);
523 response->download_file_path = result.download_file_path;
524 }
525
526 int ResourceDispatcher::StartAsync(const RequestInfo& request_info,
527 ResourceRequestBody* request_body,
528 RequestPeer* peer) {
529 GURL frame_origin;
530 scoped_ptr<ResourceHostMsg_Request> request =
531 CreateRequest(request_info, request_body, &frame_origin);
532
533 // generate the request ID, and append it to the message
534 int request_id = AddPendingRequest(peer,
535 request->resource_type,
536 request->origin_pid,
537 frame_origin,
538 request->url);
539
540 message_sender_->Send(new ResourceHostMsg_RequestResource(
541 request_info.routing_id, request_id, *request));
542
543 return request_id;
700 } 544 }
701 545
702 void ResourceDispatcher::ToResourceResponseInfo( 546 void ResourceDispatcher::ToResourceResponseInfo(
703 const PendingRequestInfo& request_info, 547 const PendingRequestInfo& request_info,
704 const ResourceResponseHead& browser_info, 548 const ResourceResponseHead& browser_info,
705 ResourceResponseInfo* renderer_info) const { 549 ResourceResponseInfo* renderer_info) const {
706 *renderer_info = browser_info; 550 *renderer_info = browser_info;
707 if (request_info.request_start.is_null() || 551 if (request_info.request_start.is_null() ||
708 request_info.response_start.is_null() || 552 request_info.response_start.is_null() ||
709 browser_info.request_start.is_null() || 553 browser_info.request_start.is_null() ||
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
804 // static 648 // static
805 void ResourceDispatcher::ReleaseResourcesInMessageQueue(MessageQueue* queue) { 649 void ResourceDispatcher::ReleaseResourcesInMessageQueue(MessageQueue* queue) {
806 while (!queue->empty()) { 650 while (!queue->empty()) {
807 IPC::Message* message = queue->front(); 651 IPC::Message* message = queue->front();
808 ReleaseResourcesInDataMessage(*message); 652 ReleaseResourcesInDataMessage(*message);
809 queue->pop_front(); 653 queue->pop_front();
810 delete message; 654 delete message;
811 } 655 }
812 } 656 }
813 657
658 scoped_ptr<ResourceHostMsg_Request> ResourceDispatcher::CreateRequest(
659 const RequestInfo& request_info,
660 ResourceRequestBody* request_body,
661 GURL* frame_origin) {
662 scoped_ptr<ResourceHostMsg_Request> request(new ResourceHostMsg_Request);
663 request->method = request_info.method;
664 request->url = request_info.url;
665 request->first_party_for_cookies = request_info.first_party_for_cookies;
666 request->referrer = request_info.referrer;
667 request->referrer_policy = request_info.referrer_policy;
668 request->headers = request_info.headers;
669 request->load_flags = request_info.load_flags;
670 request->origin_pid = request_info.requestor_pid;
671 request->resource_type = request_info.request_type;
672 request->priority = request_info.priority;
673 request->request_context = request_info.request_context;
674 request->appcache_host_id = request_info.appcache_host_id;
675 request->download_to_file = request_info.download_to_file;
676 request->has_user_gesture = request_info.has_user_gesture;
677
678 const RequestExtraData kEmptyData;
679 const RequestExtraData* extra_data;
680 if (request_info.extra_data)
681 extra_data = static_cast<RequestExtraData*>(request_info.extra_data);
682 else
683 extra_data = &kEmptyData;
684 request->visiblity_state = extra_data->visibility_state();
685 request->render_frame_id = extra_data->render_frame_id();
686 request->is_main_frame = extra_data->is_main_frame();
687 request->parent_is_main_frame = extra_data->parent_is_main_frame();
688 request->parent_render_frame_id = extra_data->parent_render_frame_id();
689 request->allow_download = extra_data->allow_download();
690 request->transition_type = extra_data->transition_type();
691 request->should_replace_current_entry =
692 extra_data->should_replace_current_entry();
693 request->transferred_request_child_id =
694 extra_data->transferred_request_child_id();
695 request->transferred_request_request_id =
696 extra_data->transferred_request_request_id();
697 request->service_worker_provider_id =
698 extra_data->service_worker_provider_id();
699 request->request_body = request_body;
700 if (frame_origin)
701 *frame_origin = extra_data->frame_origin();
702 return request.Pass();
703 }
704
814 } // namespace content 705 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698