michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: Make sure that the download manager service is given a chance to cancel the michael@0: private browsing mode transition. michael@0: **/ michael@0: michael@0: const Cm = Components.manager; michael@0: michael@0: const kPromptServiceUUID = "{6cc9c9fe-bc0b-432b-a410-253ef8bcc699}"; michael@0: const kPromptServiceContractID = "@mozilla.org/embedcomp/prompt-service;1"; michael@0: michael@0: // Save original prompt service factory michael@0: const kPromptServiceFactory = Cm.getClassObject(Cc[kPromptServiceContractID], michael@0: Ci.nsIFactory); michael@0: michael@0: let fakePromptServiceFactory = { michael@0: createInstance: function(aOuter, aIid) { michael@0: if (aOuter != null) michael@0: throw Cr.NS_ERROR_NO_AGGREGATION; michael@0: return promptService.QueryInterface(aIid); michael@0: } michael@0: }; michael@0: michael@0: let promptService = { michael@0: _buttonChoice: 0, michael@0: _called: false, michael@0: wasCalled: function() { michael@0: let called = this._called; michael@0: this._called = false; michael@0: return called; michael@0: }, michael@0: sayCancel: function() { michael@0: this._buttonChoice = 1; michael@0: this._called = false; michael@0: }, michael@0: sayProceed: function() { michael@0: this._buttonChoice = 0; michael@0: this._called = false; michael@0: }, michael@0: QueryInterface: function(aIID) { michael@0: if (aIID.equals(Ci.nsIPromptService) || michael@0: aIID.equals(Ci.nsISupports)) { michael@0: return this; michael@0: } michael@0: throw Cr.NS_ERROR_NO_INTERFACE; michael@0: }, michael@0: confirmEx: function(parent, title, text, buttonFlags, michael@0: button0Title, button1Title, button2Title, michael@0: checkMsg, checkState) { michael@0: this._called = true; michael@0: return this._buttonChoice; michael@0: } michael@0: }; michael@0: michael@0: Cm.QueryInterface(Ci.nsIComponentRegistrar) michael@0: .registerFactory(Components.ID(kPromptServiceUUID), "Prompt Service", michael@0: kPromptServiceContractID, fakePromptServiceFactory); michael@0: michael@0: this.__defineGetter__("dm", function() { michael@0: delete this.dm; michael@0: return this.dm = Cc["@mozilla.org/download-manager;1"]. michael@0: getService(Ci.nsIDownloadManager); michael@0: }); michael@0: michael@0: function trigger_pb_cleanup(expected) michael@0: { michael@0: var obs = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService); michael@0: var cancel = Cc["@mozilla.org/supports-PRBool;1"].createInstance(Ci.nsISupportsPRBool); michael@0: cancel.data = false; michael@0: obs.notifyObservers(cancel, "last-pb-context-exiting", null); michael@0: do_check_eq(expected, cancel.data); michael@0: if (!expected) michael@0: obs.notifyObservers(cancel, "last-pb-context-exited", null); michael@0: } michael@0: michael@0: function run_test() { michael@0: if (oldDownloadManagerDisabled()) { michael@0: return; michael@0: } michael@0: michael@0: function finishTest() { michael@0: // Cancel Download-G michael@0: dlG.cancel(); michael@0: dlG.remove(); michael@0: dm.cleanUp(); michael@0: dm.cleanUpPrivate(); michael@0: do_check_eq(dm.activeDownloadCount, 0); michael@0: do_check_eq(dm.activePrivateDownloadCount, 0); michael@0: michael@0: dm.removeListener(listener); michael@0: httpserv.stop(do_test_finished); michael@0: michael@0: // Unregister the factory so we do not leak michael@0: Cm.QueryInterface(Ci.nsIComponentRegistrar) michael@0: .unregisterFactory(Components.ID(kPromptServiceUUID), michael@0: fakePromptServiceFactory); michael@0: michael@0: // Restore the original factory michael@0: Cm.QueryInterface(Ci.nsIComponentRegistrar) michael@0: .registerFactory(Components.ID(kPromptServiceUUID), "Prompt Service", michael@0: kPromptServiceContractID, kPromptServiceFactory); michael@0: } michael@0: michael@0: do_test_pending(); michael@0: let httpserv = new HttpServer(); michael@0: httpserv.registerDirectory("/file/", do_get_cwd()); michael@0: httpserv.registerPathHandler("/noresume", function (meta, response) { michael@0: response.setHeader("Content-Type", "text/html", false); michael@0: response.setHeader("Accept-Ranges", "none", false); michael@0: response.write("foo"); michael@0: }); michael@0: httpserv.start(-1); michael@0: michael@0: let tmpDir = Cc["@mozilla.org/file/directory_service;1"]. michael@0: getService(Ci.nsIProperties). michael@0: get("TmpD", Ci.nsIFile); michael@0: michael@0: // make sure we're starting with an empty DB michael@0: do_check_eq(dm.activeDownloadCount, 0); michael@0: michael@0: let listener = { michael@0: onDownloadStateChange: function(aState, aDownload) michael@0: { michael@0: switch (aDownload.state) { michael@0: case dm.DOWNLOAD_QUEUED: michael@0: case dm.DOWNLOAD_DOWNLOADING: michael@0: if (aDownload.targetFile.equals(dlD.targetFile)) { michael@0: // Sanity check: Download-D must not be resumable michael@0: do_check_false(dlD.resumable); michael@0: michael@0: // Cancel the transition michael@0: promptService.sayCancel(); michael@0: trigger_pb_cleanup(true); michael@0: do_check_true(promptService.wasCalled()); michael@0: do_check_eq(dm.activePrivateDownloadCount, 1); michael@0: michael@0: promptService.sayProceed(); michael@0: trigger_pb_cleanup(false); michael@0: do_check_true(promptService.wasCalled()); michael@0: do_check_eq(dm.activePrivateDownloadCount, 0); michael@0: do_check_eq(dlD.state, dm.DOWNLOAD_CANCELED); michael@0: michael@0: // Create Download-E michael@0: dlE = addDownload(httpserv, { michael@0: isPrivate: true, michael@0: targetFile: fileE, michael@0: sourceURI: downloadESource, michael@0: downloadName: downloadEName michael@0: }); michael@0: michael@0: // Wait for Download-E to start michael@0: } else if (aDownload.targetFile.equals(dlE.targetFile)) { michael@0: // Sanity check: Download-E must be resumable michael@0: do_check_true(dlE.resumable); michael@0: michael@0: promptService.sayCancel(); michael@0: trigger_pb_cleanup(true); michael@0: do_check_true(promptService.wasCalled()); michael@0: do_check_eq(dm.activePrivateDownloadCount, 1); michael@0: michael@0: promptService.sayProceed(); michael@0: trigger_pb_cleanup(false); michael@0: do_check_true(promptService.wasCalled()); michael@0: do_check_eq(dm.activePrivateDownloadCount, 0); michael@0: do_check_eq(dlE.state, dm.DOWNLOAD_CANCELED); michael@0: michael@0: // Create Download-F michael@0: dlF = addDownload(httpserv, { michael@0: isPrivate: true, michael@0: targetFile: fileF, michael@0: sourceURI: downloadFSource, michael@0: downloadName: downloadFName michael@0: }); michael@0: michael@0: // Wait for Download-F to start michael@0: } else if (aDownload.targetFile.equals(dlF.targetFile)) { michael@0: // Sanity check: Download-F must be resumable michael@0: do_check_true(dlF.resumable); michael@0: dlF.pause(); michael@0: michael@0: } else if (aDownload.targetFile.equals(dlG.targetFile)) { michael@0: // Sanity check: Download-G must not be resumable michael@0: do_check_false(dlG.resumable); michael@0: michael@0: promptService.sayCancel(); michael@0: trigger_pb_cleanup(false); michael@0: do_check_false(promptService.wasCalled()); michael@0: do_check_eq(dm.activeDownloadCount, 1); michael@0: do_check_eq(dlG.state, dm.DOWNLOAD_DOWNLOADING); michael@0: finishTest(); michael@0: } michael@0: break; michael@0: michael@0: case dm.DOWNLOAD_PAUSED: michael@0: if (aDownload.targetFile.equals(dlF.targetFile)) { michael@0: promptService.sayProceed(); michael@0: trigger_pb_cleanup(false); michael@0: do_check_true(promptService.wasCalled()); michael@0: do_check_eq(dm.activePrivateDownloadCount, 0); michael@0: do_check_eq(dlF.state, dm.DOWNLOAD_CANCELED); michael@0: michael@0: // Create Download-G michael@0: dlG = addDownload(httpserv, { michael@0: isPrivate: false, michael@0: targetFile: fileG, michael@0: sourceURI: downloadGSource, michael@0: downloadName: downloadGName michael@0: }); michael@0: michael@0: // Wait for Download-G to start michael@0: } michael@0: break; michael@0: } michael@0: }, michael@0: onStateChange: function(a, b, c, d, e) { }, michael@0: onProgressChange: function(a, b, c, d, e, f, g) { }, michael@0: onSecurityChange: function(a, b, c, d) { } michael@0: }; michael@0: michael@0: dm.addPrivacyAwareListener(listener); michael@0: michael@0: const PORT = httpserv.identity.primaryPort; michael@0: michael@0: // properties of Download-D michael@0: const downloadDSource = "http://localhost:" + PORT + "/noresume"; michael@0: const downloadDDest = "download-file-D"; michael@0: const downloadDName = "download-D"; michael@0: michael@0: // properties of Download-E michael@0: const downloadESource = "http://localhost:" + PORT + "/file/head_download_manager.js"; michael@0: const downloadEDest = "download-file-E"; michael@0: const downloadEName = "download-E"; michael@0: michael@0: // properties of Download-F michael@0: const downloadFSource = "http://localhost:" + PORT + "/file/head_download_manager.js"; michael@0: const downloadFDest = "download-file-F"; michael@0: const downloadFName = "download-F"; michael@0: michael@0: // properties of Download-G michael@0: const downloadGSource = "http://localhost:" + PORT + "/noresume"; michael@0: const downloadGDest = "download-file-G"; michael@0: const downloadGName = "download-G"; michael@0: michael@0: // Create all target files michael@0: let fileD = tmpDir.clone(); michael@0: fileD.append(downloadDDest); michael@0: fileD.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: let fileE = tmpDir.clone(); michael@0: fileE.append(downloadEDest); michael@0: fileE.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: let fileF = tmpDir.clone(); michael@0: fileF.append(downloadFDest); michael@0: fileF.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: let fileG = tmpDir.clone(); michael@0: fileG.append(downloadGDest); michael@0: fileG.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); michael@0: michael@0: // Create Download-D michael@0: let dlD = addDownload(httpserv, { michael@0: isPrivate: true, michael@0: targetFile: fileD, michael@0: sourceURI: downloadDSource, michael@0: downloadName: downloadDName michael@0: }); michael@0: michael@0: let dlE, dlF, dlG; michael@0: michael@0: // wait for Download-D to start michael@0: }