File: ServiceWorkerCacheModel.js

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (225 lines) | stat: -rw-r--r-- 6,544 bytes parent folder | download
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * Invariant: This model can only be constructed on a ServiceWorker target.
 * @constructor
 * @extends {WebInspector.SDKModel}
 */
WebInspector.ServiceWorkerCacheModel = function(target)
{
    WebInspector.SDKModel.call(this, WebInspector.ServiceWorkerCacheModel, target);

    /** @type {!Map.<!WebInspector.ServiceWorkerCacheModel.CacheId, !WebInspector.ServiceWorkerCacheModel.Cache>} */
    this._caches = new Map();
    /** @type {!Set.<string>} */
    this._cacheNames = new Set();

    this._agent = target.serviceWorkerCacheAgent();
}

WebInspector.ServiceWorkerCacheModel.EventTypes = {
    CacheAdded: "CacheAdded",
    CacheRemoved: "CacheRemoved",
}

WebInspector.ServiceWorkerCacheModel.prototype = {
    _reset: function()
    {
        this._updateCacheNames([]);
        this._loadCacheNames();
    },

    refreshCacheNames: function()
    {
        this._loadCacheNames();
    },

    /**
     * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId
     */
    deleteCache: function(cacheId)
    {
        /**
         * @this {WebInspector.ServiceWorkerCacheModel}
         */
        function callback(error)
        {
            if (error) {
                console.error("ServiceWorkerCacheAgent error: ", error);
                return;
            }
            this._cacheRemoved(cacheId.name);
        }
        this._agent.deleteCache(cacheId.name, callback.bind(this));
    },

    /**
     * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId
     * @param {number} skipCount
     * @param {number} pageSize
     * @param {function(!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>, boolean)} callback
     */
    loadCacheData: function(cacheId, skipCount, pageSize, callback)
    {
        this._requestEntries(cacheId, cacheId.name, skipCount, pageSize, callback);
    },

    /**
     * @return {!Array.<!WebInspector.ServiceWorkerCacheModel.CacheId>}
     */
    caches: function()
    {
        var result = [];
        for (var cacheName of this._cacheNames) {
          result.push(new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName));
        }
        return result;
    },

    dispose: function()
    {
        this._updateCacheNames([]);
    },

    _loadCacheNames: function()
    {
        /**
         * @param {?Protocol.Error} error
         * @param {!Array.<string>} cacheNames
         * @this {WebInspector.ServiceWorkerCacheModel}
         */
        function callback(error, cacheNames)
        {
            if (error) {
                console.error("ServiceWorkerCacheAgent error: ", error);
                return;
            }

            if (!this._cacheNames)
                return;
            this._updateCacheNames(cacheNames);
        }

        this._agent.requestCacheNames(callback.bind(this));
    },

    /**
     * @param {!Array.<string>} cacheNames
     */
    _updateCacheNames: function(cacheNames)
    {
        /** @type {!Set.<string>} */
        var newCacheNames = new Set(cacheNames);
        /** @type {!Set.<string>} */
        var oldCacheNames = this._cacheNames;

        this._cacheNames = new Set(cacheNames);

        for (var oldCacheName of oldCacheNames) {
            if (!newCacheNames[oldCacheName])
                this._cacheRemoved(oldCacheName);
        }
        for (var newCacheName of newCacheNames) {
            if (!oldCacheNames[newCacheName])
                this._cacheAdded(newCacheName);
        }
    },

    /**
     * @param {string} cacheName
     */
    _cacheAdded: function(cacheName)
    {
        var cacheId = new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName);
        this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.EventTypes.CacheAdded, cacheId);
    },

    /**
     * @param {string} cacheName
     */
    _cacheRemoved: function(cacheName)
    {
        var cacheId = new WebInspector.ServiceWorkerCacheModel.CacheId(cacheName);
        this.dispatchEventToListeners(WebInspector.ServiceWorkerCacheModel.EventTypes.CacheRemoved, cacheId);
    },

    /**
     * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId
     * @param {string} cacheName
     * @param {number} skipCount
     * @param {number} pageSize
     * @param {function(!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>, boolean)} callback
     */
    _requestEntries: function(cacheId, cacheName, skipCount, pageSize, callback)
    {
        /**
         * @param {?Protocol.Error} error
         * @param {!Array.<!WebInspector.ServiceWorkerCacheModel.Entry>} dataEntries
         * @param {boolean} hasMore
         * @this {WebInspector.ServiceWorkerCacheModel}
         */
        function innerCallback(error, dataEntries, hasMore)
        {
            if (error) {
                console.error("ServiceWorkerCacheAgent error: ", error);
                return;
            }

            if (!this._cacheNames)
                return;
            var entries = [];
            for (var i = 0; i < dataEntries.length; ++i) {
                var request = WebInspector.RemoteObject.fromLocalObject(JSON.parse(dataEntries[i].request));
                var response = WebInspector.RemoteObject.fromLocalObject(JSON.parse(dataEntries[i].response));
                entries.push(new WebInspector.ServiceWorkerCacheModel.Entry(request, response));
            }
            callback(entries, hasMore);
        }

        this._agent.requestEntries(cacheName, skipCount, pageSize, innerCallback.bind(this));
    },

    __proto__: WebInspector.SDKModel.prototype
}

/**
 * @constructor
 * @param {!WebInspector.RemoteObject} request
 * @param {!WebInspector.RemoteObject} response
 */
WebInspector.ServiceWorkerCacheModel.Entry = function(request, response)
{
    this.request = request;
    this.response = response;
}

/**
 * @constructor
 * @param {string} name
 */
WebInspector.ServiceWorkerCacheModel.CacheId = function(name)
{
    this.name = name;
}

WebInspector.ServiceWorkerCacheModel.CacheId.prototype = {
    /**
     * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId
     * @return {boolean}
     */
    equals: function(cacheId)
    {
        return this.name === cacheId.name;
    }
}

/**
 * @constructor
 * @param {!WebInspector.ServiceWorkerCacheModel.CacheId} cacheId
 */
WebInspector.ServiceWorkerCacheModel.Cache = function(cacheId)
{
    this.cacheId = cacheId;
}