@@ -58,6 +58,7 @@ const SEARCH_ENGINE_ADDED = "engine-added";
5858const SEARCH_ENGINE_CHANGED = "engine-changed" ;
5959const SEARCH_ENGINE_LOADED = "engine-loaded" ;
6060const SEARCH_ENGINE_CURRENT = "engine-current" ;
61+ const SEARCH_ENGINE_DEFAULT = "engine-default" ;
6162
6263// The following constants are left undocumented in nsIBrowserSearchService.idl
6364// For the moment, they are meant for testing/debugging purposes only.
@@ -3463,15 +3464,22 @@ SearchService.prototype = {
34633464 FAIL ( "no engine passed to removeEngine!" ) ;
34643465
34653466 var engineToRemove = null ;
3466- for ( var e in this . _engines )
3467+ for ( var e in this . _engines ) {
34673468 if ( aEngine . wrappedJSObject == this . _engines [ e ] )
34683469 engineToRemove = this . _engines [ e ] ;
3470+ }
34693471
34703472 if ( ! engineToRemove )
34713473 FAIL ( "removeEngine: Can't find engine to remove!" , Cr . NS_ERROR_FILE_NOT_FOUND ) ;
34723474
3473- if ( engineToRemove == this . currentEngine )
3475+ if ( engineToRemove == this . currentEngine ) {
34743476 this . _currentEngine = null ;
3477+ }
3478+
3479+ if ( engineToRemove == this . defaultEngine ) {
3480+ this . _defaultEngine = null ;
3481+ Services . prefs . clearUserPref ( BROWSER_SEARCH_PREF + "defaultenginename" ) ;
3482+ }
34753483
34763484 if ( engineToRemove . _readOnly ) {
34773485 // Just hide it (the "hidden" setter will notify) and remove its alias to
@@ -3564,18 +3572,36 @@ SearchService.prototype = {
35643572 }
35653573 } ,
35663574
3567- get originalDefaultEngine ( ) {
3575+ get defaultEngine ( ) {
35683576 this . _ensureInitialized ( ) ;
3569- const defPref = BROWSER_SEARCH_PREF + "defaultenginename" ;
3570- return this . getEngineByName ( getLocalizedPref ( defPref , "" ) ) ;
3577+ if ( ! this . _defaultEngine || this . _defaultEngine . hidden ) {
3578+ let defPref = BROWSER_SEARCH_PREF + "defaultenginename" ;
3579+ let defaultEngine = this . getEngineByName ( getLocalizedPref ( defPref , "" ) )
3580+ if ( ! defaultEngine || defaultEngine . hidden )
3581+ defaultEngine = this . _getSortedEngines ( false ) [ 0 ] || null ;
3582+ this . _defaultEngine = defaultEngine ;
3583+ }
3584+ return this . _defaultEngine ;
35713585 } ,
35723586
3573- get defaultEngine ( ) {
3587+ set defaultEngine ( val ) {
35743588 this . _ensureInitialized ( ) ;
3575- let defaultEngine = this . originalDefaultEngine ;
3576- if ( ! defaultEngine || defaultEngine . hidden )
3577- defaultEngine = this . _getSortedEngines ( false ) [ 0 ] || null ;
3578- return defaultEngine ;
3589+ if ( ! ( val instanceof Ci . nsISearchEngine ) )
3590+ FAIL ( "Invalid argument passed to defaultEngine setter" ) ;
3591+
3592+ let newDefaultEngine = this . getEngineByName ( val . name ) ;
3593+ if ( ! newDefaultEngine )
3594+ FAIL ( "Can't find engine in store!" , Cr . NS_ERROR_UNEXPECTED ) ;
3595+
3596+ if ( newDefaultEngine == this . _defaultEngine )
3597+ return ;
3598+
3599+ this . _defaultEngine = newDefaultEngine ;
3600+
3601+ let defPref = BROWSER_SEARCH_PREF + "defaultenginename" ;
3602+ setLocalizedPref ( defPref , this . _defaultEngine . name ) ;
3603+
3604+ notifyAction ( this . _defaultEngine , SEARCH_ENGINE_DEFAULT ) ;
35793605 } ,
35803606
35813607 get currentEngine ( ) {
@@ -3590,6 +3616,7 @@ SearchService.prototype = {
35903616 this . _currentEngine = this . defaultEngine ;
35913617 return this . _currentEngine ;
35923618 } ,
3619+
35933620 set currentEngine ( val ) {
35943621 this . _ensureInitialized ( ) ;
35953622 if ( ! ( val instanceof Ci . nsISearchEngine ) )
@@ -3599,6 +3626,9 @@ SearchService.prototype = {
35993626 if ( ! newCurrentEngine )
36003627 FAIL ( "Can't find engine in store!" , Cr . NS_ERROR_UNEXPECTED ) ;
36013628
3629+ if ( newCurrentEngine == this . _currentEngine )
3630+ return ;
3631+
36023632 this . _currentEngine = newCurrentEngine ;
36033633
36043634 var currentEnginePref = BROWSER_SEARCH_PREF + "selectedEngine" ;
0 commit comments