summaryrefslogtreecommitdiffstats
path: root/devtools/shared/platform/cache-entry.js
blob: 7efd460ad85b5ed34d3a4420befb5d46cf31f7bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const Services = require("Services");
const { Ci } = require("chrome");
loader.lazyRequireGetter(
  this,
  "NetworkHelper",
  "devtools/shared/webconsole/network-helper"
);

/**
 * Module to fetch cache objects from CacheStorageService
 * and return them as an object.
 */
exports.CacheEntry = {
  /**
   * Flag for cache session being initialized.
   */
  isCacheSessionInitialized: false,
  /**
   * Cache session object.
   */
  cacheSession: null,

  /**
   * Initializes our cache session / cache storage session.
   */
  initializeCacheSession: function(request) {
    try {
      const cacheService = Services.cache2;
      if (cacheService) {
        let loadContext = NetworkHelper.getRequestLoadContext(request);
        if (!loadContext) {
          // Get default load context if we can't fetch.
          loadContext = Services.loadContextInfo.default;
        }
        this.cacheSession = cacheService.diskCacheStorage(loadContext, false);
        this.isCacheSessionInitialized = true;
      }
    } catch (e) {
      this.isCacheSessionInitialized = false;
    }
  },

  /**
   * Parses a cache descriptor returned from the backend into a
   * usable object.
   *
   * @param Object descriptor The descriptor from the backend.
   */
  parseCacheDescriptor: function(descriptor) {
    const descriptorObj = {};
    try {
      if (descriptor.storageDataSize) {
        descriptorObj.dataSize = descriptor.storageDataSize;
      }
    } catch (e) {
      // We just need to handle this in case it's a js file of 0B.
    }
    if (descriptor.expirationTime) {
      descriptorObj.expires = descriptor.expirationTime;
    }
    if (descriptor.fetchCount) {
      descriptorObj.fetchCount = descriptor.fetchCount;
    }
    if (descriptor.lastFetched) {
      descriptorObj.lastFetched = descriptor.lastFetched;
    }
    if (descriptor.lastModified) {
      descriptorObj.lastModified = descriptor.lastModified;
    }
    if (descriptor.deviceID) {
      descriptorObj.device = descriptor.deviceID;
    }
    return descriptorObj;
  },

  /**
   * Does the fetch for the cache descriptor from the session.
   *
   * @param string request
   *        The request object.
   * @param Function onCacheDescriptorAvailable
   *        callback function.
   */
  getCacheEntry: function(request, onCacheDescriptorAvailable) {
    if (!this.isCacheSessionInitialized) {
      this.initializeCacheSession(request);
    }
    if (this.cacheSession) {
      const uri = NetworkHelper.nsIURL(request.URI.spec);
      this.cacheSession.asyncOpenURI(
        uri,
        "",
        Ci.nsICacheStorage.OPEN_SECRETLY,
        {
          onCacheEntryCheck: (entry, appcache) => {
            return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED;
          },
          onCacheEntryAvailable: (descriptor, isnew, appcache, status) => {
            if (descriptor) {
              const descriptorObj = this.parseCacheDescriptor(descriptor);
              onCacheDescriptorAvailable(descriptorObj);
            } else {
              onCacheDescriptorAvailable(null);
            }
          },
        }
      );
    } else {
      onCacheDescriptorAvailable(null);
    }
  },
};