Home | History | Annotate | Download | only in extension
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 document.cookie = "__navigated_to_report=0; path=/";
      6 document.cookie = "__pc_done=0; path=/";
      7 document.cookie = "__pc_timings=; path=/";
      8 
      9 function dirname(path) {
     10   var match = path.match(/(.*)\//);
     11   if (match) {
     12     return match[1];
     13   } else {
     14     return ".";
     15   }
     16 }
     17 
     18 function IsWprRecordMode() {
     19   var kStatusUrl = "http://wprwprwpr/web-page-replay-command-status";
     20   var isRecordMode;
     21   var xhr = new XMLHttpRequest();
     22   var useAsync = false;
     23   xhr.open("GET", kStatusUrl, useAsync);
     24   xhr.onreadystatechange = function() {
     25     if (xhr.readyState == 4 && xhr.status == 200) {
     26       var status = JSON.parse(xhr.responseText);
     27       isRecordMode = status["is_record_mode"];
     28       console.log("WPR record mode?: " + isRecordMode);
     29     }
     30   };
     31   try {
     32     xhr.send();
     33   } catch(e) {
     34     throw "Web Page Replay is not responding. Start WPR to continue."
     35   }
     36   return isRecordMode;
     37 }
     38 
     39 
     40 function TryStart() {
     41   console.log("try start");
     42   var status_element = document.getElementById("status");
     43 
     44   var config_json;
     45   var config;
     46   try {
     47     config_json = document.getElementById("json").textContent;
     48     config = JSON.parse(config_json);
     49   } catch(err) {
     50     console.log("Bad json data: " + config_json);
     51     status_element.textContent = "Exception: " + err + "\njson data: " +
     52         config_json;
     53     return;
     54   }
     55   var isRecordMode = false;
     56   try {
     57     isRecordMode = IsWprRecordMode();
     58   } catch (err) {
     59     status_element.textContent = err;
     60     setTimeout(TryStart, 5000);
     61     return;
     62   }
     63 
     64   if (!config["shouldStart"]) {
     65     status_element.textContent =
     66         "Press 'Start' to continue (or load this page with 'auto=1').";
     67     return;
     68   }
     69 
     70   try {
     71     var reportDir = dirname(dirname(window.location.pathname)) + "/common";
     72     config["reportUrl"] = "file://" + reportDir + "/report.html";
     73     config["isRecordMode"] = isRecordMode;
     74     var port = chrome.runtime.connect();
     75     port.postMessage({message: "start", benchmark: config});
     76     console.log("sending start message: page count, " +
     77                 config["pageSets"].length);
     78   } catch(err) {
     79     console.log("TryStart retrying after exception: " + err);
     80     status_element.textContent = "Exception: " + err;
     81     setTimeout(TryStart, 1000);
     82     return;
     83   }
     84   status_element.textContent = "STARTING";
     85 }
     86 
     87 // We wait before starting the test just to let chrome warm up better.
     88 setTimeout(TryStart, 250);
     89