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

Unified 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: add comment 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/modules/serviceworkers/ServiceWorkerGlobalScope.cpp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/modules/serviceworkers/polyfills/cachePolyfill.js
diff --git a/Source/modules/serviceworkers/polyfills/cachePolyfill.js b/Source/modules/serviceworkers/polyfills/cachePolyfill.js
new file mode 100644
index 0000000000000000000000000000000000000000..1c90a0348942248916cf88abffda633f8c9cad1a
--- /dev/null
+++ b/Source/modules/serviceworkers/polyfills/cachePolyfill.js
@@ -0,0 +1,110 @@
+// 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.
+
+// A simple, incomplete implementation of the Cache API, intended to facilitate
+// end to end serviceworker testing.
+
+// See https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#cache
+
+// FIXME: Support AbstractResponse/OpaqueResponse correctly.
+// FIXME: Serialize the cache.
+// FIXME: Implement CacheStorage API.
+// FIXME: Bind all function references.
+(function (global) {
+ var _castToRequest = function (item) {
+ if (typeof item === 'string') {
+ item = new Request({
+ url: item,
+ });
+ }
+ return item;
+ };
+
+ var Cache = function() {
+ // An object containing a property for each HTTP fetch method. Those
+ // referenced objects contain a property for each URL, which is the
+ // Response.
+ this.entriesByMethod = {};
+ };
+
+ // FIXME: Should this be in the spec?
+ Cache.prototype.keys = function() {
+ var that = this;
+
+ return Promise.resolve(Array.prototype.concat.apply([], Object.keys(this.entriesByMethod).map(function (method) {
jsbell 2014/06/06 17:25:31 Maybe extract the Array.prototype.concat.apply([],
+ return Object.keys(that.entriesByMethod[method]).map(function (url) {
+ return new Request({method: method, url: url});
gavinp 2014/06/06 21:39:55 Have I followed the standard indentation practice
+ });
+ })));
+ };
+
+ // FIXME: Implement this.
+ // FIXME: Should spec rename each --> forEach?
+ Cache.prototype.forEach = Promise.reject.bind(Promise, 'Cache.prototype.forEach() not implemented.');
+ Cache.prototype.each = Promise.reject.bind(Promise, 'Cache.prototype.each() not implemented.');
+
+ Cache.prototype.put = function (request, response) {
+ request = _castToRequest(request);
jsbell 2014/06/06 17:25:31 Consider doing this inside the new Promise(functio
gavinp 2014/06/06 21:39:55 Done.
+
+ var that = this;
+ return new Promise(function (resolve, reject) {
+ if (!that.entriesByMethod.hasOwnProperty(request.method)) {
+ that.entriesByMethod[request.method] = {};
+ }
+
+ var entriesByUrl = that.entriesByMethod[request.method];
+ entriesByUrl[request.url] = response;
+
+ resolve();
+ });
+ };
+
+ // FIXME: Implement this.
+ Cache.prototype.add = Promise.reject.bind(Promise, 'Cache.prototype.add() not implemented.');
+
+ // FIXME: Add QueryParams argument.
+ Cache.prototype.delete = function (request) {
+ request = _castToRequest(request);
+
+ var that = this;
+ return new Promise(function (resolve, reject) {
+ if (!that.entriesByMethod.hasOwnProperty(request.method)) {
+ reject('not found');
jsbell 2014/06/06 17:25:31 This seems inconsistent - we reject if the *method
gavinp 2014/06/06 21:39:55 Done.
+ return;
+ }
+
+ var entriesByUrl = that.entriesByMethod[request.method];
+ delete entriesByUrl[request.url];
+ resolve();
+ });
+ };
+
+ // FIXME: Add QueryParams argument.
+ Cache.prototype.match = function (request) {
+ request = _castToRequest(request);
+
+ var that = this;
+ return new Promise(function (resolve, reject) {
+ if (!that.entriesByMethod.hasOwnProperty(request.method)) {
+ reject('not found');
+ return;
+ }
+
+ var entriesByUrl = that.entriesByMethod[request.method];
+
+ if (!entriesByUrl.hasOwnProperty(request.url)) {
+ reject('not found');
+ return;
+ }
+
+ var entry = entriesByUrl[request.url];
+ resolve(entry);
+ });
+ };
+
+ // FIXME: Implement this.
+ Cache.prototype.matchAll = Promise.reject.bind(Promise, 'Cache.prototype.matchAll not implemented.');
+
+ global.Cache = global.Cache || Cache;
+}(self)); // window or worker global scope.
« 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