Bug 1492736 - Part 2. Acquire wake lock when device actor is attached. r=ochameau
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Wed, 10 Oct 2018 13:47:51 +0900
changeset 442654 e358e36fc048abf53d144f70a371c99fd634ba07
parent 442653 77ccb45257313e29d9c93c7faa045ee18d85a57e
child 442655 6868407681bb5a527d1ebff34d5c1f081dea5554
push id109212
push userm_kato@ga2.so-net.ne.jp
push dateWed, 24 Oct 2018 01:52:25 +0000
treeherdermozilla-inbound@e358e36fc048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersochameau
bugs1492736
milestone65.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1492736 - Part 2. Acquire wake lock when device actor is attached. r=ochameau Use wake lock to keep screen when device is attached from debugger. Differential Revision: https://phabricator.services.mozilla.com/D7161
devtools/server/actors/device.js
--- a/devtools/server/actors/device.js
+++ b/devtools/server/actors/device.js
@@ -1,39 +1,88 @@
 /* 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 {Ci, Cc} = require("chrome");
 const Services = require("Services");
 const protocol = require("devtools/shared/protocol");
 const {LongStringActor} = require("devtools/server/actors/string");
 const {DebuggerServer} = require("devtools/server/main");
 const {getSystemInfo} = require("devtools/shared/system");
 const {deviceSpec} = require("devtools/shared/specs/device");
+const {AppConstants} = require("resource://gre/modules/AppConstants.jsm");
 
 exports.DeviceActor = protocol.ActorClassWithSpec(deviceSpec, {
-  _desc: null,
+  initialize: function(conn) {
+    protocol.Actor.prototype.initialize.call(this, conn);
+    // pageshow and pagehide event release wake lock, so we have to acquire
+    // wake lock again by pageshow event
+    this._onPageShow = this._onPageShow.bind(this);
+    if (this._window) {
+      this._window.addEventListener("pageshow", this._onPageShow, true);
+    }
+    this._acquireWakeLock();
+  },
+
+  destroy: function() {
+    protocol.Actor.prototype.destroy.call(this);
+    this._releaseWakeLock();
+    if (this._window) {
+      this._window.removeEventListener("pageshow", this._onPageShow, true);
+    }
+  },
 
   getDescription: function() {
     return getSystemInfo();
   },
 
   screenshotToDataURL: function() {
-    const window = Services.wm.getMostRecentWindow(DebuggerServer.chromeWindowType);
+    const window = this._window;
     const { devicePixelRatio } = window;
     const canvas = window.document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
     const width = window.innerWidth;
     const height = window.innerHeight;
     canvas.setAttribute("width", Math.round(width * devicePixelRatio));
     canvas.setAttribute("height", Math.round(height * devicePixelRatio));
     const context = canvas.getContext("2d");
     const flags =
           context.DRAWWINDOW_DRAW_CARET |
           context.DRAWWINDOW_DRAW_VIEW |
           context.DRAWWINDOW_USE_WIDGET_LAYERS;
     context.scale(devicePixelRatio, devicePixelRatio);
     context.drawWindow(window, 0, 0, width, height, "rgb(255,255,255)", flags);
     const dataURL = canvas.toDataURL("image/png");
     return new LongStringActor(this.conn, dataURL);
   },
+
+  _acquireWakeLock: function() {
+    if (AppConstants.platform !== "android") {
+      return;
+    }
+
+    const pm = Cc["@mozilla.org/power/powermanagerservice;1"]
+               .getService(Ci.nsIPowerManagerService);
+    this._wakelock = pm.newWakeLock("screen", this._window);
+  },
+
+  _releaseWakeLock: function() {
+    if (this._wakelock) {
+      try {
+        this._wakelock.unlock();
+      } catch (e) {
+        // Ignore error since wake lock is already unlocked
+      }
+      this._wakelock = null;
+    }
+  },
+
+  _onPageShow: function() {
+    this._releaseWakeLock();
+    this._acquireWakeLock();
+  },
+
+  get _window() {
+    return Services.wm.getMostRecentWindow(DebuggerServer.chromeWindowType);
+  }
 });