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

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: really fix copyright 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 /*
jsbell 2014/06/05 18:06:01 Use new shorter license header: http://dev.chromiu
gavinp 2014/06/06 16:06:45 Done.
2 * Copyright (C) 2014 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 // A simple, incomplete implementation of the Cache API, intended to facilitate
32 // end to end serviceworker testing.
33
34 var Cache = (function () {
jsbell 2014/06/05 18:06:00 A good polyfill pattern is: (function(global) {
gavinp 2014/06/06 16:06:44 Thanks. I did this, and removed the scope for "var
35 function _castToRequest(item) {
36 if (typeof item == "string") {
37 item = new Request({
38 url: item,
39 method: 'GET',
jsbell 2014/06/05 18:06:01 I notice that the Fetch polyfill doesn't explicitl
gavinp 2014/06/06 16:06:44 It is not. Now fixed.
40 });
41 }
42 return item;
43 };
44
45 function Cache() {
46 // An object containing a property for each HTTP fetch method. Those
47 // referenced objects contain a property for each URL, which is the
48 // Response.
49 this.entriesByMethod = {};
50 };
51
52 Cache.prototype.keys = function() {
53 var entriesByMethod = this.entriesByMethod;
jsbell 2014/06/05 18:06:00 FYI, it's slightly more idiomatic JS to capture th
gavinp 2014/06/06 16:06:45 Fair point; I should not have optimized out the pr
54
55 return new Promise(function (resolve, reject) {
jsbell 2014/06/05 18:06:00 Nit: be consistent between function() and function
gavinp 2014/06/06 16:06:44 Done.
56 var result = [];
57
58 for (var method in entriesByMethod) {
jsbell 2014/06/05 18:06:01 for... in is fragile and can break when interactin
gavinp 2014/06/06 16:06:44 Thanks. I took the Object.keys() approach, and wen
59 for (var url in entriesByMethod[method]) {
60 result.push(new Request({method: method, url: url}));
61 }
62 }
63
64 resolve(result);
65 });
66 };
67
68 Cache.prototype.set = function(request, response) {
gavinp 2014/06/06 16:06:45 Called "put" in the latest revision of the spec.
69 request = _castToRequest(request);
70 var entriesByMethod = this.entriesByMethod;
71
72 return new Promise(function (resolve, reject) {
73 if (entriesByMethod[request.method] == undefined) {
jsbell 2014/06/05 18:06:01 Paranoia: use entriesByMethod.hasOwnProperty(reque
gavinp 2014/06/06 16:06:44 Done. I added a TODO at the top about binding func
74 entriesByMethod[request.method] = {};
75 }
76
77 var entriesByUrl = entriesByMethod[request.method];
78 entriesByUrl[request.url] = response;
79
80 resolve();
81 });
82 };
83
84 Cache.prototype.add = function(request) {
85 // FIXME: Implement this.
86 return new Promise(function (resolve, reject) {
jsbell 2014/06/05 18:06:00 FYI, could just write: return Promise.reject("..."
gavinp 2014/06/06 16:06:44 Done. Same thing in a few other places.
87 reject("Cache.add not implemented.");
88 });
89 };
90
91 Cache.prototype.delete = function(request) {
92 request = _castToRequest(request);
93
94 var entriesByMethod = this.entriesByMethod;
95 return new Promise(function (resolve, reject) {
96 var entriesByUrl = entriesByMethod[request.method];
97
98 if (entriesByUrl == undefined) {
99 reject("not found");
jsbell 2014/06/05 18:06:00 Need to `return` after this.
gavinp 2014/06/06 16:06:45 Done.
100 }
101
102 delete entriesByUrl[request.url];
103 resolve();
104 });
105 };
106
107 Cache.prototype.match = function(request) {
108 request = _castToRequest(request);
109
110 var entriesByMethod = this.entriesByMethod;
111
112 return new Promise(function (resolve, reject) {
113 var entriesByUrl = entriesByMethod[request.method];
jsbell 2014/06/05 18:06:01 Again, may want to test with entriesByMethod.hasOw
gavinp 2014/06/06 16:06:44 Done.
114
115 if (entriesByUrl == undefined) {
116 reject("not found");
jsbell 2014/06/05 18:06:01 Need to `return` after this.
gavinp 2014/06/06 16:06:44 Done.
117 }
118
119 var entry = entriesByUrl[request.url];
120 if (entry == undefined) {
121 reject("not found");
jsbell 2014/06/05 18:06:00 Need to `return` after this.
gavinp 2014/06/06 16:06:45 Done.
122 }
123 resolve(entry);
124 });
125 };
126
127 return Cache;
128 })();
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