summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/parent/ext-idle.js
blob: ee0c0da3744dbaa07f5fedce4f6094be3f077393 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* 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";

XPCOMUtils.defineLazyServiceGetter(
  this,
  "idleService",
  "@mozilla.org/widget/useridleservice;1",
  "nsIUserIdleService"
);

// WeakMap[Extension -> Object]
let observersMap = new WeakMap();

const getIdleObserverInfo = (extension, context) => {
  let observerInfo = observersMap.get(extension);
  if (!observerInfo) {
    observerInfo = {
      observer: null,
      detectionInterval: 60,
    };
    observersMap.set(extension, observerInfo);
    context.callOnClose({
      close: () => {
        let { observer, detectionInterval } = observersMap.get(extension);
        if (observer) {
          idleService.removeIdleObserver(observer, detectionInterval);
        }
        observersMap.delete(extension);
      },
    });
  }
  return observerInfo;
};

const getIdleObserver = (extension, context) => {
  let observerInfo = getIdleObserverInfo(extension, context);
  let { observer, detectionInterval } = observerInfo;
  if (!observer) {
    observer = new (class extends ExtensionCommon.EventEmitter {
      observe(subject, topic, data) {
        if (topic == "idle" || topic == "active") {
          this.emit("stateChanged", topic);
        }
      }
    })();
    idleService.addIdleObserver(observer, detectionInterval);
    observerInfo.observer = observer;
    observerInfo.detectionInterval = detectionInterval;
  }
  return observer;
};

const setDetectionInterval = (extension, context, newInterval) => {
  let observerInfo = getIdleObserverInfo(extension, context);
  let { observer, detectionInterval } = observerInfo;
  if (observer) {
    idleService.removeIdleObserver(observer, detectionInterval);
    idleService.addIdleObserver(observer, newInterval);
  }
  observerInfo.detectionInterval = newInterval;
};

this.idle = class extends ExtensionAPI {
  getAPI(context) {
    let { extension } = context;
    return {
      idle: {
        queryState: function(detectionIntervalInSeconds) {
          if (idleService.idleTime < detectionIntervalInSeconds * 1000) {
            return Promise.resolve("active");
          }
          return Promise.resolve("idle");
        },
        setDetectionInterval: function(detectionIntervalInSeconds) {
          setDetectionInterval(extension, context, detectionIntervalInSeconds);
        },
        onStateChanged: new EventManager({
          context,
          name: "idle.onStateChanged",
          register: fire => {
            let listener = (event, data) => {
              fire.sync(data);
            };

            getIdleObserver(extension, context).on("stateChanged", listener);
            return () => {
              getIdleObserver(extension, context).off("stateChanged", listener);
            };
          },
        }).api(),
      },
    };
  }
};