Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(473)

Side by Side Diff: Source/modules/serviceworkers/polyfills/cachePolyfill.js

Issue 314133002: Initial ServiceWorker Cache API polyfill. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: clean up, remediate to review. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/modules/serviceworkers/ServiceWorkerGlobalScope.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // A simple, incomplete implementation of the Cache API, intended to facilitate
6 // end to end serviceworker testing.
7
8 // FIXME: Support AbstractResponse/OpaqueResponse correctly.
9 // FIXME: Serialize the cache.
10 // FIXME: Implement CacheStorage API.
11 // FIXME: Bind all function references.
12 (function (global) {
falken 2014/06/06 17:02:45 I don't see it in the style guide, but "function(.
gavinp 2014/06/06 21:39:54 Done.
13 var _castToRequest = function (item) {
14 if (typeof item === 'string') {
15 item = new Request({
16 url: item,
17 });
18 }
19 return item;
20 };
21
22 var Cache = function() {
23 // An object containing a property for each HTTP fetch method. Those
24 // referenced objects contain a property for each URL, which is the
25 // Response.
26 this.entriesByMethod = {};
27 };
28
29 // FIXME: Should this be in the spec?
30 Cache.prototype.keys = function() {
31 var that = this;
32
33 return Promise.resolve(Array.prototype.concat.apply([], Object.keys(this .entriesByMethod).map(function (method) {
34 return Object.keys(that.entriesByMethod[method]).map(function (url) {
35 return new Request({method: method, url: url});
36 });
37 })));
38 };
39
40 // FIXME: Implement this.
41 // FIXME: Should spec rename each --> forEach?
42 Cache.prototype.forEach = Promise.reject.bind(Promise, 'Cache.prototype.forE ach() not implemented.');
43 Cache.prototype.each = Promise.reject.bind(Promise, 'Cache.prototype.each() not implemented.');
44
45 Cache.prototype.put = function (request, response) {
46 request = _castToRequest(request);
47
48 var that = this;
49 return new Promise(function (resolve, reject) {
50 if (!that.entriesByMethod.hasOwnProperty(request.method)) {
51 that.entriesByMethod[request.method] = {};
52 }
53
54 var entriesByUrl = that.entriesByMethod[request.method];
55 entriesByUrl[request.url] = response;
56
57 resolve();
58 });
59 };
60
61 // FIXME: Implement this.
62 Cache.prototype.add = Promise.reject.bind(Promise, 'Cache.prototype.add() no t implemented.');
63
64 // FIXME: Add QueryParams argument.
65 Cache.prototype.delete = function (request) {
66 request = _castToRequest(request);
67
68 var that = this;
69 return new Promise(function (resolve, reject) {
70 if (!that.entriesByMethod.hasOwnProperty(request.method)) {
71 reject('not found');
72 return;
73 }
74
75 var entriesByUrl = that.entriesByMethod[request.method];
76 delete entriesByUrl[request.url];
falken 2014/06/06 17:02:45 Should we reject if the URL isn't present, like we
gavinp 2014/06/06 21:39:54 jsbell suggested favouring idempotence, and so I w
77 resolve();
78 });
79 };
80
81 // FIXME: Add QueryParams argument.
82 Cache.prototype.match = function (request) {
83 request = _castToRequest(request);
84
85 var that = this;
86 return new Promise(function (resolve, reject) {
87 if (!that.entriesByMethod.hasOwnProperty(request.method)) {
88 reject('not found');
89 return;
90 }
91
92 var entriesByUrl = that.entriesByMethod[request.method];
93
94 if (!entriesByUrl.hasOwnProperty(request.url)) {
95 reject('not found');
96 return;
97 }
98
99 var entry = entriesByUrl[request.url];
100 resolve(entry);
101 });
102 };
103
104 // FIXME: Implement this.
105 Cache.prototype.matchAll = Promise.reject.bind(Promise, 'Cache.prototype.mat chAll not implemented.');
106
107 global.Cache = global.Cache || Cache;
108 }(self)); // window or worker global scope.
OLDNEW
« no previous file with comments | « Source/modules/serviceworkers/ServiceWorkerGlobalScope.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698