BrowserGlue.jsm 133 KB
Newer Older
1
2
3
/* 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/. */
4

5
6
var EXPORTED_SYMBOLS = ["BrowserGlue", "ContentPermissionPrompt"];

7
8
const XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";

9
10
11
const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
const {AppConstants} = ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
12

13
14
15
ChromeUtils.defineModuleGetter(this, "ActorManagerParent",
                               "resource://gre/modules/ActorManagerParent.jsm");

16
17
const PREF_PDFJS_ENABLED_CACHE_STATE = "pdfjs.enabledCache.state";

18
let ACTORS = {
19
20
21
22
23
24
25
  AboutReader: {
    child: {
      module: "resource:///actors/AboutReaderChild.jsm",
      group: "browsers",
      events: {
        "AboutReaderContentLoaded": {wantUntrusted: true},
        "DOMContentLoaded": {},
26
27
        "pageshow": {mozSystemGroup: true},
        "pagehide": {mozSystemGroup: true},
28
29
30
31
32
33
34
35
      },
      messages: [
        "Reader:ToggleReaderMode",
        "Reader:PushState",
      ],
    },
  },

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  BlockedSite: {
    child: {
      module: "resource:///actors/BlockedSiteChild.jsm",
      events: {
        "AboutBlockedLoaded": {wantUntrusted: true},
        "click": {},
      },
      matches: ["about:blocked?*"],
      allFrames: true,
      messages: [
        "DeceptiveBlockedDetails",
      ],
    },
  },

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  BrowserTab: {
    child: {
      module: "resource:///actors/BrowserTabChild.jsm",
      group: "browsers",

      events: {
        "DOMWindowCreated": {once: true},
        "MozAfterPaint": {once: true},
        "MozDOMPointerLock:Entered": {},
        "MozDOMPointerLock:Exited": {},
      },

      messages: [
        "AllowScriptsToClose",
        "Browser:AppTab",
        "Browser:HasSiblings",
        "Browser:Reload",
        "MixedContent:ReenableProtection",
        "SwitchDocumentDirection",
        "UpdateCharacterSet",
      ],
    },
  },
74

75
76
77
78
79
  ClickHandler: {
    child: {
      module: "resource:///actors/ClickHandlerChild.jsm",
      events: {
        "click": {capture: true, mozSystemGroup: true},
80
      },
81
82
83
    },
  },

84
85
86
87
88
89
90
91
92
  ContextMenu: {
    child: {
      module: "resource:///actors/ContextMenuChild.jsm",
      events: {
        "contextmenu": {mozSystemGroup: true},
      },
    },
  },

93
94
95
96
  ContentSearch: {
    child: {
      module: "resource:///actors/ContentSearchChild.jsm",
      group: "browsers",
97
      matches: ["about:home", "about:newtab", "about:welcome", "about:privatebrowsing",
98
99
100
101
102
103
                "chrome://mochitests/content/*"],
      events: {
        "ContentSearchClient": {capture: true, wantUntrusted: true},
      },
      messages: [
        "ContentSearch",
104
      ],
105
106
107
    },
  },

108
109
110
111
112
113
114
115
116
117
118
119
120
121
  DOMFullscreen: {
    child: {
      module: "resource:///actors/DOMFullscreenChild.jsm",
      group: "browsers",
      events: {
        "MozDOMFullscreen:Request": {},
        "MozDOMFullscreen:Entered": {},
        "MozDOMFullscreen:NewOrigin": {},
        "MozDOMFullscreen:Exit": {},
        "MozDOMFullscreen:Exited": {},
      },
      messages: [
        "DOMFullscreen:Entered",
        "DOMFullscreen:CleanUp",
122
      ],
123
124
125
    },
  },

126
  FormValidation: {
127
    child: {
128
      module: "resource:///actors/FormValidationChild.jsm",
129
130
131
132
133
134
      events: {
        "MozInvalidForm": {},
      },
    },
  },

135
136
137
138
139
140
141
142
143
144
145
  LightWeightThemeInstall: {
    child: {
      module: "resource:///actors/LightWeightThemeInstallChild.jsm",
      events: {
        "InstallBrowserTheme": {wantUntrusted: true},
        "PreviewBrowserTheme": {wantUntrusted: true},
        "ResetBrowserThemePreview": {wantUntrusted: true},
      },
    },
  },

146
147
148
149
150
151
152
153
154
155
  LightweightTheme: {
    child: {
      module: "resource:///actors/LightweightThemeChild.jsm",
      matches: ["about:home", "about:newtab", "about:welcome"],
      events: {
        "pageshow": {mozSystemGroup: true},
      },
    },
  },

156
157
158
159
160
161
162
163
164
165
166
167
168
  LinkHandler: {
    child: {
      module: "resource:///actors/LinkHandlerChild.jsm",
      events: {
        "DOMHeadElementParsed": {},
        "DOMLinkAdded": {},
        "DOMLinkChanged": {},
        "pageshow": {},
        "pagehide": {},
      },
    },
  },

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  NetError: {
    child: {
      module: "resource:///actors/NetErrorChild.jsm",
      events: {
        "AboutNetErrorLoad": {wantUntrusted: true},
        "AboutNetErrorOpenCaptivePortal": {wantUntrusted: true},
        "AboutNetErrorSetAutomatic": {wantUntrusted: true},
        "AboutNetErrorResetPreferences": {wantUntrusted: true},
        "click": {},
      },
      matches: ["about:certerror?*", "about:neterror?*"],
      allFrames: true,
      messages: [
        "Browser:CaptivePortalFreed",
        "CertErrorDetails",
      ],
    },
  },

188
189
190
191
192
193
194
195
196
197
198
199
  OfflineApps: {
    child: {
      module: "resource:///actors/OfflineAppsChild.jsm",
      events: {
        "MozApplicationManifest": {},
      },
      messages: [
        "OfflineApps:StartFetching",
      ],
    },
  },

200
201
202
203
204
  PageInfo: {
    child: {
      module: "resource:///actors/PageInfoChild.jsm",
      messages: ["PageInfo:getData"],
    },
205
206
  },

207
208
209
210
211
212
213
214
215
216
  PageStyle: {
    child: {
      module: "resource:///actors/PageStyleChild.jsm",
      group: "browsers",
      events: {
        "pageshow": {},
      },
      messages: [
        "PageStyle:Switch",
        "PageStyle:Disable",
217
      ],
218
219
    },
  },
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

  Plugin: {
    child: {
      module: "resource:///actors/PluginChild.jsm",
      events: {
        "PluginBindingAttached": {capture: true, wantUntrusted: true},
        "PluginCrashed": {capture: true},
        "PluginOutdated": {capture: true},
        "PluginInstantiated": {capture: true},
        "PluginRemoved": {capture: true},
        "HiddenPlugin": {capture: true},
      },

      messages: [
        "BrowserPlugins:ActivatePlugins",
        "BrowserPlugins:ContextMenuCommand",
        "BrowserPlugins:NPAPIPluginProcessCrashed",
        "BrowserPlugins:CrashReportSubmitted",
        "BrowserPlugins:Test:ClearCrashData",
      ],

      observers: [
        "decoder-doctor-notification",
      ],
    },
  },
246

247
248
249
250
251
252
253
254
255
256
  RFPHelper: {
    child: {
      module: "resource:///actors/RFPHelperChild.jsm",
      group: "browsers",
      events: {
        "resize": {},
      },
    },
  },

257
258
259
260
261
262
263
264
265
266
  SearchTelemetry: {
    child: {
      module: "resource:///actors/SearchTelemetryChild.jsm",
      events: {
        DOMContentLoaded: {},
        "pageshow": {mozSystemGroup: true},
      },
    },
  },

267
268
269
270
271
272
273
274
275
276
  ShieldFrame: {
    child: {
      module: "resource://normandy-content/ShieldFrameChild.jsm",
      events: {
        "ShieldPageEvent": {wantUntrusted: true},
      },
      matches: ["about:studies"],
    },
  },

277
278
279
280
281
282
283
284
285
286
  UITour: {
    child: {
      module: "resource:///modules/UITourChild.jsm",
      events: {
        "mozUITour": {wantUntrusted: true},
      },
      permissions: ["uitour"],
    },
  },

287
288
289
290
291
292
293
  URIFixup: {
    child: {
      module: "resource:///actors/URIFixupChild.jsm",
      group: "browsers",
      observers: ["keyword-uri-fixup"],
    },
  },
294
295
296
297
298
299
300
301
302
303
304
305
306

  WebRTC: {
    child: {
      module: "resource:///actors/WebRTCChild.jsm",
      messages: [
        "rtcpeer:Allow",
        "rtcpeer:Deny",
        "webrtc:Allow",
        "webrtc:Deny",
        "webrtc:StopSharing",
      ],
    },
  },
307
308
};

309
310
311
312
(function earlyBlankFirstPaint() {
  if (!Services.prefs.getBoolPref("browser.startup.blankWindow", false))
    return;

313
314
315
316
317
318
  // Until bug 1450626 and bug 1488384 are fixed, skip the blank window when
  // using a non-default theme.
  if (Services.prefs.getCharPref("lightweightThemes.selectedThemeID", "") !=
        "default-theme@mozilla.org")
    return;

319
  let store = Services.xulStore;
320
  let getValue = attr =>
321
    store.getValue(AppConstants.BROWSER_CHROME_URL, "main-window", attr);
322
323
324
325
326
327
328
329
330
  let width = getValue("width");
  let height = getValue("height");

  // The clean profile case isn't handled yet. Return early for now.
  if (!width || !height)
    return;

  let browserWindowFeatures =
    "chrome,all,dialog=no,extrachrome,menubar,resizable,scrollbars,status," +
331
    "location,toolbar,personalbar";
332
333
334
335
  let win = Services.ww.openWindow(null, "about:blank", null,
                                   browserWindowFeatures, null);

  // Hide the titlebar if the actual browser window will draw in it.
336
337
338
339
  let hiddenTitlebar =
    Services.prefs.getBoolPref("browser.tabs.drawInTitlebar",
      win.matchMedia("(-moz-gtk-csd-hide-titlebar-by-default)").matches);
  if (hiddenTitlebar) {
340
    win.windowUtils.setChromeMargin(0, 2, 2, 2);
341
342
  }

343
344
345
  let docElt = win.document.documentElement;
  docElt.setAttribute("screenX", getValue("screenX"));
  docElt.setAttribute("screenY", getValue("screenY"));
346
347
348
349
350
351

  // The sizemode="maximized" attribute needs to be set before first paint.
  let sizemode = getValue("sizemode");
  if (sizemode == "maximized") {
    docElt.setAttribute("sizemode", sizemode);

352
353
354
355
356
357
358
359
    // Set the size to use when the user leaves the maximized mode.
    // The persisted size is the outer size, but the height/width
    // attributes set the inner size.
    let xulWin = win.docShell.treeOwner
                    .QueryInterface(Ci.nsIInterfaceRequestor)
                    .getInterface(Ci.nsIXULWindow);
    height -= xulWin.outerToInnerHeightDifferenceInCSSPixels;
    width -= xulWin.outerToInnerWidthDifferenceInCSSPixels;
360
361
362
363
364
365
366
367
    docElt.setAttribute("height", height);
    docElt.setAttribute("width", width);
  } else {
    // Setting the size of the window in the features string instead of here
    // causes the window to grow by the size of the titlebar.
    win.resizeTo(width, height);
  }

368
369
370
371
  // Set this before showing the window so that graphics code can use it to
  // decide to skip some expensive code paths (eg. starting the GPU process).
  docElt.setAttribute("windowtype", "navigator:blank");

372
373
374
  // The window becomes visible after OnStopRequest, so make this happen now.
  win.stop();

375
  let { TelemetryTimestamps } =
376
    ChromeUtils.import("resource://gre/modules/TelemetryTimestamps.jsm");
377
  TelemetryTimestamps.add("blankWindowShown");
378
379
})();

380
XPCOMUtils.defineLazyServiceGetters(this, {
381
  aboutNewTabService: ["@mozilla.org/browser/aboutnewtab-service;1", "nsIAboutNewTabService"],
382
});
383
384
385
XPCOMUtils.defineLazyGetter(this, "WeaveService", () =>
  Cc["@mozilla.org/weave/service;1"].getService().wrappedJSObject
);
386

387
// lazy module getters
388

389
XPCOMUtils.defineLazyModuleGetters(this, {
390
  AboutPrivateBrowsingHandler: "resource:///modules/aboutpages/AboutPrivateBrowsingHandler.jsm",
391
392
393
394
  AddonManager: "resource://gre/modules/AddonManager.jsm",
  AppMenuNotifications: "resource://gre/modules/AppMenuNotifications.jsm",
  AsyncShutdown: "resource://gre/modules/AsyncShutdown.jsm",
  AutoCompletePopup: "resource://gre/modules/AutoCompletePopup.jsm",
395
  Blocklist: "resource://gre/modules/Blocklist.jsm",
396
397
398
  BookmarkHTMLUtils: "resource://gre/modules/BookmarkHTMLUtils.jsm",
  BookmarkJSONUtils: "resource://gre/modules/BookmarkJSONUtils.jsm",
  BrowserUsageTelemetry: "resource:///modules/BrowserUsageTelemetry.jsm",
399
  BrowserWindowTracker: "resource:///modules/BrowserWindowTracker.jsm",
400
  ContextualIdentityService: "resource://gre/modules/ContextualIdentityService.jsm",
401
  DateTimePickerParent: "resource://gre/modules/DateTimePickerParent.jsm",
402
  Discovery: "resource:///modules/Discovery.jsm",
403
  ExtensionsUI: "resource:///modules/ExtensionsUI.jsm",
404
  FileSource: "resource://gre/modules/L10nRegistry.jsm",
405
  FxAccounts: "resource://gre/modules/FxAccounts.jsm",
406
  HomePage: "resource:///modules/HomePage.jsm",
407
  HybridContentTelemetry: "resource://gre/modules/HybridContentTelemetry.jsm",
408
  Integration: "resource://gre/modules/Integration.jsm",
409
  L10nRegistry: "resource://gre/modules/L10nRegistry.jsm",
410
  LightweightThemeManager: "resource://gre/modules/LightweightThemeManager.jsm",
411
  LiveBookmarkMigrator: "resource:///modules/LiveBookmarkMigrator.jsm",
412
  NewTabUtils: "resource://gre/modules/NewTabUtils.jsm",
413
  Normandy: "resource://normandy/Normandy.jsm",
414
  ObjectUtils: "resource://gre/modules/ObjectUtils.jsm",
415
416
417
418
419
  OS: "resource://gre/modules/osfile.jsm",
  PageActions: "resource:///modules/PageActions.jsm",
  PageThumbs: "resource://gre/modules/PageThumbs.jsm",
  PdfJs: "resource://pdf.js/PdfJs.jsm",
  PermissionUI: "resource:///modules/PermissionUI.jsm",
420
  PingCentre: "resource:///modules/PingCentre.jsm",
421
422
423
424
425
  PlacesBackups: "resource://gre/modules/PlacesBackups.jsm",
  PlacesUtils: "resource://gre/modules/PlacesUtils.jsm",
  PluralForm: "resource://gre/modules/PluralForm.jsm",
  PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.jsm",
  ProcessHangMonitor: "resource:///modules/ProcessHangMonitor.jsm",
426
  RemoteSettings: "resource://services-settings/remote-settings.js",
427
  RFPHelper: "resource://gre/modules/RFPHelper.jsm",
428
  SafeBrowsing: "resource://gre/modules/SafeBrowsing.jsm",
429
  Sanitizer: "resource:///modules/Sanitizer.jsm",
430
  SaveToPocket: "chrome://pocket/content/SaveToPocket.jsm",
431
  SearchTelemetry: "resource:///modules/SearchTelemetry.jsm",
432
  SessionStartup: "resource:///modules/sessionstore/SessionStartup.jsm",
433
434
435
  SessionStore: "resource:///modules/sessionstore/SessionStore.jsm",
  ShellService: "resource:///modules/ShellService.jsm",
  TabCrashHandler: "resource:///modules/ContentCrashHandlers.jsm",
436
  TabUnloader: "resource:///modules/TabUnloader.jsm",
437
438
439
440
441
  UIState: "resource://services-sync/UIState.jsm",
  UITour: "resource:///modules/UITour.jsm",
  WebChannel: "resource://gre/modules/WebChannel.jsm",
  WindowsRegistry: "resource://gre/modules/WindowsRegistry.jsm",
});
442

443
444
445
446
447
448
449
450
451
452
453
// eslint-disable-next-line no-unused-vars
XPCOMUtils.defineLazyModuleGetters(this, {
  AsyncPrefs: "resource://gre/modules/AsyncPrefs.jsm",
  ContentClick: "resource:///modules/ContentClick.jsm",
  FormValidationHandler: "resource:///modules/FormValidationHandler.jsm",
  LoginManagerParent: "resource://gre/modules/LoginManagerParent.jsm",
  PictureInPicture: "resource://gre/modules/PictureInPicture.jsm",
  ReaderParent: "resource:///modules/ReaderParent.jsm",
  RemotePrompt: "resource:///modules/RemotePrompt.jsm",
});

454
/* global ContentPrefServiceParent:false, ContentSearch:false,
455
          UpdateListener:false, webrtcUI:false */
456

457
458
459
460
461
/**
 * IF YOU ADD OR REMOVE FROM THIS LIST, PLEASE UPDATE THE LIST ABOVE AS WELL.
 * XXX Bug 1325373 is for making eslint detect these automatically.
 */

462
463
let initializedModules = {};

464
[
465
  ["ContentPrefServiceParent", "resource://gre/modules/ContentPrefServiceParent.jsm", "alwaysInit"],
466
  ["ContentSearch", "resource:///modules/ContentSearch.jsm", "init"],
467
  ["UpdateListener", "resource://gre/modules/UpdateListener.jsm", "init"],
468
469
  ["webrtcUI", "resource:///modules/webrtcUI.jsm", "init"],
].forEach(([name, resource, init]) => {
470
  XPCOMUtils.defineLazyGetter(this, name, () => {
471
    ChromeUtils.import(resource, initializedModules);
472
473
474
    initializedModules[name][init]();
    return initializedModules[name];
  });
475
});
476

477
if (AppConstants.MOZ_CRASHREPORTER) {
478
479
480
481
  XPCOMUtils.defineLazyModuleGetters(this, {
    PluginCrashReporter: "resource:///modules/ContentCrashHandlers.jsm",
    UnsubmittedCrashHandler: "resource:///modules/ContentCrashHandlers.jsm",
  });
482
}
483

484
XPCOMUtils.defineLazyGetter(this, "gBrandBundle", function() {
485
  return Services.strings.createBundle("chrome://branding/locale/brand.properties");
486
487
488
});

XPCOMUtils.defineLazyGetter(this, "gBrowserBundle", function() {
489
  return Services.strings.createBundle("chrome://browser/locale/browser.properties");
490
491
});

492
493
494
495
XPCOMUtils.defineLazyGetter(this, "gTabbrowserBundle", function() {
  return Services.strings.createBundle("chrome://browser/locale/tabbrowser.properties");
});

496
497
498
const global = this;

const listeners = {
499
500
501
502
503
504
505
  observers: {
    "update-staged": ["UpdateListener"],
    "update-downloaded": ["UpdateListener"],
    "update-available": ["UpdateListener"],
    "update-error": ["UpdateListener"],
  },

506
  ppmm: {
507
508
509
510
511
    // PLEASE KEEP THIS LIST IN SYNC WITH THE LISTENERS ADDED IN ContentPrefServiceParent.init
    "ContentPrefs:FunctionCall": ["ContentPrefServiceParent"],
    "ContentPrefs:AddObserverForName": ["ContentPrefServiceParent"],
    "ContentPrefs:RemoveObserverForName": ["ContentPrefServiceParent"],
    // PLEASE KEEP THIS LIST IN SYNC WITH THE LISTENERS ADDED IN ContentPrefServiceParent.init
512
513
514
515
516
517

    // PLEASE KEEP THIS LIST IN SYNC WITH THE LISTENERS ADDED IN AsyncPrefs.init
    "AsyncPrefs:SetPref": ["AsyncPrefs"],
    "AsyncPrefs:ResetPref": ["AsyncPrefs"],
    // PLEASE KEEP THIS LIST IN SYNC WITH THE LISTENERS ADDED IN AsyncPrefs.init

518
519
520
521
522
    "webrtc:UpdateGlobalIndicators": ["webrtcUI"],
    "webrtc:UpdatingIndicators": ["webrtcUI"],
  },

  mm: {
523
    "Content:Click": ["ContentClick"],
524
    "ContentSearch": ["ContentSearch"],
525
526
    "FormValidation:ShowPopup": ["FormValidationHandler"],
    "FormValidation:HidePopup": ["FormValidationHandler"],
527
528
    "PictureInPicture:Request": ["PictureInPicture"],
    "PictureInPicture:Close": ["PictureInPicture"],
529
    "Prompt:Open": ["RemotePrompt"],
530
531
    "Reader:FaviconRequest": ["ReaderParent"],
    "Reader:UpdateReaderButton": ["ReaderParent"],
532
    // PLEASE KEEP THIS LIST IN SYNC WITH THE MOBILE LISTENERS IN BrowserCLH.js
533
534
535
536
537
538
    "PasswordManager:findLogins": ["LoginManagerParent"],
    "PasswordManager:findRecipes": ["LoginManagerParent"],
    "PasswordManager:onFormSubmit": ["LoginManagerParent"],
    "PasswordManager:autoCompleteLogins": ["LoginManagerParent"],
    "PasswordManager:removeLogin": ["LoginManagerParent"],
    "PasswordManager:insecureLoginFormPresent": ["LoginManagerParent"],
539
    "PasswordManager:OpenPreferences": ["LoginManagerParent"],
540
    // PLEASE KEEP THIS LIST IN SYNC WITH THE MOBILE LISTENERS IN BrowserCLH.js
541
542
543
544
545
546
547
548
    "rtcpeer:CancelRequest": ["webrtcUI"],
    "rtcpeer:Request": ["webrtcUI"],
    "webrtc:CancelRequest": ["webrtcUI"],
    "webrtc:Request": ["webrtcUI"],
    "webrtc:StopRecording": ["webrtcUI"],
    "webrtc:UpdateBrowserIndicators": ["webrtcUI"],
  },

549
550
551
552
553
554
555
556
557
558
  observe(subject, topic, data) {
    for (let module of this.observers[topic]) {
      try {
        global[module].observe(subject, topic, data);
      } catch (e) {
        Cu.reportError(e);
      }
    }
  },

559
  receiveMessage(modules, data) {
560
    let val;
561
562
    for (let module of modules[data.name]) {
      try {
563
        val = global[module].receiveMessage(data) || val;
564
565
566
567
      } catch (e) {
        Cu.reportError(e);
      }
    }
568
    return val;
569
570
571
  },

  init() {
572
573
574
575
    for (let observer of Object.keys(this.observers)) {
      Services.obs.addObserver(this, observer);
    }

576
577
578
579
580
581
582
583
584
    let receiveMessageMM = this.receiveMessage.bind(this, this.mm);
    for (let message of Object.keys(this.mm)) {
      Services.mm.addMessageListener(message, receiveMessageMM);
    }

    let receiveMessagePPMM = this.receiveMessage.bind(this, this.ppmm);
    for (let message of Object.keys(this.ppmm)) {
      Services.ppmm.addMessageListener(message, receiveMessagePPMM);
    }
585
  },
586
587
};

588
// Seconds of idle before trying to create a bookmarks backup.
589
const BOOKMARKS_BACKUP_IDLE_TIME_SEC = 8 * 60;
590
591
592
593
594
// Minimum interval between backups.  We try to not create more than one backup
// per interval.
const BOOKMARKS_BACKUP_MIN_INTERVAL_DAYS = 1;
// Maximum interval between backups.  If the last backup is older than these
// days we will try to create a new one more aggressively.
595
const BOOKMARKS_BACKUP_MAX_INTERVAL_DAYS = 3;
596
597
// Seconds of idle time before the late idle tasks will be scheduled.
const LATE_TASKS_IDLE_TIME_SEC = 20;
598
599
// Time after we stop tracking startup crashes.
const STARTUP_CRASHES_END_DELAY_MS = 30 * 1000;
600

601
602
603
604
605
/*
 * OS X has the concept of zero-window sessions and therefore ignores the
 * browser-lastwindow-close-* topics.
 */
const OBSERVE_LASTWINDOW_CLOSE_TOPICS = AppConstants.platform != "macosx";
606
607

function BrowserGlue() {
608
609
610
611
  XPCOMUtils.defineLazyServiceGetter(this, "_idleService",
                                     "@mozilla.org/widget/idleservice;1",
                                     "nsIIdleService");

612
  XPCOMUtils.defineLazyGetter(this, "_distributionCustomizer", function() {
613
                                const {DistributionCustomizer} = ChromeUtils.import("resource:///modules/distribution.js");
614
615
616
                                return new DistributionCustomizer();
                              });

617
  XPCOMUtils.defineLazyServiceGetter(this, "AlertsService", "@mozilla.org/alerts-service;1", "nsIAlertsService");
618

619
620
621
622
  this._init();
}

BrowserGlue.prototype = {
623
  _saveSession: false,
624
  _migrationImportsDefaultBookmarks: false,
625
  _placesBrowserInitComplete: false,
626

627
  _setPrefToSaveSession: function BG__setPrefToSaveSession(aForce) {
628
629
630
    if (!this._saveSession && !aForce)
      return;

631
632
633
    if (!PrivateBrowsingUtils.permanentPrivateBrowsing) {
      Services.prefs.setBoolPref("browser.sessionstore.resume_session_once", true);
    }
634
635
636
637

    // This method can be called via [NSApplication terminate:] on Mac, which
    // ends up causing prefs not to be flushed to disk, so we need to do that
    // explicitly here. See bug 497652.
638
    Services.prefs.savePrefFile(null);
639
640
  },

641
642
643
644
645
646
647
648
649
650
651
652
  _setSyncAutoconnectDelay: function BG__setSyncAutoconnectDelay() {
    // Assume that a non-zero value for services.sync.autoconnectDelay should override
    if (Services.prefs.prefHasUserValue("services.sync.autoconnectDelay")) {
      let prefDelay = Services.prefs.getIntPref("services.sync.autoconnectDelay");

      if (prefDelay > 0)
        return;
    }

    // delays are in seconds
    const MAX_DELAY = 300;
    let delay = 3;
653
    for (let win of Services.wm.getEnumerator("navigator:browser")) {
654
655
656
657
658
      // browser windows without a gBrowser almost certainly means we are
      // shutting down, so instead of just ignoring that window we abort.
      if (win.closed || !win.gBrowser) {
        return;
      }
659
      delay += win.gBrowser.tabs.length;
660
661
662
    }
    delay = delay <= MAX_DELAY ? delay : MAX_DELAY;

663
    const {Weave} = ChromeUtils.import("resource://services-sync/main.js");
664
    Weave.Service.scheduler.delayedAutoConnect(delay);
665
666
  },

667
668
669
670
671
672
  /**
   * Lazily initialize PingCentre
   */
  get pingCentre() {
    const MAIN_TOPIC_ID = "main";
    Object.defineProperty(this, "pingCentre", {
673
      value: new PingCentre({ topic: MAIN_TOPIC_ID }),
674
675
676
677
678
    });
    return this.pingCentre;
  },

  _sendMainPingCentrePing() {
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
    let newTabSetting;
    let homePageSetting;

    // Check whether or not about:home and about:newtab have been overridden at this point.
    // Different settings are encoded as follows:
    //   * Value 0: default
    //   * Value 1: about:blank
    //   * Value 2: web extension
    //   * Value 3: other custom URL(s)
    // Settings for about:newtab and about:home are combined in a bitwise manner.

    // Note that a user could use about:blank and web extension at the same time
    // to overwrite the about:newtab, but the web extension takes priority, so the
    // ordering matters in the following check.
    if (Services.prefs.getBoolPref("browser.newtabpage.enabled") &&
                                   !aboutNewTabService.overridden) {
      newTabSetting = 0;
    } else if (aboutNewTabService.newTabURL.startsWith("moz-extension://")) {
      newTabSetting = 2;
    } else if (!Services.prefs.getBoolPref("browser.newtabpage.enabled")) {
      newTabSetting = 1;
    } else {
      newTabSetting = 3;
    }

704
    const homePageURL = HomePage.get();
705
706
707
708
709
710
711
712
713
714
    if (homePageURL === "about:home") {
      homePageSetting = 0;
    } else if (homePageURL === "about:blank") {
      homePageSetting = 1;
    } else if (homePageURL.startsWith("moz-extension://")) {
      homePageSetting = 2;
    } else {
      homePageSetting = 3;
    }

715
716
    const payload = {
      event: "AS_ENABLED",
717
      value: newTabSetting | (homePageSetting << 2),
718
    };
719
    const ACTIVITY_STREAM_ID = "activity-stream";
720
721
722
723
    const options = {filter: ACTIVITY_STREAM_ID};
    this.pingCentre.sendPing(payload, options);
  },

724
  // nsIObserver implementation
725
  observe: async function BG_observe(subject, topic, data) {
726
    switch (topic) {
727
      case "notifications-open-settings":
728
        this._openPreferences("privacy-permissions", { origin: "notifOpenSettings" });
729
        break;
730
      case "final-ui-startup":
731
        this._beforeUIStartup();
732
        break;
733
      case "browser-delayed-startup-finished":
734
        this._onFirstWindowLoaded(subject);
735
736
        Services.obs.removeObserver(this, "browser-delayed-startup-finished");
        break;
737
      case "sessionstore-windows-restored":
738
        this._onWindowsRestored();
739
        break;
740
741
      case "browser:purge-session-history":
        // reset the console service's error buffer
742
743
        Services.console.logStringMessage(null); // clear the console (in case it's open)
        Services.console.reset();
744
        break;
745
746
747
      case "restart-in-safe-mode":
        this._onSafeModeRestart();
        break;
748
      case "quit-application-requested":
749
        this._onQuitRequest(subject, data);
750
751
        break;
      case "quit-application-granted":
752
        this._onQuitApplicationGranted();
753
        break;
754
      case "browser-lastwindow-close-requested":
755
756
757
758
759
        if (OBSERVE_LASTWINDOW_CLOSE_TOPICS) {
          // The application is not actually quitting, but the last full browser
          // window is about to be closed.
          this._onQuitRequest(subject, "lastwindow");
        }
760
761
        break;
      case "browser-lastwindow-close-granted":
762
763
764
        if (OBSERVE_LASTWINDOW_CLOSE_TOPICS) {
          this._setPrefToSaveSession();
        }
765
        break;
766
767
768
      case "weave:service:ready":
        this._setSyncAutoconnectDelay();
        break;
769
770
771
      case "fxaccounts:onverified":
        this._showSyncStartedDoorhanger();
        break;
772
773
774
      case "fxaccounts:device_connected":
        this._onDeviceConnected(data);
        break;
775
776
777
      case "fxaccounts:verify_login":
        this._onVerifyLoginNotification(JSON.parse(data));
        break;
778
      case "fxaccounts:device_disconnected":
779
780
781
782
        data = JSON.parse(data);
        if (data.isLocalDevice) {
          this._onDeviceDisconnected();
        }
783
        break;
784
      case "fxaccounts:commands:open-uri":
785
786
      case "weave:engine:clients:display-uris":
        this._onDisplaySyncURIs(subject);
787
        break;
788
      case "session-save":
789
        this._setPrefToSaveSession(true);
790
791
792
        subject.QueryInterface(Ci.nsISupportsPRBool);
        subject.data = true;
        break;
793
      case "places-init-complete":
794
        Services.obs.removeObserver(this, "places-init-complete");
795
796
        if (!this._migrationImportsDefaultBookmarks)
          this._initPlaces(false);
797
        break;
798
      case "idle":
799
        this._backupBookmarks();
800
        break;
801
      case "distribution-customization-complete":
802
        Services.obs.removeObserver(this, "distribution-customization-complete");
803
804
805
        // Customization has finished, we don't need the customizer anymore.
        delete this._distributionCustomizer;
        break;
806
807
      case "browser-glue-test": // used by tests
        if (data == "post-update-notification") {
808
          if (Services.prefs.prefHasUserValue("app.update.postupdate"))
809
            this._showUpdateNotification();
810
        } else if (data == "force-ui-migration") {
811
          this._migrateUI();
812
        } else if (data == "force-distribution-customization") {
813
          this._distributionCustomizer.applyCustomizations();
814
          // To apply distribution bookmarks use "places-init-complete".
815
        } else if (data == "force-places-init") {
816
          this._initPlaces(false);
817
818
        } else if (data == "mock-alerts-service") {
          Object.defineProperty(this, "AlertsService", {
819
            value: subject.wrappedJSObject,
820
          });
821
822
823
824
        } else if (data == "places-browser-init-complete") {
          if (this._placesBrowserInitComplete) {
            Services.obs.notifyObservers(null, "places-browser-init-complete");
          }
825
826
827
828
829
        } else if (data == "migrateMatchBucketsPrefForUI66") {
          this._migrateMatchBucketsPrefForUI66().then(() => {
            Services.obs.notifyObservers(null, "browser-glue-test",
                                         "migrateMatchBucketsPrefForUI66-done");
          });
830
        }
831
        break;
832
833
834
835
836
      case "initial-migration-will-import-default-bookmarks":
        this._migrationImportsDefaultBookmarks = true;
        break;
      case "initial-migration-did-import-default-bookmarks":
        this._initPlaces(true);
837
        break;
838
839
840
      case "handle-xul-text-link":
        let linkHandled = subject.QueryInterface(Ci.nsISupportsPRBool);
        if (!linkHandled.data) {
841
          let win = BrowserWindowTracker.getTopWindow();
842
          if (win) {
843
844
            data = JSON.parse(data);
            let where = win.whereToOpenLink(data);
845
846
847
848
849
            // Preserve legacy behavior of non-modifier left-clicks
            // opening in a new selected tab.
            if (where == "current") {
              where = "tab";
            }
850
            win.openTrustedLinkIn(data.href, where);
851
852
853
854
            linkHandled.data = true;
          }
        }
        break;
855
      case "profile-before-change":
856
857
858
859
         // Any component depending on Places should be finalized in
         // _onPlacesShutdown.  Any component that doesn't need to act after
         // the UI has gone should be finalized in _onQuitApplicationGranted.
        this._dispose();
860
        break;
861
      case "keyword-search":
862
863
        // This notification is broadcast by the docshell when it "fixes up" a
        // URI that it's been asked to load into a keyword search.
864
865
866
867
868
869
        let engine = null;
        try {
          engine = subject.QueryInterface(Ci.nsISearchEngine);
        } catch (ex) {
          Cu.reportError(ex);
        }
870
        let win = BrowserWindowTracker.getTopWindow();
871
        win.BrowserSearch.recordSearchInTelemetry(engine, "urlbar");
872
        break;
873
      case "browser-search-engine-modified":
874
875
876
877
        // Ensure we cleanup the hiddenOneOffs pref when removing
        // an engine, and that newly added engines are visible.
        if (data == "engine-added" || data == "engine-removed") {
          let engineName = subject.QueryInterface(Ci.nsISearchEngine).name;
878
          let pref = Services.prefs.getStringPref("browser.search.hiddenOneOffs");
879
880
          let hiddenList = pref ? pref.split(",") : [];
          hiddenList = hiddenList.filter(x => x !== engineName);
881
          Services.prefs.setStringPref("browser.search.hiddenOneOffs", hiddenList.join(","));
882
        }
883
        break;
884
885
886
      case "flash-plugin-hang":
        this._handleFlashHang();
        break;
887
      case "xpi-signature-changed":
888
        let disabledAddons = JSON.parse(data).disabled;
889
890
891
892
        let addons = await AddonManager.getAddonsByIDs(disabledAddons);
        if (addons.some(addon => addon)) {
          this._notifyUnsignedAddonsDisabled();
        }
893
        break;
894
895
896
      case "sync-ui-state:update":
        this._updateFxaBadges();
        break;
897
898
899
900
901
902
903
904
905
906
      case "handlersvc-store-initialized":
        // Initialize PdfJs when running in-process and remote. This only
        // happens once since PdfJs registers global hooks. If the PdfJs
        // extension is installed the init method below will be overridden
        // leaving initialization to the extension.
        // parent only: configure default prefs, set up pref observers, register
        // pdf content handler, and initializes parent side message manager
        // shim for privileged api access.
        PdfJs.init(true);
        break;
907
      case "shield-init-complete":
908
        this._shieldInitComplete = true;
909
910
        this._sendMainPingCentrePing();
        break;
911
912
913
    }
  },

914
  // initialization (called on application startup)
915
  _init: function BG__init() {
916
    let os = Services.obs;
917
918
919
920
921
922
923
    os.addObserver(this, "notifications-open-settings");
    os.addObserver(this, "final-ui-startup");
    os.addObserver(this, "browser-delayed-startup-finished");
    os.addObserver(this, "sessionstore-windows-restored");
    os.addObserver(this, "browser:purge-session-history");
    os.addObserver(this, "quit-application-requested");
    os.addObserver(this, "quit-application-granted");
924
    if (OBSERVE_LASTWINDOW_CLOSE_TOPICS) {
925
926
      os.addObserver(this, "browser-lastwindow-close-requested");
      os.addObserver(this, "browser-lastwindow-close-granted");
927
    }
928
929
930
    os.addObserver(this, "weave:service:ready");
    os.addObserver(this, "fxaccounts:onverified");
    os.addObserver(this, "fxaccounts:device_connected");
931
    os.addObserver(this, "fxaccounts:verify_login");
932
    os.addObserver(this, "fxaccounts:device_disconnected");
933
    os.addObserver(this, "fxaccounts:commands:open-uri");
934
935
936
937
938
939
    os.addObserver(this, "weave:engine:clients:display-uris");
    os.addObserver(this, "session-save");
    os.addObserver(this, "places-init-complete");
    os.addObserver(this, "distribution-customization-complete");
    os.addObserver(this, "handle-xul-text-link");
    os.addObserver(this, "profile-before-change");
940
    os.addObserver(this, "keyword-search");
941
942
943
944
    os.addObserver(this, "browser-search-engine-modified");
    os.addObserver(this, "restart-in-safe-mode");
    os.addObserver(this, "flash-plugin-hang");
    os.addObserver(this, "xpi-signature-changed");
945
    os.addObserver(this, "sync-ui-state:update");
946
    os.addObserver(this, "handlersvc-store-initialized");
947
    os.addObserver(this, "shield-init-complete");
948

949
950
951
    ActorManagerParent.addActors(ACTORS);
    ActorManagerParent.flush();

952
    this._flashHangCount = 0;
953
    this._firstWindowReady = new Promise(resolve => this._firstWindowLoaded = resolve);
954
955
956
    if (AppConstants.platform == "win") {
      JawsScreenReaderVersionCheck.init();
    }
957
958
959
  },

  // cleanup (called on application shutdown)
960
  _dispose: function BG__dispose() {
961
    let os = Services.obs;
962
    os.removeObserver(this, "notifications-open-settings");
963
964
965
966
967
    os.removeObserver(this, "final-ui-startup");
    os.removeObserver(this, "sessionstore-windows-restored");
    os.removeObserver(this, "browser:purge-session-history");
    os.removeObserver(this, "quit-application-requested");
    os.removeObserver(this, "quit-application-granted");
968
    os.removeObserver(this, "restart-in-safe-mode");
969
970
971
972
    if (OBSERVE_LASTWINDOW_CLOSE_TOPICS) {
      os.removeObserver(this, "browser-lastwindow-close-requested");
      os.removeObserver(this, "browser-lastwindow-close-granted");
    }
973
    os.removeObserver(this, "weave:service:ready");
974
    os.removeObserver(this, "fxaccounts:onverified");
975
    os.removeObserver(this, "fxaccounts:device_connected");
976
    os.removeObserver(this, "fxaccounts:verify_login");
977
    os.removeObserver(this, "fxaccounts:device_disconnected");
978
    os.removeObserver(this, "fxaccounts:commands:open-uri");
979
    os.removeObserver(this, "weave:engine:clients:display-uris");
980
    os.removeObserver(this, "session-save");
981
982
983
984
    if (this._bookmarksBackupIdleTime) {
      this._idleService.removeIdleObserver(this, this._bookmarksBackupIdleTime);
      delete this._bookmarksBackupIdleTime;
    }
985
986
987
988
    if (this._lateTasksIdleObserver) {
      this._idleService.removeIdleObserver(this._lateTasksIdleObserver, LATE_TASKS_IDLE_TIME_SEC);
      delete this._lateTasksIdleObserver;
    }
989
990
991
992
    if (this._gmpInstallManager) {
      this._gmpInstallManager.uninit();
      delete this._gmpInstallManager;
    }
993
    try {
994
      os.removeObserver(this, "places-init-complete");
995
    } catch (ex) { /* Could have been removed already */ }
996
    os.removeObserver(this, "handle-xul-text-link");
997
    os.removeObserver(this, "profile-before-change");
998
    os.removeObserver(this, "keyword-search");
999
    os.removeObserver(this, "browser-search-engine-modified");
1000
    os.removeObserver(this, "flash-plugin-hang");
1001
    os.removeObserver(this, "xpi-signature-changed");
1002
    os.removeObserver(this, "sync-ui-state:update");
1003
    os.removeObserver(this, "shield-init-complete");
1004

1005
    Services.prefs.removeObserver("privacy.trackingprotection", this._matchCBCategory);
1006
1007
1008
    Services.prefs.removeObserver("urlclassifier.trackingTable", this._matchCBCategory);
    Services.prefs.removeObserver("network.cookie.cookieBehavior", this._matchCBCategory);
    Services.prefs.removeObserver(ContentBlockingCategoriesPrefs.PREF_CB_CATEGORY, this._updateCBCategory);
1009
1010
  },

1011
1012
  // runs on startup, before the first command line handler is invoked
  // (i.e. before the first window is opened)
1013
  _beforeUIStartup: function BG__beforeUIStartup() {
1014
1015
    SessionStartup.init();

1016
1017
1018
1019
1020
1021
1022
    if (Services.prefs.prefHasUserValue(PREF_PDFJS_ENABLED_CACHE_STATE)) {
      Services.ppmm.sharedData.set(
        "pdfjs.enabled",
        Services.prefs.getBoolPref(PREF_PDFJS_ENABLED_CACHE_STATE));
    } else {
      PdfJs.earlyInit();
    }
1023

1024
1025
1026
1027
1028
1029
1030
1031
    // Initialize the default l10n resource sources for L10nRegistry.
    let locales = Services.locale.packagedLocales;
    const greSource = new FileSource("toolkit", locales, "resource://gre/localization/{locale}/");
    L10nRegistry.registerSource(greSource);

    const appSource = new FileSource("app", locales, "resource://app/localization/{locale}/");
    L10nRegistry.registerSource(appSource);

1032
    // check if we're in safe mode
1033
    if (Services.appinfo.inSafeMode) {
1034
      Services.ww.openWindow(null, "chrome://browser/content/safeMode.xul",
1035
                             "_blank", "chrome,centerscreen,modal,resizable=no", null);
1036
    }
1037

1038
    // apply distribution customizations
1039
    this._distributionCustomizer.applyCustomizations();
1040

1041
    // handle any UI migration
1042
    this._migrateUI();
1043

1044
1045
    listeners.init();

1046
    SessionStore.init();
1047