| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // A simple, incomplete implementation of the Cache API, intended to facilitate | 5 // A simple, incomplete implementation of the Cache API, intended to facilitate |
| 6 // end to end serviceworker testing. | 6 // end to end serviceworker testing. |
| 7 | 7 |
| 8 // See https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.htm
l#cache | 8 // See https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.htm
l#cache |
| 9 | 9 |
| 10 // FIXME: Support AbstractResponse/OpaqueResponse correctly. | 10 // FIXME: Support AbstractResponse/OpaqueResponse correctly. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 that.entriesByMethod[request.method] = {}; | 53 that.entriesByMethod[request.method] = {}; |
| 54 } | 54 } |
| 55 | 55 |
| 56 var entriesByUrl = that.entriesByMethod[request.method]; | 56 var entriesByUrl = that.entriesByMethod[request.method]; |
| 57 entriesByUrl[request.url] = response; | 57 entriesByUrl[request.url] = response; |
| 58 | 58 |
| 59 resolve(); | 59 resolve(); |
| 60 }); | 60 }); |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 // FIXME: Implement this. | 63 Cache.prototype.add = function(request) { |
| 64 Cache.prototype.add = Promise.reject.bind(Promise, 'Cache.prototype.add() no
t implemented.'); | 64 var that = this; |
| 65 request = _castToRequest(request); |
| 66 return new Promise(function (resolve, reject) { |
| 67 fetch(request).then( |
| 68 function(response) { |
| 69 that.put(request, response).then(resolve); |
| 70 }, |
| 71 reject); |
| 72 }); |
| 73 }; |
| 65 | 74 |
| 66 // FIXME: Add QueryParams argument. | 75 // FIXME: Add QueryParams argument. |
| 67 Cache.prototype.delete = function (request) { | 76 Cache.prototype.delete = function (request) { |
| 68 request = _castToRequest(request); | 77 request = _castToRequest(request); |
| 69 | 78 |
| 70 var that = this; | 79 var that = this; |
| 71 return new Promise(function (resolve, reject) { | 80 return new Promise(function (resolve, reject) { |
| 72 if (!that.entriesByMethod.hasOwnProperty(request.method)) { | 81 if (!that.entriesByMethod.hasOwnProperty(request.method)) { |
| 73 reject('not found'); | 82 reject('not found'); |
| 74 return; | 83 return; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 101 var entry = entriesByUrl[request.url]; | 110 var entry = entriesByUrl[request.url]; |
| 102 resolve(entry); | 111 resolve(entry); |
| 103 }); | 112 }); |
| 104 }; | 113 }; |
| 105 | 114 |
| 106 // FIXME: Implement this. | 115 // FIXME: Implement this. |
| 107 Cache.prototype.matchAll = Promise.reject.bind(Promise, 'Cache.prototype.mat
chAll not implemented.'); | 116 Cache.prototype.matchAll = Promise.reject.bind(Promise, 'Cache.prototype.mat
chAll not implemented.'); |
| 108 | 117 |
| 109 global.Cache = global.Cache || Cache; | 118 global.Cache = global.Cache || Cache; |
| 110 }(self)); // window or worker global scope. | 119 }(self)); // window or worker global scope. |
| OLD | NEW |