From 85f0bb938dce3b65484a5e1f182a4ffc84ae4baf Mon Sep 17 00:00:00 2001 From: "gavin%gavinsharp.com" Date: Thu, 27 Jul 2006 21:54:53 +0000 Subject: [PATCH] Bug 344059: Don't write search prefs. on shutdown unless something has changed, patch by Adam Guthrie , r=me --- browser/components/search/nsSearchService.js | 25 ++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/browser/components/search/nsSearchService.js b/browser/components/search/nsSearchService.js index 6a0322e9b808..0b1ec908c429 100755 --- a/browser/components/search/nsSearchService.js +++ b/browser/components/search/nsSearchService.js @@ -2148,6 +2148,9 @@ function SearchService() { SearchService.prototype = { _engines: { }, _sortedEngines: null, + // Whether or not we need to write the order of engines on shutdown. This + // needs to happen anytime _sortedEngines is modified after initial startup. + _needToSetOrderPrefs: false, _init: function() { engineMetadataService.init(); @@ -2217,8 +2220,10 @@ SearchService.prototype = { // has already been built (i.e. if this._sortedEngines is non-null). If // it hasn't, we're still loading engines from disk, and will build the // sorted engine list when that initial loading is done. - if (this._sortedEngines) + if (this._sortedEngines) { this._sortedEngines.push(aEngine); + this._needToSetOrderPrefs = true; + } notifyAction(aEngine, SEARCH_ENGINE_ADDED); } @@ -2306,6 +2311,10 @@ SearchService.prototype = { }, _saveSortedEngineList: function SRCH_SVC_saveSortedEngineList() { + // We only need to write the prefs. if something has changed. + if (!this._needToSetOrderPrefs) + return; + var engines = this._getSortedEngines(true); var values = []; var names = []; @@ -2334,11 +2343,17 @@ SearchService.prototype = { if (orderNumber && !this._sortedEngines[orderNumber-1]) { this._sortedEngines[orderNumber-1] = engine; addedEngines[engine.name] = engine; + } else { + // We need to call _saveSortedEngines so this gets sorted out. + this._needToSetOrderPrefs = true; } } // Filter out any nulls for engines that may have been removed - this._sortedEngines = this._sortedEngines.filter(function(a) { return !!a; }); + var filteredEngines = this._sortedEngines.filter(function(a) { return !!a; }); + if (this._sortedEngines.length != filteredEngines.length) + this._needToSetOrderPrefs = true; + this._sortedEngines = filteredEngines; // Array for the remaining engines, alphabetically sorted var alphaEngines = []; @@ -2611,6 +2626,9 @@ SearchService.prototype = { delete this._engines[engineToRemove.name]; notifyAction(engineToRemove, SEARCH_ENGINE_REMOVED); + + // Since we removed an engine, we need to update the preferences. + this._needToSetOrderPrefs = true; } }, @@ -2631,6 +2649,9 @@ SearchService.prototype = { this._sortedEngines.splice(aNewIndex, 0, movedEngine); notifyAction(engine, SEARCH_ENGINE_CHANGED); + + // Since we moved an engine, we need to update the preferences. + this._needToSetOrderPrefs = true; }, restoreDefaultEngines: function SRCH_SVC_resetDefaultEngines() {