Policies.jsm 5.67 KB
Newer Older
1
2
3
4
5
6
/* 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";

7
8
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
ChromeUtils.import("resource://gre/modules/Services.jsm");
9
10
11
XPCOMUtils.defineLazyServiceGetter(this, "gXulStore",
                                   "@mozilla.org/xul/xulstore;1",
                                   "nsIXULStore");
12
13

const PREF_LOGLEVEL           = "browser.policies.loglevel";
14
15
const PREF_MENU_ALREADY_DISPLAYED = "browser.policies.menuBarWasDisplayed";
const BROWSER_DOCUMENT_URL        = "chrome://browser/content/browser.xul";
16
const PREF_BOOKMARKS_ALREADY_DISPLAYED = "browser.policies.bookmarkBarWasDisplayed";
17
18

XPCOMUtils.defineLazyGetter(this, "log", () => {
19
  let { ConsoleAPI } = ChromeUtils.import("resource://gre/modules/Console.jsm", {});
20
21
22
23
24
25
26
27
28
29
30
31
  return new ConsoleAPI({
    prefix: "Policies.jsm",
    // tip: set maxLogLevel to "debug" and use log.debug() to create detailed
    // messages during development. See LOG_LEVELS in Console.jsm for details.
    maxLogLevel: "error",
    maxLogLevelPref: PREF_LOGLEVEL,
  });
});

this.EXPORTED_SYMBOLS = ["Policies"];

this.Policies = {
32
  "BlockAboutConfig": {
33
    onBeforeUIStartup(manager, param) {
34
      if (param) {
35
36
37
38
        manager.disallowFeature("about:config", true);
      }
    }
  },
39

40
41
  "DisableAppUpdate": {
    onBeforeAddons(manager, param) {
42
      if (param) {
43
44
45
46
47
        manager.disallowFeature("appUpdate");
      }
    }
  },

48
49
  "display_menu_bar": {
    onBeforeUIStartup(manager, param) {
50
      if (param) {
51
52
53
54
55
56
57
58
59
60
61
62
63
64
        // This policy is meant to change the default behavior, not to force it.
        // If this policy was alreay applied and the user chose to re-hide the
        // menu bar, do not show it again.
        if (!Services.prefs.getBoolPref(PREF_MENU_ALREADY_DISPLAYED, false)) {
          log.debug("Showing the menu bar");
          gXulStore.setValue(BROWSER_DOCUMENT_URL, "toolbar-menubar", "autohide", "false");
          Services.prefs.setBoolPref(PREF_MENU_ALREADY_DISPLAYED, true);
        } else {
          log.debug("Not showing the menu bar because it has already been shown.");
        }
      }
    }
  },

65
66
  "display_bookmarks_toolbar": {
    onBeforeUIStartup(manager, param) {
67
      if (param) {
68
69
70
71
72
73
74
75
76
77
78
79
80
81
        // This policy is meant to change the default behavior, not to force it.
        // If this policy was alreay applied and the user chose to re-hide the
        // bookmarks toolbar, do not show it again.
        if (!Services.prefs.getBoolPref(PREF_BOOKMARKS_ALREADY_DISPLAYED, false)) {
          log.debug("Showing the bookmarks toolbar");
          gXulStore.setValue(BROWSER_DOCUMENT_URL, "PersonalToolbar", "collapsed", "false");
          Services.prefs.setBoolPref(PREF_BOOKMARKS_ALREADY_DISPLAYED, true);
        } else {
          log.debug("Not showing the bookmarks toolbar because it has already been shown.");
        }
      }
    }
  },

82
83
  "block_set_desktop_background": {
    onBeforeUIStartup(manager, param) {
84
      if (param) {
85
        manager.disallowFeature("setDesktopBackground", true);
86
87
88
89
      }
    }
  },

90
91
  "DisableFirefoxScreenshots": {
    onBeforeAddons(manager, param) {
92
      if (param) {
93
94
95
96
97
        setAndLockPref("extensions.screenshots.disabled", true);
      }
    }
  },

98
99
  "DisableFirefoxStudies": {
    onBeforeAddons(manager, param) {
100
      if (param) {
101
102
103
104
105
        manager.disallowFeature("Shield");
      }
    }
  },

106
107
  "DisableFormHistory": {
    onBeforeUIStartup(manager, param) {
108
      if (param) {
109
110
111
112
113
        setAndLockPref("browser.formfill.enable", false);
      }
    }
  },

114
115
116
117
  "dont_check_default_browser": {
    onBeforeUIStartup(manager, param) {
      setAndLockPref("browser.shell.checkDefaultBrowser", false);
    }
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  },

  "flash_plugin": {
    onBeforeUIStartup(manager, param) {
      addAllowDenyPermissions("plugin:flash", param.allow, param.block);
    }
  },

  "popups": {
    onBeforeUIStartup(manager, param) {
      addAllowDenyPermissions("popup", param.allow, param.block);
    }
  },

  "install_addons": {
    onBeforeUIStartup(manager, param) {
      addAllowDenyPermissions("install", param.allow, param.block);
    }
  },

  "cookies": {
    onBeforeUIStartup(manager, param) {
      addAllowDenyPermissions("cookie", param.allow, param.block);
    }
  },
143
};
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

/*
 * ====================
 * = HELPER FUNCTIONS =
 * ====================
 *
 * The functions below are helpers to be used by several policies.
 */

function setAndLockPref(prefName, prefValue) {
  if (Services.prefs.prefIsLocked(prefName)) {
    Services.prefs.unlockPref(prefName);
  }

  let defaults = Services.prefs.getDefaultBranch("");

  switch (typeof(prefValue)) {
    case "boolean":
      defaults.setBoolPref(prefName, prefValue);
      break;

    case "number":
      if (!Number.isInteger(prefValue)) {
        throw new Error(`Non-integer value for ${prefName}`);
      }

      defaults.setIntPref(prefName, prefValue);
      break;

    case "string":
      defaults.setStringPref(prefName, prefValue);
      break;
  }

  Services.prefs.lockPref(prefName);
}
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

function addAllowDenyPermissions(permissionName, allowList, blockList) {
  allowList = allowList || [];
  blockList = blockList || [];

  for (let origin of allowList) {
    Services.perms.add(origin,
                       permissionName,
                       Ci.nsIPermissionManager.ALLOW_ACTION,
                       Ci.nsIPermissionManager.EXPIRE_POLICY);
  }

  for (let origin of blockList) {
    Services.perms.add(origin,
                       permissionName,
                       Ci.nsIPermissionManager.DENY_ACTION,
                       Ci.nsIPermissionManager.EXPIRE_POLICY);
  }
}