tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CachePanel.js (4379B)


      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 "use strict";
      6 
      7 const {
      8  Component,
      9  createFactory,
     10 } = require("resource://devtools/client/shared/vendor/react.mjs");
     11 const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.mjs");
     12 const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js");
     13 const {
     14  L10N,
     15 } = require("resource://devtools/client/netmonitor/src/utils/l10n.js");
     16 const {
     17  fetchNetworkUpdatePacket,
     18 } = require("resource://devtools/client/netmonitor/src/utils/request-utils.js");
     19 
     20 // Components
     21 const TreeViewClass = ChromeUtils.importESModule(
     22  "resource://devtools/client/shared/components/tree/TreeView.mjs"
     23 ).default;
     24 const PropertiesView = createFactory(
     25  require("resource://devtools/client/netmonitor/src/components/request-details/PropertiesView.js")
     26 );
     27 
     28 const { div, input } = dom;
     29 
     30 const CACHE = L10N.getStr("netmonitor.cache.cache");
     31 const DATA_SIZE = L10N.getStr("netmonitor.cache.dataSize");
     32 const EXPIRES = L10N.getStr("netmonitor.cache.expires");
     33 const FETCH_COUNT = L10N.getStr("netmonitor.cache.fetchCount");
     34 const LAST_FETCHED = L10N.getStr("netmonitor.cache.lastFetched");
     35 const LAST_MODIFIED = L10N.getStr("netmonitor.cache.lastModified");
     36 const DEVICE = L10N.getStr("netmonitor.cache.device");
     37 const NOT_AVAILABLE = L10N.getStr("netmonitor.cache.notAvailable");
     38 const EMPTY = L10N.getStr("netmonitor.cache.empty");
     39 
     40 /**
     41 * Cache panel component
     42 * This tab lists full details of any cache information of the response.
     43 */
     44 class CachePanel extends Component {
     45  static get propTypes() {
     46    return {
     47      connector: PropTypes.object.isRequired,
     48      openLink: PropTypes.func,
     49      request: PropTypes.object.isRequired,
     50    };
     51  }
     52 
     53  componentDidMount() {
     54    const { connector, request } = this.props;
     55    fetchNetworkUpdatePacket(connector.requestData, request, ["responseCache"]);
     56  }
     57 
     58  // FIXME: https://bugzilla.mozilla.org/show_bug.cgi?id=1774507
     59  UNSAFE_componentWillReceiveProps(nextProps) {
     60    const { connector, request } = nextProps;
     61    fetchNetworkUpdatePacket(connector.requestData, request, ["responseCache"]);
     62  }
     63 
     64  renderSummary(label, value) {
     65    return div(
     66      { className: "tabpanel-summary-container cache-summary" },
     67      div(
     68        {
     69          className: "tabpanel-summary-label cache-summary-label",
     70        },
     71        label + ":"
     72      ),
     73      input({
     74        className: "tabpanel-summary-value textbox-input devtools-monospace",
     75        readOnly: true,
     76        value,
     77      })
     78    );
     79  }
     80 
     81  getProperties(responseCache) {
     82    let responseCacheObj;
     83    let cacheObj;
     84    try {
     85      responseCacheObj = Object.assign({}, responseCache);
     86      responseCacheObj = responseCacheObj.cache;
     87    } catch (e) {
     88      return null;
     89    }
     90    try {
     91      cacheObj = Object.assign({}, responseCacheObj);
     92    } catch (e) {
     93      return null;
     94    }
     95    return cacheObj;
     96  }
     97 
     98  getDate(timestamp) {
     99    if (!timestamp) {
    100      return null;
    101    }
    102    const d = new Date(parseInt(timestamp, 10) * 1000);
    103    return d.toLocaleDateString() + " " + d.toLocaleTimeString();
    104  }
    105 
    106  render() {
    107    const { request } = this.props;
    108    const { responseCache } = request;
    109 
    110    let object;
    111    const cache = this.getProperties(responseCache);
    112 
    113    if (
    114      cache.lastFetched ||
    115      cache.fetchCount ||
    116      cache.storageDataSize ||
    117      cache.lastModified ||
    118      cache.expirationTime ||
    119      cache.deviceID
    120    ) {
    121      object = {
    122        [CACHE]: {
    123          [LAST_FETCHED]: this.getDate(cache.lastFetched) || NOT_AVAILABLE,
    124          [FETCH_COUNT]: cache.fetchCount || NOT_AVAILABLE,
    125          [DATA_SIZE]: cache.storageDataSize || NOT_AVAILABLE,
    126          [LAST_MODIFIED]: this.getDate(cache.lastModified) || NOT_AVAILABLE,
    127          [EXPIRES]: this.getDate(cache.expirationTime) || NOT_AVAILABLE,
    128          [DEVICE]: cache.deviceID || NOT_AVAILABLE,
    129        },
    130      };
    131    } else {
    132      return div({ className: "empty-notice" }, EMPTY);
    133    }
    134 
    135    return div(
    136      { className: "panel-container security-panel" },
    137      PropertiesView({
    138        object,
    139        enableFilter: false,
    140        expandedNodes: TreeViewClass.getExpandedNodes(object),
    141      })
    142    );
    143  }
    144 }
    145 
    146 module.exports = CachePanel;