Next let's take a look at the Renderer entry function. The entry function of each chrome process is wwinmain, which we have previously introduced. However, in the chromemain function, different process initialization functions are called based on the process type, for the Renderer process, the correct initialization function is renderermain, which is located in the file SRC \ chrome \ Renderer \ renderer_main.cc.
The Renderer process initialization is similar to the browser process. It also creates an object that represents the process: renderprocessimpl render_process;, an object that represents the main thread: main_thread _,
This object is a member of renderprocessimpl. Use the following statement:
Render_process.set_main_thread (New renderthread ());
Renderprocessimpl inherits from renderprocess, while renderprocess inherits from childprocess. It has two important members: main thread object and IO thread object.
Now let's take a look at main_thread _ initialization. main_thread _ is an renderthread object. In the code above, we use a new renderthread class object, the initialization process is completed in the default constructor of renderthread.
The default constructor of renderthread is as follows:
Renderthread: renderthread (){
Init ();
}
All member variables are initialized in the init (); function. Next, let's take a look at the default constructor of the parent childthread. The Code is as follows:
Childthread: childthread (){
Channel_name _ = CommandLine: forcurrentprocess ()-> getswitchvalueascii (
Switches: kprocesschannelid );
Init ();
}
First, you can obtain the pipeline name for IPC, that is, channel_name _, CommandLine: forcurrentprocess (), to obtain the command line Object of the current process. This is because in wwinmain, you can call CommandLine :: init (0, null) initializes the command line Object and calls current_process_commandline _-> parsefromstring (: getcommandlinew ()); save the commands written during Renderer initialization in the browser process to the current_process_commandline _ object. The code is located in SRC \ base \ command_line.cc.
Next we will call the init () of childthread. The most important thing here is to initialize the message loop object, which calls message_loop _ = messageloop: Current (); because the message loop object has been created and the local thread TLS is set before the renderprocessimpl object is initialized, The messageloop pointer saved can be obtained by calling current () here.
And initialize the IPC object, which is called
Channel _. Reset (new IPC: syncchannel (channel_name _,
IPC: Channel: mode_client, this, null,
Childprocess: Current ()-> io_message_loop (), true,
Childprocess: Current ()-> getshutdownevent ()));
In addition, the resourcedispatcher object and IPC filter object are initialized.
Let's take a look at the default constructor of render_process, which mainly performs initialization for WebKit. I will not analyze it in detail here. The default constructor of the grandparent childprocess class is mainly used to initialize and start Io functions. The code is located in SRC \ chrome \ common \ child_process.cc:
Childprocess: childprocess ()
: Ref_count _ (0 ),
Shutdown_event _ (true, false ),
Io_thread _ ("chrome_childiothread "){
Dcheck (! Child_process _);
Child_process _ = this;
Io_thread _. startwitexceptions (base: thread: Options (messageloop: type_io, 0 ));
}
The main function is to set the thread type to type_io, call startwitexceptions to create a thread, and start a message loop.