device.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 {Ci} = require("chrome");
  6. const Services = require("Services");
  7. const protocol = require("devtools/shared/protocol");
  8. const promise = require("promise");
  9. const {LongStringActor} = require("devtools/server/actors/string");
  10. const {DebuggerServer} = require("devtools/server/main");
  11. const {getSystemInfo, getSetting} = require("devtools/shared/system");
  12. const {deviceSpec} = require("devtools/shared/specs/device");
  13. const FileReader = require("FileReader");
  14. const {PermissionsTable} = require("resource://gre/modules/PermissionsTable.jsm");
  15. var DeviceActor = exports.DeviceActor = protocol.ActorClassWithSpec(deviceSpec, {
  16. _desc: null,
  17. getDescription: function () {
  18. return getSystemInfo();
  19. },
  20. getWallpaper: function () {
  21. let deferred = promise.defer();
  22. getSetting("wallpaper.image").then((blob) => {
  23. let reader = new FileReader();
  24. let conn = this.conn;
  25. reader.addEventListener("load", function () {
  26. let str = new LongStringActor(conn, reader.result);
  27. deferred.resolve(str);
  28. });
  29. reader.addEventListener("error", function () {
  30. deferred.reject(reader.error);
  31. });
  32. reader.readAsDataURL(blob);
  33. });
  34. return deferred.promise;
  35. },
  36. screenshotToDataURL: function () {
  37. let window = Services.wm.getMostRecentWindow(DebuggerServer.chromeWindowType);
  38. var devicePixelRatio = window.devicePixelRatio;
  39. let canvas = window.document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
  40. let width = window.innerWidth;
  41. let height = window.innerHeight;
  42. canvas.setAttribute("width", Math.round(width * devicePixelRatio));
  43. canvas.setAttribute("height", Math.round(height * devicePixelRatio));
  44. let context = canvas.getContext("2d");
  45. let flags =
  46. context.DRAWWINDOW_DRAW_CARET |
  47. context.DRAWWINDOW_DRAW_VIEW |
  48. context.DRAWWINDOW_USE_WIDGET_LAYERS;
  49. context.scale(devicePixelRatio, devicePixelRatio);
  50. context.drawWindow(window, 0, 0, width, height, "rgb(255,255,255)", flags);
  51. let dataURL = canvas.toDataURL("image/png");
  52. return new LongStringActor(this.conn, dataURL);
  53. },
  54. getRawPermissionsTable: function () {
  55. return {
  56. rawPermissionsTable: PermissionsTable,
  57. UNKNOWN_ACTION: Ci.nsIPermissionManager.UNKNOWN_ACTION,
  58. ALLOW_ACTION: Ci.nsIPermissionManager.ALLOW_ACTION,
  59. DENY_ACTION: Ci.nsIPermissionManager.DENY_ACTION,
  60. PROMPT_ACTION: Ci.nsIPermissionManager.PROMPT_ACTION
  61. };
  62. }
  63. });