imgutils.js (4038B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 // Helper file for shared image functionality 3 // 4 // Note that this is use by tests elsewhere in the source tree. When in doubt, 5 // check mxr before removing or changing functionality. 6 7 // Helper function to clear both the content and chrome image caches 8 function clearAllImageCaches() { 9 var tools = SpecialPowers.Cc["@mozilla.org/image/tools;1"].getService( 10 SpecialPowers.Ci.imgITools 11 ); 12 var imageCache = tools.getImgCacheForDocument(window.document); 13 imageCache.clearCache(); // no parameter=all 14 } 15 16 // Helper function to clear the image cache of content images 17 function clearImageCache() { 18 var tools = SpecialPowers.Cc["@mozilla.org/image/tools;1"].getService( 19 SpecialPowers.Ci.imgITools 20 ); 21 var imageCache = tools.getImgCacheForDocument(window.document); 22 imageCache.clearCache(false); // true=chrome, false=content 23 } 24 25 // Helper function to determine if the frame is decoded for a given image id 26 function isFrameDecoded(id) { 27 return !!( 28 getImageStatus(id) & SpecialPowers.Ci.imgIRequest.STATUS_FRAME_COMPLETE 29 ); 30 } 31 32 // Helper function to determine if the image is loaded for a given image id 33 function isImageLoaded(id) { 34 return !!( 35 getImageStatus(id) & SpecialPowers.Ci.imgIRequest.STATUS_LOAD_COMPLETE 36 ); 37 } 38 39 // Helper function to get the status flags of an image 40 function getImageStatus(id) { 41 // Get the image 42 var img = SpecialPowers.wrap(document.getElementById(id)); 43 44 // Get the request 45 var request = img.getRequest( 46 SpecialPowers.Ci.nsIImageLoadingContent.CURRENT_REQUEST 47 ); 48 49 // Return the status 50 return request.imageStatus; 51 } 52 53 // Forces a synchronous decode of an image by drawing it to a canvas. Only 54 // really meaningful if the image is fully loaded first 55 function forceDecode(id) { 56 // Get the image 57 var img = document.getElementById(id); 58 59 // Make a new canvas 60 var canvas = document.createElement("canvas"); 61 62 // Draw the image to the canvas. This forces a synchronous decode 63 var ctx = canvas.getContext("2d"); 64 ctx.drawImage(img, 0, 0); 65 } 66 67 // Functions to facilitate getting/setting various image-related prefs 68 // 69 // If you change a pref in a mochitest, Don't forget to reset it to its 70 // original value! 71 // 72 // Null indicates no pref set 73 74 const DISCARD_ENABLED_PREF = { 75 name: "discardable", 76 branch: "image.mem.", 77 type: "bool", 78 }; 79 const DECODEONDRAW_ENABLED_PREF = { 80 name: "decodeondraw", 81 branch: "image.mem.", 82 type: "bool", 83 }; 84 const DISCARD_TIMEOUT_PREF = { 85 name: "min_discard_timeout_ms", 86 branch: "image.mem.", 87 type: "int", 88 }; 89 90 function setImagePref(pref, val) { 91 var prefService = SpecialPowers.Services.prefs; 92 var branch = prefService.getBranch(pref.branch); 93 if (val != null) { 94 switch (pref.type) { 95 case "bool": 96 branch.setBoolPref(pref.name, val); 97 break; 98 case "int": 99 branch.setIntPref(pref.name, val); 100 break; 101 default: 102 throw new Error("Unknown pref type"); 103 } 104 } else if (branch.prefHasUserValue(pref.name)) { 105 branch.clearUserPref(pref.name); 106 } 107 } 108 109 function getImagePref(pref) { 110 var prefService = SpecialPowers.Services.prefs; 111 var branch = prefService.getBranch(pref.branch); 112 if (branch.prefHasUserValue(pref.name)) { 113 switch (pref.type) { 114 case "bool": 115 return branch.getBoolPref(pref.name); 116 case "int": 117 return branch.getIntPref(pref.name); 118 default: 119 throw new Error("Unknown pref type"); 120 } 121 } else { 122 return null; 123 } 124 } 125 126 // JS implementation of imgIScriptedNotificationObserver with stubs for all of its methods. 127 function ImageDecoderObserverStub() { 128 this.sizeAvailable = function sizeAvailable() {}; 129 this.frameComplete = function frameComplete() {}; 130 this.decodeComplete = function decodeComplete() {}; 131 this.loadComplete = function loadComplete() {}; 132 this.frameUpdate = function frameUpdate() {}; 133 this.discard = function discard() {}; 134 this.isAnimated = function isAnimated() {}; 135 this.hasTransparency = function hasTransparency() {}; 136 }