VandanRogheliya

talos-powers-content.js

Jun 12th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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/. */
  4.  
  5. // This file is loaded as a framescript
  6. /* global docShell */
  7. // eslint-env mozilla/frame-script
  8.  
  9. // const {performance} = require('perf_hooks');
  10.  
  11. addEventListener(
  12.   "TalosContentProfilerCommand",
  13.   e => {
  14.     let name = e.detail.name;
  15.     let data = e.detail.data;
  16.     sendAsyncMessage("TalosContentProfiler:Command", { name, data });
  17.   },
  18.   { wantUntrusted: true } // since we're exposing to unprivileged
  19. );
  20.  
  21. addMessageListener("TalosContentProfiler:Response", msg => {
  22.   let name = msg.data.name;
  23.   let data = msg.data.data;
  24.  
  25.   let event = Cu.cloneInto(
  26.     {
  27.       bubbles: true,
  28.       detail: {
  29.         name,
  30.         data,
  31.       },
  32.     },
  33.     content
  34.   );
  35.   content.dispatchEvent(
  36.     new content.CustomEvent("TalosContentProfilerResponse", event)
  37.   );
  38. });
  39.  
  40. addEventListener(
  41.   "TalosPowersContentForceCCAndGC",
  42.   e => {
  43.     Cu.forceGC();
  44.     Cu.forceCC();
  45.     Cu.forceShrinkingGC();
  46.     sendSyncMessage("TalosPowersContent:ForceCCAndGC");
  47.   },
  48.   { wantUntrusted: true } // since we're exposing to unprivileged
  49. );
  50.  
  51. addEventListener(
  52.   "TalosPowersContentFocus",
  53.   e => {
  54.     if (
  55.       content.location.protocol != "file:" &&
  56.       content.location.hostname != "localhost" &&
  57.       content.location.hostname != "127.0.0.1"
  58.     ) {
  59.       throw new Error(
  60.         "TalosPowersContentFocus may only be used with local content"
  61.       );
  62.     }
  63.     content.focus();
  64.     let contentEvent = Cu.cloneInto(
  65.       {
  66.         bubbles: true,
  67.       },
  68.       content
  69.     );
  70.     content.dispatchEvent(
  71.       new content.CustomEvent("TalosPowersContentFocused", contentEvent)
  72.     );
  73.   },
  74.   { capture: true, wantUntrusted: true } // since we're exposing to unprivileged
  75. );
  76.  
  77. addEventListener(
  78.   "TalosPowersContentGetStartupInfo",
  79.   e => {
  80.     sendAsyncMessage("TalosPowersContent:GetStartupInfo");
  81.     addMessageListener(
  82.       "TalosPowersContent:GetStartupInfo:Result",
  83.       function onResult(msg) {
  84.         removeMessageListener(
  85.           "TalosPowersContent:GetStartupInfo:Result",
  86.           onResult
  87.         );
  88.         let event = Cu.cloneInto(
  89.           {
  90.             bubbles: true,
  91.             detail: msg.data,
  92.           },
  93.           content
  94.         );
  95.  
  96.         content.dispatchEvent(
  97.           new content.CustomEvent(
  98.             "TalosPowersContentGetStartupInfoResult",
  99.             event
  100.           )
  101.         );
  102.       }
  103.     );
  104.   },
  105.   { wantUntrusted: true } // since we're exposing to unprivileged
  106. );
  107.  
  108. addEventListener(
  109.   "TalosPowersContentDumpConsole",
  110.   e => {
  111.     var messages;
  112.     try {
  113.       messages = Services.console.getMessageArray();
  114.     } catch (ex) {
  115.       dump(ex + "\n");
  116.       messages = [];
  117.     }
  118.  
  119.     for (var i = 0; i < messages.length; i++) {
  120.       dump(messages[i].message + "\n");
  121.     }
  122.   },
  123.   { wantUntrusted: true } // since we're exposing to unprivileged
  124. );
  125.  
  126. /**
  127.  * Content that wants to quit the whole session should
  128.  * fire the TalosPowersGoQuitApplication custom event. This will
  129.  * attempt to force-quit the browser.
  130.  */
  131. addEventListener(
  132.   "TalosPowersGoQuitApplication",
  133.   e => {
  134.     // If we're loaded in a low-priority background process, like
  135.     // the background page thumbnailer, then we shouldn't be allowed
  136.     // to quit the whole application. This is a workaround until
  137.     // bug 1164459 is fixed.
  138.     let priority = docShell
  139.       .QueryInterface(Ci.nsIDocumentLoader)
  140.       .loadGroup.QueryInterface(Ci.nsISupportsPriority).priority;
  141.     if (priority != Ci.nsISupportsPriority.PRIORITY_LOWEST) {
  142.       sendAsyncMessage("Talos:ForceQuit", e.detail);
  143.     }
  144.   },
  145.   { wantUntrusted: true } // since we're exposing to unprivileged
  146. );
  147.  
  148. /* *
  149.  * Mediator for the generic ParentExec mechanism.
  150.  * Listens for a query event from the content, forwards it as a query message
  151.  * to the parent, listens to a parent reply message, and forwards it as a reply
  152.  * event for the content to capture.
  153.  * The consumer API for this mechanism is at content/TalosPowersContent.js
  154.  * and the callees are at ParentExecServices at components/TalosPowersService.js
  155.  */
  156. addEventListener(
  157.   "TalosPowers:ParentExec:QueryEvent",
  158.   function(e) {
  159.     if (
  160.       content.location.protocol != "file:" &&
  161.       content.location.hostname != "localhost" &&
  162.       content.location.hostname != "127.0.0.1"
  163.     ) {
  164.       throw new Error(
  165.         "TalosPowers:ParentExec may only be used with local content"
  166.       );
  167.     }
  168.     let uniqueMessageId =
  169.       "TalosPowers:ParentExec:" +
  170.       content.document.documentURI +
  171.       // eslint-disable-next-line mozilla/avoid-Date-timing
  172.       performance.now() +
  173.       Math.random();
  174.  
  175.     // Listener for the reply from the parent process
  176.     addMessageListener("TalosPowers:ParentExec:ReplyMsg", function done(reply) {
  177.       if (reply.data.id != uniqueMessageId) {
  178.         return;
  179.       }
  180.  
  181.       removeMessageListener("TalosPowers:ParentExec:ReplyMsg", done);
  182.  
  183.       // reply to content via an event
  184.       let contentEvent = Cu.cloneInto(
  185.         {
  186.           bubbles: true,
  187.           detail: reply.data.result,
  188.         },
  189.         content
  190.       );
  191.       content.dispatchEvent(
  192.         new content.CustomEvent(e.detail.listeningTo, contentEvent)
  193.       );
  194.     });
  195.  
  196.     // Send the query to the parent process
  197.     sendAsyncMessage("TalosPowers:ParentExec:QueryMsg", {
  198.       command: e.detail.command,
  199.       id: uniqueMessageId,
  200.     });
  201.   },
  202.   { wantUntrusted: true } // since we're exposing to unprivileged
  203. );
Add Comment
Please, Sign In to add comment