Firefox OS启动过程分析-system应用启动

Firefox OS的system应用启动是通过加载一个内部资源页

chrome://b2g/content/shell.html

来实现的,我们先看下这个文件的部分内容:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      id="shell"
      windowtype="navigator:browser"
      >

<head>
  <link rel="stylesheet" href="shell.css" type="text/css">
<script type="application/javascript;version=1.8"
          src="chrome://b2g/content/settings.js"> </script>
  <script type="application/javascript;version=1.8"
          src="chrome://b2g/content/shell.js"> </script>

  <!-- this file is only loaded on Gonk to manage ADB state -->
  <script type="application/javascript;version=1.8"
          src="chrome://b2g/content/devtools/adb.js"> </script>

  <!-- manages DevTools server state -->
  <script type="application/javascript;version=1.8"
          src="chrome://b2g/content/devtools/debugger.js"> </script>
</head>
  <body id="container">
  <!-- The html:iframe containing the UI is created here. -->
  </body>
</html>

该html加载时,主要加载以及运行两个js文件:

<script type="application/javascript;version=1.8"
          src="chrome://b2g/content/settings.js"> </script>
<script type="application/javascript;version=1.8"
          src="chrome://b2g/content/shell.js"> </script>

shell.js中:

bootstrap: function() {
  //...   
  this.start();
},

start: function shell_start() {
  // ...
    let homeURL = this.homeURL;
    if (!homeURL) {
      let msg = 'Fatal error during startup: No homescreen found: try setting B2G_HOMESCREEN';
      alert(msg);
      return;
    }
    let manifestURL = this.manifestURL;
    // <html:iframe id="systemapp"
    //              mozbrowser="true" allowfullscreen="true"
    //              style="overflow: hidden; height: 100%; width: 100%; border: none;"
    //              src="data:text/html;charset=utf-8,%3C!DOCTYPE html>%3Cbody style='background:black;'>"/>
    let systemAppFrame =
      document.createElementNS('http://www.w3.org/1999/xhtml', 'html:iframe');
    systemAppFrame.setAttribute('id', 'systemapp');
    systemAppFrame.setAttribute('mozbrowser', 'true');
    systemAppFrame.setAttribute('mozapp', manifestURL);
    systemAppFrame.setAttribute('allowfullscreen', 'true');
    systemAppFrame.setAttribute('src', 'blank.html');
    let container = document.getElementById('container');
#ifdef MOZ_WIDGET_COCOA
    // See shell.html
    let hotfix = document.getElementById('placeholder');
    if (hotfix) {
      container.removeChild(hotfix);
    }
#endif
    this.contentBrowser = container.appendChild(systemAppFrame);

    systemAppFrame.contentWindow
                  .QueryInterface(Ci.nsIInterfaceRequestor)
                  .getInterface(Ci.nsIWebNavigation)
                  .sessionHistory = Cc["@mozilla.org/browser/shistory;1"]
                                      .createInstance(Ci.nsISHistory);

    this.allowedAudioChannels = new Map();
    let audioChannels = systemAppFrame.allowedAudioChannels;
    audioChannels && audioChannels.forEach(function(audioChannel) {
      this.allowedAudioChannels.set(audioChannel.name, audioChannel);
      audioChannel.addEventListener('activestatechanged', this);
      // Set all audio channels as unmuted by default
      // because some audio in System app will be played
      // before AudioChannelService[1] is Gaia is loaded.
      // [1]: https://github.com/mozilla-b2g/gaia/blob/master/apps/system/js/audio_channel_service.js
      audioChannel.setMuted(false);
    }.bind(this));

    // On firefox mulet, shell.html is loaded in a tab
    // and we have to listen on the chrome event handler
    // to catch key events
    let chromeEventHandler = window.QueryInterface(Ci.nsIInterfaceRequestor)
                                   .getInterface(Ci.nsIWebNavigation)
                                   .QueryInterface(Ci.nsIDocShell)
                                   .chromeEventHandler || window;
    // Capture all key events so we can filter out hardware buttons
    // And send them to Gaia via mozChromeEvents.
    // Ideally, hardware buttons wouldn't generate key events at all, or
    // if they did, they would use keycodes that conform to DOM 3 Events.
    // See discussion in https://bugzilla.mozilla.org/show_bug.cgi?id=762362
    chromeEventHandler.addEventListener('keydown', this, true);
    chromeEventHandler.addEventListener('keyup', this, true);

    window.addEventListener('MozApplicationManifest', this);
    window.addEventListener('MozAfterPaint', this);
    window.addEventListener('sizemodechange', this);
    window.addEventListener('unload', this);
    this.contentBrowser.addEventListener('mozbrowserloadstart', this, true);
    this.contentBrowser.addEventListener('mozbrowserselectionstatechanged', this, true);
    this.contentBrowser.addEventListener('mozbrowserscrollviewchange', this, true);
    this.contentBrowser.addEventListener('mozbrowsercaretstatechanged', this);

    CustomEventManager.init();
    WebappsHelper.init();
    UserAgentOverrides.init();
    CaptivePortalLoginHelper.init();

    this.contentBrowser.src = homeURL;

},

几个问题:

  1. homeURL(b2g.system_startup_url):

     app://system.gaiamobile.org/index.html
    
     "app:// for packaged app resources"
  2. manifestURL:

    app://system.gaiamobile.org/manifest.webapp
    
    "app:// for packaged app resources"
  3. system app实际上是一个iframe
    let systemAppFrame =
      document.createElementNS('http://www.w3.org/1999/xhtml', 'html:iframe');

该iframe是放在shell.html中id为”container”中:

 let container = document.getElementById('container');
 ...
 this.contentBrowser = container.appendChild(systemAppFrame);
 ...

4.systemapp的iframe实际上有一系列属性:

    systemAppFrame.setAttribute('id', 'systemapp');
    systemAppFrame.setAttribute('mozbrowser', 'true');
    systemAppFrame.setAttribute('mozapp', manifestURL);
    systemAppFrame.setAttribute('allowfullscreen', 'true');
    systemAppFrame.setAttribute('src', 'blank.html');

system app的iframe里放的是 ‘blank.html’。其它属性:

    mozbrowser: 
        An "iframe" is turned into a browser frame by setting the mozbrowser attribute

    mozapp:
        see [mozapp](https://wiki.mozilla.org/Security/Reviews/B2G/mozapp)

system app刚开始加载的是

'blank.html'

也就是没有界面的,之后开始加载

homeURL,即"app://system.gaiamobile.org/index.html"

“system.gaiamobile.org”,实际上就是gaia里的system app。该app负责系统ui的显示以及home应用的加载等工作。

问题是:

b2g进程、system进程与Home进程的关系是什么样的? Home进程是如何创建出来的?

我们下节再说。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值