Repository: electron/electron
File: shell/browser/electron_browser_context.cc (C++)

Code before:
    path_ = std::move(partition_path);
  }

  BrowserContextDependencyManager::GetInstance()->MarkBrowserContextLive(this);

  // Initialize Pref Registry.
  InitPrefs();

  cookie_change_notifier_ = std::make_unique<CookieChangeNotifier>(this);

#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  if (!in_memory_) {
    BrowserContextDependencyManager::GetInstance()
        ->CreateBrowserContextServices(this);

    auto* extension_system = static_cast<extensions::ElectronExtensionSystem*>(
        extensions::ExtensionSystem::Get(this));
    extension_system->InitForRegularProfile(true /* extensions_enabled */);
    extension_system->FinishInitialization();
  }
#endif

  // Subscribe to Network Service restart notifications to reset the cached
  // URLLoaderFactory.
  if (auto* manager = SystemNetworkContextManager::GetInstance()) {
    network_service_restart_subscription_ =
        manager->AddNetworkServiceRestartCallback(base::BindRepeating(
            &ElectronBrowserContext::OnNetworkServiceRestarted,
            base::Unretained(this)));
  }
}

ElectronBrowserContext::~ElectronBrowserContext() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  NotifyWillBeDestroyed();

  // Notify any keyed services of browser context destruction.
  BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
      this);
  ShutdownStoragePartitions();
}

void ElectronBrowserContext::InitPrefs() {
  auto prefs_path = GetPath().Append(FILE_PATH_LITERAL("Preferences"));
  ScopedAllowBlockingForElectron allow_blocking;
  PrefServiceFactory prefs_factory;
  scoped_refptr<JsonPrefStore> pref_store =
      base::MakeRefCounted<JsonPrefStore>(prefs_path);
  pref_store->ReadPrefs();  // Synchronous.
  prefs_factory.set_user_prefs(pref_store);

Review comment (bug):
Can you rely on `RegisterNetworkServiceProcessGoneHandler` to get notified of network service crash ?

Code after:
    path_ = std::move(partition_path);
  }

  BrowserContextDependencyManager::GetInstance()->MarkBrowserContextLive(this);

  // Initialize Pref Registry.
  InitPrefs();

  cookie_change_notifier_ = std::make_unique<CookieChangeNotifier>(this);

#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  if (!in_memory_) {
    BrowserContextDependencyManager::GetInstance()
        ->CreateBrowserContextServices(this);

    auto* extension_system = static_cast<extensions::ElectronExtensionSystem*>(
        extensions::ExtensionSystem::Get(this));
    extension_system->InitForRegularProfile(true /* extensions_enabled */);
    extension_system->FinishInitialization();
  }
#endif

  // Subscribe to Network Service process gone notifications to reset the
  // cached URLLoaderFactory when the Network Service crashes or restarts.
  network_service_gone_subscription_ =
      content::RegisterNetworkServiceProcessGoneHandler(base::BindRepeating(
          &ElectronBrowserContext::OnNetworkServiceProcessGone,
          weak_factory_.GetWeakPtr()));
}

ElectronBrowserContext::~ElectronBrowserContext() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  NotifyWillBeDestroyed();

  // Notify any keyed services of browser context destruction.
  BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
      this);
  ShutdownStoragePartitions();
}

void ElectronBrowserContext::InitPrefs() {
  auto prefs_path = GetPath().Append(FILE_PATH_LITERAL("Preferences"));
  ScopedAllowBlockingForElectron allow_blocking;
  PrefServiceFactory prefs_factory;
  scoped_refptr<JsonPrefStore> pref_store =
      base::MakeRefCounted<JsonPrefStore>(prefs_path);
  pref_store->ReadPrefs();  // Synchronous.
  prefs_factory.set_user_prefs(pref_store);
  prefs_factory.set_command_line_prefs(in_memory_pref_store());