Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #16593 from dwi2/bug971498
Browse files Browse the repository at this point in the history
Bug 971498 - [System2] Instantiable HomescreenLauncher
  • Loading branch information
dwi2 committed Feb 27, 2014
2 parents feae3a1 + 322b82d commit 5b4c4ee
Show file tree
Hide file tree
Showing 17 changed files with 392 additions and 204 deletions.
6 changes: 3 additions & 3 deletions apps/system/js/app_window_factory.js
Expand Up @@ -102,10 +102,10 @@
var app = AppWindowManager.getApp(config.origin);
if (app) {
app.reviveBrowser();
} else if (config.origin !== HomescreenLauncher.origin) {
} else if (config.origin !== homescreenLauncher.origin) {
new AppWindow(config);
} else if (config.origin == HomescreenLauncher.origin) {
HomescreenLauncher.getHomescreen().ensure();
} else if (config.origin == homescreenLauncher.origin) {
homescreenLauncher.getHomescreen().ensure();
}
this.publish('launchapp', config);
},
Expand Down
20 changes: 10 additions & 10 deletions apps/system/js/app_window_manager.js
Expand Up @@ -76,7 +76,7 @@
display: function awm_display(newApp, openAnimation, closeAnimation) {
this._dumpAllWindows();
var appCurrent = this._activeApp, appNext = newApp ||
HomescreenLauncher.getHomescreen();
homescreenLauncher.getHomescreen();

if (!appNext) {
console.warn('no next app.');
Expand All @@ -93,7 +93,7 @@

// XXX: Do this in HomescreenWindow.
if (appCurrent === null) {
HomescreenLauncher.getHomescreen().setVisible(false);
homescreenLauncher.getHomescreen().setVisible(false);
} else if (appCurrent.instanceID == appNext.instanceID) {
// Do nothing.
console.warn('the app has been displayed.');
Expand Down Expand Up @@ -150,7 +150,7 @@
}
this.debug('ready to open/close' + switching);
if (switching)
HomescreenLauncher.getHomescreen().fadeOut();
homescreenLauncher.getHomescreen().fadeOut();
this._updateActiveApp(appNext.instanceID);

var immediateTranstion = false;
Expand Down Expand Up @@ -350,7 +350,7 @@
var detail = evt.detail;

if (activeApp &&
activeApp.origin !== HomescreenLauncher.origin) {
activeApp.origin !== homescreenLauncher.origin) {
// This is coming from attention screen.
// If attention screen has the same origin as our active app,
// we cannot turn off its page visibility
Expand All @@ -363,16 +363,16 @@
}
activeApp.setVisible(false);
} else {
var home = HomescreenLauncher.getHomescreen();
var home = homescreenLauncher.getHomescreen();
home && home.setVisible(false);
}
break;

case 'showwindow':
if (activeApp && activeApp.origin !== HomescreenLauncher.origin) {
if (activeApp && activeApp.origin !== homescreenLauncher.origin) {
activeApp.setVisible(true);
} else {
var home = HomescreenLauncher.getHomescreen();
var home = homescreenLauncher.getHomescreen();
home && home.setVisible(true);
}
break;
Expand Down Expand Up @@ -406,7 +406,7 @@
// be included in index.html before this one, so they can register their
// event handlers before we do.
case 'home':
if (!HomescreenLauncher.ready)
if (!homescreenLauncher.ready)
return;

if (activeApp && !activeApp.isHomescreen) {
Expand All @@ -420,7 +420,7 @@
} else {
// dispatch event to close activity.
this.debug('ensure home.');
HomescreenLauncher.getHomescreen().ensure(true);
homescreenLauncher.getHomescreen().ensure(true);
}
break;

Expand Down Expand Up @@ -497,7 +497,7 @@
if (config.isActivity && this._activeApp) {
this.linkWindowActivity(config);
}
if (config.origin == HomescreenLauncher.origin) {
if (config.origin == homescreenLauncher.origin) {
this.display();
} else {
this.display(this.getApp(config.origin));
Expand Down
3 changes: 2 additions & 1 deletion apps/system/js/bootstrap.js
Expand Up @@ -27,7 +27,8 @@ window.addEventListener('load', function startup() {
window.removeEventListener('homescreen-ready', onHomescreenReady);
FtuLauncher.retrieve();
});
HomescreenLauncher.init();
/** @global */
window.homescreenLauncher = new HomescreenLauncher().start();
}

if (Applications.ready) {
Expand Down
190 changes: 134 additions & 56 deletions apps/system/js/homescreen_launcher.js
@@ -1,28 +1,67 @@
(function(window) {
var currentManifestURL = '';
var instance = undefined;
var _inited = false;

'use strict';
/* global Applications, SettingsListener, HomescreenWindow, TrustedUIManager */
(function(exports) {
/**
* HomescreenLauncher is responsible to launch the homescreen window
* HomescreenLauncher is responsible for launching the homescreen window
* instance and make sure it's a singleton.
*
* Every extermal modules should use
* <code>HomescreenLauncher.getHomescreen()</code>
* to access the homescreen window instance.
* <code>window.homescreenLauncher.getHomescreen()</code>
* to access the homescreen window instance. Since window.homescreenLauncher
* should be instantiated and started in bootstrap.js
*
* @example
* var home = HomescreenLauncher.getHomescreen();
* home.open(); // Do the open animation.
*
* @module HomescreenLauncher
* @class HomescreenLauncher
* @requires module:TrustedUIManager
* @requires module:Applications
* @requires module:SettingsListener
* @requires HomescreenWindow
* @requires Applications
*/
var HomescreenLauncher = function() {
};

/**
* Fired when homescreen launcher detect 'homescreen.manifestURL' changed
* @event HomescreenLauncher#homescreen-changed
*/
/**
* Fired when homescreen launcher is done retriving 'homescreen.manifestURL'
* @event HomescreenLauncher#homescreen-ready
*/
var HomescreenLauncher = {
ready: false,

HomescreenLauncher.prototype = {
_currentManifestURL: '',

_instance: undefined,

_started: false,

_ready: false,

/**
* Homescreen launcher is ready or not. Homescreen launcher is ready
* only when it is done retrieving 'homescreen.manifestURL'
* from settings DB.
*
* @access public
* @memberOf HomescreenLauncher.prototype
* @type {boolean}
*/
get ready() {
return this._ready;
},

/**
* Origin of homescreen.
*
* @access public
* @memberOf HomescreenLauncher.prototype
* @type {string}
*/
get origin() {
// We don't really care the origin of homescreen,
// and it may change when we swap the homescreen app.
Expand All @@ -33,34 +72,89 @@
return 'homescreen';
},

_fetchSettings: function hl_fetchSettings() {
var that = this;
SettingsListener.observe('homescreen.manifestURL', '',
// XXX: After landing of bug 976986, we should write a deregister
// function of onRetrievingHomescreenManifestURL
// see https://bugzilla.mozilla.org/show_bug.cgi?id=976998
function onRetrievingHomescreenManifestURL(value) {
var previousManifestURL = that._currentManifestURL;
that._currentManifestURL = value;
if (typeof(that._instance) !== 'undefined') {
if (previousManifestURL !== '' &&
previousManifestURL !== that._currentManifestURL) {
that._instance.kill();
that._instance = new HomescreenWindow(value);
// Dispatch 'homescreen is changed' event.
window.dispatchEvent(new CustomEvent('homescreen-changed'));
} else {
that._instance.ensure();
}
}
that._ready = true;
window.dispatchEvent(new CustomEvent('homescreen-ready'));
});
},

_onAppReady: function hl_onAppReady() {
window.removeEventListener('applicationready', this._onAppReady);
this._fetchSettings();
},

/**
* Init process
* Start process
* ![Homescreen launch process](http://i.imgur.com/JZ1ibkc.png)
*
* @memberOf module:HomescreenLauncher
* @memberOf HomescreenLauncher.prototype
*/
init: function hl_init() {
if (_inited)
return;

_inited = true;
start: function hl_start() {
if (this._started) {
return this;
}

var self = this;
this._started = true;
if (Applications.ready) {
this.fetchSettings();
this._fetchSettings();
} else {
window.addEventListener('applicationready', function onAppReady() {
window.removeEventListener('applicationready', onAppReady);
self.fetchSettings();
});
window.addEventListener('applicationready',
this._onAppReady.bind(this));
}

window.addEventListener('trusteduishow', this);
window.addEventListener('trusteduihide', this);
window.addEventListener('appopening', this);
window.addEventListener('appopened', this);
return this;
},

/**
* Stop process
*
* @memberOf HomescreenLauncher.prototype
*/
stop: function hl_stop() {
if (typeof(this._instance) !== 'undefined') {
// XXX: After landing of bug 976986, we should move action of
// cleaing _instance into a deregister function of
// onRetrievingHomescreenManifestURL
// see https://bugzilla.mozilla.org/show_bug.cgi?id=976998
this._instance.kill();
this._instance = undefined;
}
this._currentManifestURL = '';
window.removeEventListener('appopening', this);
window.removeEventListener('trusteduihide', this);
window.removeEventListener('trusteduishow', this);
window.removeEventListener('applicationready', this._onAppReady);
this._started = false;
},

/**
* General event handler interface.
*
* @param {DOMEvent} evt The event.
* @type {boolean}
*/
handleEvent: function hl_handleEvent(evt) {
switch (evt.type) {
case 'trusteduishow':
Expand All @@ -86,43 +180,27 @@
}
},

fetchSettings: function hl_fetchSettings() {
var self = this;
SettingsListener.observe('homescreen.manifestURL', '',
function onRetrievingHomescreenManifestURL(value) {
var previousManifestURL = currentManifestURL;
currentManifestURL = value;
if (typeof(instance) !== 'undefined') {
if (previousManifestURL !== '' &&
previousManifestURL !== currentManifestURL) {
instance.kill();
instance = new HomescreenWindow(value);
// Dispatch 'homescreen is changed' event.
window.dispatchEvent(new CustomEvent('homescreen-changed'));
} else {
instance.ensure();
}
}

self.ready = true;
window.dispatchEvent(new CustomEvent('homescreen-ready'));
});
},

/**
* Get instance of homescreen window singleton
*
* @memberOf HomescreenLauncher.prototype
* @returns {HomescreenWindow} Instance of homescreen window singleton, or
* null if HomescreenLauncher is not ready
*/
getHomescreen: function hl_getHomescreen() {
if (currentManifestURL === '') {
if (this._currentManifestURL === '') {
console.warn('HomescreenLauncher: not ready right now.');
return null;
}
if (typeof instance == 'undefined') {
instance = new HomescreenWindow(currentManifestURL);
return instance;
if (typeof this._instance == 'undefined') {
this._instance = new HomescreenWindow(this._currentManifestURL);
return this._instance;
} else {
instance.ensure();
return instance;
this._instance.ensure();
return this._instance;
}
}
};

window.HomescreenLauncher = HomescreenLauncher;
}(this));
exports.HomescreenLauncher = HomescreenLauncher;
}(window));
2 changes: 1 addition & 1 deletion apps/system/js/sheets_transition.js
Expand Up @@ -14,7 +14,7 @@ var SheetsTransition = {
begin: function st_begin(direction) {
// Ask Homescreen App to fade out when sheets begin moving.
// Homescreen App would fade in next time it's opened automatically.
var home = HomescreenLauncher.getHomescreen();
var home = homescreenLauncher.getHomescreen();
home && home.fadeOut();
var currentSheet = StackManager.getCurrent();
var newSheet = (direction == 'ltr') ?
Expand Down
4 changes: 3 additions & 1 deletion apps/system/test/unit/app_window_factory_test.js
Expand Up @@ -2,7 +2,7 @@

mocha.globals(['AppWindowManager', 'Applications',
'ManifestHelper', 'AppWindow', 'System', 'AppWindowFactory',
'BrowserConfigHelper']);
'BrowserConfigHelper', 'homescreenLauncher']);

requireApp('system/shared/test/unit/mocks/mock_manifest_helper.js');
requireApp('system/test/unit/mock_applications.js');
Expand Down Expand Up @@ -102,13 +102,15 @@ suite('system/AppWindowFactory', function() {
MockApplications.mRegisterMockApp(fakeLaunchConfig5);
stubById = this.sinon.stub(document, 'getElementById');
stubById.returns(document.createElement('div'));
window.homescreenLauncher = new HomescreenLauncher().start();

requireApp('system/js/system.js');
requireApp('system/js/browser_config_helper.js');
requireApp('system/js/app_window_factory.js', done);
});

teardown(function() {
window.homescreenLauncher = undefined;
stubById.restore();
});

Expand Down

0 comments on commit 5b4c4ee

Please sign in to comment.