summaryrefslogtreecommitdiffstats
path: root/b2g/chrome/content/shell_remote.js
blob: 1f1115ef0a02050c1bde06b84a5aac2d2c1280a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* 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/. */

"use strict";

var {classes: Cc, interfaces: Ci, utils: Cu} = Components;

Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/SystemAppProxy.jsm");

function debug(aStr) {
  // dump(" -*- ShellRemote.js: " + aStr + "\n");
}

var remoteShell = {

  _started: false,

  get homeURL() {
    let systemAppManifestURL = Services.io.newURI(this.systemAppManifestURL, null, null);
    let shellRemoteURL = Services.prefs.getCharPref("b2g.multiscreen.system_remote_url");
    shellRemoteURL = Services.io.newURI(shellRemoteURL, null, systemAppManifestURL);
    return shellRemoteURL.spec;
  },

  get systemAppManifestURL() {
    return Services.prefs.getCharPref("b2g.system_manifest_url");
  },

  hasStarted: function () {
    return this._started;
  },

  start: function () {
    this._started = true;
    this._isEventListenerReady = false;
    this.id = window.location.hash.substring(1);

    let homeURL = this.homeURL;
    if (!homeURL) {
      debug("ERROR! Remote home URL undefined.");
      return;
    }
    let manifestURL = this.systemAppManifestURL;
    // <html:iframe id="this.id"
    //              mozbrowser="true"
    //              allowfullscreen="true"
    //              src="blank.html"/>
    let systemAppFrame =
      document.createElementNS("http://www.w3.org/1999/xhtml", "html:iframe");
    systemAppFrame.setAttribute("id", this.id);
    systemAppFrame.setAttribute("mozbrowser", "true");
    systemAppFrame.setAttribute("mozapp", manifestURL);
    systemAppFrame.setAttribute("allowfullscreen", "true");
    systemAppFrame.setAttribute("src", "blank.html");

    let container = document.getElementById("container");
    this.contentBrowser = container.appendChild(systemAppFrame);
    this.contentBrowser.src = homeURL + window.location.hash;

    window.addEventListener("unload", this);
    this.contentBrowser.addEventListener("mozbrowserloadstart", this);
  },

  stop: function () {
    window.removeEventListener("unload", this);
    this.contentBrowser.removeEventListener("mozbrowserloadstart", this);
    this.contentBrowser.removeEventListener("mozbrowserlocationchange", this, true);
    SystemAppProxy.unregisterFrameWithId(this.id);
  },

  notifyContentStart: function(evt) {
    this.contentBrowser.removeEventListener("mozbrowserloadstart", this);
    this.contentBrowser.removeEventListener("mozbrowserlocationchange", this, true);

    SystemAppProxy.registerFrameWithId(remoteShell.id, remoteShell.contentBrowser);
    SystemAppProxy.addEventListenerWithId(this.id, "mozContentEvent", this);

    let content = this.contentBrowser.contentWindow;
    content.addEventListener("load", this, true);
  },

  notifyContentWindowLoaded: function () {
    SystemAppProxy.setIsLoadedWithId(this.id);
  },

  notifyEventListenerReady: function () {
    if (this._isEventListenerReady) {
      Cu.reportError("shell_remote.js: SystemApp has already been declared as being ready.");
      return;
    }
    this._isEventListenerReady = true;
    SystemAppProxy.setIsReadyWithId(this.id);
  },

  handleEvent: function(evt) {
    debug("Got an event: " + evt.type);
    let content = this.contentBrowser.contentWindow;

    switch(evt.type) {
      case "mozContentEvent":
        if (evt.detail.type === "system-message-listener-ready") {
          this.notifyEventListenerReady();
        }
        break;
      case "load":
        if (content.document.location == "about:blank") {
          return;
        }
        content.removeEventListener("load", this, true);
        this.notifyContentWindowLoaded();
        break;
      case "mozbrowserloadstart":
        if (content.document.location == "about:blank") {
          this.contentBrowser.addEventListener("mozbrowserlocationchange", this, true);
          return;
        }
      case "mozbrowserlocationchange":
        if (content.document.location == "about:blank") {
          return;
        }
        this.notifyContentStart();
        break;
      case "unload":
        this.stop();
        break;
    }
  }
};

window.onload = function() {
  if (remoteShell.hasStarted() == false) {
    remoteShell.start();
  }
};