device.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const {Cc, Ci, Cu} = require("chrome");
  6. const {deviceSpec} = require("devtools/shared/specs/device");
  7. const protocol = require("devtools/shared/protocol");
  8. const defer = require("devtools/shared/defer");
  9. const DeviceFront = protocol.FrontClassWithSpec(deviceSpec, {
  10. initialize: function (client, form) {
  11. protocol.Front.prototype.initialize.call(this, client);
  12. this.actorID = form.deviceActor;
  13. this.manage(this);
  14. },
  15. screenshotToBlob: function () {
  16. return this.screenshotToDataURL().then(longstr => {
  17. return longstr.string().then(dataURL => {
  18. let deferred = defer();
  19. longstr.release().then(null, Cu.reportError);
  20. let req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
  21. .createInstance(Ci.nsIXMLHttpRequest);
  22. req.open("GET", dataURL, true);
  23. req.responseType = "blob";
  24. req.onload = () => {
  25. deferred.resolve(req.response);
  26. };
  27. req.onerror = () => {
  28. deferred.reject(req.status);
  29. };
  30. req.send();
  31. return deferred.promise;
  32. });
  33. });
  34. },
  35. });
  36. const _knownDeviceFronts = new WeakMap();
  37. exports.getDeviceFront = function (client, form) {
  38. if (!form.deviceActor) {
  39. return null;
  40. }
  41. if (_knownDeviceFronts.has(client)) {
  42. return _knownDeviceFronts.get(client);
  43. }
  44. let front = new DeviceFront(client, form);
  45. _knownDeviceFronts.set(client, front);
  46. return front;
  47. };