diff options
author | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
---|---|---|
committer | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
commit | 5f8de423f190bbb79a62f804151bc24824fa32d8 (patch) | |
tree | 10027f336435511475e392454359edea8e25895d /browser/components/newtab/PreviewProvider.jsm | |
parent | 49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff) | |
download | UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip |
Add m-esr52 at 52.6.0
Diffstat (limited to 'browser/components/newtab/PreviewProvider.jsm')
-rw-r--r-- | browser/components/newtab/PreviewProvider.jsm | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/browser/components/newtab/PreviewProvider.jsm b/browser/components/newtab/PreviewProvider.jsm new file mode 100644 index 000000000..8624b8544 --- /dev/null +++ b/browser/components/newtab/PreviewProvider.jsm @@ -0,0 +1,49 @@ +/* global XPCOMUtils, BackgroundPageThumbs, FileUtils, PageThumbsStorage, Task, MIMEService */ +/* exported PreviewProvider */ + +"use strict"; + +this.EXPORTED_SYMBOLS = ["PreviewProvider"]; + +const {utils: Cu} = Components; +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); +Cu.import("resource://gre/modules/Task.jsm"); +Cu.import("resource://gre/modules/PageThumbs.jsm"); +Cu.import("resource://gre/modules/FileUtils.jsm"); +const {OS} = Cu.import("resource://gre/modules/osfile.jsm"); + +XPCOMUtils.defineLazyModuleGetter(this, "BackgroundPageThumbs", + "resource://gre/modules/BackgroundPageThumbs.jsm"); +XPCOMUtils.defineLazyServiceGetter(this, "MIMEService", + "@mozilla.org/mime;1", "nsIMIMEService"); + +let PreviewProvider = { + /** + * Returns a thumbnail as a data URI for a url, creating it if necessary + * + * @param {String} url + * a url to obtain a thumbnail for + * @return {Promise} A Promise that resolves with a base64 encoded thumbnail + */ + getThumbnail: Task.async(function* PreviewProvider_getThumbnail(url) { + try { + yield BackgroundPageThumbs.captureIfMissing(url); + let imgPath = PageThumbsStorage.getFilePathForURL(url); + + // OS.File object used to easily read off-thread + let file = yield OS.File.open(imgPath, {read: true, existing: true}); + + // nsIFile object needed for MIMEService + let nsFile = FileUtils.File(imgPath); + + let contentType = MIMEService.getTypeFromFile(nsFile); + let bytes = yield file.read(); + let encodedData = btoa(String.fromCharCode.apply(null, bytes)); + file.close(); + return `data:${contentType};base64,${encodedData}`; + } catch (err) { + Cu.reportError(`PreviewProvider_getThumbnail error: ${err}`); + throw err; + } + }) +}; |