2008-09-26 00:34:54 -07:00
// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
2008-05-02 13:15:45 -07:00
/ * * * * * * B E G I N L I C E N S E B L O C K * * * * *
* Version : MPL 1.1 / GPL 2.0 / LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 ( the "License" ) ; you may not use this file except in compliance with
* the License . You may obtain a copy of the License at
* http : //www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis ,
* WITHOUT WARRANTY OF ANY KIND , either express or implied . See the License
* for the specific language governing rights and limitations under the
* License .
*
* The Original Code is Mozilla Mobile Browser .
*
* The Initial Developer of the Original Code is
* Mozilla Corporation .
* Portions created by the Initial Developer are Copyright ( C ) 2008
* the Initial Developer . All Rights Reserved .
*
* Contributor ( s ) :
* Mark Finkle < mfinkle @ mozilla . com >
2010-10-16 06:25:15 -07:00
* Matt Brubeck < mbrubeck @ mozilla . com >
2008-05-02 13:15:45 -07:00
*
* Alternatively , the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later ( the "GPL" ) , or
* the GNU Lesser General Public License Version 2.1 or later ( the "LGPL" ) ,
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above . If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL , and not to allow others to
* use your version of this file under the terms of the MPL , indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL . If you do not delete
* the provisions above , a recipient may use your version of this file under
* the terms of any one of the MPL , the GPL or the LGPL .
*
* * * * * * END LICENSE BLOCK * * * * * * /
2010-03-22 07:05:27 -07:00
Cu . import ( "resource://gre/modules/XPCOMUtils.jsm" ) ;
2010-07-13 07:36:09 -07:00
Cu . import ( "resource://gre/modules/Services.jsm" ) ;
2011-06-29 14:50:43 -07:00
Cu . import ( "resource://gre/modules/AddonManager.jsm" ) ;
2010-03-22 07:05:27 -07:00
2008-09-29 06:53:49 -07:00
[
2010-09-03 02:01:25 -07:00
[ "AllPagesList" , "popup_autocomplete" , "cmd_openLocation" ] ,
[ "HistoryList" , "history-items" , "cmd_history" ] ,
[ "BookmarkList" , "bookmarks-items" , "cmd_bookmarks" ] ,
# ifdef MOZ _SERVICES _SYNC
[ "RemoteTabsList" , "remotetabs-items" , "cmd_remoteTabs" ]
# endif
] . forEach ( function ( aPanel ) {
let [ name , id , command ] = aPanel ;
XPCOMUtils . defineLazyGetter ( window , name , function ( ) {
return new AwesomePanel ( id , command ) ;
2008-09-29 06:53:49 -07:00
} ) ;
} ) ;
2010-09-03 02:01:25 -07:00
/ * *
* Cache of commonly used elements .
* /
let Elements = { } ;
[
[ "contentShowing" , "bcast_contentShowing" ] ,
[ "urlbarState" , "bcast_urlbarState" ] ,
[ "stack" , "stack" ] ,
2011-09-14 06:39:25 -07:00
[ "tabList" , "tabs" ] ,
2010-09-03 02:01:25 -07:00
[ "tabs" , "tabs-container" ] ,
[ "controls" , "browser-controls" ] ,
[ "panelUI" , "panel-container" ] ,
[ "toolbarContainer" , "toolbar-container" ] ,
2010-11-18 06:47:16 -08:00
[ "browsers" , "browsers" ] ,
[ "contentViewport" , "content-viewport" ] ,
[ "contentNavigator" , "content-navigator" ]
2010-09-03 02:01:25 -07:00
] . forEach ( function ( aElementGlobal ) {
let [ name , id ] = aElementGlobal ;
XPCOMUtils . defineLazyGetter ( Elements , name , function ( ) {
return document . getElementById ( id ) ;
} ) ;
} ) ;
2011-01-03 12:07:41 -08:00
/ * *
* Cache of commonly used string bundles .
* /
var Strings = { } ;
[
[ "browser" , "chrome://browser/locale/browser.properties" ] ,
[ "brand" , "chrome://branding/locale/brand.properties" ]
] . forEach ( function ( aStringBundle ) {
let [ name , bundle ] = aStringBundle ;
XPCOMUtils . defineLazyGetter ( Strings , name , function ( ) {
return Services . strings . createBundle ( bundle ) ;
} ) ;
} ) ;
2010-09-03 02:01:25 -07:00
const TOOLBARSTATE _LOADING = 1 ;
const TOOLBARSTATE _LOADED = 2 ;
2008-07-03 23:39:25 -07:00
var BrowserUI = {
2010-11-02 12:20:11 -07:00
_edit : null ,
_title : null ,
_throbber : null ,
_favicon : null ,
2009-07-08 07:55:07 -07:00
_dialogs : [ ] ,
2008-05-02 13:15:45 -07:00
2010-06-08 08:22:20 -07:00
_domWillOpenModalDialog : function ( aBrowser ) {
2009-09-30 12:14:06 -07:00
// We're about to open a modal dialog, make sure the opening
// tab is brought to the front.
2010-06-08 08:22:20 -07:00
for ( let i = 0 ; i < Browser . tabs . length ; i ++ ) {
if ( Browser . _tabs [ i ] . browser == aBrowser ) {
Browser . selectedTab = Browser . tabs [ i ] ;
2009-10-14 13:44:09 -07:00
break ;
2009-09-30 12:14:06 -07:00
}
}
} ,
2010-07-18 22:27:52 -07:00
_titleChanged : function ( aBrowser ) {
2011-09-14 14:03:08 -07:00
let url = this . getDisplayURI ( aBrowser ) ;
2011-09-28 09:22:41 -07:00
let contentTitle = aBrowser . contentTitle ;
let caption = contentTitle || url ;
let tabCaption = contentTitle || ( Util . isURLEmpty ( url ) ? "" : url ) ;
2008-05-02 13:15:45 -07:00
2011-09-28 09:22:41 -07:00
if ( contentTitle == "" && ! Util . isURLEmpty ( aBrowser . userTypedValue ) )
caption = tabCaption = aBrowser . userTypedValue ;
2010-12-09 08:06:21 -08:00
else if ( Util . isURLEmpty ( url ) )
2010-04-08 22:08:53 -07:00
caption = "" ;
2009-09-18 21:17:52 -07:00
2011-09-14 14:03:08 -07:00
let tab = Browser . getTabForBrowser ( aBrowser ) ;
if ( tab )
2011-09-28 09:22:41 -07:00
tab . chromeTab . updateTitle ( tabCaption ) ;
2011-09-14 14:03:08 -07:00
let browser = Browser . selectedBrowser ;
if ( browser && aBrowser != browser )
return ;
2010-11-02 12:20:11 -07:00
if ( caption ) {
this . _title . value = caption ;
this . _title . classList . remove ( "placeholder" ) ;
} else {
this . _title . value = this . _title . getAttribute ( "placeholder" ) ;
this . _title . classList . add ( "placeholder" ) ;
}
2008-05-02 13:15:45 -07:00
} ,
2009-07-16 08:45:50 -07:00
/ *
* Dispatched by window . close ( ) to allow us to turn window closes into tabs
* closes .
* /
2010-06-08 08:22:20 -07:00
_domWindowClose : function ( aBrowser ) {
// Find the relevant tab, and close it.
let browsers = Browser . browsers ;
for ( let i = 0 ; i < browsers . length ; i ++ ) {
if ( browsers [ i ] == aBrowser ) {
2009-07-16 08:45:50 -07:00
Browser . closeTab ( Browser . getTabAtIndex ( i ) ) ;
2010-06-08 08:22:20 -07:00
return { preventDefault : true } ;
2009-07-16 08:45:50 -07:00
}
}
} ,
2010-07-18 22:27:52 -07:00
_updateButtons : function ( aBrowser ) {
2009-11-22 20:08:18 -08:00
let back = document . getElementById ( "cmd_back" ) ;
let forward = document . getElementById ( "cmd_forward" ) ;
2009-06-24 15:43:26 -07:00
back . setAttribute ( "disabled" , ! aBrowser . canGoBack ) ;
forward . setAttribute ( "disabled" , ! aBrowser . canGoForward ) ;
} ,
2010-04-29 22:28:40 -07:00
_updateToolbar : function _updateToolbar ( ) {
2010-08-26 15:48:27 -07:00
let mode = Elements . urlbarState . getAttribute ( "mode" ) ;
2011-09-01 10:04:29 -07:00
if ( mode == "edit" && AwesomeScreen . activePanel )
2010-11-17 02:18:21 -08:00
return ;
if ( Browser . selectedTab . isLoading ( ) && mode != "loading" )
2010-08-26 15:48:27 -07:00
Elements . urlbarState . setAttribute ( "mode" , "loading" ) ;
2010-11-17 02:18:21 -08:00
else if ( mode != "view" )
2010-08-26 15:48:27 -07:00
Elements . urlbarState . setAttribute ( "mode" , "view" ) ;
2009-11-22 20:08:18 -08:00
} ,
2010-07-18 22:27:52 -07:00
_tabSelect : function ( aEvent ) {
2009-11-22 20:08:18 -08:00
let browser = Browser . selectedBrowser ;
2010-06-08 08:22:20 -07:00
this . _titleChanged ( browser ) ;
2010-04-29 22:28:40 -07:00
this . _updateToolbar ( ) ;
2009-06-24 15:43:26 -07:00
this . _updateButtons ( browser ) ;
2009-10-09 05:34:02 -07:00
this . _updateIcon ( browser . mIconURL ) ;
2009-07-05 09:22:09 -07:00
this . updateStar ( ) ;
2008-05-02 13:15:45 -07:00
} ,
2009-08-24 07:18:26 -07:00
_toolbarLocked : 0 ,
2009-11-23 23:46:01 -08:00
isToolbarLocked : function isToolbarLocked ( ) {
return this . _toolbarLocked ;
} ,
2009-08-24 07:18:26 -07:00
lockToolbar : function lockToolbar ( ) {
2011-09-07 13:47:35 -07:00
if ( Util . isTablet ( ) )
2011-09-07 13:47:25 -07:00
return ;
2009-08-24 07:18:26 -07:00
this . _toolbarLocked ++ ;
document . getElementById ( "toolbar-moveable-container" ) . top = "0" ;
2009-11-02 21:13:42 -08:00
if ( this . _toolbarLocked == 1 )
Browser . forceChromeReflow ( ) ;
2009-08-24 07:18:26 -07:00
} ,
2010-01-15 21:41:48 -08:00
2009-08-24 07:18:26 -07:00
unlockToolbar : function unlockToolbar ( ) {
if ( ! this . _toolbarLocked )
return ;
2010-01-15 21:41:48 -08:00
2009-08-24 07:18:26 -07:00
this . _toolbarLocked -- ;
2009-08-26 10:18:13 -07:00
if ( ! this . _toolbarLocked )
2009-08-24 07:18:26 -07:00
document . getElementById ( "toolbar-moveable-container" ) . top = "" ;
} ,
2010-11-02 12:20:11 -07:00
_setURL : function _setURL ( aURL ) {
2011-09-01 10:04:29 -07:00
if ( AwesomeScreen . activePanel )
2010-11-02 12:20:11 -07:00
this . _edit . defaultValue = aURL ;
2009-09-18 21:17:52 -07:00
else
2010-11-02 12:20:11 -07:00
this . _edit . value = aURL ;
2009-09-18 21:17:52 -07:00
} ,
2010-07-18 22:27:52 -07:00
_editURI : function _editURI ( aEdit ) {
2010-10-14 06:15:29 -07:00
Elements . urlbarState . setAttribute ( "mode" , "edit" ) ;
this . _edit . defaultValue = this . _edit . value ;
} ,
_showURI : function _showURI ( ) {
// Replace the web page title by the url of the page
let urlString = this . getDisplayURI ( Browser . selectedBrowser ) ;
if ( Util . isURLEmpty ( urlString ) )
urlString = "" ;
this . _edit . value = urlString ;
2008-09-11 18:11:46 -07:00
} ,
2010-10-10 14:23:28 -07:00
updateAwesomeHeader : function updateAwesomeHeader ( aString ) {
2010-10-14 06:15:29 -07:00
document . getElementById ( "awesome-header" ) . hidden = ( aString != "" ) ;
2010-10-10 14:23:28 -07:00
// During an awesome search we always show the popup_autocomplete/AllPagesList
// panel since this looks in every places and the rationale behind typing
// is to find something, whereever it is.
2011-09-01 10:04:29 -07:00
if ( AwesomeScreen . activePanel != AllPagesList ) {
2010-10-10 14:23:28 -07:00
let inputField = this . _edit ;
let oldClickSelectsAll = inputField . clickSelectsAll ;
inputField . clickSelectsAll = false ;
2011-09-01 10:04:29 -07:00
AwesomeScreen . activePanel = AllPagesList ;
2010-10-10 14:23:28 -07:00
// changing the searchString property call updateAwesomeHeader again
inputField . controller . searchString = aString ;
inputField . readOnly = false ;
inputField . clickSelectsAll = oldClickSelectsAll ;
return ;
}
2010-10-14 06:15:29 -07:00
let event = document . createEvent ( "Events" ) ;
event . initEvent ( "onsearchbegin" , true , true ) ;
this . _edit . dispatchEvent ( event ) ;
2010-08-24 01:42:28 -07:00
} ,
2009-07-08 07:55:07 -07:00
_closeOrQuit : function _closeOrQuit ( ) {
// Close active dialog, if we have one. If not then close the application.
2011-09-01 10:04:29 -07:00
if ( AwesomeScreen . activePanel ) {
AwesomeScreen . activePanel = null ;
2010-08-24 01:42:28 -07:00
} else if ( this . activeDialog ) {
this . activeDialog . close ( ) ;
2010-03-30 10:27:59 -07:00
} else {
// Check to see if we should really close the window
if ( Browser . closing ( ) )
window . close ( ) ;
}
2009-07-08 07:55:07 -07:00
} ,
get activeDialog ( ) {
// Return the topmost dialog
if ( this . _dialogs . length )
return this . _dialogs [ this . _dialogs . length - 1 ] ;
return null ;
} ,
2010-07-18 22:27:52 -07:00
pushDialog : function pushDialog ( aDialog ) {
2009-07-08 07:55:07 -07:00
// If we have a dialog push it on the stack and set the attr for CSS
if ( aDialog ) {
2009-08-24 07:18:26 -07:00
this . lockToolbar ( ) ;
2009-07-08 07:55:07 -07:00
this . _dialogs . push ( aDialog ) ;
2009-07-20 13:22:22 -07:00
document . getElementById ( "toolbar-main" ) . setAttribute ( "dialog" , "true" ) ;
2009-11-23 09:23:59 -08:00
Elements . contentShowing . setAttribute ( "disabled" , "true" ) ;
2009-07-08 07:55:07 -07:00
}
} ,
2009-07-21 08:28:29 -07:00
2010-07-18 22:27:52 -07:00
popDialog : function popDialog ( ) {
2009-08-24 07:22:26 -07:00
if ( this . _dialogs . length ) {
2009-07-08 07:55:07 -07:00
this . _dialogs . pop ( ) ;
2009-08-24 07:22:26 -07:00
this . unlockToolbar ( ) ;
}
2009-07-08 07:55:07 -07:00
// If no more dialogs are being displayed, remove the attr for CSS
2009-11-23 09:23:59 -08:00
if ( ! this . _dialogs . length ) {
2009-07-20 13:22:22 -07:00
document . getElementById ( "toolbar-main" ) . removeAttribute ( "dialog" ) ;
2009-11-23 09:23:59 -08:00
Elements . contentShowing . removeAttribute ( "disabled" ) ;
}
2009-07-08 07:55:07 -07:00
} ,
2009-08-14 10:30:50 -07:00
pushPopup : function pushPopup ( aPanel , aElements ) {
2009-12-04 12:03:56 -08:00
this . _hidePopup ( ) ;
2010-01-15 21:41:48 -08:00
this . _popup = { "panel" : aPanel ,
2009-08-14 10:30:50 -07:00
"elements" : ( aElements instanceof Array ) ? aElements : [ aElements ] } ;
2010-10-14 10:19:04 -07:00
this . _dispatchPopupChanged ( true ) ;
2009-08-14 10:30:50 -07:00
} ,
2010-11-01 08:42:13 -07:00
popPopup : function popPopup ( aPanel ) {
2010-11-01 16:58:56 -07:00
if ( ! this . _popup || aPanel != this . _popup . panel )
2010-11-01 08:42:13 -07:00
return ;
2009-08-14 10:30:50 -07:00
this . _popup = null ;
2010-10-14 10:19:04 -07:00
this . _dispatchPopupChanged ( false ) ;
2009-08-14 10:30:50 -07:00
} ,
2011-06-02 09:32:12 -07:00
// Will the on-screen keyboard cover the whole screen when opened?
_isKeyboardFullscreen : function _isKeyboardFullscreen ( ) {
# ifdef ANDROID
if ( ! Util . isPortrait ( ) ) {
switch ( Services . prefs . getIntPref ( "widget.ime.android.landscape_fullscreen" ) ) {
case 1 :
return true ;
case - 1 : {
let threshold = Services . prefs . getIntPref ( "widget.ime.android.fullscreen_threshold" ) ;
2011-06-13 13:32:17 -07:00
let dpi = Util . displayDPI ;
2011-06-02 09:32:12 -07:00
return ( window . innerHeight * 100 < threshold * dpi ) ;
}
}
}
# endif
return false ;
} ,
2010-10-14 10:19:04 -07:00
_dispatchPopupChanged : function _dispatchPopupChanged ( aVisible ) {
2009-12-04 12:03:56 -08:00
let stack = document . getElementById ( "stack" ) ;
2010-10-14 10:19:04 -07:00
let event = document . createEvent ( "UIEvents" ) ;
event . initUIEvent ( "PopupChanged" , true , true , window , aVisible ) ;
2009-12-04 12:03:56 -08:00
event . popup = this . _popup ;
stack . dispatchEvent ( event ) ;
} ,
_hidePopup : function _hidePopup ( ) {
2009-08-14 10:30:50 -07:00
if ( ! this . _popup )
return ;
2009-12-04 12:03:56 -08:00
let panel = this . _popup . panel ;
if ( panel . hide )
panel . hide ( ) ;
} ,
2010-01-15 21:41:48 -08:00
2009-12-04 12:03:56 -08:00
_isEventInsidePopup : function _isEventInsidePopup ( aEvent ) {
if ( ! this . _popup )
2009-12-17 14:35:48 -08:00
return false ;
2009-12-04 12:03:56 -08:00
let elements = this . _popup . elements ;
2009-10-28 23:12:10 -07:00
let targetNode = aEvent ? aEvent . target : null ;
2009-12-04 12:03:56 -08:00
while ( targetNode && elements . indexOf ( targetNode ) == - 1 )
2009-08-14 10:30:50 -07:00
targetNode = targetNode . parentNode ;
2009-12-04 12:03:56 -08:00
return targetNode ? true : false ;
2009-08-14 10:30:50 -07:00
} ,
2010-09-30 14:30:27 -07:00
switchPane : function switchPane ( aPanelId ) {
2010-06-14 06:14:16 -07:00
this . blurFocusedElement ( ) ;
2011-02-11 11:57:41 -08:00
let panels = document . getElementById ( "panel-items" )
let panel = aPanelId ? document . getElementById ( aPanelId ) : panels . selectedPanel ;
2011-03-23 08:13:45 -07:00
let oldPanel = panels . selectedPanel ;
2011-02-11 11:57:41 -08:00
2011-03-23 08:13:45 -07:00
if ( oldPanel != panel ) {
2011-02-11 11:57:41 -08:00
panels . selectedPanel = panel ;
let button = document . getElementsByAttribute ( "linkedpanel" , aPanelId ) [ 0 ] ;
if ( button )
button . checked = true ;
2011-03-23 08:13:45 -07:00
let event = document . createEvent ( "Events" ) ;
event . initEvent ( "ToolPanelHidden" , true , true ) ;
oldPanel . dispatchEvent ( event ) ;
2011-02-11 11:57:41 -08:00
}
2011-03-23 08:13:45 -07:00
let event = document . createEvent ( "Events" ) ;
event . initEvent ( "ToolPanelShown" , true , true ) ;
panel . dispatchEvent ( event ) ;
2008-09-28 06:45:47 -07:00
} ,
2009-05-28 22:58:30 -07:00
get toolbarH ( ) {
if ( ! this . _toolbarH ) {
let toolbar = document . getElementById ( "toolbar-main" ) ;
this . _toolbarH = toolbar . boxObject . height ;
}
return this . _toolbarH ;
} ,
2010-01-15 21:41:48 -08:00
2010-07-18 22:27:52 -07:00
sizeControls : function ( windowW , windowH ) {
2009-10-15 09:54:36 -07:00
// tabs
2011-09-14 06:39:25 -07:00
Elements . tabList . resize ( ) ;
2011-09-01 10:04:29 -07:00
AwesomeScreen . doResize ( windowW , windowH ) ;
2008-11-21 21:12:25 -08:00
2010-07-19 07:51:03 -07:00
// content navigator helper
2011-02-23 17:07:51 -08:00
document . getElementById ( "content-navigator" ) . contentHasChanged ( ) ;
2008-08-19 12:19:47 -07:00
} ,
2010-07-18 22:27:52 -07:00
init : function ( ) {
2008-07-03 23:39:25 -07:00
this . _edit = document . getElementById ( "urlbar-edit" ) ;
2010-11-02 12:20:11 -07:00
this . _title = document . getElementById ( "urlbar-title" ) ;
2008-07-03 23:39:25 -07:00
this . _throbber = document . getElementById ( "urlbar-throbber" ) ;
this . _favicon = document . getElementById ( "urlbar-favicon" ) ;
2008-05-02 13:15:45 -07:00
this . _favicon . addEventListener ( "error" , this , false ) ;
2010-06-24 15:25:40 -07:00
this . _edit . addEventListener ( "click" , this , false ) ;
this . _edit . addEventListener ( "mousedown" , this , false ) ;
2009-07-14 07:16:23 -07:00
2010-10-14 10:19:04 -07:00
window . addEventListener ( "NavigationPanelShown" , this , false ) ;
window . addEventListener ( "NavigationPanelHidden" , this , false ) ;
2010-08-26 15:48:27 -07:00
2010-12-15 22:45:40 -08:00
Elements . browsers . addEventListener ( "PanFinished" , this , true ) ;
2011-01-25 06:32:05 -08:00
# if MOZ _PLATFORM _MAEMO == 6
2010-12-29 07:40:51 -08:00
Elements . browsers . addEventListener ( "SizeChanged" , this , true ) ;
2011-01-25 06:32:05 -08:00
# endif
2009-07-14 07:16:23 -07:00
2010-06-08 08:22:20 -07:00
// listen content messages
messageManager . addMessageListener ( "DOMLinkAdded" , this ) ;
messageManager . addMessageListener ( "DOMTitleChanged" , this ) ;
messageManager . addMessageListener ( "DOMWillOpenModalDialog" , this ) ;
messageManager . addMessageListener ( "DOMWindowClose" , this ) ;
2010-01-15 21:41:48 -08:00
2010-06-29 11:15:07 -07:00
messageManager . addMessageListener ( "Browser:OpenURI" , this ) ;
messageManager . addMessageListener ( "Browser:SaveAs:Return" , this ) ;
2010-06-09 19:12:05 -07:00
2009-08-14 10:30:50 -07:00
// listening mousedown for automatically dismiss some popups (e.g. larry)
window . addEventListener ( "mousedown" , this , true ) ;
2009-07-16 08:45:50 -07:00
2009-08-24 08:00:28 -07:00
// listening escape to dismiss dialog on VK_ESCAPE
2011-09-30 21:30:36 -07:00
window . addEventListener ( "keypress" , this , false ) ;
2009-08-24 08:00:28 -07:00
2010-04-27 13:33:59 -07:00
// listening AppCommand to handle special keys
window . addEventListener ( "AppCommand" , this , true ) ;
2011-09-15 13:33:20 -07:00
// Initialize the number of tabs in toolbar
TabsPopup . init ( ) ;
2010-10-12 11:30:19 -07:00
// We can delay some initialization until after startup. We wait until
// the first page is shown, then dispatch a UIReadyDelayed event.
2010-10-11 08:13:33 -07:00
messageManager . addMessageListener ( "pageshow" , function ( ) {
if ( getBrowser ( ) . currentURI . spec == "about:blank" )
return ;
messageManager . removeMessageListener ( "pageshow" , arguments . callee , true ) ;
2010-06-21 13:16:37 -07:00
2011-01-02 08:56:56 -08:00
setTimeout ( function ( ) {
let event = document . createEvent ( "Events" ) ;
event . initEvent ( "UIReadyDelayed" , true , false ) ;
window . dispatchEvent ( event ) ;
} , 0 ) ;
2010-10-12 11:30:19 -07:00
} ) ;
2011-04-29 16:46:20 -07:00
// Only load IndexedDB.js when we actually need it. A general fix will happen in bug 647079.
messageManager . addMessageListener ( "IndexedDB:Prompt" , function ( aMessage ) {
return IndexedDB . receiveMessage ( aMessage ) ;
} ) ;
2010-10-12 11:30:19 -07:00
// Delay the panel UI and Sync initialization.
window . addEventListener ( "UIReadyDelayed" , function ( aEvent ) {
window . removeEventListener ( aEvent . type , arguments . callee , false ) ;
2009-12-17 14:35:48 -08:00
// We unhide the panelUI so the XBL and settings can initialize
Elements . panelUI . hidden = false ;
2010-03-20 14:33:35 -07:00
2011-02-08 14:46:24 -08:00
// Login Manager and Form History initialization
Cc [ "@mozilla.org/login-manager;1" ] . getService ( Ci . nsILoginManager ) ;
Cc [ "@mozilla.org/satchel/form-history;1" ] . getService ( Ci . nsIFormHistory2 ) ;
// Listen tabs event
Elements . tabs . addEventListener ( "TabSelect" , BrowserUI , true ) ;
Elements . tabs . addEventListener ( "TabOpen" , BrowserUI , true ) ;
2011-02-08 15:22:27 -08:00
Elements . tabs . addEventListener ( "TabRemove" , BrowserUI , true ) ;
2011-02-08 14:46:24 -08:00
2011-02-17 13:25:59 -08:00
// Init the tool panel views
2009-12-17 14:35:48 -08:00
ExtensionsView . init ( ) ;
DownloadsView . init ( ) ;
ConsoleView . init ( ) ;
2010-06-29 08:09:04 -07:00
2010-08-24 01:49:32 -07:00
# ifdef MOZ _SERVICES _SYNC
2010-06-29 08:09:04 -07:00
// Init the sync system
WeaveGlue . init ( ) ;
2010-08-24 01:49:32 -07:00
# endif
2010-06-21 13:36:56 -07:00
2011-08-05 16:01:33 -07:00
Services . prefs . addObserver ( "browser.ui.layout.tablet" , BrowserUI , false ) ;
2011-02-08 14:46:24 -08:00
Services . obs . addObserver ( BrowserSearch , "browser-search-engine-modified" , false ) ;
2011-02-11 10:52:49 -08:00
messageManager . addMessageListener ( "Browser:MozApplicationManifest" , OfflineApps ) ;
2011-02-08 14:46:24 -08:00
2011-01-05 08:59:32 -08:00
// Init helpers
BadgeHandlers . register ( BrowserUI . _edit . popup ) ;
FormHelperUI . init ( ) ;
FindHelperUI . init ( ) ;
FullScreenVideo . init ( ) ;
NewTabPopup . init ( ) ;
2011-08-15 12:16:50 -07:00
WebappsUI . init ( ) ;
2011-09-26 17:25:41 -07:00
CapturePickerUI . init ( ) ;
2010-12-28 20:39:36 -08:00
2011-03-23 20:50:48 -07:00
// If some add-ons were disabled during during an application update, alert user
2011-06-29 14:50:43 -07:00
let addonIDs = AddonManager . getStartupChanges ( "disabled" ) ;
if ( addonIDs . length > 0 ) {
let disabledStrings = Strings . browser . GetStringFromName ( "alertAddonsDisabled" ) ;
let label = PluralForm . get ( addonIDs . length , disabledStrings ) . replace ( "#1" , addonIDs . length ) ;
let image = "chrome://browser/skin/images/alert-addons-30.png" ;
let alerts = Cc [ "@mozilla.org/toaster-alerts-service;1" ] . getService ( Ci . nsIAlertsService ) ;
alerts . showAlertNotification ( image , Strings . browser . GetStringFromName ( "alertAddons" ) , label , false , "" , null ) ;
2011-03-23 20:50:48 -07:00
}
2010-12-28 20:39:36 -08:00
# ifdef MOZ _UPDATER
// Check for updates in progress
let updatePrompt = Cc [ "@mozilla.org/updates/update-prompt;1" ] . createInstance ( Ci . nsIUpdatePrompt ) ;
updatePrompt . checkForUpdates ( ) ;
# endif
2011-01-05 08:59:32 -08:00
} , false ) ;
2011-01-20 11:21:55 -08:00
2011-02-17 13:25:59 -08:00
let panels = document . getElementById ( "panel-items" ) ;
let panelViews = { // Use strings to avoid lazy-loading objects too soon.
"prefs-container" : "PreferencesView" ,
"downloads-container" : "DownloadsView" ,
"addons-container" : "ExtensionsView" ,
"console-container" : "ConsoleView"
} ;
// Some initialization can be delayed until a panel is selected.
2011-03-23 08:13:45 -07:00
panels . addEventListener ( "ToolPanelShown" , function ( aEvent ) {
2011-02-17 13:25:59 -08:00
let viewName = panelViews [ panels . selectedPanel . id ] ;
if ( viewName )
window [ viewName ] . delayedInit ( ) ;
} , true ) ;
2011-01-20 11:21:55 -08:00
# ifndef MOZ _OFFICIAL _BRANDING
setTimeout ( function ( ) {
2011-03-24 11:44:07 -07:00
let startup = Cc [ "@mozilla.org/toolkit/app-startup;1" ] . getService ( Ci . nsIAppStartup ) . getStartupInfo ( ) ;
2011-01-20 11:21:55 -08:00
for ( let name in startup ) {
if ( name != "process" )
Services . console . logStringMessage ( "[timing] " + name + ": " + ( startup [ name ] - startup . process ) + "ms" ) ;
}
} , 3000 ) ;
# endif
2008-05-02 13:15:45 -07:00
} ,
2010-07-18 22:27:52 -07:00
uninit : function ( ) {
2011-02-08 14:46:24 -08:00
Services . obs . removeObserver ( BrowserSearch , "browser-search-engine-modified" ) ;
2011-08-05 16:01:33 -07:00
Services . prefs . removeObserver ( "browser.ui.layout.tablet" , BrowserUI ) ;
2011-02-11 10:52:49 -08:00
messageManager . removeMessageListener ( "Browser:MozApplicationManifest" , OfflineApps ) ;
2009-05-26 18:46:44 -07:00
ExtensionsView . uninit ( ) ;
2009-10-14 12:04:33 -07:00
ConsoleView . uninit ( ) ;
2009-05-26 18:46:44 -07:00
} ,
2011-08-05 16:01:33 -07:00
observe : function observe ( aSubject , aTopic , aData ) {
if ( aTopic == "nsPref:changed" && aData == "browser.ui.layout.tablet" )
this . updateTabletLayout ( ) ;
} ,
updateTabletLayout : function updateTabletLayout ( ) {
2011-09-09 09:06:36 -07:00
let wasTablet = Elements . urlbarState . hasAttribute ( "tablet" ) ;
let isTablet = Util . isTablet ( { forceUpdate : true } ) ;
if ( wasTablet == isTablet )
return ;
if ( isTablet ) {
2011-09-07 13:47:25 -07:00
this . unlockToolbar ( ) ;
2011-08-05 16:01:33 -07:00
Elements . urlbarState . setAttribute ( "tablet" , "true" ) ;
2011-09-07 13:47:25 -07:00
} else {
2011-08-05 16:01:33 -07:00
Elements . urlbarState . removeAttribute ( "tablet" ) ;
2011-09-07 13:47:25 -07:00
}
2011-09-07 13:47:39 -07:00
// Tablet mode changes the size of the thumbnails
// in the tabs container. Hence we have to force a
// thumbnail update on all tabs.
setTimeout ( function ( self ) {
self . _updateAllTabThumbnails ( ) ;
} , 0 , this ) ;
} ,
_updateAllTabThumbnails : function ( ) {
let tabs = Browser . tabs ;
tabs . forEach ( function ( tab ) {
tab . updateThumbnail ( { force : true } ) ;
} ) ;
2011-08-05 16:01:33 -07:00
} ,
2010-07-18 22:27:52 -07:00
update : function ( aState ) {
2009-10-09 05:34:02 -07:00
let browser = Browser . selectedBrowser ;
2009-09-08 11:14:59 -07:00
2008-11-21 21:12:25 -08:00
switch ( aState ) {
case TOOLBARSTATE _LOADED :
2010-11-17 02:18:21 -08:00
this . _updateToolbar ( ) ;
2009-10-09 05:34:02 -07:00
this . _updateIcon ( browser . mIconURL ) ;
2010-06-02 10:04:03 -07:00
this . unlockToolbar ( ) ;
2008-11-21 21:12:25 -08:00
break ;
case TOOLBARSTATE _LOADING :
2010-11-17 02:18:21 -08:00
this . _updateToolbar ( ) ;
2009-06-18 22:14:04 -07:00
2009-10-09 05:34:02 -07:00
browser . mIconURL = "" ;
this . _updateIcon ( ) ;
2010-06-02 10:04:03 -07:00
this . lockToolbar ( ) ;
2008-11-21 21:12:25 -08:00
break ;
2008-10-22 08:39:00 -07:00
}
} ,
2010-07-18 22:27:52 -07:00
_updateIcon : function ( aIconSrc ) {
2009-10-09 05:34:02 -07:00
this . _favicon . src = aIconSrc || "" ;
2009-01-08 22:51:13 -08:00
if ( Browser . selectedTab . isLoading ( ) ) {
2008-11-21 21:12:25 -08:00
this . _throbber . hidden = false ;
2008-10-22 08:39:00 -07:00
this . _throbber . setAttribute ( "loading" , "true" ) ;
2008-11-21 21:12:25 -08:00
this . _favicon . hidden = true ;
2008-10-22 08:39:00 -07:00
}
else {
2008-11-21 21:12:25 -08:00
this . _favicon . hidden = false ;
this . _throbber . hidden = true ;
2008-10-22 08:39:00 -07:00
this . _throbber . removeAttribute ( "loading" ) ;
2008-05-02 13:15:45 -07:00
}
} ,
2010-07-18 22:27:52 -07:00
getDisplayURI : function ( browser ) {
2010-03-31 06:39:05 -07:00
let uri = browser . currentURI ;
2008-09-27 07:07:04 -07:00
try {
2010-07-22 20:55:58 -07:00
uri = gURIFixup . createExposableURI ( uri ) ;
2008-09-27 07:07:04 -07:00
} catch ( ex ) { }
return uri . spec ;
} ,
2008-05-02 13:15:45 -07:00
/* Set the location to the current content */
2011-02-11 08:24:15 -08:00
updateURI : function ( aOptions ) {
aOptions = aOptions || { } ;
2010-10-14 06:15:29 -07:00
let browser = Browser . selectedBrowser ;
2011-02-11 08:24:15 -08:00
let urlString = this . getDisplayURI ( browser ) ;
if ( Util . isURLEmpty ( urlString ) )
urlString = "" ;
2008-09-06 21:06:04 -07:00
2011-02-11 08:24:15 -08:00
this . _setURL ( urlString ) ;
if ( "captionOnly" in aOptions && aOptions . captionOnly )
2008-09-06 21:06:04 -07:00
return ;
2009-06-24 15:43:26 -07:00
// Update the navigation buttons
this . _updateButtons ( browser ) ;
2008-05-02 13:15:45 -07:00
// Check for a bookmarked page
2008-12-15 08:44:27 -08:00
this . updateStar ( ) ;
2008-05-02 13:15:45 -07:00
} ,
2010-07-18 22:27:52 -07:00
goToURI : function ( aURI ) {
2009-09-13 21:49:09 -07:00
aURI = aURI || this . _edit . value ;
2008-05-02 13:15:45 -07:00
if ( ! aURI )
2009-09-13 21:49:09 -07:00
return ;
2009-11-24 10:23:41 -08:00
// Make sure we're online before attempting to load
Util . forceOnline ( ) ;
2011-05-09 04:28:48 -07:00
// Close the autocomplete panel and quickly update the urlbar value
2010-10-14 06:15:29 -07:00
this . closeAutoComplete ( ) ;
2009-10-08 22:49:43 -07:00
this . _edit . value = aURI ;
2008-05-06 07:56:21 -07:00
2010-11-01 16:36:07 -07:00
let postData = { } ;
aURI = Browser . getShortcutOrURI ( aURI , postData ) ;
Browser . loadURI ( aURI , { flags : Ci . nsIWebNavigation . LOAD _FLAGS _ALLOW _THIRD _PARTY _FIXUP , postData : postData } ) ;
2010-07-23 06:02:44 -07:00
2011-05-09 04:28:48 -07:00
// If a page goes from remote (about:blank) to local (about:home), fennec
// create a tab for the local page and then close the about:blank tab.
// If the newly created tab spawn a new column the sidebars position can
// be messed up. Hopefully, the viewable area should be maximized when a
// new page is opened, so a call to Browser.hideSidebars() fill this
// requirement and fix the sidebars position.
Browser . hideSidebars ( ) ;
2011-09-14 06:39:25 -07:00
Elements . tabList . removeClosedTab ( ) ;
2011-05-09 04:28:48 -07:00
2010-07-22 20:55:58 -07:00
// Delay doing the fixup so the raw URI is passed to loadURIWithFlags
// and the proper third-party fixup can be done
let fixupFlags = Ci . nsIURIFixup . FIXUP _FLAG _ALLOW _KEYWORD _LOOKUP ;
let uri = gURIFixup . createFixupURI ( aURI , fixupFlags ) ;
2010-05-25 08:32:24 -07:00
gHistSvc . markPageAsTyped ( uri ) ;
2010-12-09 08:06:21 -08:00
this . _titleChanged ( Browser . selectedBrowser ) ;
2008-05-02 13:15:45 -07:00
} ,
2010-07-18 22:27:52 -07:00
showAutoComplete : function showAutoComplete ( ) {
2009-09-18 21:17:52 -07:00
if ( this . isAutoCompleteOpen ( ) )
return ;
2009-12-03 06:04:57 -08:00
2010-11-04 11:42:07 -07:00
this . hidePanel ( ) ;
2010-05-21 10:05:46 -07:00
this . _hidePopup ( ) ;
2011-04-22 06:08:00 -07:00
if ( this . activeDialog )
this . activeDialog . close ( ) ;
2011-09-01 10:04:29 -07:00
AwesomeScreen . activePanel = AllPagesList ;
2008-07-16 08:41:38 -07:00
} ,
2010-01-15 21:41:48 -08:00
2010-10-14 06:15:29 -07:00
closeAutoComplete : function closeAutoComplete ( ) {
if ( this . isAutoCompleteOpen ( ) )
this . _edit . popup . closePopup ( ) ;
2010-09-09 02:08:43 -07:00
2011-09-01 10:04:29 -07:00
AwesomeScreen . activePanel = null ;
2009-11-30 22:04:55 -08:00
} ,
2008-07-16 08:41:38 -07:00
2009-09-18 21:17:52 -07:00
isAutoCompleteOpen : function isAutoCompleteOpen ( ) {
2011-09-01 10:04:29 -07:00
return AwesomeScreen . activePanel == AllPagesList ;
2009-09-18 21:17:52 -07:00
} ,
2010-08-26 16:09:56 -07:00
doOpenSearch : function doOpenSearch ( aName ) {
2010-08-25 08:44:18 -07:00
// save the current value of the urlbar
let searchValue = this . _edit . value ;
2009-10-28 20:55:47 -07:00
// Give the new page lots of room
Browser . hideSidebars ( ) ;
2010-10-14 06:15:29 -07:00
this . closeAutoComplete ( ) ;
2008-07-16 08:41:38 -07:00
2009-11-24 10:23:41 -08:00
// Make sure we're online before attempting to load
Util . forceOnline ( ) ;
2010-08-26 16:09:56 -07:00
let engine = Services . search . getEngineByName ( aName ) ;
let submission = engine . getSubmission ( searchValue , null ) ;
2010-08-10 14:32:32 -07:00
Browser . loadURI ( submission . uri . spec , { postData : submission . postData } ) ;
2010-12-29 07:39:37 -08:00
2011-09-27 12:45:49 -07:00
// loadURI may open a new tab, so get the selectedBrowser afterward.
Browser . selectedBrowser . userTypedValue = submission . uri . spec ;
2010-12-29 07:39:37 -08:00
this . _titleChanged ( Browser . selectedBrowser ) ;
2008-07-16 08:41:38 -07:00
} ,
2010-09-30 14:28:07 -07:00
updateUIFocus : function _updateUIFocus ( ) {
2010-11-29 10:15:03 -08:00
if ( Elements . contentShowing . getAttribute ( "disabled" ) == "true" )
Browser . selectedBrowser . messageManager . sendAsyncMessage ( "Browser:Blur" , { } ) ;
2010-09-30 14:14:47 -07:00
} ,
2010-07-18 22:27:52 -07:00
updateStar : function ( ) {
2011-02-11 08:24:15 -08:00
let uri = getBrowser ( ) . currentURI ;
if ( uri . spec == "about:blank" ) {
2011-08-05 16:01:33 -07:00
this . _setStar ( false ) ;
2011-02-11 08:24:15 -08:00
return ;
}
2011-08-05 16:01:33 -07:00
PlacesUtils . asyncGetBookmarkIds ( uri , function ( aItemIds ) {
this . _setStar ( aItemIds . length > 0 )
2010-12-29 13:35:06 -08:00
} , this ) ;
2008-12-15 08:44:27 -08:00
} ,
2011-08-05 16:01:33 -07:00
_setStar : function _setStar ( aIsStarred ) {
let buttons = document . getElementsByClassName ( "tool-star" ) ;
for ( let i = 0 ; i < buttons . length ; i ++ ) {
if ( aIsStarred )
buttons [ i ] . setAttribute ( "starred" , "true" ) ;
else
buttons [ i ] . removeAttribute ( "starred" ) ;
}
} ,
2010-07-18 22:27:52 -07:00
newTab : function newTab ( aURI , aOwner ) {
2009-08-26 10:24:27 -07:00
aURI = aURI || "about:blank" ;
2010-06-15 14:56:13 -07:00
let tab = Browser . addTab ( aURI , true , aOwner ) ;
2009-08-26 10:24:27 -07:00
2009-09-22 15:05:20 -07:00
this . hidePanel ( ) ;
2010-01-15 21:41:48 -08:00
2009-09-18 21:17:52 -07:00
if ( aURI == "about:blank" ) {
2009-11-30 22:04:55 -08:00
// Display awesomebar UI
2010-10-14 06:15:29 -07:00
this . showAutoComplete ( ) ;
2009-09-18 21:17:52 -07:00
}
2009-10-28 20:55:47 -07:00
else {
// Give the new page lots of room
Browser . hideSidebars ( ) ;
2010-10-14 06:15:29 -07:00
this . closeAutoComplete ( ) ;
2009-10-28 20:55:47 -07:00
}
2009-08-25 20:45:11 -07:00
return tab ;
2008-05-02 13:15:45 -07:00
} ,
2010-07-16 08:48:53 -07:00
newOrSelectTab : function newOrSelectTab ( aURI , aOwner ) {
let tabs = Browser . tabs ;
for ( let i = 0 ; i < tabs . length ; i ++ ) {
if ( tabs [ i ] . browser . currentURI . spec == aURI ) {
Browser . selectedTab = tabs [ i ] ;
return ;
}
}
this . newTab ( aURI , aOwner ) ;
} ,
2010-07-18 22:27:52 -07:00
closeTab : function closeTab ( aTab ) {
2009-06-15 05:44:30 -07:00
// If no tab is passed in, assume the current tab
Browser . closeTab ( aTab || Browser . selectedTab ) ;
2008-09-28 23:43:33 -07:00
} ,
2010-07-18 22:27:52 -07:00
selectTab : function selectTab ( aTab ) {
2011-09-01 10:04:29 -07:00
AwesomeScreen . activePanel = null ;
2009-01-08 22:51:13 -08:00
Browser . selectedTab = aTab ;
2011-09-14 06:39:25 -07:00
Elements . tabList . removeClosedTab ( ) ;
2009-02-10 13:52:23 -08:00
} ,
2010-07-18 22:27:52 -07:00
undoCloseTab : function undoCloseTab ( aIndex ) {
let tab = null ;
let ss = Cc [ "@mozilla.org/browser/sessionstore;1" ] . getService ( Ci . nsISessionStore ) ;
if ( ss . getClosedTabCount ( window ) > ( aIndex || 0 ) ) {
2010-11-10 13:49:23 -08:00
let chromeTab = ss . undoCloseTab ( window , aIndex || 0 ) ;
tab = Browser . getTabFromChrome ( chromeTab ) ;
2010-07-18 22:27:52 -07:00
}
return tab ;
} ,
2009-08-25 21:49:26 -07:00
isTabsVisible : function isTabsVisible ( ) {
2009-08-28 12:08:39 -07:00
// The _1, _2 and _3 are to make the js2 emacs mode happy
let [ leftvis , _1 , _2 , _3 ] = Browser . computeSidebarVisibility ( ) ;
2009-08-25 21:49:26 -07:00
return ( leftvis > 0.002 ) ;
} ,
2011-02-11 11:57:41 -08:00
showPanel : function showPanel ( aPanelId ) {
2011-09-01 10:04:29 -07:00
if ( AwesomeScreen . activePanel )
AwesomeScreen . activePanel = null ; // Hide the awesomescreen.
2010-09-29 12:54:50 -07:00
2009-12-17 14:35:48 -08:00
Elements . panelUI . left = 0 ;
Elements . panelUI . hidden = false ;
2009-11-23 09:23:59 -08:00
Elements . contentShowing . setAttribute ( "disabled" , "true" ) ;
2011-02-11 11:57:41 -08:00
this . switchPane ( aPanelId ) ;
2009-04-17 07:41:44 -07:00
} ,
hidePanel : function hidePanel ( ) {
2010-06-22 13:20:56 -07:00
if ( ! this . isPanelVisible ( ) )
return ;
2009-12-17 14:35:48 -08:00
Elements . panelUI . hidden = true ;
2009-11-23 09:23:59 -08:00
Elements . contentShowing . removeAttribute ( "disabled" ) ;
2010-06-14 06:14:16 -07:00
this . blurFocusedElement ( ) ;
2011-03-23 08:13:45 -07:00
let panels = document . getElementById ( "panel-items" )
let event = document . createEvent ( "Events" ) ;
event . initEvent ( "ToolPanelHidden" , true , true ) ;
panels . selectedPanel . dispatchEvent ( event ) ;
2008-05-02 13:15:45 -07:00
} ,
2009-12-17 14:35:48 -08:00
isPanelVisible : function isPanelVisible ( ) {
return ( ! Elements . panelUI . hidden && Elements . panelUI . left == 0 ) ;
} ,
2010-06-14 06:14:16 -07:00
blurFocusedElement : function blurFocusedElement ( ) {
let focusedElement = document . commandDispatcher . focusedElement ;
if ( focusedElement )
focusedElement . blur ( ) ;
} ,
2009-09-23 09:09:44 -07:00
switchTask : function switchTask ( ) {
try {
2011-08-08 19:37:57 -07:00
let shell = Cc [ "@mozilla.org/browser/shell-service;1" ] . createInstance ( Ci . nsIShellService ) ;
shell . switchTask ( ) ;
2009-09-23 09:09:44 -07:00
} catch ( e ) { }
} ,
2010-01-15 21:41:48 -08:00
2010-08-17 09:38:45 -07:00
handleEscape : function ( aEvent ) {
aEvent . stopPropagation ( ) ;
2010-08-27 01:24:17 -07:00
// Check open popups
if ( this . _popup ) {
this . _hidePopup ( ) ;
return ;
}
2010-04-28 11:32:17 -07:00
// Check open dialogs
let dialog = this . activeDialog ;
2011-09-01 10:04:29 -07:00
if ( dialog && dialog != AwesomeScreen . activePanel ) {
2010-04-28 11:32:17 -07:00
dialog . close ( ) ;
return ;
}
2010-07-28 17:41:42 -07:00
2011-08-19 09:29:23 -07:00
// Check active panel
2011-09-01 10:04:29 -07:00
if ( AwesomeScreen . activePanel ) {
AwesomeScreen . activePanel = null ;
2011-08-19 09:29:23 -07:00
return ;
}
2010-04-28 11:32:17 -07:00
// Check open modal elements
let modalElementsLength = document . getElementsByClassName ( "modal-block" ) . length ;
2010-07-28 14:27:23 -07:00
if ( modalElementsLength > 0 )
2010-04-28 11:32:17 -07:00
return ;
// Check open panel
if ( this . isPanelVisible ( ) ) {
this . hidePanel ( ) ;
return ;
}
2010-09-28 16:19:00 -07:00
// Check content helper
let contentHelper = document . getElementById ( "content-navigator" ) ;
if ( contentHelper . isActive ) {
2010-11-10 07:57:55 -08:00
contentHelper . model . hide ( ) ;
2010-09-28 16:19:00 -07:00
return ;
}
2010-04-28 11:32:17 -07:00
// Only if there are no dialogs, popups, or panels open
2010-06-15 14:56:13 -07:00
let tab = Browser . selectedTab ;
let browser = tab . browser ;
2010-09-30 14:05:43 -07:00
if ( browser . canGoBack ) {
2010-06-15 14:56:13 -07:00
browser . goBack ( ) ;
2010-09-30 14:05:43 -07:00
} else if ( tab . owner ) {
2011-04-27 09:10:52 -07:00
// When going back, always return to the owner (not a sibling).
Browser . selectedTab = tab . owner ;
2010-06-15 14:56:13 -07:00
this . closeTab ( tab ) ;
2010-09-30 14:05:43 -07:00
}
2010-06-17 10:36:38 -07:00
# ifdef ANDROID
2010-09-30 14:05:43 -07:00
else {
2010-06-17 10:36:38 -07:00
window . QueryInterface ( Ci . nsIDOMChromeWindow ) . minimize ( ) ;
2010-09-30 14:05:43 -07:00
if ( tab . closeOnExit )
this . closeTab ( tab ) ;
}
2010-06-17 10:36:38 -07:00
# endif
2010-04-28 11:32:17 -07:00
} ,
2010-06-09 19:12:05 -07:00
handleEvent : function handleEvent ( aEvent ) {
2008-05-02 13:15:45 -07:00
switch ( aEvent . type ) {
2008-07-03 23:39:25 -07:00
// Browser events
2008-08-19 23:09:22 -07:00
case "TabSelect" :
this . _tabSelect ( aEvent ) ;
break ;
2009-08-25 21:49:26 -07:00
case "TabOpen" :
2011-09-14 06:39:25 -07:00
Elements . tabList . removeClosedTab ( ) ;
Browser . hidePartialTabSidebar ( ) ;
break ;
2011-02-07 04:56:11 -08:00
case "TabRemove" :
2011-09-14 06:39:25 -07:00
Browser . hidePartialTabSidebar ( ) ;
2009-08-25 21:49:26 -07:00
break ;
2011-09-14 06:39:25 -07:00
case "PanFinished" : {
let tabs = Elements . tabList ;
2011-02-07 04:56:11 -08:00
let [ tabsVisibility , , oldLeftWidth , oldRightWidth ] = Browser . computeSidebarVisibility ( ) ;
if ( tabsVisibility == 0.0 && tabs . hasClosedTab ) {
let { x : x1 , y : y1 } = Browser . getScrollboxPosition ( Browser . controlsScrollboxScroller ) ;
tabs . removeClosedTab ( ) ;
2011-05-09 04:28:48 -07:00
// If the tabs sidebar lives on the left side of the window, width
// variation should be taken into account to reposition the sidebars
if ( tabs . getBoundingClientRect ( ) . left < 0 ) {
let [ , , leftWidth , rightWidth ] = Browser . computeSidebarVisibility ( ) ;
let delta = ( oldLeftWidth - leftWidth ) || ( oldRightWidth - rightWidth ) ;
x1 += ( x1 == leftWidth ) ? delta : - delta ;
}
2011-02-07 04:56:11 -08:00
Browser . controlsScrollboxScroller . scrollTo ( x1 , 0 ) ;
Browser . tryFloatToolbar ( 0 , 0 ) ;
}
2010-10-06 08:08:56 -07:00
break ;
2011-09-14 06:39:25 -07:00
}
2010-12-29 07:40:51 -08:00
case "SizeChanged" :
2010-12-30 01:05:30 -08:00
this . sizeControls ( ViewableAreaObserver . width , ViewableAreaObserver . height ) ;
2010-12-29 07:40:51 -08:00
break ;
2010-04-06 16:42:36 -07:00
// Window events
2008-05-02 13:15:45 -07:00
case "keypress" :
2010-04-28 11:32:17 -07:00
if ( aEvent . keyCode == aEvent . DOM _VK _ESCAPE )
2010-08-17 09:38:45 -07:00
this . handleEscape ( aEvent ) ;
2008-05-02 13:15:45 -07:00
break ;
2010-04-27 13:33:59 -07:00
case "AppCommand" :
aEvent . stopPropagation ( ) ;
switch ( aEvent . command ) {
case "Menu" :
this . doCommand ( "cmd_menu" ) ;
break ;
case "Search" :
2011-09-01 10:04:29 -07:00
if ( ! AwesomeScreen . activePanel )
2010-11-03 06:45:19 -07:00
AllPagesList . doCommand ( ) ;
else
this . doCommand ( "cmd_opensearch" ) ;
2010-04-27 13:33:59 -07:00
break ;
default :
break ;
}
break ;
2010-04-06 16:42:36 -07:00
// URL textbox events
2010-05-24 09:11:23 -07:00
case "click" :
2010-11-02 12:20:11 -07:00
if ( this . _edit . readOnly )
2010-10-10 14:23:28 -07:00
this . _edit . readOnly = false ;
2010-04-06 16:42:36 -07:00
break ;
2009-07-21 17:05:01 -07:00
case "mousedown" :
2009-12-04 12:03:56 -08:00
if ( ! this . _isEventInsidePopup ( aEvent ) )
this . _hidePopup ( ) ;
2009-07-21 17:05:01 -07:00
break ;
2008-07-03 23:39:25 -07:00
// Favicon events
2008-05-02 13:15:45 -07:00
case "error" :
2009-10-09 05:34:02 -07:00
this . _favicon . src = "" ;
2008-05-02 13:15:45 -07:00
break ;
2010-08-26 15:48:27 -07:00
// Awesome popup event
2010-10-14 10:19:04 -07:00
case "NavigationPanelShown" :
2010-11-02 12:20:11 -07:00
this . _edit . collapsed = false ;
this . _title . collapsed = true ;
if ( ! this . _edit . readOnly )
this . _edit . focus ( ) ;
2010-10-21 10:53:15 -07:00
// Disabled the search button if no search engines are available
let button = document . getElementById ( "urlbar-icons" ) ;
if ( BrowserSearch . engines . length )
button . removeAttribute ( "disabled" ) ;
else
button . setAttribute ( "disabled" , "true" ) ;
2010-08-26 15:48:27 -07:00
break ;
2010-11-03 06:45:19 -07:00
case "NavigationPanelHidden" : {
2010-11-02 12:20:11 -07:00
this . _edit . collapsed = true ;
this . _title . collapsed = false ;
2010-11-17 02:18:21 -08:00
this . _updateToolbar ( ) ;
2010-11-02 12:20:11 -07:00
2010-11-03 06:45:19 -07:00
let button = document . getElementById ( "urlbar-icons" ) ;
button . removeAttribute ( "open" ) ;
button . removeAttribute ( "disabled" ) ;
2010-08-26 15:48:27 -07:00
break ;
2010-11-03 06:45:19 -07:00
}
2008-05-02 13:15:45 -07:00
}
} ,
2010-06-09 19:12:05 -07:00
receiveMessage : function receiveMessage ( aMessage ) {
2010-06-08 08:22:20 -07:00
let browser = aMessage . target ;
2010-06-21 13:16:37 -07:00
let json = aMessage . json ;
2010-06-08 08:22:20 -07:00
switch ( aMessage . name ) {
case "DOMTitleChanged" :
this . _titleChanged ( browser ) ;
break ;
case "DOMWillOpenModalDialog" :
return this . _domWillOpenModalDialog ( browser ) ;
break ;
case "DOMWindowClose" :
return this . _domWindowClose ( browser ) ;
break ;
case "DOMLinkAdded" :
2011-06-10 14:02:00 -07:00
// checks for an icon to use for a web app
2011-07-11 02:00:57 -07:00
// apple-touch-icon size is 57px and default size is 16px
2011-06-10 14:02:00 -07:00
let rel = json . rel . toLowerCase ( ) . split ( " " ) ;
2011-07-11 02:00:57 -07:00
if ( rel . indexOf ( "icon" ) != - 1 ) {
// We use the sizes attribute if available
2011-06-10 14:02:00 -07:00
// see http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#rel-icon
2011-07-11 02:00:57 -07:00
let size = 16 ;
if ( json . sizes ) {
let sizes = json . sizes . toLowerCase ( ) . split ( " " ) ;
sizes . forEach ( function ( item ) {
if ( item != "any" ) {
let [ w , h ] = item . split ( "x" ) ;
size = Math . max ( Math . min ( w , h ) , size ) ;
}
} ) ;
}
if ( size > browser . appIcon . size ) {
browser . appIcon . href = json . href ;
browser . appIcon . size = size ;
}
2011-06-10 14:02:00 -07:00
}
2011-07-11 02:00:57 -07:00
else if ( ( rel . indexOf ( "apple-touch-icon" ) != - 1 ) && ( browser . appIcon . size < 57 ) ) {
2011-06-10 14:02:00 -07:00
// XXX should we support apple-touch-icon-precomposed ?
// see http://developer.apple.com/safari/library/documentation/appleapplications/reference/safariwebcontent/configuringwebapplications/configuringwebapplications.html
2011-07-11 02:00:57 -07:00
browser . appIcon . href = json . href ;
browser . appIcon . size = 57 ;
2011-06-10 14:02:00 -07:00
}
// Handle favicon changes
2010-06-08 08:22:20 -07:00
if ( Browser . selectedBrowser == browser )
this . _updateIcon ( Browser . selectedBrowser . mIconURL ) ;
break ;
2010-06-09 19:12:05 -07:00
case "Browser:SaveAs:Return" :
if ( json . type != Ci . nsIPrintSettings . kOutputFormatPDF )
return ;
let dm = Cc [ "@mozilla.org/download-manager;1" ] . getService ( Ci . nsIDownloadManager ) ;
let db = dm . DBConnection ;
let stmt = db . createStatement ( "UPDATE moz_downloads SET endTime = :endTime, state = :state WHERE id = :id" ) ;
stmt . params . endTime = Date . now ( ) * 1000 ;
stmt . params . state = Ci . nsIDownloadManager . DOWNLOAD _FINISHED ;
stmt . params . id = json . id ;
stmt . execute ( ) ;
stmt . finalize ( ) ;
let download = dm . getDownload ( json . id ) ;
2010-11-16 15:38:47 -08:00
# ifdef ANDROID
// since our content process doesn't have write permissions to the
// downloads dir, we save it to the tmp dir and then move it here
let dlFile = download . targetFile ;
if ( ! dlFile . exists ( ) )
2010-12-27 18:50:30 -08:00
dlFile . create ( file . NORMAL _FILE _TYPE , 0666 ) ;
2011-01-10 21:56:29 -08:00
let tmpDir = Cc [ "@mozilla.org/file/directory_service;1" ] . getService ( Ci . nsIProperties ) . get ( "TmpD" , Ci . nsIFile ) ;
2010-11-16 15:38:47 -08:00
let tmpFile = tmpDir . clone ( ) ;
tmpFile . append ( dlFile . leafName ) ;
// we sometimes race with the content process, so make sure its finished
// creating/writing the file
while ( ! tmpFile . exists ( ) ) ;
tmpFile . moveTo ( dlFile . parent , dlFile . leafName ) ;
# endif
2010-06-09 19:12:05 -07:00
try {
DownloadsView . downloadCompleted ( download ) ;
let element = DownloadsView . getElementForDownload ( json . id ) ;
element . setAttribute ( "state" , Ci . nsIDownloadManager . DOWNLOAD _FINISHED ) ;
element . setAttribute ( "endTime" , Date . now ( ) ) ;
element . setAttribute ( "referrer" , json . referrer ) ;
DownloadsView . _updateTime ( element ) ;
DownloadsView . _updateStatus ( element ) ;
}
catch ( e ) { }
2010-07-13 07:36:09 -07:00
Services . obs . notifyObservers ( download , "dl-done" , null ) ;
2010-06-09 19:12:05 -07:00
break ;
2010-06-21 13:16:37 -07:00
2010-06-29 11:15:07 -07:00
case "Browser:OpenURI" :
2010-11-22 12:08:49 -08:00
let referrerURI = null ;
if ( json . referrer )
referrerURI = Services . io . newURI ( json . referrer , null , null ) ;
Browser . addTab ( json . uri , json . bringFront , Browser . selectedTab , { referrerURI : referrerURI } ) ;
2010-09-22 16:27:11 -07:00
break ;
2010-06-08 08:22:20 -07:00
}
} ,
2008-05-02 13:15:45 -07:00
supportsCommand : function ( cmd ) {
var isSupported = false ;
switch ( cmd ) {
case "cmd_back" :
case "cmd_forward" :
case "cmd_reload" :
2010-02-16 15:38:37 -08:00
case "cmd_forceReload" :
2008-05-02 13:15:45 -07:00
case "cmd_stop" :
case "cmd_go" :
2008-11-12 18:40:57 -08:00
case "cmd_openLocation" :
2008-05-02 13:15:45 -07:00
case "cmd_star" :
2010-08-26 16:09:56 -07:00
case "cmd_opensearch" :
2008-05-02 13:15:45 -07:00
case "cmd_bookmarks" :
2010-08-24 01:42:28 -07:00
case "cmd_history" :
case "cmd_remoteTabs" :
2009-03-19 16:20:32 -07:00
case "cmd_quit" :
case "cmd_close" :
2008-07-25 11:10:35 -07:00
case "cmd_menu" :
2011-08-05 16:01:33 -07:00
case "cmd_showTabs" :
2008-07-25 11:10:35 -07:00
case "cmd_newTab" :
case "cmd_closeTab" :
2010-07-18 22:27:52 -07:00
case "cmd_undoCloseTab" :
2008-08-19 19:18:36 -07:00
case "cmd_actions" :
2008-09-10 17:38:02 -07:00
case "cmd_panel" :
2008-09-06 23:54:06 -07:00
case "cmd_sanitize" :
2008-10-09 21:11:27 -07:00
case "cmd_zoomin" :
case "cmd_zoomout" :
2010-04-30 12:30:28 -07:00
case "cmd_volumeLeft" :
case "cmd_volumeRight" :
2010-04-12 07:58:38 -07:00
case "cmd_lockscreen" :
2008-05-02 13:15:45 -07:00
isSupported = true ;
break ;
default :
isSupported = false ;
break ;
}
return isSupported ;
} ,
isCommandEnabled : function ( cmd ) {
2011-05-06 09:49:56 -07:00
// disable all commands during the first-run sidebar discovery
let broadcaster = document . getElementById ( "bcast_uidiscovery" ) ;
if ( broadcaster && broadcaster . getAttribute ( "mode" ) == "discovery" )
return false ;
2010-09-23 02:00:00 -07:00
let elem = document . getElementById ( cmd ) ;
2011-05-06 09:49:56 -07:00
if ( elem && elem . getAttribute ( "disabled" ) == "true" )
2010-09-23 02:00:00 -07:00
return false ;
2008-05-02 13:15:45 -07:00
return true ;
} ,
doCommand : function ( cmd ) {
2010-09-23 02:00:00 -07:00
if ( ! this . isCommandEnabled ( cmd ) )
return ;
2010-08-24 01:42:28 -07:00
let browser = getBrowser ( ) ;
2008-05-02 13:15:45 -07:00
switch ( cmd ) {
case "cmd_back" :
browser . goBack ( ) ;
break ;
case "cmd_forward" :
browser . goForward ( ) ;
break ;
case "cmd_reload" :
browser . reload ( ) ;
break ;
2010-02-16 15:38:37 -08:00
case "cmd_forceReload" :
{
2010-04-30 13:53:25 -07:00
// Simulate a new page
2010-06-04 06:15:13 -07:00
browser . lastLocation = null ;
2010-04-30 13:53:25 -07:00
2010-02-16 15:38:37 -08:00
const reloadFlags = Ci . nsIWebNavigation . LOAD _FLAGS _BYPASS _PROXY |
Ci . nsIWebNavigation . LOAD _FLAGS _BYPASS _CACHE ;
browser . reloadWithFlags ( reloadFlags ) ;
break ;
}
2008-05-02 13:15:45 -07:00
case "cmd_stop" :
browser . stop ( ) ;
break ;
case "cmd_go" :
this . goToURI ( ) ;
break ;
2008-11-12 18:40:57 -08:00
case "cmd_openLocation" :
2010-10-14 06:15:29 -07:00
this . showAutoComplete ( ) ;
2008-11-12 18:40:57 -08:00
break ;
2008-05-02 13:15:45 -07:00
case "cmd_star" :
{
2010-12-30 09:58:31 -08:00
BookmarkPopup . toggle ( ) ;
2011-08-05 16:01:33 -07:00
this . _setStar ( true ) ;
2009-08-17 14:05:06 -07:00
2010-12-30 09:58:31 -08:00
let bookmarkURI = browser . currentURI ;
PlacesUtils . asyncGetBookmarkIds ( bookmarkURI , function ( aItemIds ) {
if ( ! aItemIds . length ) {
let bookmarkTitle = browser . contentTitle || bookmarkURI . spec ;
try {
let bookmarkService = PlacesUtils . bookmarks ;
let bookmarkId = bookmarkService . insertBookmark ( BookmarkList . panel . mobileRoot , bookmarkURI ,
bookmarkService . DEFAULT _INDEX ,
bookmarkTitle ) ;
} catch ( e ) {
// Insert failed; reset the star state.
this . updateStar ( ) ;
}
// XXX Used for browser-chrome tests
let event = document . createEvent ( "Events" ) ;
event . initEvent ( "BookmarkCreated" , true , false ) ;
window . dispatchEvent ( event ) ;
}
} , this ) ;
2008-05-02 13:15:45 -07:00
break ;
}
2010-08-26 16:09:56 -07:00
case "cmd_opensearch" :
2010-10-19 17:51:12 -07:00
this . blurFocusedElement ( ) ;
2010-10-19 15:54:45 -07:00
BrowserSearch . toggle ( ) ;
2010-08-26 16:09:56 -07:00
break ;
2008-05-02 13:15:45 -07:00
case "cmd_bookmarks" :
2011-09-01 10:04:29 -07:00
AwesomeScreen . activePanel = BookmarkList ;
2010-08-24 01:42:28 -07:00
break ;
case "cmd_history" :
2011-09-01 10:04:29 -07:00
AwesomeScreen . activePanel = HistoryList ;
2010-08-24 01:42:28 -07:00
break ;
case "cmd_remoteTabs" :
2011-01-27 11:05:01 -08:00
if ( Weave . Status . checkSetup ( ) == Weave . CLIENT _NOT _CONFIGURED ) {
2011-08-19 09:29:23 -07:00
// We have to set activePanel before showing sync's dialog
// to make the sure the dialog stacking is correct.
2011-09-01 10:04:29 -07:00
AwesomeScreen . activePanel = RemoteTabsList ;
2011-01-27 11:05:01 -08:00
WeaveGlue . open ( ) ;
2011-08-19 09:29:23 -07:00
} else {
2011-09-01 10:04:29 -07:00
AwesomeScreen . activePanel = RemoteTabsList ;
2011-01-27 11:05:01 -08:00
}
2011-05-17 02:55:05 -07:00
2008-05-02 13:15:45 -07:00
break ;
2009-03-19 16:20:32 -07:00
case "cmd_quit" :
2011-06-10 14:02:00 -07:00
// Only close one window
this . _closeOrQuit ( ) ;
2009-03-19 16:20:32 -07:00
break ;
case "cmd_close" :
2009-07-08 07:55:07 -07:00
this . _closeOrQuit ( ) ;
2009-03-19 16:20:32 -07:00
break ;
2008-07-25 11:10:35 -07:00
case "cmd_menu" :
2010-11-09 13:42:16 -08:00
AppMenu . toggle ( ) ;
2008-07-25 11:10:35 -07:00
break ;
2011-08-05 16:01:33 -07:00
case "cmd_showTabs" :
TabsPopup . toggle ( ) ;
break ;
2008-07-25 11:10:35 -07:00
case "cmd_newTab" :
this . newTab ( ) ;
break ;
case "cmd_closeTab" :
2008-11-21 21:12:25 -08:00
this . closeTab ( ) ;
2008-08-19 19:18:36 -07:00
break ;
2010-07-18 22:27:52 -07:00
case "cmd_undoCloseTab" :
this . undoCloseTab ( ) ;
break ;
2008-09-06 23:54:06 -07:00
case "cmd_sanitize" :
2009-05-20 10:31:53 -07:00
{
2011-01-03 12:07:41 -08:00
let title = Strings . browser . GetStringFromName ( "clearPrivateData.title" ) ;
let message = Strings . browser . GetStringFromName ( "clearPrivateData.message" ) ;
2010-12-21 09:42:04 -08:00
let clear = Services . prompt . confirm ( window , title , message ) ;
if ( clear ) {
// disable the button temporarily to indicate something happened
let button = document . getElementById ( "prefs-clear-data" ) ;
button . disabled = true ;
setTimeout ( function ( ) { button . disabled = false ; } , 5000 ) ;
Sanitizer . sanitize ( ) ;
}
2008-08-19 19:18:36 -07:00
break ;
2009-05-20 10:31:53 -07:00
}
2008-09-10 17:38:02 -07:00
case "cmd_panel" :
2011-02-08 14:46:24 -08:00
if ( this . isPanelVisible ( ) )
2009-04-17 07:41:44 -07:00
this . hidePanel ( ) ;
2009-12-17 14:35:48 -08:00
else
this . showPanel ( ) ;
2008-09-10 12:30:52 -07:00
break ;
2008-10-09 21:11:27 -07:00
case "cmd_zoomin" :
2009-11-24 11:44:05 -08:00
Browser . zoom ( - 1 ) ;
2008-10-09 21:11:27 -07:00
break ;
case "cmd_zoomout" :
2009-11-24 11:44:05 -08:00
Browser . zoom ( 1 ) ;
2008-10-09 21:11:27 -07:00
break ;
2010-04-30 12:30:28 -07:00
case "cmd_volumeLeft" :
// Zoom in (portrait) or out (landscape)
Browser . zoom ( Util . isPortrait ( ) ? - 1 : 1 ) ;
break ;
case "cmd_volumeRight" :
// Zoom out (portrait) or in (landscape)
Browser . zoom ( Util . isPortrait ( ) ? 1 : - 1 ) ;
break ;
2010-04-12 07:58:38 -07:00
case "cmd_lockscreen" :
{
2010-07-13 07:36:09 -07:00
let locked = Services . prefs . getBoolPref ( "toolkit.screen.lock" ) ;
Services . prefs . setBoolPref ( "toolkit.screen.lock" , ! locked ) ;
2010-05-06 12:16:58 -07:00
2011-01-03 12:07:41 -08:00
let strings = Strings . browser ;
2011-01-27 10:54:36 -08:00
let alerts = Cc [ "@mozilla.org/toaster-alerts-service;1" ] . getService ( Ci . nsIAlertsService ) ;
2011-01-03 12:07:41 -08:00
alerts . showAlertNotification ( null , strings . GetStringFromName ( "alertLockScreen" ) ,
strings . GetStringFromName ( "alertLockScreen." + ( ! locked ? "locked" : "unlocked" ) ) , false , "" , null ) ;
2010-04-12 07:58:38 -07:00
break ;
}
2008-05-02 13:15:45 -07:00
}
}
} ;