Policies.jsm 6.8 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
14
15
16
XPCOMUtils.defineLazyModuleGetters(this, {
  BookmarksPolicies: "resource:///modules/policies/BookmarksPolicies.jsm",
});

17
const PREF_LOGLEVEL           = "browser.policies.loglevel";
18
19
const PREF_MENU_ALREADY_DISPLAYED = "browser.policies.menuBarWasDisplayed";
const BROWSER_DOCUMENT_URL        = "chrome://browser/content/browser.xul";
20
const PREF_BOOKMARKS_ALREADY_DISPLAYED = "browser.policies.bookmarkBarWasDisplayed";
21
22

XPCOMUtils.defineLazyGetter(this, "log", () => {
23
  let { ConsoleAPI } = ChromeUtils.import("resource://gre/modules/Console.jsm", {});
24
25
26
27
28
29
30
31
32
  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,
  });
});

33
var EXPORTED_SYMBOLS = ["Policies"];
34

35
var Policies = {
36
37
38
39
40
41
42
43
  "BlockAboutAddons": {
    onBeforeUIStartup(manager, param) {
      if (param) {
        manager.disallowFeature("about:addons", true);
      }
    }
  },

44
  "BlockAboutConfig": {
45
    onBeforeUIStartup(manager, param) {
46
      if (param) {
47
48
49
50
        manager.disallowFeature("about:config", true);
      }
    }
  },
51

52
53
54
55
56
57
58
59
  "BlockAboutProfiles": {
    onBeforeUIStartup(manager, param) {
      if (param) {
        manager.disallowFeature("about:profiles", true);
      }
    }
  },

60
61
62
63
64
65
66
67
  "BlockAboutSupport": {
    onBeforeUIStartup(manager, param) {
      if (param) {
        manager.disallowFeature("about:support", true);
      }
    }
  },

68
  "BlockSetDesktopBackground": {
69
    onBeforeUIStartup(manager, param) {
70
      if (param) {
71
        manager.disallowFeature("setDesktopBackground", true);
72
73
74
75
      }
    }
  },

76
77
78
79
80
81
  "Bookmarks": {
    onAllWindowsRestored(manager, param) {
      BookmarksPolicies.processBookmarks(param);
    }
  },

82
  "Cookies": {
83
    onBeforeUIStartup(manager, param) {
84
      addAllowDenyPermissions("cookie", param.Allow, param.Block);
85
86
87
    }
  },

88
  "CreateMasterPassword": {
89
    onBeforeUIStartup(manager, param) {
90
91
      if (!param) {
        manager.disallowFeature("createMasterPassword");
92
93
94
95
      }
    }
  },

96
97
98
99
  "DisableAppUpdate": {
    onBeforeAddons(manager, param) {
      if (param) {
        manager.disallowFeature("appUpdate");
100
101
102
103
      }
    }
  },

104
105
  "DisableFirefoxScreenshots": {
    onBeforeAddons(manager, param) {
106
      if (param) {
107
108
109
110
111
        setAndLockPref("extensions.screenshots.disabled", true);
      }
    }
  },

112
113
  "DisableFirefoxStudies": {
    onBeforeAddons(manager, param) {
114
      if (param) {
115
116
117
118
119
        manager.disallowFeature("Shield");
      }
    }
  },

120
121
  "DisableFormHistory": {
    onBeforeUIStartup(manager, param) {
122
      if (param) {
123
124
125
126
127
        setAndLockPref("browser.formfill.enable", false);
      }
    }
  },

128
129
130
131
132
133
134
135
  "DisablePocket": {
    onBeforeAddons(manager, param) {
      if (param) {
        setAndLockPref("extensions.pocket.enabled", false);
      }
    }
  },

136
  "DisplayBookmarksToolbar": {
137
    onBeforeUIStartup(manager, param) {
138
139
140
141
142
143
144
145
146
147
148
149
      if (param) {
        // 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.");
        }
      }
150
    }
151
152
  },

153
  "DisplayMenuBar": {
154
    onBeforeUIStartup(manager, param) {
155
156
157
158
159
160
161
162
163
164
165
166
      if (param) {
        // 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.");
        }
      }
167
168
169
    }
  },

170
  "DontCheckDefaultBrowser": {
171
    onBeforeUIStartup(manager, param) {
172
      setAndLockPref("browser.shell.checkDefaultBrowser", false);
173
174
175
    }
  },

176
  "FlashPlugin": {
177
    onBeforeUIStartup(manager, param) {
178
      addAllowDenyPermissions("plugin:flash", param.Allow, param.Block);
179
180
181
    }
  },

182
  "InstallAddons": {
183
    onBeforeUIStartup(manager, param) {
184
      addAllowDenyPermissions("install", param.Allow, param.Block);
185
186
187
    }
  },

188
  "Popups": {
189
    onBeforeUIStartup(manager, param) {
190
191
192
193
194
195
196
      addAllowDenyPermissions("popup", param.Allow, param.Block);
    }
  },

  "RememberPasswords": {
    onBeforeUIStartup(manager, param) {
      setAndLockPref("signon.rememberSignons", param);
197
198
    }
  },
199
};
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235

/*
 * ====================
 * = 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);
}
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

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);
  }
}