browser/base/content/test/social/browser_social_workercrash.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 // This tests our recovery if a child content process hosting providers
     6 // crashes.
     8 // A content script we inject into one of our browsers
     9 const TEST_CONTENT_HELPER = "chrome://mochitests/content/browser/browser/base/content/test/social/social_crash_content_helper.js";
    11 let {getFrameWorkerHandle} = Cu.import("resource://gre/modules/FrameWorker.jsm", {});
    12 let {Promise} = Cu.import("resource://gre/modules/Promise.jsm", {}).Promise;
    14 function test() {
    15   waitForExplicitFinish();
    17   // We need to ensure all our workers are in the same content process.
    18   Services.prefs.setIntPref("dom.ipc.processCount", 1);
    20   // This test generates many uncaught promises that should not cause failures.
    21   Promise.Debugging.clearUncaughtErrorObservers();
    23   runSocialTestWithProvider(gProviders, function (finishcb) {
    24     runSocialTests(tests, undefined, undefined, function() {
    25       Services.prefs.clearUserPref("dom.ipc.processCount");
    26       finishcb();
    27     });
    28   });
    29 }
    31 let gProviders = [
    32   {
    33     name: "provider 1",
    34     origin: "https://example.com",
    35     sidebarURL: "https://example.com/browser/browser/base/content/test/social/social_sidebar.html?provider1",
    36     workerURL: "https://example.com/browser/browser/base/content/test/social/social_worker.js",
    37     iconURL: "chrome://branding/content/icon48.png"
    38   },
    39   {
    40     name: "provider 2",
    41     origin: "https://test1.example.com",
    42     sidebarURL: "https://test1.example.com/browser/browser/base/content/test/social/social_sidebar.html?provider2",
    43     workerURL: "https://test1.example.com/browser/browser/base/content/test/social/social_worker.js",
    44     iconURL: "chrome://branding/content/icon48.png"
    45   }
    46 ];
    48 var tests = {
    49   testCrash: function(next) {
    50     // open the sidebar, then crash the child.
    51     let sbrowser = document.getElementById("social-sidebar-browser");
    52     onSidebarLoad(function() {
    53       // get the browser element for our provider.
    54       let fw = getFrameWorkerHandle(gProviders[0].workerURL);
    55       fw.port.close();
    56       fw._worker.browserPromise.then(browser => {
    57         let mm = browser.messageManager;
    58         mm.loadFrameScript(TEST_CONTENT_HELPER, false);
    59         // add an observer for the crash - after it sees the crash we attempt
    60         // a reload.
    61         let observer = new crashObserver(function() {
    62           info("Saw the process crash.")
    63           Services.obs.removeObserver(observer, 'ipc:content-shutdown');
    64           // Add another sidebar load listener - it should be the error page.
    65           onSidebarLoad(function() {
    66             ok(sbrowser.contentDocument.location.href.indexOf("about:socialerror?")==0, "is on social error page");
    67             // after reloading, the sidebar should reload
    68             onSidebarLoad(function() {
    69               // now ping both workers - they should both be alive.
    70               ensureWorkerLoaded(gProviders[0], function() {
    71                 ensureWorkerLoaded(gProviders[1], function() {
    72                   // and we are done!
    73                   next();
    74                 });
    75               });
    76             });
    77             // click the try-again button.
    78             sbrowser.contentDocument.getElementById("btnTryAgain").click();
    79           });
    80         });
    81         Services.obs.addObserver(observer, 'ipc:content-shutdown', false);
    82         // and cause the crash.
    83         mm.sendAsyncMessage("social-test:crash");
    84       });
    85     })
    86     SocialSidebar.show();
    87   },
    88 }
    90 function onSidebarLoad(callback) {
    91   let sbrowser = document.getElementById("social-sidebar-browser");
    92   sbrowser.addEventListener("load", function load() {
    93     sbrowser.removeEventListener("load", load, true);
    94     callback();
    95   }, true);
    96 }
    98 function ensureWorkerLoaded(manifest, callback) {
    99   let fw = getFrameWorkerHandle(manifest.workerURL);
   100   // once the worker responds to a ping we know it must be up.
   101   let port = fw.port;
   102   port.onmessage = function(msg) {
   103     if (msg.data.topic == "pong") {
   104       port.close();
   105       callback();
   106     }
   107   }
   108   port.postMessage({topic: "ping"})
   109 }
   111 // More duplicated code from browser_thumbnails_brackground_crash.
   112 // Bug 915518 exists to unify these.
   114 // This observer is needed so we can clean up all evidence of the crash so
   115 // the testrunner thinks things are peachy.
   116 let crashObserver = function(callback) {
   117   this.callback = callback;
   118 }
   119 crashObserver.prototype = {
   120   observe: function(subject, topic, data) {
   121     is(topic, 'ipc:content-shutdown', 'Received correct observer topic.');
   122     ok(subject instanceof Components.interfaces.nsIPropertyBag2,
   123        'Subject implements nsIPropertyBag2.');
   124     // we might see this called as the process terminates due to previous tests.
   125     // We are only looking for "abnormal" exits...
   126     if (!subject.hasKey("abnormal")) {
   127       info("This is a normal termination and isn't the one we are looking for...");
   128       return;
   129     }
   131     var dumpID;
   132     if ('nsICrashReporter' in Components.interfaces) {
   133       dumpID = subject.getPropertyAsAString('dumpID');
   134       ok(dumpID, "dumpID is present and not an empty string");
   135     }
   137     if (dumpID) {
   138       var minidumpDirectory = getMinidumpDirectory();
   139       removeFile(minidumpDirectory, dumpID + '.dmp');
   140       removeFile(minidumpDirectory, dumpID + '.extra');
   141     }
   142     this.callback();
   143   }
   144 }
   146 function getMinidumpDirectory() {
   147   var dir = Services.dirsvc.get('ProfD', Components.interfaces.nsIFile);
   148   dir.append("minidumps");
   149   return dir;
   150 }
   151 function removeFile(directory, filename) {
   152   var file = directory.clone();
   153   file.append(filename);
   154   if (file.exists()) {
   155     file.remove(false);
   156   }
   157 }

mercurial