fix: settings not persisting across devtools loads (#33120)

* fix: settings not persisting across devtools loads

* chore: remove redundant RegisterPreference impl
This commit is contained in:
Shelley Vohr 2022-03-08 17:17:43 -08:00 committed by GitHub
parent 27527fe5ca
commit 373a905319
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 119 deletions

View file

@ -86,13 +86,6 @@ const char kChromeUIDevToolsRemoteFrontendPath[] = "serve_file";
const char kDevToolsBoundsPref[] = "electron.devtools.bounds"; const char kDevToolsBoundsPref[] = "electron.devtools.bounds";
const char kDevToolsZoomPref[] = "electron.devtools.zoom"; const char kDevToolsZoomPref[] = "electron.devtools.zoom";
const char kDevToolsPreferences[] = "electron.devtools.preferences"; const char kDevToolsPreferences[] = "electron.devtools.preferences";
const char kDevToolsSyncPreferences[] = "electron.devtools.sync_preferences";
const char kDevToolsSyncedPreferencesSyncEnabled[] =
"electron.devtools.synced_preferences_sync_enabled";
const char kDevToolsSyncedPreferencesSyncDisabled[] =
"electron.devtools.synced_preferences_sync_disabled";
const char kSyncDevToolsPreferencesFrontendName[] = "electron.sync_preferences";
const bool kSyncDevToolsPreferencesDefault = false;
const char kFrontendHostId[] = "id"; const char kFrontendHostId[] = "id";
const char kFrontendHostMethod[] = "method"; const char kFrontendHostMethod[] = "method";
@ -346,10 +339,6 @@ void InspectableWebContents::RegisterPrefs(PrefRegistrySimple* registry) {
RectToDictionary(gfx::Rect(0, 0, 800, 600))); RectToDictionary(gfx::Rect(0, 0, 800, 600)));
registry->RegisterDoublePref(kDevToolsZoomPref, 0.); registry->RegisterDoublePref(kDevToolsZoomPref, 0.);
registry->RegisterDictionaryPref(kDevToolsPreferences); registry->RegisterDictionaryPref(kDevToolsPreferences);
registry->RegisterDictionaryPref(kDevToolsSyncedPreferencesSyncEnabled);
registry->RegisterDictionaryPref(kDevToolsSyncedPreferencesSyncDisabled);
registry->RegisterBooleanPref(kDevToolsSyncPreferences,
kSyncDevToolsPreferencesDefault);
} }
InspectableWebContents::InspectableWebContents( InspectableWebContents::InspectableWebContents(
@ -867,85 +856,19 @@ void InspectableWebContents::SendJsonRequest(DispatchCallback callback,
std::move(callback).Run(nullptr); std::move(callback).Run(nullptr);
} }
void InspectableWebContents::RegisterPreference(
const std::string& name,
const RegisterOptions& options) {
// kSyncDevToolsPreferenceFrontendName is not stored in any of the relevant
// dictionaries. Skip registration.
if (name == kSyncDevToolsPreferencesFrontendName)
return;
if (options.sync_mode == RegisterOptions::SyncMode::kSync) {
synced_setting_names_.insert(name);
}
// Setting might have had a different sync status in the past. Move the
// setting to the correct dictionary.
const char* dictionary_to_remove_from =
options.sync_mode == RegisterOptions::SyncMode::kSync
? kDevToolsPreferences
: GetDictionaryNameForSyncedPrefs();
const std::string* settings_value =
pref_service_->GetDictionary(dictionary_to_remove_from)
->FindStringKey(name);
if (!settings_value) {
return;
}
const char* dictionary_to_insert_into =
GetDictionaryNameForSettingsName(name);
// Settings already moved to the synced dictionary on a different device have
// precedence.
const std::string* already_synced_value =
pref_service_->GetDictionary(dictionary_to_insert_into)
->FindStringKey(name);
if (dictionary_to_insert_into == kDevToolsPreferences ||
!already_synced_value) {
DictionaryPrefUpdate insert_update(pref_service_,
dictionary_to_insert_into);
insert_update.Get()->SetKey(name, base::Value(*settings_value));
}
DictionaryPrefUpdate remove_update(pref_service_, dictionary_to_remove_from);
remove_update.Get()->RemoveKey(name);
}
void InspectableWebContents::GetPreferences(DispatchCallback callback) { void InspectableWebContents::GetPreferences(DispatchCallback callback) {
base::Value settings(base::Value::Type::DICTIONARY); const base::Value* prefs = pref_service_->GetDictionary(kDevToolsPreferences);
settings.SetBoolKey(kSyncDevToolsPreferencesFrontendName, std::move(callback).Run(prefs);
pref_service_->GetBoolean(kDevToolsSyncPreferences));
settings.MergeDictionary(pref_service_->GetDictionary(kDevToolsPreferences));
settings.MergeDictionary(
pref_service_->GetDictionary(GetDictionaryNameForSyncedPrefs()));
std::move(callback).Run(&settings);
} }
void InspectableWebContents::GetPreference(DispatchCallback callback, void InspectableWebContents::GetPreference(DispatchCallback callback,
const std::string& name) { const std::string& name) {
// Handle kSyncDevToolsPreferencesFrontendName
if (name == kSyncDevToolsPreferencesFrontendName) {
base::Value pref =
base::Value(pref_service_->GetBoolean(kDevToolsSyncPreferences));
std::move(callback).Run(&pref);
return;
}
// Check dev tools prefs
if (auto* pref = if (auto* pref =
pref_service_->GetDictionary(kDevToolsPreferences)->FindKey(name)) { pref_service_->GetDictionary(kDevToolsPreferences)->FindKey(name)) {
std::move(callback).Run(pref); std::move(callback).Run(pref);
return; return;
} }
// Check synced prefs
if (auto* pref =
pref_service_->GetDictionary(GetDictionaryNameForSyncedPrefs())
->FindKey(name)) {
std::move(callback).Run(pref);
return;
}
// Pref wasn't found, return an empty value // Pref wasn't found, return an empty value
base::Value no_pref; base::Value no_pref;
std::move(callback).Run(&no_pref); std::move(callback).Run(&no_pref);
@ -953,41 +876,21 @@ void InspectableWebContents::GetPreference(DispatchCallback callback,
void InspectableWebContents::SetPreference(const std::string& name, void InspectableWebContents::SetPreference(const std::string& name,
const std::string& value) { const std::string& value) {
if (name == kSyncDevToolsPreferencesFrontendName) { DictionaryPrefUpdate update(pref_service_, kDevToolsPreferences);
pref_service_->SetBoolean(kDevToolsSyncPreferences, value == "true");
return;
}
DictionaryPrefUpdate update(pref_service_,
GetDictionaryNameForSettingsName(name));
update.Get()->SetKey(name, base::Value(value)); update.Get()->SetKey(name, base::Value(value));
} }
void InspectableWebContents::RemovePreference(const std::string& name) { void InspectableWebContents::RemovePreference(const std::string& name) {
if (name == kSyncDevToolsPreferencesFrontendName) { DictionaryPrefUpdate update(pref_service_, kDevToolsPreferences);
pref_service_->SetBoolean(kDevToolsSyncPreferences,
kSyncDevToolsPreferencesDefault);
return;
}
DictionaryPrefUpdate update(pref_service_,
GetDictionaryNameForSettingsName(name));
update.Get()->RemoveKey(name); update.Get()->RemoveKey(name);
} }
void InspectableWebContents::ClearPreferences() { void InspectableWebContents::ClearPreferences() {
pref_service_->SetBoolean(kDevToolsSyncPreferences,
kSyncDevToolsPreferencesDefault);
DictionaryPrefUpdate unsynced_update(pref_service_, kDevToolsPreferences); DictionaryPrefUpdate unsynced_update(pref_service_, kDevToolsPreferences);
unsynced_update.Get()->DictClear(); unsynced_update.Get()->DictClear();
DictionaryPrefUpdate sync_enabled_update(
pref_service_, kDevToolsSyncedPreferencesSyncEnabled);
sync_enabled_update.Get()->DictClear();
DictionaryPrefUpdate sync_disabled_update(
pref_service_, kDevToolsSyncedPreferencesSyncDisabled);
sync_disabled_update.Get()->DictClear();
} }
void InspectableWebContents::GetSyncInformation(DispatchCallback callback) { void InspectableWebContents::GetSyncInformation(DispatchCallback callback) {
// TODO(anyone): do we want devtool syncing in Electron?
base::Value result(base::Value::Type::DICTIONARY); base::Value result(base::Value::Type::DICTIONARY);
result.SetBoolKey("isSyncActive", false); result.SetBoolKey("isSyncActive", false);
std::move(callback).Run(&result); std::move(callback).Run(&result);
@ -1172,18 +1075,4 @@ void InspectableWebContents::SendMessageAck(int request_id,
CallClientFunction("DevToolsAPI.embedderMessageAck", &id_value, arg, nullptr); CallClientFunction("DevToolsAPI.embedderMessageAck", &id_value, arg, nullptr);
} }
const char* InspectableWebContents::GetDictionaryNameForSettingsName(
const std::string& name) const {
return synced_setting_names_.contains(name)
? kDevToolsSyncedPreferencesSyncEnabled
: kDevToolsPreferences;
}
const char* InspectableWebContents::GetDictionaryNameForSyncedPrefs() const {
const bool isDevToolsSyncEnabled =
pref_service_->GetBoolean(kDevToolsSyncPreferences);
return isDevToolsSyncEnabled ? kDevToolsSyncedPreferencesSyncEnabled
: kDevToolsSyncedPreferencesSyncDisabled;
}
} // namespace electron } // namespace electron

View file

@ -140,7 +140,7 @@ class InspectableWebContents
const std::string& browser_id, const std::string& browser_id,
const std::string& url) override; const std::string& url) override;
void RegisterPreference(const std::string& name, void RegisterPreference(const std::string& name,
const RegisterOptions& options) override; const RegisterOptions& options) override {}
void GetPreferences(DispatchCallback callback) override; void GetPreferences(DispatchCallback callback) override;
void GetPreference(DispatchCallback callback, void GetPreference(DispatchCallback callback,
const std::string& name) override; const std::string& name) override;
@ -198,9 +198,6 @@ class InspectableWebContents
void SendMessageAck(int request_id, const base::Value* arg1); void SendMessageAck(int request_id, const base::Value* arg1);
const char* GetDictionaryNameForSettingsName(const std::string& name) const;
const char* GetDictionaryNameForSyncedPrefs() const;
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS) #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
void AddDevToolsExtensionsToClient(); void AddDevToolsExtensionsToClient();
#endif #endif