michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: // Helper file for shared image functionality michael@0: // michael@0: // Note that this is use by tests elsewhere in the source tree. When in doubt, michael@0: // check mxr before removing or changing functionality. michael@0: michael@0: // Helper function to clear the image cache of content images michael@0: function clearImageCache() michael@0: { michael@0: var tools = SpecialPowers.Cc["@mozilla.org/image/tools;1"] michael@0: .getService(SpecialPowers.Ci.imgITools); michael@0: var imageCache = tools.getImgCacheForDocument(window.document); michael@0: imageCache.clearCache(false); // true=chrome, false=content michael@0: } michael@0: michael@0: // Helper function to determine if the frame is decoded for a given image id michael@0: function isFrameDecoded(id) michael@0: { michael@0: return (getImageStatus(id) & michael@0: SpecialPowers.Ci.imgIRequest.STATUS_FRAME_COMPLETE) michael@0: ? true : false; michael@0: } michael@0: michael@0: // Helper function to determine if the image is loaded for a given image id michael@0: function isImageLoaded(id) michael@0: { michael@0: return (getImageStatus(id) & michael@0: SpecialPowers.Ci.imgIRequest.STATUS_LOAD_COMPLETE) michael@0: ? true : false; michael@0: } michael@0: michael@0: // Helper function to get the status flags of an image michael@0: function getImageStatus(id) michael@0: { michael@0: // Get the image michael@0: var img = SpecialPowers.wrap(document.getElementById(id)); michael@0: michael@0: // QI the image to nsImageLoadingContent michael@0: img.QueryInterface(SpecialPowers.Ci.nsIImageLoadingContent); michael@0: michael@0: // Get the request michael@0: var request = img.getRequest(SpecialPowers.Ci michael@0: .nsIImageLoadingContent michael@0: .CURRENT_REQUEST); michael@0: michael@0: // Return the status michael@0: return request.imageStatus; michael@0: } michael@0: michael@0: // Forces a synchronous decode of an image by drawing it to a canvas. Only michael@0: // really meaningful if the image is fully loaded first michael@0: function forceDecode(id) michael@0: { michael@0: // Get the image michael@0: var img = document.getElementById(id); michael@0: michael@0: // Make a new canvas michael@0: var canvas = document.createElement("canvas"); michael@0: michael@0: // Draw the image to the canvas. This forces a synchronous decode michael@0: var ctx = canvas.getContext("2d"); michael@0: ctx.drawImage(img, 0, 0); michael@0: } michael@0: michael@0: michael@0: // Functions to facilitate getting/setting various image-related prefs michael@0: // michael@0: // If you change a pref in a mochitest, Don't forget to reset it to its michael@0: // original value! michael@0: // michael@0: // Null indicates no pref set michael@0: michael@0: const DISCARD_ENABLED_PREF = {name: "discardable", branch: "image.mem.", type: "bool"}; michael@0: const DECODEONDRAW_ENABLED_PREF = {name: "decodeondraw", branch: "image.mem.", type: "bool"}; michael@0: const DISCARD_TIMEOUT_PREF = {name: "min_discard_timeout_ms", branch: "image.mem.", type: "int"}; michael@0: michael@0: function setImagePref(pref, val) michael@0: { michael@0: var prefService = SpecialPowers.Cc["@mozilla.org/preferences-service;1"] michael@0: .getService(SpecialPowers.Ci.nsIPrefService); michael@0: var branch = prefService.getBranch(pref.branch); michael@0: if (val != null) { michael@0: switch(pref.type) { michael@0: case "bool": michael@0: branch.setBoolPref(pref.name, val); michael@0: break; michael@0: case "int": michael@0: branch.setIntPref(pref.name, val); michael@0: break; michael@0: default: michael@0: throw new Error("Unknown pref type"); michael@0: } michael@0: } michael@0: else if (branch.prefHasUserValue(pref.name)) michael@0: branch.clearUserPref(pref.name); michael@0: } michael@0: michael@0: function getImagePref(pref) michael@0: { michael@0: var prefService = SpecialPowers.Cc["@mozilla.org/preferences-service;1"] michael@0: .getService(SpecialPowers.Ci.nsIPrefService); michael@0: var branch = prefService.getBranch(pref.branch); michael@0: if (branch.prefHasUserValue(pref.name)) { michael@0: switch (pref.type) { michael@0: case "bool": michael@0: return branch.getBoolPref(pref.name); michael@0: case "int": michael@0: return branch.getIntPref(pref.name); michael@0: default: michael@0: throw new Error("Unknown pref type"); michael@0: } michael@0: } michael@0: else michael@0: return null; michael@0: } michael@0: michael@0: // JS implementation of imgIScriptedNotificationObserver with stubs for all of its methods. michael@0: function ImageDecoderObserverStub() michael@0: { michael@0: this.sizeAvailable = function sizeAvailable(aRequest) {} michael@0: this.frameComplete = function frameComplete(aRequest) {} michael@0: this.decodeComplete = function decodeComplete(aRequest) {} michael@0: this.loadComplete = function loadComplete(aRequest) {} michael@0: this.frameUpdate = function frameUpdate(aRequest) {} michael@0: this.discard = function discard(aRequest) {} michael@0: this.isAnimated = function isAnimated(aRequest) {} michael@0: }