In the last article we briefly understood the browser processes and the various process modes, so in this article we will explore the collaboration between processes. It may be a bit too much, I could break it down a bit, but this is not a water article

Attached: historical articles

  • Browser process learning

Collaboration between processes

What happens when you open the browser, from entering the URL to displaying the page

This is actually an old question, it is said that we ctos used to like to ask this question… In that case, let’s rearrange it.

Open a browser

To tell the truth, this place of information really some difficult to find, can only force me to find the source code, simply say it, otherwise I how to install…. If there is a big god, please advise ~, there is too much code, not interested students can directly see the summary.

Chrome startup file

Chrome_exe_main. cc is the startup file of Chrome, one is to load chrome. DLL kernel file, check version update, initialization crash report

const wchar_t* dll_name = L"chrome.dll";
#if defined(GOOGLE_CHROME_BUILD)
  google_update::GoogleUpdateClient client;
  client.Init(L"{8A69D345-D564-463c-AFF1-A69D9E530F96}", dll_name);
  InitCrashReporter(client.GetDLLPath());
Copy the code

Chrome_dll_main. cc in this file is visible as the chrome.dll entry function is ChromeMain

DLLEXPORT int __cdecl ChromeMain(HINSTANCE instance,
                                sandbox::SandboxInterfaceInfo* sandbox_info,
                                TCHAR* command_line, int show_command)
Copy the code

I found some processing in this file

extern int BrowserMain(CommandLine &, int, sandbox::BrokerServices*);
extern int RendererMain(CommandLine &, int, sandbox::TargetServices*);
extern int PluginMain(CommandLine &, int, sandbox::TargetServices*); .if (single_process)
 RenderProcessHost::set_run_renderer_in_process(true); . .if (process_type.empty()) {
 file_state = logging::DELETE_OLD_LOG_FILE;
}

int rv;
if (process_type == switches::kRendererProcess) {
 rv = RendererMain(parsed_command_line, show_command, target_services);
} else if (process_type == switches::kPluginProcess) {
 rv = PluginMain(parsed_command_line, show_command, target_services);
} else if (process_type.empty()) {
 int ole_result = OleInitialize(NULL);
 DCHECK(ole_result == S_OK);
 rv = BrowserMain(parsed_command_line, show_command, broker_services);
 OleUninitialize(a); }else {
 NOTREACHED() < <"Unknown process type";
 rv = - 1;
}
if(! process_type.empty()) {
 ResourceBundle::CleanupSharedInstance(a);Copy the code
conclusion

In summary, ChromeMain does some initialization operations at the beginning, and different types of process startup entries are handled uniformly through ChromeMain. Browser, Render and Plugin processes are the main ones.

What happens from entering the URL to presenting the page

I have a feeling that this problem might be common, so let’s look at what a user does after opening the browser normally. In addition to the TAB rendering process, there are many interactive operations handled by the Browser process, we are more common:

  • UI Thread: Controls buttons and input fields on the browser;
  • Network thread: processes network requests and obtains data from the network.
  • Storage thread: controls file access.



When we enter an address in the address bar, the first thing that comes into play is the UI thread. The addresses we enter are a lot of oddities, but most of them are actually urls, but this is for the UI Thread to determine.

Parsing the URL

It first analyzes what the URI is, performs DNS queries if HTTP is relevant, etc., and then directly calls HTTP related modules. But it could also be a search address.

Browser* StartupBrowserCreatorImpl::OpenTabsInBrowser(Browser* browser,
                                                      bool process_startup,
                                                      const StartupTabs& tabs) {
  DCHECK(! tabs.empty());

  if(! profile_ && browser) profile_ = browser->profile(a);bool first_tab = true;
  ProtocolHandlerRegistry* registry = profile_ ?
      ProtocolHandlerRegistryFactory::GetForBrowserContext(profile_) : NULL;
  for (size_t i = 0; i < tabs.size(a); ++i) {// We skip URLs that we'd have to launch an external protocol handler for.
    // This avoids us getting into an infinite loop asking ourselves to open
    // a URL, should the handler be (incorrectly) configured to be us. Anyone
    // asking us to open such a URL should really ask the handler directly.
    bool handled_by_chrome = ProfileIOData::IsHandledURL(tabs[i].url) ||
        (registry && registry->IsHandledProtocol(tabs[i].url.scheme()));
    if(! process_startup && ! handled_by_chrome)continue;

    int add_types = first_tab ? TabStripModel::ADD_ACTIVE :
                                TabStripModel::ADD_NONE;
    add_types |= TabStripModel::ADD_FORCE_INDEX;
    if (tabs[i].is_pinned)
      add_types |= TabStripModel::ADD_PINNED;

    NavigateParams params(browser, tabs[i].url, ui::PAGE_TRANSITION_AUTO_TOPLEVEL);
    params.disposition = first_tab ? WindowOpenDisposition::NEW_FOREGROUND_TAB
                                   : WindowOpenDisposition::NEW_BACKGROUND_TAB;
    params.tabstrip_add_types = add_types;

#if BUILDFLAG(ENABLE_RLZ)
    if (process_startup && google_util::IsGoogleHomePageUrl(tabs[i].url)) {
      params.extra_headers = rlz::RLZTracker::GetAccessPointHttpHeader(
          rlz::RLZTracker::ChromeHomePage());
    }
#endif  // BUILDFLAG(ENABLE_RLZ)

    Navigate(&params);

    first_tab = false;
  }
  if(! browser->tab_strip_model() - >GetActiveWebContents()) {
    // TODO(sky): this is a work around for 110909. Figure out why it's needed.
    if(! browser->tab_strip_model() - >count())
      chrome::AddTabAt(browser, GURL(), - 1.true);
    else
      browser->tab_strip_model() - >ActivateTabAt(0);
  }

  browser->window() - >Show(a);return browser;
}
Copy the code

Based on the above understanding, the actual flow is as follows:

In the next article, we will talk in detail about the process of the browser from sending a request to receiving a request and processing it