diff options
Diffstat (limited to '')
-rw-r--r-- | devtools/shared/platform/cache-entry.js | 118 | ||||
-rw-r--r-- | devtools/shared/platform/clipboard.js | 61 | ||||
-rw-r--r-- | devtools/shared/platform/moz.build | 11 | ||||
-rw-r--r-- | devtools/shared/platform/stack.js | 66 |
4 files changed, 256 insertions, 0 deletions
diff --git a/devtools/shared/platform/cache-entry.js b/devtools/shared/platform/cache-entry.js new file mode 100644 index 0000000000..7efd460ad8 --- /dev/null +++ b/devtools/shared/platform/cache-entry.js @@ -0,0 +1,118 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +const Services = require("Services"); +const { Ci } = require("chrome"); +loader.lazyRequireGetter( + this, + "NetworkHelper", + "devtools/shared/webconsole/network-helper" +); + +/** + * Module to fetch cache objects from CacheStorageService + * and return them as an object. + */ +exports.CacheEntry = { + /** + * Flag for cache session being initialized. + */ + isCacheSessionInitialized: false, + /** + * Cache session object. + */ + cacheSession: null, + + /** + * Initializes our cache session / cache storage session. + */ + initializeCacheSession: function(request) { + try { + const cacheService = Services.cache2; + if (cacheService) { + let loadContext = NetworkHelper.getRequestLoadContext(request); + if (!loadContext) { + // Get default load context if we can't fetch. + loadContext = Services.loadContextInfo.default; + } + this.cacheSession = cacheService.diskCacheStorage(loadContext, false); + this.isCacheSessionInitialized = true; + } + } catch (e) { + this.isCacheSessionInitialized = false; + } + }, + + /** + * Parses a cache descriptor returned from the backend into a + * usable object. + * + * @param Object descriptor The descriptor from the backend. + */ + parseCacheDescriptor: function(descriptor) { + const descriptorObj = {}; + try { + if (descriptor.storageDataSize) { + descriptorObj.dataSize = descriptor.storageDataSize; + } + } catch (e) { + // We just need to handle this in case it's a js file of 0B. + } + if (descriptor.expirationTime) { + descriptorObj.expires = descriptor.expirationTime; + } + if (descriptor.fetchCount) { + descriptorObj.fetchCount = descriptor.fetchCount; + } + if (descriptor.lastFetched) { + descriptorObj.lastFetched = descriptor.lastFetched; + } + if (descriptor.lastModified) { + descriptorObj.lastModified = descriptor.lastModified; + } + if (descriptor.deviceID) { + descriptorObj.device = descriptor.deviceID; + } + return descriptorObj; + }, + + /** + * Does the fetch for the cache descriptor from the session. + * + * @param string request + * The request object. + * @param Function onCacheDescriptorAvailable + * callback function. + */ + getCacheEntry: function(request, onCacheDescriptorAvailable) { + if (!this.isCacheSessionInitialized) { + this.initializeCacheSession(request); + } + if (this.cacheSession) { + const uri = NetworkHelper.nsIURL(request.URI.spec); + this.cacheSession.asyncOpenURI( + uri, + "", + Ci.nsICacheStorage.OPEN_SECRETLY, + { + onCacheEntryCheck: (entry, appcache) => { + return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED; + }, + onCacheEntryAvailable: (descriptor, isnew, appcache, status) => { + if (descriptor) { + const descriptorObj = this.parseCacheDescriptor(descriptor); + onCacheDescriptorAvailable(descriptorObj); + } else { + onCacheDescriptorAvailable(null); + } + }, + } + ); + } else { + onCacheDescriptorAvailable(null); + } + }, +}; diff --git a/devtools/shared/platform/clipboard.js b/devtools/shared/platform/clipboard.js new file mode 100644 index 0000000000..d9d471c504 --- /dev/null +++ b/devtools/shared/platform/clipboard.js @@ -0,0 +1,61 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// Helpers for clipboard handling. + +"use strict"; + +const { Cc, Ci } = require("chrome"); +const Services = require("Services"); +const clipboardHelper = Cc["@mozilla.org/widget/clipboardhelper;1"].getService( + Ci.nsIClipboardHelper +); + +function copyString(string) { + clipboardHelper.copyString(string); +} + +/** + * Retrieve the current clipboard data matching the flavor "text/unicode". + * + * @return {String} Clipboard text content, null if no text clipboard data is available. + */ +function getText() { + const flavor = "text/unicode"; + + const xferable = Cc["@mozilla.org/widget/transferable;1"].createInstance( + Ci.nsITransferable + ); + + if (!xferable) { + throw new Error( + "Couldn't get the clipboard data due to an internal error " + + "(couldn't create a Transferable object)." + ); + } + + xferable.init(null); + xferable.addDataFlavor(flavor); + + // Get the data into our transferable. + Services.clipboard.getData(xferable, Services.clipboard.kGlobalClipboard); + + const data = {}; + try { + xferable.getTransferData(flavor, data); + } catch (e) { + // Clipboard doesn't contain data in flavor, return null. + return null; + } + + // There's no data available, return. + if (!data.value) { + return null; + } + + return data.value.QueryInterface(Ci.nsISupportsString).data; +} + +exports.copyString = copyString; +exports.getText = getText; diff --git a/devtools/shared/platform/moz.build b/devtools/shared/platform/moz.build new file mode 100644 index 0000000000..58d8a07773 --- /dev/null +++ b/devtools/shared/platform/moz.build @@ -0,0 +1,11 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +DevToolsModules( + "cache-entry.js", + "clipboard.js", + "stack.js", +) diff --git a/devtools/shared/platform/stack.js b/devtools/shared/platform/stack.js new file mode 100644 index 0000000000..245ff507eb --- /dev/null +++ b/devtools/shared/platform/stack.js @@ -0,0 +1,66 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// A few wrappers for stack-manipulation. This version of the module +// is used in chrome code. + +"use strict"; + +const { Cu, components } = require("chrome"); + +/** + * Return the Nth path from the stack excluding substr. + * + * @param {Number} + * n the Nth path from the stack to describe. + * @param {String} substr + * A segment of the path that should be excluded. + */ +function getNthPathExcluding(n, substr) { + let stack = components.stack.formattedStack.split("\n"); + + // Remove this method from the stack + stack = stack.splice(1); + + stack = stack.map(line => { + if (line.includes(" -> ")) { + return line.split(" -> ")[1]; + } + return line; + }); + + if (substr) { + stack = stack.filter(line => { + return line && !line.includes(substr); + }); + } + + if (!stack[n]) { + n = 0; + } + return stack[n] || ""; +} + +/** + * Return a stack object that can be serialized and, when + * deserialized, passed to callFunctionWithAsyncStack. + */ +function getStack() { + return components.stack.caller; +} + +/** + * Like Cu.callFunctionWithAsyncStack but handles the isWorker case + * -- |Cu| isn't defined in workers. + */ +function callFunctionWithAsyncStack(callee, stack, id) { + if (isWorker) { + return callee(); + } + return Cu.callFunctionWithAsyncStack(callee, stack, id); +} + +exports.callFunctionWithAsyncStack = callFunctionWithAsyncStack; +exports.getNthPathExcluding = getNthPathExcluding; +exports.getStack = getStack; |