diff options
author | Matt A. Tobin <email@mattatobin.com> | 2018-02-02 03:32:58 -0500 |
---|---|---|
committer | Matt A. Tobin <email@mattatobin.com> | 2018-02-02 03:32:58 -0500 |
commit | e72ef92b5bdc43cd2584198e2e54e951b70299e8 (patch) | |
tree | 01ceb4a897c33eca9e7ccf2bc3aefbe530169fe5 /application/basilisk/components/newtab/PreviewProvider.jsm | |
parent | 0d19b77d3eaa5b8d837bf52c19759e68e42a1c4c (diff) | |
download | UXP-e72ef92b5bdc43cd2584198e2e54e951b70299e8.tar UXP-e72ef92b5bdc43cd2584198e2e54e951b70299e8.tar.gz UXP-e72ef92b5bdc43cd2584198e2e54e951b70299e8.tar.lz UXP-e72ef92b5bdc43cd2584198e2e54e951b70299e8.tar.xz UXP-e72ef92b5bdc43cd2584198e2e54e951b70299e8.zip |
Add Basilisk
Diffstat (limited to 'application/basilisk/components/newtab/PreviewProvider.jsm')
-rw-r--r-- | application/basilisk/components/newtab/PreviewProvider.jsm | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/application/basilisk/components/newtab/PreviewProvider.jsm b/application/basilisk/components/newtab/PreviewProvider.jsm new file mode 100644 index 000000000..7e6d06e3d --- /dev/null +++ b/application/basilisk/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; + } + }) +}; |