main.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "use strict";
  5. /* global BrowserLoader */
  6. var { utils: Cu } = Components;
  7. // Initialize module loader and load all modules of the new inline
  8. // preview feature. The entire code-base doesn't need any extra
  9. // privileges and runs entirely in content scope.
  10. const rootUrl = "resource://devtools/client/webconsole/net/";
  11. const require = BrowserLoader({
  12. baseURI: rootUrl,
  13. window}).require;
  14. const NetRequest = require("./net-request");
  15. const { loadSheet } = require("sdk/stylesheet/utils");
  16. // Localization
  17. const {LocalizationHelper} = require("devtools/shared/l10n");
  18. const L10N = new LocalizationHelper("devtools/client/locales/netmonitor.properties");
  19. // Stylesheets
  20. var styleSheets = [
  21. "resource://devtools/client/jsonview/css/toolbar.css",
  22. "resource://devtools/client/shared/components/tree/tree-view.css",
  23. "resource://devtools/client/shared/components/reps/reps.css",
  24. "resource://devtools/client/webconsole/net/net-request.css",
  25. "resource://devtools/client/webconsole/net/components/size-limit.css",
  26. "resource://devtools/client/webconsole/net/components/net-info-body.css",
  27. "resource://devtools/client/webconsole/net/components/net-info-group.css",
  28. "resource://devtools/client/webconsole/net/components/net-info-params.css",
  29. "resource://devtools/client/webconsole/net/components/response-tab.css"
  30. ];
  31. // Load theme stylesheets into the Console frame. This should be
  32. // done automatically by UI Components as soon as we have consensus
  33. // on the right CSS strategy FIXME.
  34. // It would also be nice to include them using @import.
  35. styleSheets.forEach(url => {
  36. loadSheet(this, url, "author");
  37. });
  38. // Localization API used by React components
  39. // accessing strings from *.properties file.
  40. // Example:
  41. // let localizedString = Locale.$STR('string-key');
  42. //
  43. // Resources:
  44. // http://l20n.org/
  45. // https://github.com/yahoo/react-intl
  46. this.Locale = {
  47. $STR: key => {
  48. try {
  49. return L10N.getStr(key);
  50. } catch (err) {
  51. console.error(key + ": " + err);
  52. }
  53. }
  54. };
  55. // List of NetRequest instances represents the state.
  56. // As soon as Redux is in place it should be maintained using a reducer.
  57. var netRequests = new Map();
  58. /**
  59. * This function handles network events received from the backend. It's
  60. * executed from within the webconsole.js
  61. */
  62. function onNetworkEvent(log) {
  63. // The 'from' field is set only in case of a 'networkEventUpdate' packet.
  64. // The initial 'networkEvent' packet uses 'actor'.
  65. // Check if NetRequest object is already created for this event actor and
  66. // if there is none make sure to create one.
  67. let response = log.response;
  68. let netRequest = response.from ? netRequests.get(response.from) : null;
  69. if (!netRequest && !log.update) {
  70. netRequest = new NetRequest(log);
  71. netRequests.set(response.actor, netRequest);
  72. }
  73. if (!netRequest) {
  74. return;
  75. }
  76. if (log.update) {
  77. netRequest.updateBody(response);
  78. }
  79. return;
  80. }
  81. // Make the 'onNetworkEvent' accessible from chrome (see webconsole.js)
  82. this.NetRequest = {
  83. onNetworkEvent: onNetworkEvent
  84. };