消息的流通过程,是一个不同类相互交流的过程,如果不了解这个过程,根本就不知道这些类是怎么样相互协作的。由于上一次说到
ViewHostMsg_RequestResource
消息已经发送出来,它的处理过徎其实就是一般资源的消息处理过程,下面就来看看这个消息的处理过程,如下:
1.
base::MessagePumpWin::Run
函数消息
2.
base::MessagePumpWin::RunWithDispatcher
3.
base::MessagePumpWin::DoRunLoop
4.
base::MessagePumpWin::WaitForWork
5.
base::MessagePumpWin::SignalWatcher
6.
IPC::Channel::OnObjectSignaled
通道的消息处理。
7.
IPC::Channel::ProcessIncomingMessages
8.
IPC::SyncChannel::SyncContext::OnMessageReceived
9.
IPC::ChannelProxy::Context::TryFilters
10.
ResourceMessageFilter::OnMessageReceived
11.
IPC::MessageWithTuple<Tuple2<int,ViewHostMsg_Resource_Request> >::Dispatch<ResourceMessageFilter,int,ViewHostMsg_Resource_Request const &>
12.
ResourceMessageFilter::OnRequestResource
13.
ResourceDispatcherHost::BeginRequest
消息通过上面
12
个函数的流转,到达到处理这个消息的函数
ResourceDispatcherHost::BeginRequest
,它在里面就会把消息变成一个网络下载请求,函数代码如下:
#001
void ResourceDispatcherHost::BeginRequest(
#002
Receiver* receiver,
#003
HANDLE render_process_handle,
#004
int render_process_host_id,
#005
int render_view_id,
#006
int request_id,
#007
const ViewHostMsg_Resource_Request& request_data,
#008
URLRequestContext* request_context,
#009
IPC::Message* sync_result) {
是否已经传送完成,或者关闭。
#010
if (is_shutdown_ ||
#011
!ShouldServiceRequest(render_process_host_id, request_data)) {
#012
// Tell the renderer that this request was disallowed.
#013
receiver->Send(new ViewMsg_Resource_RequestComplete(
#014
render_view_id,
#015
request_id,
#016
URLRequestStatus(URLRequestStatus::FAILED, net::ERR_ABORTED)));
#017
return;
#018
}
#019
保证所有插件已经加载。
#020
// Ensure the Chrome plugins are loaded, as they may intercept network
#021
// requests.
Does nothing if they are already loaded.
#022
// TODO(mpcomplete): This takes 200 ms!
Investigate parallelizing this by
#023
// starting the load earlier in a BG thread.
#024
plugin_service_->LoadChromePlugins(this);
#025
构造事件处理器。
#026
// Construct the event handler.
#027
scoped_refptr<EventHandler> handler;
#028
if (sync_result) {
#029
handler = new SyncEventHandler(receiver, request_data.url, sync_result);
#030
} else {
#031
handler = new AsyncEventHandler(receiver,
#032
render_process_host_id,
#033
render_view_id,
#034
render_process_handle,
#035
request_data.url,
#036
this);
#037
}
#038
#039
if (HandleExternalProtocol(request_id, render_process_host_id, render_view_id,
#040
request_data.url, request_data.resource_type,
#041
handler)) {
#042
return;
#043
}
#044
构造下载请求。
#045
// Construct the request.
#046
URLRequest* request = new URLRequest(request_data.url, this);
#047
request->set_method(request_data.method);
#048
request->set_policy_url(request_data.policy_url);
#049
request->set_referrer(request_data.referrer.spec());
#050
request->SetExtraRequestHeaders(request_data.headers);
#051
request->set_load_flags(request_data.load_flags);
#052
request->set_context(request_context);
#053
request->set_origin_pid(request_data.origin_pid);
#054
设置上传数据。
#055
// Set upload data.
#056
uint64 upload_size = 0;
#057
if (!request_data.upload_content.empty()) {
#058
scoped_refptr<net::UploadData> upload = new net::UploadData();
#059
upload->set_elements(request_data.upload_content);
// Deep copy.
#060
request->set_upload(upload);
#061
upload_size = upload->GetContentLength();
#062
}
#063
安装一个
CrossSiteEventHandler
事件处理器。
#064
// Install a CrossSiteEventHandler if this request is coming from a
#065
// RenderViewHost with a pending cross-site request.
We only check this for
#066
// MAIN_FRAME requests.
#067
// TODO(mpcomplete): remove "render_process_host_id != -1"
#068
//
when http://b/viewIssue?id=1080959 is fixed.
#069
if (request_data.resource_type == ResourceType::MAIN_FRAME &&
#070
render_process_host_id != -1 &&
#071
Singleton<CrossSiteRequestManager>::get()->
#072
HasPendingCrossSiteRequest(render_process_host_id, render_view_id)) {
#073
// Wrap the event handler to be sure the current page's onunload handler
#074
// has a chance to run before we render the new page.
#075
handler = new CrossSiteEventHandler(handler,
#076
render_process_host_id,
#077
render_view_id,
#078
this);
#079
}
#080
#081
if (safe_browsing_->enabled() &&
#082
safe_browsing_->CanCheckUrl(request_data.url)) {
#083
handler = new SafeBrowsingEventHandler(handler,
#084
render_process_host_id,
#085
render_view_id,
#086
request_data.url,
#087
request_data.resource_type,
#088
safe_browsing_,
#089
this);
#090
}
#091
创建一个缓冲区处理。
#092
// Insert a buffered event handler before the actual one.
#093
handler = new BufferedEventHandler(handler, this, request);
#094
#095
// Make extra info and read footer (contains request ID).
#096
ExtraRequestInfo* extra_info =
#097
new ExtraRequestInfo(handler,
#098
request_id,
#099
render_process_host_id,
#100
render_view_id,
#101
request_data.mixed_content,
#102
request_data.resource_type,
#103
upload_size);
#104
extra_info->allow_download =
#105
ResourceType::IsFrame(request_data.resource_type);
#106
request->set_user_data(extra_info);
// takes pointer ownership
#107
开始调用内部处理请求函数。
#108
BeginRequestInternal(request, request_data.mixed_content);
#109
}
通过上面的分析,已经知道消息转换为一个请求任务
URLRequest
,这个任务就需要交给后面的工作进程来处理了,它是通过函数
BeginRequestInternal
来把任务进一步发送出去。