Skip to content

Commit

Permalink
Bug 1287664 - Replacing useless module HelperApps.js by using nsIHand…
Browse files Browse the repository at this point in the history
…lerService. r=Paolo

--HG--
extra : rebase_source : 8de756d1b9e7c79bff2ee9c144e7efc6bcefb5ea
  • Loading branch information
edenchuang committed Nov 9, 2016
1 parent 115286c commit bf2f956
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 834 deletions.
726 changes: 0 additions & 726 deletions toolkit/mozapps/downloads/content/helperApps.js

This file was deleted.

1 change: 0 additions & 1 deletion toolkit/mozapps/downloads/content/unknownContentType.xul
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
ondialogaccept="return dialog.onOK()"
ondialogcancel="return dialog.onCancel()">

<script type="application/javascript" src="chrome://mozapps/content/downloads/helperApps.js"/>

<stringbundle id="strings" src="chrome://mozapps/locale/downloads/unknownContentType.properties"/>

Expand Down
1 change: 0 additions & 1 deletion toolkit/mozapps/downloads/jar.mn
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

toolkit.jar:
% content mozapps %content/mozapps/
content/mozapps/downloads/helperApps.js (content/helperApps.js)
* content/mozapps/downloads/unknownContentType.xul (content/unknownContentType.xul)
* content/mozapps/downloads/downloads.xul (content/downloads.xul)
content/mozapps/downloads/downloads.js (content/downloads.js)
Expand Down
6 changes: 3 additions & 3 deletions toolkit/mozapps/downloads/nsHelperAppDlg.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,9 +871,9 @@ nsUnknownContentTypeDialog.prototype = {
// See if the user changed things, and if so, update the
// mimeTypes.rdf entry for this mime type.
updateHelperAppPref: function() {
var ha = new this.mDialog.HelperApps();
ha.updateTypeInfo(this.mLauncher.MIMEInfo);
ha.destroy();
var handlerInfo = this.mLauncher.MIMEInfo;
var hs = Cc["@mozilla.org/uriloader/handler-service;1"].getService(Ci.nsIHandlerService);
hs.store(handlerInfo);
},

// onOK:
Expand Down
16 changes: 15 additions & 1 deletion uriloader/exthandler/nsHandlerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ HandlerService.prototype = {
this._storePreferredHandler(aHandlerInfo);
this._storePossibleHandlers(aHandlerInfo);
this._storeAlwaysAsk(aHandlerInfo);
this._storeExtensions(aHandlerInfo);

// Write the changes to the database immediately so we don't lose them
// if the application crashes.
Expand Down Expand Up @@ -818,7 +819,7 @@ HandlerService.prototype = {
this._removeTarget(aHandlerAppID, NC_URI_TEMPLATE);
}
else {
throw "unknown handler type";
throw "unknown handler type";
}

},
Expand All @@ -830,6 +831,19 @@ HandlerService.prototype = {
aHandlerInfo.alwaysAskBeforeHandling ? "true" : "false");
},

_storeExtensions: function HS__storeExtensions(aHandlerInfo) {
if (aHandlerInfo instanceof Ci.nsIMIMEInfo) {
var typeID = this._getTypeID(this._getClass(aHandlerInfo), aHandlerInfo.type);
var extEnum = aHandlerInfo.getFileExtensions();
while (extEnum.hasMore()) {
let ext = extEnum.getNext().toLowerCase();
if (!this._hasLiteralAssertion(typeID, NC_FILE_EXTENSIONS, ext)) {
this._setLiteral(typeID, NC_FILE_EXTENSIONS, ext);
}
}
}
},


//**************************************************************************//
// Convenience Getters
Expand Down
2 changes: 2 additions & 0 deletions uriloader/exthandler/tests/mochitest/browser.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[DEFAULT]
head = head.js
support-files =
protocolHandler.html

[browser_download_always_ask_preferred_app.js]
[browser_remember_download_option.js]
[browser_web_protocol_handlers.js]
Original file line number Diff line number Diff line change
@@ -1,105 +1,8 @@
add_task(function*() {
// Get a helper app dialog instance:
let helperAppDialog = Cc["@mozilla.org/helperapplauncherdialog;1"].
createInstance(Ci.nsIHelperAppLauncherDialog);
// Mock the mime info:
let mockedMIME = {
_launched: 0,

// nsIHandlerInfo
type: "text/magic-automated-test",
description: "My magic test mime type",
defaultDescription: "Use the default app, luke!",
hasDefaultHandler: true,
possibleApplicationHandlers: {
appendElement() {},
removeElementAt() {},
insertElementAt() {},
replaceElementAt() {},
clear() {},
queryElementAt() {},
indexOf() { return -1 },
enumerate() { return null },
length: 0,
QueryInterface: XPCOMUtils.generateQI([Ci.nsIMutableArray, Ci.nsIArray])
},
preferredApplicationHandler: null,
launchWithURI() {
this._launched++;
},
alwaysAskBeforeHandling: true,
preferredAction: 2, // useHelperApp

// nsIMIMEInfo
getFileExtensions() { throw Cr.NS_ERROR_NOT_IMPLEMENTED; },
setFileExtensions() {},
extensionExists(ext) { return ext == this.primaryExtension; },
appendExtension() {},
primaryExtension: "something",
MIMEType: "text/magic-automated-test",

equals() { return false },

possibleLocalHandlers: {
appendElement() {},
removeElementAt() {},
insertElementAt() {},
replaceElementAt() {},
clear() {},
queryElementAt() {},
indexOf() { return -1 },
enumerate() { return null },
length: 0,
QueryInterface: XPCOMUtils.generateQI([Ci.nsIArray])
},

launchWithFile() {
this._launched++;
},

QueryInterface: XPCOMUtils.generateQI([Ci.nsIMIMEInfo, Ci.nsIHandlerInfo])
};
// Mock the launcher:
let launcher = {
_saveCount: 0,
_cancelCount: 0,
_launched: 0,

MIMEInfo: mockedMIME,
source: Services.io.newURI("http://www.mozilla.org/", null, null),
suggestedFileName: "test_always_ask_preferred_app.something",
targetFileIsExecutable: false,
saveToDisk() {
this._saveCount++;
},
cancel() {
this._cancelCount++;
},
launchWithApplication() {
this._launched++;
},
setWebProgressListener() {
},
saveDestinationAvailable() {
},
contentLength: 42,
targetFile: null, // never read
// PRTime is microseconds since epoch, Date.now() returns milliseconds:
timeDownloadStarted: Date.now() * 1000,
QueryInterface: XPCOMUtils.generateQI([Ci.nsICancelable, Ci.nsIHelperAppLauncher])
};

let helperAppDialogShownPromise = BrowserTestUtils.domWindowOpened();
try {
helperAppDialog.show(launcher, window, "foopy");
} catch (ex) {
ok(false, "Trying to show unknownContentType.xul failed with exception: " + ex);
Cu.reportError(ex);
}
let dlg = yield helperAppDialogShownPromise;
yield BrowserTestUtils.waitForEvent(dlg, "load", false);
is(dlg.location.href, "chrome://mozapps/content/downloads/unknownContentType.xul",
"Got correct dialog");
// Create mocked objects for test
let launcher = createMockedObjects(false);
// Open helper app dialog with mocked launcher
let dlg = yield* openHelperAppDialog(launcher);
let doc = dlg.document;
let location = doc.getElementById("source");
let expectedValue = launcher.source.prePath;
Expand All @@ -114,4 +17,3 @@ add_task(function*() {
doc.documentElement.cancelDialog();
yield helperAppDialogHiddenPromise;
});

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
add_task(function*() {
// create mocked objects
let launcher = createMockedObjects(true);

// open helper app dialog with mocked launcher
let dlg = yield* openHelperAppDialog(launcher);

let doc = dlg.document;

// Set remember choice
ok(!doc.getElementById("rememberChoice").checked,
"Remember choice checkbox should be not checked.");
doc.getElementById("rememberChoice").checked = true;

// Make sure the mock handler information is not in nsIHandlerService
ok(!gHandlerSvc.exists(launcher.MIMEInfo), "Should not be in nsIHandlerService.");

// close the dialog by pushing the ok button.
let dialogClosedPromise = BrowserTestUtils.windowClosed(dlg);
// Make sure the ok button is enabled, since the ok button might be disabled by
// EnableDelayHelper mechanism. Please refer the detailed
// https://dxr.mozilla.org/mozilla-central/source/toolkit/components/prompts/src/SharedPromptUtils.jsm#53
doc.documentElement.getButton("accept").disabled = false;
doc.documentElement.acceptDialog();
yield dialogClosedPromise;

// check the mocked handler information is saved in nsIHandlerService
ok(gHandlerSvc.exists(launcher.MIMEInfo), "Should be in nsIHandlerService.");
// check the extension.
var mimeType = gHandlerSvc.getTypeFromExtension("abc");
is(mimeType, launcher.MIMEInfo.type, "Got correct mime type.");
var handlerInfos = gHandlerSvc.enumerate();
while (handlerInfos.hasMoreElements()) {
let handlerInfo = handlerInfos.getNext().QueryInterface(Ci.nsIHandlerInfo);
if (handlerInfo.type == launcher.MIMEInfo.type) {
// check the alwaysAskBeforeHandling
ok(!handlerInfo.alwaysAskBeforeHandling,
"Should turn off the always ask.");
// check the preferredApplicationHandler
ok(handlerInfo.preferredApplicationHandler.equals(
launcher.MIMEInfo.preferredApplicationHandler),
"Should be equal to the mockedHandlerApp.");
// check the perferredAction
is(handlerInfo.preferredAction, launcher.MIMEInfo.preferredAction,
"Should be equal to Ci.nsIHandlerInfo.useHelperApp.");
break;
}
}
});
105 changes: 105 additions & 0 deletions uriloader/exthandler/tests/mochitest/head.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
Components.utils.import("resource://gre/modules/FileUtils.jsm");
Components.utils.import("resource://gre/modules/Task.jsm");

var gMimeSvc = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
var gHandlerSvc = Cc["@mozilla.org/uriloader/handler-service;1"].getService(Ci.nsIHandlerService);

function createMockedHandlerApp() {
// Mock the executable
let mockedExecutable = FileUtils.getFile("TmpD", ["mockedExecutable"]);
if (!mockedExecutable.exists()) {
mockedExecutable.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o755);
}

// Mock the handler app
let mockedHandlerApp = Cc["@mozilla.org/uriloader/local-handler-app;1"]
.createInstance(Ci.nsILocalHandlerApp);
mockedHandlerApp.executable = mockedExecutable;
mockedHandlerApp.detailedDescription = "Mocked handler app";

registerCleanupFunction(function() {
// remove the mocked executable from disk.
if (mockedExecutable.exists()) {
mockedExecutable.remove(true);
}
});

return mockedHandlerApp;
}

function createMockedObjects(createHandlerApp) {
// Mock the mime info
let internalMockedMIME = gMimeSvc.getFromTypeAndExtension("text/x-test-handler", null);
internalMockedMIME.alwaysAskBeforeHandling = true;
internalMockedMIME.preferredAction = Ci.nsIHandlerInfo.useHelperApp;
internalMockedMIME.appendExtension("abc");
if (createHandlerApp) {
let mockedHandlerApp = createMockedHandlerApp();
internalMockedMIME.description = mockedHandlerApp.detailedDescription;
internalMockedMIME.possibleApplicationHandlers.appendElement(mockedHandlerApp, false);
internalMockedMIME.preferredApplicationHandler = mockedHandlerApp;
}

// Proxy for the mocked MIME info for faking the read-only attributes
let mockedMIME = new Proxy(internalMockedMIME, {
get: function (target, property) {
switch (property) {
case "hasDefaultHandler":
return true;
case "defaultDescription":
return "Default description";
default:
return target[property];
}
},
});

// Mock the launcher:
let mockedLauncher = {
MIMEInfo: mockedMIME,
source: Services.io.newURI("http://www.mozilla.org/", null, null),
suggestedFileName: "test_download_dialog.abc",
targetFileIsExecutable: false,
saveToDisk() {},
cancel() {},
launchWithApplication() {},
setWebProgressListener() {},
saveDestinationAvailable() {},
contentLength: 42,
targetFile: null, // never read
// PRTime is microseconds since epoch, Date.now() returns milliseconds:
timeDownloadStarted: Date.now() * 1000,
QueryInterface: XPCOMUtils.generateQI([Ci.nsICancelable, Ci.nsIHelperAppLauncher])
};

registerCleanupFunction(function() {
// remove the mocked mime info from database.
let mockHandlerInfo = gMimeSvc.getFromTypeAndExtension("text/x-test-handler", null);
if (gHandlerSvc.exists(mockHandlerInfo)) {
gHandlerSvc.remove(mockHandlerInfo);
}
});

return mockedLauncher;
}

function* openHelperAppDialog(launcher) {
let helperAppDialog = Cc["@mozilla.org/helperapplauncherdialog;1"].
createInstance(Ci.nsIHelperAppLauncherDialog);

let helperAppDialogShownPromise = BrowserTestUtils.domWindowOpened();
try {
helperAppDialog.show(launcher, window, "foopy");
} catch (ex) {
ok(false, "Trying to show unknownContentType.xul failed with exception: " + ex);
Cu.reportError(ex);
}
let dlg = yield helperAppDialogShownPromise;

yield BrowserTestUtils.waitForEvent(dlg, "load", false);

is(dlg.location.href, "chrome://mozapps/content/downloads/unknownContentType.xul",
"Got correct dialog");

return dlg;
}

0 comments on commit bf2f956

Please sign in to comment.