Commit 6e3c3c33 authored by gavinp@chromium.org's avatar gavinp@chromium.org

Initial ServiceWorker Cache API polyfill.

A basic Cache API. No serialization of any kind, no testing, and no
add() method, as it depends on Fetch().

R=falken@chromium.org,jkarlin@chromium.org
TBR=jochen@chromium.org
BUG=374822

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

git-svn-id: svn://svn.chromium.org/blink/trunk@175749 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent ade21272
...@@ -58,6 +58,33 @@ ...@@ -58,6 +58,33 @@
'<@(modules_files)', '<@(modules_files)',
'<@(bindings_modules_v8_generated_aggregate_files)', '<@(bindings_modules_v8_generated_aggregate_files)',
], ],
'actions': [
{
'action_name': 'CachePolyfill',
'process_outputs_as_sources': 1,
'variables': {
'resources': [
'serviceworkers/polyfills/cachePolyfill.js',
],
},
'inputs': [
'../build/scripts/make-file-arrays.py',
'<@(resources)',
],
'outputs': [
'<(SHARED_INTERMEDIATE_DIR)/blink/CachePolyfill.h',
'<(SHARED_INTERMEDIATE_DIR)/blink/CachePolyfill.cpp',
],
'action': [
'python',
'../build/scripts/make-file-arrays.py',
'--out-h=<(SHARED_INTERMEDIATE_DIR)/blink/CachePolyfill.h',
'--out-cpp=<(SHARED_INTERMEDIATE_DIR)/blink/CachePolyfill.cpp',
'--namespace=WebCore',
'<@(resources)',
],
},
],
# Disable c4267 warnings until we fix size_t to int truncations. # Disable c4267 warnings until we fix size_t to int truncations.
'msvs_disabled_warnings': [ 4267, 4334, ] 'msvs_disabled_warnings': [ 4267, 4334, ]
}, },
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "config.h" #include "config.h"
#include "ServiceWorkerGlobalScope.h" #include "ServiceWorkerGlobalScope.h"
#include "CachePolyfill.h"
#include "core/workers/WorkerClients.h" #include "core/workers/WorkerClients.h"
#include "core/workers/WorkerThreadStartupData.h" #include "core/workers/WorkerThreadStartupData.h"
#include "modules/EventTargetModules.h" #include "modules/EventTargetModules.h"
...@@ -47,6 +48,9 @@ PassRefPtrWillBeRawPtr<ServiceWorkerGlobalScope> ServiceWorkerGlobalScope::creat ...@@ -47,6 +48,9 @@ PassRefPtrWillBeRawPtr<ServiceWorkerGlobalScope> ServiceWorkerGlobalScope::creat
RefPtrWillBeRawPtr<ServiceWorkerGlobalScope> context = adoptRefWillBeRefCountedGarbageCollected(new ServiceWorkerGlobalScope(startupData->m_scriptURL, startupData->m_userAgent, thread, monotonicallyIncreasingTime(), startupData->m_workerClients.release())); RefPtrWillBeRawPtr<ServiceWorkerGlobalScope> context = adoptRefWillBeRefCountedGarbageCollected(new ServiceWorkerGlobalScope(startupData->m_scriptURL, startupData->m_userAgent, thread, monotonicallyIncreasingTime(), startupData->m_workerClients.release()));
context->applyContentSecurityPolicyFromString(startupData->m_contentSecurityPolicy, startupData->m_contentSecurityPolicyType); context->applyContentSecurityPolicyFromString(startupData->m_contentSecurityPolicy, startupData->m_contentSecurityPolicyType);
context->script()->evaluate(String(cachePolyfillJs));
return context.release(); return context.release();
} }
......
// 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;
var flatten = Array.prototype.concat.apply.bind(Array.prototype.concat, []);
return Promise.resolve(flatten(
Object.keys(this.entriesByMethod).map(function(method) {
return Object.keys(that.entriesByMethod[method]).map(function(url) {
return new Request({method: method, url: url});
});
})));
};
// 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) {
var that = this;
return new Promise(function(resolve, reject) {
request = _castToRequest(request);
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)) {
var entriesByUrl = that.entriesByMethod[request.method];
delete entriesByUrl[request.url];
}
resolve();
});
};
// FIXME: Add QueryParams argument.
Cache.prototype.match = function(request) {
var that = this;
return new Promise(function(resolve, reject) {
request = _castToRequest(request);
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.
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