<?xml version="1.0"?>
<overlay id="Scrapper-Overlay" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

<script type="application/x-javascript" src="Profiler.js" />
<script type="application/x-javascript" src="damp.js" />
<script type="application/x-javascript">

(function(){
  const Cc = Components.classes;
  const Ci = Components.interfaces;
  var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);

  const PREFIX = "damp@mozilla.org:";

  // "services" which the framescript can execute at the chrome process
  var proxiedServices = {
    runTest: function(config, callback) {
      (new Damp()).startTest(callback, config);
    },

    toClipboard: function(text) {
      const gClipboardHelper = Cc["@mozilla.org/widget/clipboardhelper;1"]
                               .getService(Ci.nsIClipboardHelper);
      gClipboardHelper.copyString(text);
    }
  };

  var groupMM = window.getGroupMessageManager("browsers");
  groupMM.loadFrameScript("chrome://damp/content/framescript.js", true);

  // listener/executor on the chrome process for damp.html
  groupMM.addMessageListener(PREFIX + "chrome-exec-message", function listener(m) {
    function sendResult(result) {
      groupMM.broadcastAsyncMessage(PREFIX + "chrome-exec-reply", {
        id: m.data.id,
        result: result
      });
    }

    var command = m.data.command;
    if (!proxiedServices.hasOwnProperty(command.name))
      throw new Error("DAMP: service doesn't exist: '" + command.name + "'");

    var service = proxiedServices[command.name];
    if (command.name == "runTest") // Needs async execution
      service(command.data, sendResult);
    else
      sendResult(service(command.data));

  });
})();

</script>
</overlay>
