chromium : How KeyedService was created

DependencyManager::CreateContextServices

void DependencyManager::CreateContextServices(base::SupportsUserData* context,
                                              bool is_testing_context) {
  MarkContextLive(context);

  std::vector<DependencyNode*> construction_order;
  if (!dependency_graph_.GetConstructionOrder(&construction_order)) {
    NOTREACHED();
  }

#ifndef NDEBUG
  DumpContextDependencies(context);
#endif

  for (auto* dependency_node : construction_order) {
    KeyedServiceBaseFactory* factory =
        static_cast<KeyedServiceBaseFactory*>(dependency_node);
    if (is_testing_context && factory->ServiceIsNULLWhileTesting() &&
        !factory->HasTestingFactory(context)) {
      factory->SetEmptyTestingFactory(context);
    } else if (factory->ServiceIsCreatedWithContext()) {
      factory->CreateServiceNow(context);
    }
  }
}
#14 0x7f08ec0395de DependencyManager::CreateContextServices()
#15 0x7f08eb2b2410 BrowserContextDependencyManager::DoCreateBrowserContextServices()
#16 0x7f08eb2b22df BrowserContextDependencyManager::CreateBrowserContextServices()
#17 0x55aa9c98a995 ProfileImpl::OnLocaleReady()
#18 0x55aa9c986389 ProfileImpl::OnPrefsLoaded()
#19 0x55aa9c98605d ProfileImpl::ProfileImpl()
#20 0x55aa9c982084 Profile::CreateProfile()
#21 0x55aa9c9f7887 ProfileManager::CreateProfileHelper()
#22 0x55aa9c9e96ba ProfileManager::CreateAndInitializeProfile()
#23 0x55aa9c9e9237 ProfileManager::GetProfile()
#24 0x55aaa015d608 GetStartupProfile()
#25 0x55aa9c56d68f (anonymous namespace)::CreatePrimaryProfile()
#26 0x55aa9c56b376 ChromeBrowserMainParts::PreMainMessageLoopRunImpl()
#27 0x55aa9c56a5dc ChromeBrowserMainParts::PreMainMessageLoopRun()
#28 0x7f08f12591e4 content::BrowserMainLoop::PreMainMessageLoopRun()
#29 0x7f08f0140cfd _ZN4base8internal13FunctorTraitsIMN7content14NetworkContext11DiskCheckerEFvvEvE6InvokeIPS4_JEEEvS6_OT_DpOT0_
#30 0x7f08f0140c74 _ZN4base8internal12InvokeHelperILb0EvE8MakeItSoIMN7content14NetworkContextEFvvEJPS5_EEEvOT_DpOT0_
#31 0x7f08f0140c25 _ZN4base8internal7InvokerINS0_9BindStateIMN7content14NetworkContext11DiskCheckerEFvvEJNS0_17UnretainedWrapperIS5_EEEEEFvvEE7RunImplIRKS7_RKNSt3__15tupleIJS9_EEEJLm0EEEEvOT_OT0_NSG_16integer_sequenceImJXspT1_EEEE
#32 0x7f08f01469dc _ZN4base8internal7InvokerINS0_9BindStateIMN7content14NetworkContext11DiskCheckerEFvvEJNS0_17UnretainedWrapperIS5_EEEEEFvvEE3RunEPNS0_13BindStateBaseE
#33 0x7f08f010150d _ZNKR4base17RepeatingCallbackIFvvEE3RunEv
#34 0x7f08f1ebeedd content::StartupTaskRunner::RunAllTasksNow()
#35 0x7f08f1254a67 content::BrowserMainLoop::CreateStartupTasks()
#36 0x7f08f126360d content::BrowserMainRunnerImpl::Initialize()
#37 0x7f08f124d644 content::BrowserMain()
#38 0x7f08f2e1b9b8 content::RunNamedProcessTypeMain()
#39 0x7f08f2e1e5aa content::ContentMainRunnerImpl::Run()
#40 0x7f08f2e14a6d content::ContentServiceManagerMainDelegate::RunEmbedderProcess()
#41 0x7f08f775689d service_manager::Main()
#42 0x7f08f2e1a7ff content::ContentMain()
#43 0x55aa9ad4d23e ChromeMain
#44 0x55aa9ad4d152 main
#45 0x7f08de135f45 __libc_start_main
#46 0x55aa9ad4d02a _start

BuildServiceInstanceFor AppShortcutManager

#9 0x55aa9b11a513 AppShortcutManager::AppShortcutManager()
#10 0x55aa9b11be82 AppShortcutManagerFactory::BuildServiceInstanceFor()
#11 0x7f08eb2b605a BrowserContextKeyedServiceFactory::BuildServiceInstanceFor()
#12 0x7f08ec03d727 KeyedServiceFactory::GetServiceForContext()
#13 0x7f08ec03ea52 KeyedServiceFactory::CreateServiceNow()

AppShortcutManagerFactory::GetInstance

#2 0x7f5a9625a2d6 DependencyGraph::AddNode()
#3 0x7f5a96269e3c DependencyManager::AddComponent()
#4 0x7f5a9626cdab KeyedServiceBaseFactory::KeyedServiceBaseFactory()
#5 0x7f5a9626d88a KeyedServiceFactory::KeyedServiceFactory()
#6 0x7f5a954e6d7a BrowserContextKeyedServiceFactory::BrowserContextKeyedServiceFactory()
#7 0x5635734e7de3 AppShortcutManagerFactory::AppShortcutManagerFactory()
#8 0x5635734e7fc0 base::DefaultSingletonTraits<>::New()
#9 0x5635734e7f36 base::Singleton<>::get()
#10 0x5635734e7da9 AppShortcutManagerFactory::GetInstance()
#11 0x5635734e3f92 chrome_apps::EnsureBrowserContextKeyedServiceFactoriesBuilt()
#12 0x563574ccc9f8 ChromeBrowserMainExtraPartsProfiles::EnsureBrowserContextKeyedServiceFactoriesBuilt()
#13 0x563574cccdf1 ChromeBrowserMainExtraPartsProfiles::PreProfileInit()
#14 0x5635749387b5 ChromeBrowserMainParts::PreProfileInit()
#15 0x56357492ef9c ChromeBrowserMainPartsLinux::PreProfileInit()
#16 0x5635749371e0 ChromeBrowserMainParts::PreMainMessageLoopRunImpl()
#17 0x5635749365dc ChromeBrowserMainParts::PreMainMessageLoopRun()
#18 0x7f5a9b48a1e4 content::BrowserMainLoop::PreMainMessageLoopRun()

Summary

each KeyedService, such as AppShortcutManager, has a KeyedServiceFactory which are derived from KeyedServiceBaseFactory.
each KeyedServiceFactory must implement BuildServiceInstanceFor interface.
When new instance for each KeyedServiceFactory, it will be added to DependencyManager as a component.
Chromium will call DependencyManager::CreateContextServices at the initial state.
Then traverse all node in DependencyGraph and call each BuildServiceInstanceFor to create the real KeyedService.

Subscribe to Post, Code and Quiet Time.

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe