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

Unified Diff: Source/modules/serviceworkers/polyfills/cacheStoragePolyfill.js

Issue 329433002: Initial ServiceWorker CacheStorage API polyfill. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: some fixes 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/polyfills/cachePolyfill.js ('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/cacheStoragePolyfill.js
diff --git a/Source/modules/serviceworkers/polyfills/cacheStoragePolyfill.js b/Source/modules/serviceworkers/polyfills/cacheStoragePolyfill.js
new file mode 100644
index 0000000000000000000000000000000000000000..9d1406a2fcfd30363e4d311742668e03bdfed030
--- /dev/null
+++ b/Source/modules/serviceworkers/polyfills/cacheStoragePolyfill.js
@@ -0,0 +1,79 @@
+// 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 CacheStorage API, intended to facilitate
+// end to end serviceworker testing.
+
+// See https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#cache-storage
+
+(function(global) {
+ var CacheStorage = function() {
+ this.cachesByName = {};
+ };
+
+ CacheStorage.prototype.get = function(key) {
+ if (this.cachesByName.hasOwnProperty(key)) {
+ return Promise.resolve(this.cachesByName[key]);
+ }
+ return Promise.reject("not found");
+ }
+
+ CacheStorage.prototype.has = function(key) {
+ if (this.cachesByName.hasOwnProperty(key)) {
+ return Promise.resolve(true);
+ }
+ return Promise.reject(false);
+ }
+
+ CacheStorage.prototype.set = function(key, cache) {
+ this.cachesByName[key] = cache;
+
+ return Promise.resolve();
+ }
+
+ CacheStorage.prototype.clear = function() {
+ this.cachesByName = {};
+
+ return Promise.resolve();
+ }
+
+ CacheStorage.prototype.delete = function(key) {
+ delete this.cachesByName[key];
+
+ return Promise.resolve();
+ }
+
+ CacheStorage.prototype.forEach = function(callback, thisArg) {
+ Object.keys(this.cachesByName).map(function (key) {
falken 2014/06/10 17:47:03 I think your other patches removed the space after
+ thisArg.callback(this.cachesByName[key], key, this);
+ });
+ return Promise.resolve();
+ }
+
+ // FIXME: Implement this.
+ CacheStorage.prototype.entries = Promise.reject.bind(Promise, 'CacheStorage.prototype.entries() not implemented.');
+
+ CacheStorage.prototype.keys = function() {
+ return Promise.resolve(this.cachesByName.keys());
+ }
+
+ CacheStorage.prototype.values = function() {
+ return Promise.resolve(Object.keys(this.cachesByName).map(function (key) {
+ return this.cachesByName[key];
+ }));
+ }
+
+ CacheStorage.prototype.size = function() {
+ return Promise.resolve(Object.keys(this.cachesByName).length);
+ }
+
+ CacheStorage.prototype.match = function(url, cacheName) {
+ this.get(cacheName).then(function (cache) {
+ return cache.match(url);
+ });
+ }
+
+ global.CacheStorage = global.CacheStorage || CacheStorage;
+ global.caches = global.caches || new CacheStorage();
+}(self)); // window or worker global scope.
« no previous file with comments | « Source/modules/serviceworkers/polyfills/cachePolyfill.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698