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

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: 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 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..30c145279c4523c5724931a430c36a965a4fc9c7
--- /dev/null
+++ b/Source/modules/serviceworkers/polyfills/cachePolyfill.js
@@ -0,0 +1,128 @@
+/*
jsbell 2014/06/05 18:06:01 Use new shorter license header: http://dev.chromiu
gavinp 2014/06/06 16:06:45 Done.
+ * Copyright (C) 2014 Google Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+// A simple, incomplete implementation of the Cache API, intended to facilitate
+// end to end serviceworker testing.
+
+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
+ function _castToRequest(item) {
+ if (typeof item == "string") {
+ item = new Request({
+ url: item,
+ 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.
+ });
+ }
+ return item;
+ };
+
+ function Cache() {
+ // 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 = {};
+ };
+
+ Cache.prototype.keys = function() {
+ 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
+
+ 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.
+ var result = [];
+
+ 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
+ for (var url in entriesByMethod[method]) {
+ result.push(new Request({method: method, url: url}));
+ }
+ }
+
+ resolve(result);
+ });
+ };
+
+ Cache.prototype.set = function(request, response) {
gavinp 2014/06/06 16:06:45 Called "put" in the latest revision of the spec.
+ request = _castToRequest(request);
+ var entriesByMethod = this.entriesByMethod;
+
+ return new Promise(function (resolve, reject) {
+ 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
+ entriesByMethod[request.method] = {};
+ }
+
+ var entriesByUrl = entriesByMethod[request.method];
+ entriesByUrl[request.url] = response;
+
+ resolve();
+ });
+ };
+
+ Cache.prototype.add = function(request) {
+ // FIXME: Implement this.
+ 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.
+ reject("Cache.add not implemented.");
+ });
+ };
+
+ Cache.prototype.delete = function(request) {
+ request = _castToRequest(request);
+
+ var entriesByMethod = this.entriesByMethod;
+ return new Promise(function (resolve, reject) {
+ var entriesByUrl = entriesByMethod[request.method];
+
+ if (entriesByUrl == undefined) {
+ reject("not found");
jsbell 2014/06/05 18:06:00 Need to `return` after this.
gavinp 2014/06/06 16:06:45 Done.
+ }
+
+ delete entriesByUrl[request.url];
+ resolve();
+ });
+ };
+
+ Cache.prototype.match = function(request) {
+ request = _castToRequest(request);
+
+ var entriesByMethod = this.entriesByMethod;
+
+ return new Promise(function (resolve, reject) {
+ 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.
+
+ if (entriesByUrl == undefined) {
+ reject("not found");
jsbell 2014/06/05 18:06:01 Need to `return` after this.
gavinp 2014/06/06 16:06:44 Done.
+ }
+
+ var entry = entriesByUrl[request.url];
+ if (entry == undefined) {
+ reject("not found");
jsbell 2014/06/05 18:06:00 Need to `return` after this.
gavinp 2014/06/06 16:06:45 Done.
+ }
+ resolve(entry);
+ });
+ };
+
+ return Cache;
+})();
« 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