std::unique_ptr CreatePrefService()

in libcef/browser/prefs/browser_prefs.cc [133:341]


std::unique_ptr<PrefService> CreatePrefService(Profile* profile,
                                               const base::FilePath& cache_path,
                                               bool persist_user_preferences) {
  const base::CommandLine* command_line =
      base::CommandLine::ForCurrentProcess();

  // Use of PrefServiceSyncable is required by Chrome code such as
  // HostContentSettingsMapFactory that calls PrefServiceSyncableFromProfile.
  sync_preferences::PrefServiceSyncableFactory factory;

  // Used to store command-line preferences, most of which will be evaluated in
  // the CommandLinePrefStore constructor. Preferences set in this manner cannot
  // be overridden by the user.
  scoped_refptr<ChromeCommandLinePrefStore> command_line_pref_store(
      new ChromeCommandLinePrefStore(command_line));
  renderer_prefs::SetCommandLinePrefDefaults(command_line_pref_store.get());
  factory.set_command_line_prefs(command_line_pref_store);

  // True if preferences will be stored on disk.
  const bool store_on_disk = !cache_path.empty() && persist_user_preferences;

  scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner;
  if (store_on_disk) {
    // Get sequenced task runner for making sure that file operations are
    // executed in expected order (what was previously assured by the FILE
    // thread).
    sequenced_task_runner = base::ThreadPool::CreateSequencedTaskRunner(
        {base::MayBlock(), base::TaskShutdownBehavior::BLOCK_SHUTDOWN});
  }

  // Used to store user preferences.
  scoped_refptr<PersistentPrefStore> user_pref_store;
  if (store_on_disk) {
    const base::FilePath& pref_path = cache_path.AppendASCII(
        profile ? kUserPrefsFileName : kLocalPrefsFileName);
    scoped_refptr<JsonPrefStore> json_pref_store = new JsonPrefStore(
        pref_path, std::unique_ptr<PrefFilter>(), sequenced_task_runner);
    factory.set_user_prefs(json_pref_store.get());
  } else {
    scoped_refptr<CefPrefStore> cef_pref_store = new CefPrefStore();
    cef_pref_store->SetInitializationCompleted();
    factory.set_user_prefs(cef_pref_store.get());
  }

#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
  if (profile) {
    // Used to store supervised user preferences.
    SupervisedUserSettingsService* supervised_user_settings =
        SupervisedUserSettingsServiceFactory::GetForKey(
            profile->GetProfileKey());

    if (store_on_disk) {
      supervised_user_settings->Init(cache_path, sequenced_task_runner.get(),
                                     true);
    } else {
      scoped_refptr<CefPrefStore> cef_pref_store = new CefPrefStore();
      cef_pref_store->SetInitializationCompleted();
      supervised_user_settings->Init(cef_pref_store);
    }

    scoped_refptr<PrefStore> supervised_user_prefs =
        base::MakeRefCounted<SupervisedUserPrefStore>(supervised_user_settings);
    DCHECK(supervised_user_prefs->IsInitializationComplete());
    factory.set_supervised_user_prefs(supervised_user_prefs);
  }
#endif  // BUILDFLAG(ENABLE_SUPERVISED_USERS)

  // Registry that will be populated with all known preferences. Preferences
  // are registered with default values that may be changed via a *PrefStore.
  scoped_refptr<user_prefs::PrefRegistrySyncable> registry(
      new user_prefs::PrefRegistrySyncable());

  // Some preferences are specific to CEF and others are defined in Chromium.
  // The preferred approach for registering preferences defined in Chromium is
  // as follows:
  //
  // 1. If a non-static RegisterProfilePrefs() method exists in a *Factory
  //    class then add a *Factory::GetInstance() call in
  //    EnsureBrowserContextKeyedServiceFactoriesBuilt().
  // 2. If a static RegisterPrefs() method exists then call that method in the
  //    "Default preferences" section below.
  // 3. If the default values are not appropriate but the set of registered
  //    preferences is otherwise fine then change the defaults by calling
  //    SetDefaultPrefValue after calling the existing registration method.
  // 4. If the original registration method contains many unused preferences or
  //    otherwise inappropiate logic (e.g. calls to objects that CEF doesn't
  //    use) then register the preferences directly instead of calling the
  //    existing registration method.

  // Default preferences.
  CefMediaCaptureDevicesDispatcher::RegisterPrefs(registry.get());
  certificate_transparency::prefs::RegisterPrefs(registry.get());
  flags_ui::PrefServiceFlagsStorage::RegisterPrefs(registry.get());
  media_router::RegisterLocalStatePrefs(registry.get());
  PluginInfoHostImpl::RegisterUserPrefs(registry.get());
  PrefProxyConfigTrackerImpl::RegisterPrefs(registry.get());
  ProfileNetworkContextService::RegisterLocalStatePrefs(registry.get());
  SSLConfigServiceManager::RegisterPrefs(registry.get());
  update_client::RegisterPrefs(registry.get());

  if (!profile) {
    component_updater::RegisterPrefsForChromeComponentUpdaterConfigurator(
        registry.get());
    SystemNetworkContextManager::RegisterPrefs(registry.get());
#if defined(OS_WIN)
    OSCrypt::RegisterLocalPrefs(registry.get());
#endif
  }

  // Browser process preferences.
  // Based on chrome/browser/browser_process_impl.cc RegisterPrefs.
  registry->RegisterBooleanPref(prefs::kAllowCrossOriginAuthPrompt, false);

  // Browser UI preferences.
  // Based on chrome/browser/ui/browser_ui_prefs.cc RegisterBrowserPrefs.
  registry->RegisterBooleanPref(prefs::kAllowFileSelectionDialogs, true);

  // Based on chrome/browser/ui/browser_ui_prefs.cc RegisterBrowserUserPrefs.
  registry->RegisterBooleanPref(prefs::kPrintPreviewUseSystemDefaultPrinter,
                                false);
  registry->RegisterBooleanPref(prefs::kWebRTCAllowLegacyTLSProtocols, false);

  // Profile preferences.
  // Based on chrome/browser/profiles/profiles_state.cc RegisterPrefs.
  registry->RegisterStringPref(prefs::kProfileLastUsed, std::string());

  if (profile) {
    // Call RegisterProfilePrefs() for all services listed by
    // EnsureBrowserContextKeyedServiceFactoriesBuilt().
    BrowserContextDependencyManager::GetInstance()
        ->RegisterProfilePrefsForServices(registry.get());

    // Default profile preferences.
    AccessibilityUIMessageHandler::RegisterProfilePrefs(registry.get());
    chrome_browser_net::RegisterPredictionOptionsProfilePrefs(registry.get());
    extensions::ExtensionPrefs::RegisterProfilePrefs(registry.get());
    HostContentSettingsMap::RegisterProfilePrefs(registry.get());
    language::LanguagePrefs::RegisterProfilePrefs(registry.get());
    media_router::RegisterProfilePrefs(registry.get());
    MediaDeviceIDSalt::RegisterProfilePrefs(registry.get());
    ProfileNetworkContextService::RegisterProfilePrefs(registry.get());
    safe_browsing::RegisterProfilePrefs(registry.get());
    RegisterProfilePrefs(registry.get());

    const std::string& locale =
        command_line->GetSwitchValueASCII(switches::kLang);
    DCHECK(!locale.empty());
    renderer_prefs::RegisterProfilePrefs(registry.get(), locale);

    // Print preferences.
    // Based on ProfileImpl::RegisterProfilePrefs.
    registry->RegisterBooleanPref(prefs::kForceGoogleSafeSearch, false);
    registry->RegisterIntegerPref(prefs::kForceYouTubeRestrict,
                                  safe_search_util::YOUTUBE_RESTRICT_OFF);
    registry->RegisterStringPref(prefs::kAllowedDomainsForApps, std::string());
    registry->RegisterBooleanPref(prefs::kPrintingEnabled, true);
    registry->RegisterBooleanPref(prefs::kPrintPreviewDisabled,
                                  !extensions::PrintPreviewEnabled());
    registry->RegisterStringPref(
        prefs::kPrintPreviewDefaultDestinationSelectionRules, std::string());
    registry->RegisterBooleanPref(prefs::kCloudPrintSubmitEnabled, false);
    registry->RegisterBooleanPref(prefs::kEnableMediaRouter, true);
    printing::PolicySettings::RegisterProfilePrefs(registry.get());
    printing::PrintPreviewStickySettings::RegisterProfilePrefs(registry.get());
    DownloadPrefs::RegisterProfilePrefs(registry.get());

    // Cache preferences.
    // Based on ProfileImpl::RegisterProfilePrefs.
    registry->RegisterFilePathPref(prefs::kDiskCacheDir, cache_path);
    registry->RegisterIntegerPref(prefs::kDiskCacheSize, 0);

    // Based on Profile::RegisterProfilePrefs.
    registry->RegisterBooleanPref(prefs::kSearchSuggestEnabled, false);

    // Spell checking preferences.
    // Modify defaults from SpellcheckServiceFactory::RegisterProfilePrefs.
    std::string spellcheck_lang =
        command_line->GetSwitchValueASCII(switches::kOverrideSpellCheckLang);
    if (!spellcheck_lang.empty()) {
      registry->SetDefaultPrefValue(spellcheck::prefs::kSpellCheckDictionary,
                                    base::Value(spellcheck_lang));
    }
    const bool enable_spelling_service_ =
        command_line->HasSwitch(switches::kEnableSpellingService);
    registry->SetDefaultPrefValue(
        spellcheck::prefs::kSpellCheckUseSpellingService,
        base::Value(enable_spelling_service_));
    registry->SetDefaultPrefValue(spellcheck::prefs::kSpellCheckEnable,
                                  base::Value(!enable_spelling_service_));

    // DevTools preferences.
    // Based on DevToolsWindow::RegisterProfilePrefs.
    registry->RegisterDictionaryPref(prefs::kDevToolsPreferences);
    registry->RegisterDictionaryPref(prefs::kDevToolsEditedFiles);

    // Language preferences. Used by ProfileNetworkContextService and
    // InterceptedRequestHandlerWrapper.
    const std::string& accept_language_list = GetAcceptLanguageListSetting(
        CefBrowserContext::FromProfile(profile), /*browser=*/nullptr);
    if (!accept_language_list.empty()) {
      registry->SetDefaultPrefValue(language::prefs::kAcceptLanguages,
                                    base::Value(accept_language_list));
    }
    registry->RegisterListPref(prefs::kWebRtcLocalIpsAllowedUrls);
  }

  // Build the PrefService that manages the PrefRegistry and PrefStores.
  return factory.CreateSyncable(registry.get());
}