Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[embedlite-components] Implement PromptService.js as a JS Component. …
…JB#49896
  • Loading branch information
rainemak committed May 19, 2020
1 parent fc088b8 commit 71c2284
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 42 deletions.
8 changes: 4 additions & 4 deletions jscomps/EmbedLiteJSComponents.manifest
Expand Up @@ -80,10 +80,10 @@ contract @mozilla.org/embedlite-webapp-installer;1 {62dea3ae-c36f-11e2-aa1d-b337
category app-startup EmbedLiteWebAppInstall service,@mozilla.org/embedlite-webapp-installer;1

# PromptService.js
# component {44df5fae-c5a1-11e2-8e91-1ff32ee4f840} PromptService.js
# contract @mozilla.org/prompter;1 {44df5fae-c5a1-11e2-8e91-1ff32ee4f840}
# contract @mozilla.org/embedcomp/prompt-service;1 {44df5fae-c5a1-11e2-8e91-1ff32ee4f840}
# category wakeup-request PromptService @mozilla.org/embedcomp/prompt-service;1,nsIPromptService,getService,Prompt:Call
component {44df5fae-c5a1-11e2-8e91-1ff32ee4f840} PromptService.js
contract @mozilla.org/prompter;1 {44df5fae-c5a1-11e2-8e91-1ff32ee4f840}
contract @mozilla.org/embedcomp/prompt-service;1 {44df5fae-c5a1-11e2-8e91-1ff32ee4f840}
contract @mozilla.org/network/authprompt-adapter-factory;1 {44df5fae-c5a1-11e2-8e91-1ff32ee4f840}

# PrivateDataManager.js
component {6a7dd2ef-b7c8-4ab5-8c35-c0e5d7557ccf} PrivateDataManager.js
Expand Down
65 changes: 41 additions & 24 deletions jscomps/PromptService.js
Expand Up @@ -9,17 +9,21 @@ const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");

Services.scriptloader.loadSubScript("chrome://embedlite/content/Logger.js");

XPCOMUtils.defineLazyModuleGetter(this, "Prompt",
"resource://gre/modules/Prompt.jsm");
"chrome://embedlite/content/Prompt.jsm");

var gPromptService = null;

function PromptService() {
gPromptService = this;
Logger.debug("JSComp: PromptService.js loaded");
}

PromptService.prototype = {
classID: Components.ID("{9a61149b-2276-4a0a-b79c-be994ad106cf}"),
inModalState: false,
classID: Components.ID("{44df5fae-c5a1-11e2-8e91-1ff32ee4f840}"),

QueryInterface: XPCOMUtils.generateQI([Ci.nsIPromptFactory, Ci.nsIPromptService, Ci.nsIPromptService2]),

Expand Down Expand Up @@ -148,10 +152,17 @@ InternalPrompt.prototype = {
* for a response
*/
showPrompt: function showPrompt(aPrompt) {
if (gPromptService.inModalState) {
return {
"accepted": false
};
}

if (this._domWin) {
PromptUtils.fireDialogEvent(this._domWin, "DOMWillOpenModalDialog");
let winUtils = this._domWin.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
winUtils.enterModalState();
gPromptService.inModalState = true;
}

let retval = null;
Expand All @@ -168,6 +179,7 @@ InternalPrompt.prototype = {
let winUtils = this._domWin.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
winUtils.leaveModalState();
PromptUtils.fireDialogEvent(this._domWin, "DOMModalDialogClosed");
gPromptService.inModalState = false;
}

return retval;
Expand Down Expand Up @@ -220,26 +232,28 @@ InternalPrompt.prototype = {

alertCheck: function alertCheck(aTitle, aText, aCheckMsg, aCheckState) {
let p = this._getPrompt(aTitle, aText, [ PromptUtils.getLocaleString("OK") ]);
p.setHint("alert");
this.addCheckbox(p, aCheckMsg, aCheckState);
let data = this.showPrompt(p);
if (aCheckState && data.button > -1)
aCheckState.value = data.checkbox0;
if (aCheckState)
aCheckState.value = data.checkvalue || false;
},

confirm: function confirm(aTitle, aText) {
let p = this._getPrompt(aTitle, aText);
p.setHint("confirm");
let data = this.showPrompt(p);
return (data.button == 0);
return data.accepted;
},

confirmCheck: function confirmCheck(aTitle, aText, aCheckMsg, aCheckState) {
let p = this._getPrompt(aTitle, aText, null);
p.setHint("confirm");
this.addCheckbox(p, aCheckMsg, aCheckState);
let data = this.showPrompt(p);
let ok = data.button == 0;
if (aCheckState && data.button > -1)
aCheckState.value = data.checkbox0;
let ok = data.accepted;
if (aCheckState)
aCheckState.value = data.checkvalue || false;
return ok;
},

Expand Down Expand Up @@ -283,11 +297,12 @@ InternalPrompt.prototype = {
}

let p = this._getPrompt(aTitle, aText, buttons);
p.setHint("confirm");
this.addCheckbox(p, aCheckMsg, aCheckState);
let data = this.showPrompt(p);
if (aCheckState && data.button > -1)
aCheckState.value = data.checkbox0;
return data.button;
if (aCheckState)
aCheckState.value = data.checkvalue || false;;
return data.accepted;
},

nsIPrompt_prompt: function nsIPrompt_prompt(aTitle, aText, aValue, aCheckMsg, aCheckState) {
Expand All @@ -297,44 +312,46 @@ InternalPrompt.prototype = {
this.addCheckbox(p, aCheckMsg, aCheckState);
let data = this.showPrompt(p);

let ok = data.button == 0;
if (aCheckState && data.button > -1)
aCheckState.value = data.checkbox0;
let ok = data.accepted;
if (aCheckState)
aCheckState.value = data.checkvalue || false;
if (ok)
aValue.value = data.textbox0;
aValue.value = data.promptvalue || "";
return ok;
},

nsIPrompt_promptPassword: function nsIPrompt_promptPassword(
aTitle, aText, aPassword, aCheckMsg, aCheckState) {
let p = this._getPrompt(aTitle, aText, null);
p.setHint("prompt");
this.addPassword(p, aPassword.value, true, PromptUtils.getLocaleString("password", "passwdmgr"));
this.addCheckbox(p, aCheckMsg, aCheckState);
let data = this.showPrompt(p);

let ok = data.button == 0;
if (aCheckState && data.button > -1)
aCheckState.value = data.checkbox0;
let ok = data.accepted;
if (aCheckState)
aCheckState.value = data.checkvalue || false;
if (ok)
aPassword.value = data.password0;
aPassword.value = data.password0 || "";
return ok;
},

nsIPrompt_promptUsernameAndPassword: function nsIPrompt_promptUsernameAndPassword(
aTitle, aText, aUsername, aPassword, aCheckMsg, aCheckState) {
let p = this._getPrompt(aTitle, aText, null);
p.setHint("prompt");
this.addTextbox(p, aUsername.value, true, PromptUtils.getLocaleString("username", "passwdmgr"));
this.addPassword(p, aPassword.value, false, PromptUtils.getLocaleString("password", "passwdmgr"));
this.addCheckbox(p, aCheckMsg, aCheckState);
let data = this.showPrompt(p);

let ok = data.button == 0;
if (aCheckState && data.button > -1)
aCheckState.value = data.checkbox0;
let ok = data.accepted;
if (aCheckState)
aCheckState.value = data.checkvalue || false;

if (ok) {
aUsername.value = data.textbox0;
aPassword.value = data.password0;
aUsername.value = data.textbox0 || "";
aPassword.value = data.password0 || "";
}
return ok;
},
Expand Down
1 change: 1 addition & 0 deletions jsscripts/Makefile.am
Expand Up @@ -9,6 +9,7 @@ jsscripts_manifest_DATA = \
SelectionPrototype.js \
Util.js \
ContextMenuHandler.js \
Prompt.jsm \
$(NULL)

sync_engine_manifestdir=$(libdir)/mozembedlite/chrome/embedlite/content/sync
Expand Down
56 changes: 42 additions & 14 deletions jsscripts/Prompt.jsm
Expand Up @@ -8,7 +8,13 @@ var Cc = Components.classes;
var Ci = Components.interfaces;

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/Messaging.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

XPCOMUtils.defineLazyServiceGetter(Services, "embedlite",
"@mozilla.org/embedlite-app-service;1",
"nsIEmbedAppService");

Services.scriptloader.loadSubScript("chrome://embedlite/content/Logger.js");

this.EXPORTED_SYMBOLS = ["Prompt"];

Expand All @@ -19,15 +25,10 @@ function log(msg) {
function Prompt(aOptions) {
this.window = "window" in aOptions ? aOptions.window : null;

this.msg = { async: true };

if (this.window) {
let window = Services.wm.getMostRecentWindow("navigator:browser");
var tab = window.BrowserApp.getTabForWindow(this.window);
if (tab) {
this.msg.tabId = tab.id;
}
}
this.msg = {
async: true,
winId: Services.embedlite.getIDByWindow(this.window)
};

if (aOptions.priority === 1)
this.msg.type = "Prompt:ShowTop"
Expand All @@ -51,6 +52,17 @@ function Prompt(aOptions) {
}

Prompt.prototype = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIEmbedMessageListener]),
onMessageReceived: function(messageName, message) {
let data = JSON.parse(message);

if (this.callback) {
this.callback(data)
}

Services.embedlite.removeMessageListener(this.getResponseName(), this);
},

setHint: function(aHint) {
if (!aHint)
delete this.msg.hint;
Expand All @@ -59,6 +71,24 @@ Prompt.prototype = {
return this;
},

getMessageName: function() {
let hint = this.msg && this.msg.hint

if (!hint)
Logger.warn("Prompt.jsm is doomed to fail without a hint");

return "embed:" + hint;
},

getResponseName: function() {
let hint = this.msg && this.msg.hint;

if (!hint)
Logger.warn("Prompt.jsm is doomed to fail without a hint (for response)");

return hint + "response";
},

addButton: function(aOptions) {
if (!this.msg.buttons)
this.msg.buttons = [];
Expand Down Expand Up @@ -176,10 +206,8 @@ Prompt.prototype = {
},

_innerShow: function() {
Messaging.sendRequestForResult(this.msg).then((data) => {
if (this.callback)
this.callback(data);
});
Services.embedlite.addMessageListener(this.getResponseName(), this);
Services.embedlite.sendAsyncMessage(this.msg.winId, this.getMessageName(), JSON.stringify(this.msg));
},

_setListItems: function(aItems) {
Expand Down
1 change: 1 addition & 0 deletions link_to_system.sh
Expand Up @@ -59,6 +59,7 @@ rm -rf $TARGET_DIR/chrome/embedlite;
mkdir -p $TARGET_DIR/chrome/embedlite/content/sync;
ln -s $(pwd)/jsscripts/embedhelper.js $TARGET_DIR/chrome/embedlite/content/embedhelper.js;
ln -s $(pwd)/jsscripts/OrientationChangeHandler.jsm $TARGET_DIR/chrome/embedlite/content/OrientationChangeHandler.jsm;
ln -s $(pwd)/jsscripts/Prompt.jsm $TARGET_DIR/chrome/embedlite/content/Prompt.jsm;
ln -s $(pwd)/jsscripts/SelectHelper.js $TARGET_DIR/chrome/embedlite/content/SelectHelper.js;
ln -s $(pwd)/jsscripts/SelectAsyncHelper.js $TARGET_DIR/chrome/embedlite/content/SelectAsyncHelper.js;
ln -s $(pwd)/jsscripts/SelectionHandler.js $TARGET_DIR/chrome/embedlite/content/SelectionHandler.js;
Expand Down

0 comments on commit 71c2284

Please sign in to comment.