Chromium Code Reviews| OLD | NEW |
|---|---|
| (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. | |
| OLD | NEW |