Commit f8a98672 authored by gavinp@chromium.org's avatar gavinp@chromium.org

Initial ServiceWorker CacheStorage API polyfill.

This provides CacheStorage, the namespace of caches, although without
serialization. The idea is to let us test this API end-to-end early,
and we can replace it with something more sophisticated as we
progress.

R=jsbell@chromium.org,falken@chromium.org,jkarlin@chromium.org,asanka@chromium.org,slightlyoff@chromium.org,jakearchibald@chromium.org
TBR=jochen@chromium.org
BUG=374822

Review URL: https://codereview.chromium.org/329433002

git-svn-id: svn://svn.chromium.org/blink/trunk@175988 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent bcb946d0
......@@ -109,6 +109,31 @@
'<@(resources)',
],
},
{
'action_name': 'CacheStoragePolyfill',
'process_outputs_as_sources': 1,
'variables': {
'resources': [
'serviceworkers/polyfills/cacheStoragePolyfill.js',
],
},
'inputs': [
'../build/scripts/make-file-arrays.py',
'<@(resources)',
],
'outputs': [
'<(SHARED_INTERMEDIATE_DIR)/blink/CacheStoragePolyfill.h',
'<(SHARED_INTERMEDIATE_DIR)/blink/CacheStoragePolyfill.cpp',
],
'action': [
'python',
'../build/scripts/make-file-arrays.py',
'--out-h=<(SHARED_INTERMEDIATE_DIR)/blink/CacheStoragePolyfill.h',
'--out-cpp=<(SHARED_INTERMEDIATE_DIR)/blink/CacheStoragePolyfill.cpp',
'--namespace=WebCore',
'<@(resources)',
],
},
],
# Disable c4267 warnings until we fix size_t to int truncations.
'msvs_disabled_warnings': [ 4267, 4334, ]
......
......@@ -31,6 +31,7 @@
#include "ServiceWorkerGlobalScope.h"
#include "CachePolyfill.h"
#include "CacheStoragePolyfill.h"
#include "FetchPolyfill.h"
#include "core/workers/WorkerClients.h"
#include "core/workers/WorkerThreadStartupData.h"
......@@ -52,6 +53,7 @@ PassRefPtrWillBeRawPtr<ServiceWorkerGlobalScope> ServiceWorkerGlobalScope::creat
context->script()->evaluate(String(fetchPolyfillJs, sizeof(fetchPolyfillJs)));
context->script()->evaluate(String(cachePolyfillJs, sizeof(cachePolyfillJs)));
context->script()->evaluate(String(cacheStoragePolyfillJs, sizeof(cacheStoragePolyfillJs)));
return context.release();
}
......
......@@ -5,11 +5,10 @@
// 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
// See https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#cache-objects
// FIXME: Support AbstractResponse/OpaqueResponse correctly.
// FIXME: Serialize the cache.
// FIXME: Implement CacheStorage API.
// FIXME: Bind all function references.
(function(global) {
var _castToRequest = function(item) {
......
// 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) {
return Promise.resolve(this.cachesByName.hasOwnProperty(key));
};
// FIXME: Engage standardization on removing this method from the spec.
CacheStorage.prototype.set = Promise.reject.bind(Promise, 'CacheStorage.prototype.set() not implemented.');
// FIXME: Engage standardization on adding this method to the spec.
CacheStorage.prototype.create = function(key) {
this.cachesByName[key] = new Cache();
return Promise.resolve();
};
// FIXME: Engage standarization on adding this method to the spec.
CacheStorage.prototype.rename = function(fromKey, toKey) {
if (!this.cachesByName.hasOwnProperty(fromKey)) {
return Promise.reject('not found');
}
this.cachesByName[toKey] = this.cachesByName[fromKey];
delete this.cachesByName[fromKey];
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) {
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(Object.keys(this.cachesByName));
};
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);
};
// FIXME: Figure out what should be done with undefined or poorly defined |cacheName| values.
CacheStorage.prototype.match = function(url, cacheName) {
return this.get(cacheName).then(function(cache) {
return cache.match(url);
});
};
global.caches = global.caches || new CacheStorage();
}(self)); // window or worker global scope.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment