<?xml version="1.0"?>

<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<bindings id="firefoxBrowserBindings"
          xmlns="http://www.mozilla.org/xbl"
          xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <binding id="remote-browser" extends="chrome://global/content/bindings/browser.xml#browser">

    <implementation type="application/javascript"
                    implements="nsIObserver, nsIDOMEventListener, nsIMessageListener, nsIRemoteBrowser">

      <field name="_securityUI">null</field>

      <property name="securityUI"
                readonly="true">
        <getter><![CDATA[
          if (!this._securityUI) {
            // Don't attempt to create the remote web progress if the
            // messageManager has already gone away
            if (!this.messageManager)
              return null;

            let jsm = "resource://gre/modules/RemoteSecurityUI.jsm";
            let RemoteSecurityUI = Components.utils.import(jsm, {}).RemoteSecurityUI;
            this._securityUI = new RemoteSecurityUI();
          }

          // We want to double-wrap the JS implemented interface, so that QI and instanceof works.
          var ptr = Cc["@mozilla.org/supports-interface-pointer;1"].
                        createInstance(Ci.nsISupportsInterfacePointer);
          ptr.data = this._securityUI;
          return ptr.data.QueryInterface(Ci.nsISecureBrowserUI);
        ]]></getter>
      </property>

      <method name="adjustPriority">
        <parameter name="adjustment"/>
        <body><![CDATA[
          this.messageManager.sendAsyncMessage("NetworkPrioritizer:AdjustPriority",
                                               {adjustment: adjustment});
        ]]></body>
      </method>

      <field name="_controller">null</field>

      <field name="_remoteWebNavigation">null</field>

      <property name="webNavigation"
                onget="return this._remoteWebNavigation;"
                readonly="true"/>

      <field name="_remoteWebProgress">null</field>

      <property name="webProgress" readonly="true">
      	<getter>
      	  <![CDATA[
            if (!this._remoteWebProgress) {
              // Don't attempt to create the remote web progress if the
              // messageManager has already gone away
              if (!this.messageManager)
                return null;

              let jsm = "resource://gre/modules/RemoteWebProgress.jsm";
              let { RemoteWebProgressManager } = Cu.import(jsm, {});
              this._remoteWebProgressManager = new RemoteWebProgressManager(this);
              this._remoteWebProgress = this._remoteWebProgressManager.topLevelWebProgress;
            }
            return this._remoteWebProgress;
      	  ]]>
      	</getter>
      </property>

      <field name="_remoteFinder">null</field>

      <property name="finder" readonly="true">
        <getter><![CDATA[
          if (!this._remoteFinder) {
            // Don't attempt to create the remote web progress if the
            // messageManager has already gone away
            if (!this.messageManager)
              return null;

            let jsm = "resource://gre/modules/RemoteFinder.jsm";
            let { RemoteFinder } = Cu.import(jsm, {});
            this._remoteFinder = new RemoteFinder(this);
          }
          return this._remoteFinder;
        ]]></getter>
      </property>

      <field name="_documentURI">null</field>

      <field name="_documentContentType">null</field>

      <!--
        Used by session restore to ensure that currentURI is set so
        that switch-to-tab works before the tab is fully
        restored. This function also invokes onLocationChanged
        listeners in tabbrowser.xml.
      -->
      <method name="_setCurrentURI">
        <parameter name="aURI"/>
        <body><![CDATA[
          this._remoteWebProgressManager.setCurrentURI(aURI);
        ]]></body>
      </method>

      <property name="documentURI"
                onget="return this._documentURI;"
                readonly="true"/>

      <property name="documentContentType"
                onget="return this._documentContentType;"
                readonly="true"/>

      <field name="_contentTitle">""</field>

      <property name="contentTitle"
                onget="return this._contentTitle"
                readonly="true"/>

      <field name="_characterSet">""</field>

      <property name="characterSet"
                onget="return this._characterSet">
        <setter><![CDATA[
          this.messageManager.sendAsyncMessage("UpdateCharacterSet", {value: val});
          this._characterSet = val;
        ]]></setter>
      </property>

      <field name="_mayEnableCharacterEncodingMenu">null</field>

      <property name="mayEnableCharacterEncodingMenu"
                onget="return this._mayEnableCharacterEncodingMenu;"
                readonly="true"/>

      <field name="_contentWindow">null</field>

      <property name="contentWindow"
                onget="return null"
                readonly="true"/>

      <property name="contentWindowAsCPOW"
                onget="return this._contentWindow"
                readonly="true"/>

      <property name="contentDocument"
                onget="return null"
                readonly="true"/>

      <field name="_contentPrincipal">null</field>

      <property name="contentPrincipal"
                onget="return this._contentPrincipal"
                readonly="true"/>

      <property name="contentDocumentAsCPOW"
                onget="return this.contentWindowAsCPOW ? this.contentWindowAsCPOW.document : null"
                readonly="true"/>

      <field name="_syncHandler">null</field>

      <property name="syncHandler"
                onget="return this._syncHandler"
                readonly="true"/>

      <field name="_imageDocument">null</field>

      <property name="imageDocument"
                onget="return this._imageDocument"
                readonly="true"/>

      <field name="_fullZoom">1</field>
      <property name="fullZoom">
        <getter><![CDATA[
          return this._fullZoom;
        ]]></getter>
        <setter><![CDATA[
          this._fullZoom = val;
          this.messageManager.sendAsyncMessage("FullZoom", {value: val});
        ]]></setter>
      </property>

      <field name="_textZoom">1</field>
      <property name="textZoom">
        <getter><![CDATA[
          return this._textZoom;
        ]]></getter>
        <setter><![CDATA[
          this._textZoom = val;
          this.messageManager.sendAsyncMessage("TextZoom", {value: val});
        ]]></setter>
      </property>

      <field name="_isSyntheticDocument">false</field>
      <property name="isSyntheticDocument">
        <getter><![CDATA[
          return this._isSyntheticDocument;
        ]]></getter>
      </property>

      <property name="autoCompletePopup"
                onget="return document.getElementById(this.getAttribute('autocompletepopup'))"
                readonly="true"/>

      <property name="docShellIsActive">
        <getter>
          <![CDATA[
            throw new Error("not supported");
          ]]>
        </getter>
        <setter>
          <![CDATA[
            let {frameLoader} = this.QueryInterface(Ci.nsIFrameLoaderOwner);
            frameLoader.tabParent.setIsDocShellActive(val);
            return val;
          ]]>
        </setter>
      </property>

      <field name="mDestroyed">false</field>

      <constructor>
        <![CDATA[
          /*
           * Don't try to send messages from this function. The message manager for
           * the <browser> element may not be initialized yet.
           */

          let jsm = "resource://gre/modules/RemoteWebNavigation.jsm";
          let RemoteWebNavigation = Cu.import(jsm, {}).RemoteWebNavigation;
          this._remoteWebNavigation = new RemoteWebNavigation(this);

          this.messageManager.addMessageListener("Browser:Init", this);
          this.messageManager.addMessageListener("DOMTitleChanged", this);
          this.messageManager.addMessageListener("ImageDocumentLoaded", this);
          this.messageManager.addMessageListener("SetSyncHandler", this);
          this.messageManager.addMessageListener("DocumentInserted", this);
          this.messageManager.addMessageListener("FullZoomChange", this);
          this.messageManager.addMessageListener("TextZoomChange", this);
          this.messageManager.addMessageListener("ZoomChangeUsingMouseWheel", this);
          this.messageManager.addMessageListener("DOMFullscreen:RequestExit", this);
          this.messageManager.addMessageListener("DOMFullscreen:RequestRollback", this);
          this.messageManager.loadFrameScript("chrome://global/content/browser-child.js", true);

          if (this.hasAttribute("selectmenulist")) {
            this.messageManager.addMessageListener("Forms:ShowDropDown", this);
            this.messageManager.addMessageListener("Forms:HideDropDown", this);
            this.messageManager.loadFrameScript("chrome://global/content/select-child.js", true);
          }

          jsm = "resource://gre/modules/RemoteController.jsm";
          let RemoteController = Components.utils.import(jsm, {}).RemoteController;
          this._controller = new RemoteController(this);
          this.controllers.appendController(this._controller);

          Services.obs.addObserver(this, "ask-children-to-exit-fullscreen", false);
        ]]>
      </constructor>

      <destructor>
        <![CDATA[
          this.destroy();
        ]]>
      </destructor>

      <!-- This is necessary because the destructor doesn't always get called when
           we are removed from a tabbrowser. This will be explicitly called by tabbrowser -->
      <method name="destroy">
        <body><![CDATA[
          if (this.mDestroyed)
            return;
          this.mDestroyed = true;

          this.controllers.removeController(this._controller);

          Services.obs.removeObserver(this, "ask-children-to-exit-fullscreen");
        ]]></body>
      </method>

      <method name="receiveMessage">
        <parameter name="aMessage"/>
        <body><![CDATA[
          let data = aMessage.data;
          switch (aMessage.name) {
            case "Browser:Init":
              let result = {};
              result.useGlobalHistory = !this.hasAttribute("disableglobalhistory");
              result.initPopup = this.autoCompletePopup != null;
              return result;
              break;
            case "DOMTitleChanged":
              this._contentTitle = data.title;
              break;
            case "ImageDocumentLoaded":
              this._imageDocument = {
                width: data.width,
                height: data.height
              };
              break;

            case "SetSyncHandler":
              this._syncHandler = aMessage.objects.handler;
              break;

            case "Forms:ShowDropDown": {
              Cu.import("resource://gre/modules/SelectParentHelper.jsm");
              let menulist = document.getElementById(this.getAttribute("selectmenulist"));
              SelectParentHelper.populate(menulist, data.options, data.selectedIndex);
              SelectParentHelper.open(this, menulist, data.rect);
              break;
            }

            case "DocumentInserted":
              this._isSyntheticDocument = data.synthetic;
              break;

            case "FullZoomChange": {
              this._fullZoom = data.value;
              let event = document.createEvent("Events");
              event.initEvent("FullZoomChange", true, false);
              this.dispatchEvent(event);
              break;
            }

            case "TextZoomChange": {
              this._textZoom = data.value;
              let event = document.createEvent("Events");
              event.initEvent("TextZoomChange", true, false);
              this.dispatchEvent(event);
              break;
            }

            case "ZoomChangeUsingMouseWheel": {
              let event = document.createEvent("Events");
              event.initEvent("ZoomChangeUsingMouseWheel", true, false);
              this.dispatchEvent(event);
              break;
            }

            case "Forms:HideDropDown": {
              Cu.import("resource://gre/modules/SelectParentHelper.jsm");
              let menulist = document.getElementById(this.getAttribute("selectmenulist"));
              SelectParentHelper.hide(menulist);
              break;
            }

            case "DOMFullscreen:RequestExit": {
              let windowUtils = window.QueryInterface(Ci.nsIInterfaceRequestor)
                                      .getInterface(Ci.nsIDOMWindowUtils);
              windowUtils.exitFullscreen();
              break;
            }

            case "DOMFullscreen:RequestRollback": {
              let windowUtils = window.QueryInterface(Ci.nsIInterfaceRequestor)
                                      .getInterface(Ci.nsIDOMWindowUtils);
              windowUtils.remoteFrameFullscreenReverted();
              break;
            }

            default:
              // Delegate to browser.xml.
              return this._receiveMessage(aMessage);
              break;
          }
        ]]></body>
      </method>

      <method name="observe">
        <parameter name="aSubject"/>
        <parameter name="aTopic"/>
        <parameter name="aData"/>
        <body><![CDATA[
          if (aTopic == "ask-children-to-exit-fullscreen") {
            if (aSubject == window.document) {
              this.messageManager.sendAsyncMessage("DOMFullscreen:ChildrenMustExit");
            }
          }
        ]]></body>
      </method>

      <!--
        For out-of-process code, event.screen[XY] is relative to the
        left/top of the content view. For in-process code,
        event.screen[XY] is relative to the left/top of the screen. We
        use this method to map screen coordinates received from a
        (possibly out-of-process) <browser> element to coordinates
        that are relative to the screen. This code handles the
        out-of-process case, where we need to translate by the screen
        position of the <browser> element.
      -->
      <method name="mapScreenCoordinatesFromContent">
        <parameter name="aScreenX"/>
        <parameter name="aScreenY"/>
        <body>
        <![CDATA[
          return { x: aScreenX + this.boxObject.screenX,
                   y: aScreenY + this.boxObject.screenY };
        ]]>
        </body>
      </method>

      <method name="enableDisableCommands">
        <parameter name="aAction"/>
        <parameter name="aEnabledLength"/>
        <parameter name="aEnabledCommands"/>
        <parameter name="aDisabledLength"/>
        <parameter name="aDisabledCommands"/>
        <body>
          if (this._controller) {
            this._controller.enableDisableCommands(aAction,
                                                   aEnabledLength, aEnabledCommands,
                                                   aDisabledLength, aDisabledCommands);
          }
        </body>
      </method>

    </implementation>

  </binding>

</bindings>
