Cross-process file handle delivery in chromium

Source: Internet
Author: User

Implementation Notes

Under the chromium cross-process architecture, there is also the need for browser/renderer two processes to operate on the same file. For example, a task in browser relies on the output of the renderer end to the specified file. Under Poxis, it is possible to pass file descriptors (files descriptor) between different processes, such as passing sockets, or plain files, to the effect of reading and writing to the same file without having to reopen the file (not to share a file handle). Chromium has encapsulated this feature and also includes implementations under Windows (including implementations under Windows). The basic structure involved is as follows:

Among them dbus::FileDescriptor , defined in Dbus/file_descriptor.h. The directory's FD cannot be passed for security reasons.
base::Fileis the encapsulation of file handles for different platforms, defined in Base/file.h.
Platformfile is a set of functions, defined in base/ipc_platform_file.h, where two important APIs are:

IPC_EXPORT PlatformFileForTransit GetFileHandleForProcess(    base::PlatformFile file,    base::ProcessHandle process,     bool close_source_handle);  

Parameter explanation:
base::PlatformFile file,//The file handle opened by the current process
base::ProcessHandle process,//target process
bool close_source_handle);//Whether the current file handle is closed, and if not, you need to create a copy of the current file handle more to avoid an exception when the IPC is passed. Otherwise, the current file descriptor (handle under File Descriptor,windows) is reused.

IPC_EXPORT PlatformFileForTransit TakeFileHandleForProcess(    base::Filefile,    process);

This version is getfilehandleforprocess the third parameter (Close_source_handle) is true, when the current process does not need to hold the file handle, it looks like the transfer of ownership to the target process.

Using the example
//Mhtmlgenerationmanager (Browser)voidMhtmlgenerationmanager:: streammhtml(webcontents*Web_contents, Base:: FileBrowser_file,//Pass in a file handle Browser_fileConst Generatemhtmlcallback&Callback) {//Convert to a cross-process FD that does not release ownership, so the third argument passes false. Pc::P latformfilefortransitRenderer_file=Ipc:: Getfilehandleforprocess(Browser_file.Getplatformfile (), renderer_process,false);//The fileavailable function then passes the renderer_file. Rvh -Send (NewViewmsg_savepageasmhtml (RVH -Getroutingid (), job_id, Renderer_file));}

Passed through the IPC to the renderer process.

// MHTMLGenerator (Renderer)void MHTMLGenerator::OnSavePageAsMHTML(int job_id, IPC::PlatformFileForTransit file_for_transit) {  // 从消息中的FD,转换到base::File, 可以进行相关的文件操作了。  base::File= IPC::PlatformFileForTransitToFile(file_for_transit);  = file_.Write(total_bytes_written,                                    data+ total_bytes_written, copy_size);  file_.Close();

Note that under multi-process, just share the file descriptor, you can understand the share of the same read and write operations, but not the shared file handle, so the individual processes are still independent of the semi-closed operation (open when the initiator process is completed. )

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Cross-process file handle delivery in chromium

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.