Bug 1496684 - Dispatch commonly expected startup notifications when opening a GeckoView window. r=snorp a=lizzard
authorJan Henning <jh+bugzilla@buttercookie.de>
Thu, 07 Feb 2019 20:41:16 +0000
changeset 512996 95b884dcbd6f001b990a6353980280050fab9ac6
parent 512995 36ae47c3423bbcbf174f140640929fcce685d7ae
child 512997 e4d259ea825ac517f8d2de85430fbd955be2df92
push id10672
push usernbeleuzu@mozilla.com
push dateMon, 11 Feb 2019 16:55:03 +0000
treeherdermozilla-beta@95b884dcbd6f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp, lizzard
bugs1496684, 1465832
milestone66.0
Bug 1496684 - Dispatch commonly expected startup notifications when opening a GeckoView window. r=snorp a=lizzard Once a webextension using a blocking WebRequest listener has started loading, all network connections covered by the extension's manifest are held until the extension is ready the process them. One condition for the extension being ready apparently includes browser startup having progressed far enough, as signified by "browser-delayed-startup-finished" having been dispatched. Therefore, we have to start sending that notification when opening a new Gecko- View window, too, and copy Fennec's InitLater() system for that. Unlike Fennec, we cannot tie registration of those InitLater() runnables to the initial content load having progressed far enough because of a) e10s, which makes that approach neither easily possible nor really sensible, as content will load in a different process in that case, and b) because we're racing with extension startup here - if extensions are loaded quick enough to block even the initial page load, we'd be deadlocked: We cannot send the notification until the page finishes loading, but the page cannot load until we send the notification. Fennec isn't affected by the latter problem because "sessionstore-windows-restored", which Fennec will send in any case, serves as an alternative pathway for completing extension startup. And unlike Desktop, we don't really have any chrome content to paint, so we cannot tie delayed initialisation to a paint listener for that, either. Therefore, we simply fire off a runnable at the *end* of geckoview.js's startup() method, which should give more pressing initialisation tasks enough of a headstart. For completeness, we're also adding the "browser-idle-startup-tasks-finished" notification and thereby solve bug 1465832 as well, allowing the ScriptPreloader to detect which scripts are commonly loaded during GeckoView startup and to start caching and pre-parsing them. Differential Revision: https://phabricator.services.mozilla.com/D18865
mobile/android/chrome/geckoview/geckoview.js
mobile/android/modules/DelayedInit.jsm
mobile/android/modules/geckoview/DelayedInit.jsm
mobile/android/modules/geckoview/moz.build
mobile/android/modules/moz.build
--- a/mobile/android/chrome/geckoview/geckoview.js
+++ b/mobile/android/chrome/geckoview/geckoview.js
@@ -1,15 +1,16 @@
 /* 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";
 
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
+ChromeUtils.import("resource://gre/modules/DelayedInit.jsm");
 
 XPCOMUtils.defineLazyModuleGetters(this, {
   E10SUtils: "resource://gre/modules/E10SUtils.jsm",
   EventDispatcher: "resource://gre/modules/Messaging.jsm",
   GeckoViewUtils: "resource://gre/modules/GeckoViewUtils.jsm",
   HistogramStopwatch: "resource://gre/modules/GeckoViewTelemetry.jsm",
   Services: "resource://gre/modules/Services.jsm",
 });
@@ -384,16 +385,20 @@ function createBrowser() {
   if (settings.useMultiprocess) {
     browser.setAttribute("remote", "true");
     browser.setAttribute("remoteType", E10SUtils.DEFAULT_REMOTE_TYPE);
   }
 
   return browser;
 }
 
+function InitLater(fn, object, name) {
+  return DelayedInit.schedule(fn, object, name, 15000 /* 15s max wait */);
+}
+
 function startup() {
   GeckoViewUtils.initLogging("XUL", window);
 
   const browser = createBrowser();
   ModuleManager.init(browser, [{
     name: "GeckoViewAccessibility",
     onInit: {
       resource: "resource://gre/modules/GeckoViewAccessibility.jsm",
@@ -445,12 +450,33 @@ function startup() {
     },
   }, {
     name: "GeckoViewTrackingProtection",
     onEnable: {
       resource: "resource://gre/modules/GeckoViewTrackingProtection.jsm",
     },
   }]);
 
+  Services.tm.dispatchToMainThread(() => {
+    // This should always be the first thing we do here - any additional delayed
+    // initialisation tasks should be added between "browser-delayed-startup-finished"
+    // and "browser-idle-startup-tasks-finished".
+
+    // Bug 1496684: Various bits of platform stuff depend on this notification
+    // to learn when a browser window has finished its initial (chrome)
+    // initialisation, especially with regards to the very first window that is
+    // created. Therefore, GeckoView "windows" need to send this, too.
+    InitLater(() => Services.obs.notifyObservers(window, "browser-delayed-startup-finished"));
+
+    // This should always go last, since the idle tasks (except for the ones with
+    // timeouts) should execute in order. Note that this observer notification is
+    // not guaranteed to fire, since the window could close before we get here.
+
+    // This notification in particular signals the ScriptPreloader that we have
+    // finished startup, so it can now stop recording script usage and start
+    // updating the startup cache for faster script loading.
+    InitLater(() => Services.obs.notifyObservers(window, "browser-idle-startup-tasks-finished"));
+  });
+
   // Move focus to the content window at the end of startup,
   // so things like text selection can work properly.
   browser.focus();
 }
rename from mobile/android/modules/DelayedInit.jsm
rename to mobile/android/modules/geckoview/DelayedInit.jsm
--- a/mobile/android/modules/geckoview/moz.build
+++ b/mobile/android/modules/geckoview/moz.build
@@ -2,16 +2,17 @@
 # vim: set filetype=python:
 # 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/.
 
 EXTRA_JS_MODULES += [
     'AndroidLog.jsm',
     'ContentCrashHandler.jsm',
+    'DelayedInit.jsm',
     'GeckoViewAccessibility.jsm',
     'GeckoViewAutoFill.jsm',
     'GeckoViewChildModule.jsm',
     'GeckoViewConsole.jsm',
     'GeckoViewContent.jsm',
     'GeckoViewMedia.jsm',
     'GeckoViewModule.jsm',
     'GeckoViewNavigation.jsm',
--- a/mobile/android/modules/moz.build
+++ b/mobile/android/modules/moz.build
@@ -19,17 +19,16 @@ with Files('geckoview/**'):
 
 DIRS += ['geckoview']
 
 EXTRA_JS_MODULES += [
     'Accounts.jsm',
     'ActionBarHandler.jsm',
     'BrowserActions.jsm',
     'dbg-browser-actors.js',
-    'DelayedInit.jsm',
     'DownloadNotifications.jsm',
     'FormAssistant.jsm',
     'FxAccountsWebChannel.jsm',
     'HelperApps.jsm',
     'Home.jsm',
     'HomeProvider.jsm',
     'InputWidgetHelper.jsm',
     'LightweightThemeConsumer.jsm',