Multi-process Resource Loading

 

Multi-process Resource Loading

Background

All network communication is handled by the main browser process. This is done not only so that the browser process can control each renderer's access to the network, but also so that we can maintain consistent session state across processes like cookies and cached data. It is also important because as a HTTP/1.1 user-agent, the browser as a whole should not open too many connections per host.

On Windows, we currently use WinHTTP [note: this is out of date; we have switched to our own custom network stack]. In the future, we plan to replace this with a custom HTTP implementation.  Compared to WinInet (which we have a legacy implementation for):

  1. Both come with all Windows versions we plan to support (Win2K and later).
  2. WinHTTP is better documented. WinInet has a more difficult API and spotty documentation.
  3. WinHTTP has a cleaner API with the support we need. A number of WinInet functions that are necessary for us (for example, for SSL state) are undocumented and unsupported, even though IE uses them.
  4. WinHTTP allows us to implement our own cache. WinInet always shares a cache with IE. We would like to control our own caching, and also sharing a cache with IE can cause problems since some sites may serve browser-specific pages.
  5. WinInet is better battle tested on account of being used by IE. WinHTTP is not used very much, and was designed for server use. It has problems with non-ASCII URL encodings sent by the server and keepalive connections. We have to provide workarounds for these issues.

Overview

Our multi-process application can be viewed in three layers. At the lowest layer is the WebKit library which renders pages. Above that are the renderer process (simplistically, one-per-tab), each of which contains one WebKit instance. Managing all the renderers is the browser process, which controls all network accesses.



WebKit

WebKit has a ResourceLoader object which is responsible for fetching data. Each loader has a ResourceHandle for performing the actual requests. The header file for this object is inside the WebKit code, and we are unwilling to fork it. Fortunately, it contains a d member which we can define. We remove the old implementation of ResourceHandleInternal and provide our own, located in webkit/glue/resource_handle_win.cc. Despite the name, it has nothing to do with Win32.

ResourceHandleInternal implements the virtual interface ResourceLoaderBridge::Peer, defined in webkit/glue/resource_loader_bridge.h. This is the callback interface used by the renderer to dispatch data and other events to WebKit.

ResourceHandleInternal inside WebKit talks to the renderer (to initiate or cancel requests) via the ResourceLoaderBridge virtual interface. An implementation of this interface is retrieved by calling the static function ResourceLoaderBridge::Create, which the renderer implements. The test shell uses a different resource loader, so provides a different implementation, non-IPC version of ResourceLoaderBridge, located in webkit/tools/test_shell/simple_resource_loader_bridge.

Renderer

The renderer's implementation of ResourceLoaderBridge, called IPCResourceLoaderBridge, is located in renderer/resource_dispatcher. It uses the global ResourceDispatcher singleton object (one for each renderer process) to create a unique request ID and forward the request to the browser via IPC. Responses from the browser will reference this request ID, which can then be converted back to the ResourceLoaderBridge::Peer object (inside WebKit) by the resource dispatcher.

The conversion between resource requests and their data is all handled by common/resource_loader_ipc. The renderer and the browser both share this code so that the conversions are in sync.

Browser

The RenderProcessHost objects inside the browser receive the IPC requests from each renderer. It forwards these requests to the global ResourceDispatcherHost, using a pointer to the render process host (specifically, an implementation of ResourceDispatcherHost::Receiver) and the request ID generated by the renderer to uniquely identify the request.

Each request is then converted into a URLRequest object, which in turn forwards it to it's internal URLRequestJob that implements the specific protocol desired. When the URLRequest generates notifications, its ResourceDispatcherHost::Receiver and request ID are used to send the notification to the correct RenderProcessHost for sending back to the renderer. Since the ID generated by the renderer is preserved, it is able to correlate all responses with a specific request first generated by WebKit.

Cookies

All cookies are handled by our CookieMonster object in /net/base. We do not share cookies with WinInet. The cookie monster lives in the browser process which handles all network requests because cookies need to be the same across all tabs.

Pages can request cookies for a document via document.cookie. When this occurs, we send a synchronous message from the renderer to the browser requesting the cookie. While the browser is processing the cookie, the thread that WebKit works on is suspended. When the renderer's I/O thread receives the response from the browser, it un-suspends the thread and passes the result back to the JavaScript engine.

附件 (2)

  • Resource loading 2.ai - 创建时间:2008-7-30 下午7:50,创建者:Brett Wilson (版本 1)
    608k 查看下载
  • resource_loader.png - 创建时间:2008-7-25 下午2:17,创建者:Mike Jeffrey (版本 1)
    46k 查看下载
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值