# BrowserMain Initialization [BrowserMainParts](https://source.chromium.org/chromium/chromium/src/+/main:content/public/browser/browser_main_parts.h;l=119;drc=1d2715064cc686e5eac4e41f34dbc212d47deff1) is a critical class to initialize browser instance on startup. This class defines the methods executed by BrowserMain() call. Let's see the each step and see the example of Chrome Impl [ChromeBrowserMainParts](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/chrome_browser_main.h;l=43;drc=d21b40da81ef43caea8c9452514041c1dc60612d) ## Overview [BrowserMainRunnerImpl::Initialize](https://source.chromium.org/chromium/chromium/src/+/main:content/browser/browser_main_runner_impl.cc;l=71;drc=1d2715064cc686e5eac4e41f34dbc212d47deff1) calls the methods in BrowserMainParts. It creates [`main_loop_`](https://source.chromium.org/chromium/chromium/src/+/main:content/browser/browser_main_runner_impl.cc;l=108;drc=1d2715064cc686e5eac4e41f34dbc212d47deff1) and initialize it. Then it goes through in the below order. - EarlyInitialization - InitializeToolkit - PreCreateMainMessageLoop - CreateMainMessageLoop - PostCreateMainMessageLoop - CreateStartupTasks BrowserMainParts class is defined to make these steps clear and let platform-specific implementations align each steps. This design makes it clearer how the initialization goes. ## EarlyInitialization Pre/PostEarlyInitialization runs at [BrowserMainLoop::EarlyInitialization](https://source.chromium.org/chromium/chromium/src/+/main:content/browser/browser_main_loop.cc;l=534;drc=1d2715064cc686e5eac4e41f34dbc212d47deff1). [PreEarlyInitialization](https://source.chromium.org/chromium/chromium/src/+/main:content/public/browser/browser_main_parts.h;l=128;drc=1d2715064cc686e5eac4e41f34dbc212d47deff1) runs soon after the program start. It must contains things to be done ASAP. For example, [ChromeBrowserMainParts::PreEarlyInitialization](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/chrome_browser_main.cc;l=747;drc=1d2715064cc686e5eac4e41f34dbc212d47deff1) first creates BrowserProcessImpl instance, getting chrome feature lists, creating Metrics service, getting local state. It returns int value as an error code. If the error is non-zero, BrowserMainRunnerImpl immediately calls CreateMessageLoopForEarlyShutdown and skip everything comes next. On the other hand, [PostEarlyInitialization](https://source.chromium.org/chromium/chromium/src/+/main:content/public/browser/browser_main_parts.h;l=129;drc=1d2715064cc686e5eac4e41f34dbc212d47deff1) runs at the end of [BrowserMainLoop::EarlyInitialization](https://source.chromium.org/chromium/chromium/src/+/main:content/browser/browser_main_loop.cc;l=534;drc=1d2715064cc686e5eac4e41f34dbc212d47deff1). Similarly to PreEarlyInitalization, it should be things to be done soon, but can wait or require initialization. In the [Chrome](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/chrome_browser_main.cc;l=798;drc=1d2715064cc686e5eac4e41f34dbc212d47deff1) implementation, it does not do anything special and just delegate it to extra parts. ## InitializeToolkit [BrowserMainLoop::InitializeToolkit](https://source.chromium.org/chromium/chromium/src/+/main:content/browser/browser_main_loop.cc;l=1473;drc=1d2715064cc686e5eac4e41f34dbc212d47deff1) runs next. This is for UI related initialization. This creates aura::Env instance. For non-win platform, that's all. Then [BrowserMainParts::ToolkipInitialized](https://source.chromium.org/chromium/chromium/src/+/main:content/public/browser/browser_main_parts.h;l=130;drc=adac219925ef5d9c9a954d189c2e4b8852a4bbed) is called. It creates [ColorProviderManager](https://source.chromium.org/chromium/chromium/src/+/main:ui/color/color_provider_manager.h;l=32;drc=1d2715064cc686e5eac4e41f34dbc212d47deff1), the class to manages and provides color providers. Here, color means a real color on UI. ## CreateMainMessageLoop [BrowserMainParts::PreCreateMainMessageLoop](https://source.chromium.org/chromium/chromium/src/+/main:content/public/browser/browser_main_parts.h;l=131;drc=adac219925ef5d9c9a954d189c2e4b8852a4bbed) is called next. In general, there is nothing here. Platform-specific codes implement it. For example, Ash creates ChromeSessionManager. Then, [BrowserMainLoop::CreateMainMessageLoop](https://source.chromium.org/chromium/chromium/src/+/main:content/browser/browser_main_loop.cc;l=644;drc=1d2715064cc686e5eac4e41f34dbc212d47deff1) run. This creates `main_thread_` initialized by BrowserThreadImpl. The thread type is BrowserThread::UI and the task runner is SingleThreadTaskRunner. After that, [BrowserMainParts::PostCreateMainMessageLoop](https://source.chromium.org/chromium/chromium/src/+/main:content/public/browser/browser_main_parts.h;l=132;drc=adac219925ef5d9c9a954d189c2e4b8852a4bbed) runs. It sets main thread created by the previous step, and creates TraceEventSystemStatsMonitor. ## CreateStartupTasks [BrowserMainLoop::CreateStartupTasks](https://source.chromium.org/chromium/chromium/src/+/main:content/browser/browser_main_loop.cc;l=848;drc=1d2715064cc686e5eac4e41f34dbc212d47deff1) is the last step in BrowserMainRunnerImpl::Initialize. This creates `startup_task_runner_` and create threads. It will trigger [BrowserMainParts::PreCreateThread](https://source.chromium.org/chromium/chromium/src/+/main:content/public/browser/browser_main_parts.h;l=133;drc=adac219925ef5d9c9a954d189c2e4b8852a4bbed) and [BrowserMainParts::PostCreateThreads](https://source.chromium.org/chromium/chromium/src/+/main:content/public/browser/browser_main_parts.h;l=134;drc=adac219925ef5d9c9a954d189c2e4b8852a4bbed) before and after the creation. This CreateThread step is a single-threaded initialization, so immutable Singletons that are initialized once nd read-only from all threads must be created at this point.