Skip to content

Commit

Permalink
ServiceWorker: Add a Response ctor that accepts the body as a Blob
Browse files Browse the repository at this point in the history
This ctor is expected to be added to the spec:
w3c/ServiceWorker#192

Eventually we'll also have ctors for String, Stream, and
ArrayBuffer bodies.

Multi-sided patch to implement blob-type fetch event
response bodies:
#1: blink-side, THIS PATCH
#2: chromium-side: https://codereview.chromium.org/304153015/
#3: blink-side: https://codereview.chromium.org/304233017

BUG=379074

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

git-svn-id: svn://svn.chromium.org/blink/trunk@175350 bbb929c8-8fbe-4397-9dbb-9b2b20218538
  • Loading branch information
falken@chromium.org committed Jun 3, 2014
1 parent 8a67586 commit 18fe14c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test(function() {
headers.set('Content-Type', 'text/html; charset=UTF-8');
headers.set('X-ServiceWorker-Test', 'response test field');

var response = new Response({
var response = new Response(new Blob(), {
status: 303,
statusText: 'See Other',
headers: headers
Expand Down
18 changes: 13 additions & 5 deletions Source/modules/serviceworkers/Response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@
#include "Response.h"

#include "bindings/v8/Dictionary.h"
#include "core/fileapi/Blob.h"
#include "modules/serviceworkers/ResponseInit.h"
#include "platform/NotImplemented.h"
#include "public/platform/WebServiceWorkerResponse.h"

namespace WebCore {

PassRefPtr<Response> Response::create()
// FIXME: Remove this legacy function when the required Chromium-side patch lands.
PassRefPtr<Response> Response::create(const Dictionary& responseInit)
{
return create(Dictionary());
return create(nullptr, responseInit);
}

PassRefPtr<Response> Response::create(const Dictionary& responseInit)
PassRefPtr<Response> Response::create(Blob* body, const Dictionary& responseInit)
{
return adoptRef(new Response(ResponseInit(responseInit)));
RefPtr<BlobDataHandle> blobDataHandle = body ? body->blobDataHandle() : nullptr;

// FIXME: Maybe append or override content-length and content-type headers using the blob. The spec will clarify what to do:
// https://github.com/slightlyoff/ServiceWorker/issues/192
return adoptRef(new Response(blobDataHandle.release(), ResponseInit(responseInit)));
}

PassRefPtr<HeaderMap> Response::headers() const
Expand All @@ -33,12 +39,14 @@ void Response::populateWebServiceWorkerResponse(blink::WebServiceWorkerResponse&
response.setStatus(status());
response.setStatusText(statusText());
response.setHeaders(m_headers->headerMap());
response.setBlobDataHandle(m_blobDataHandle);
}

Response::Response(const ResponseInit& responseInit)
Response::Response(PassRefPtr<BlobDataHandle> blobDataHandle, const ResponseInit& responseInit)
: m_status(responseInit.status)
, m_statusText(responseInit.statusText)
, m_headers(responseInit.headers)
, m_blobDataHandle(blobDataHandle)
{
ScriptWrappable::init(this);

Expand Down
7 changes: 5 additions & 2 deletions Source/modules/serviceworkers/Response.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "bindings/v8/Dictionary.h"
#include "bindings/v8/ScriptWrappable.h"
#include "modules/serviceworkers/HeaderMap.h"
#include "platform/blob/BlobData.h"
#include "wtf/RefCounted.h"
#include "wtf/RefPtr.h"
#include "wtf/text/WTFString.h"
Expand All @@ -16,12 +17,13 @@ namespace blink { class WebServiceWorkerResponse; }

namespace WebCore {

class Blob;
struct ResponseInit;

class Response FINAL : public ScriptWrappable, public RefCounted<Response> {
public:
static PassRefPtr<Response> create();
static PassRefPtr<Response> create(const Dictionary& responseInit);
static PassRefPtr<Response> create(Blob* body, const Dictionary& responseInit);
~Response() { };

unsigned short status() const { return m_status; }
Expand All @@ -31,10 +33,11 @@ class Response FINAL : public ScriptWrappable, public RefCounted<Response> {
void populateWebServiceWorkerResponse(blink::WebServiceWorkerResponse&);

private:
explicit Response(const ResponseInit&);
Response(PassRefPtr<BlobDataHandle>, const ResponseInit&);
unsigned short m_status;
String m_statusText;
RefPtr<HeaderMap> m_headers;
RefPtr<BlobDataHandle> m_blobDataHandle;
};

} // namespace WebCore
Expand Down
3 changes: 3 additions & 0 deletions Source/modules/serviceworkers/Response.idl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
// https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#response-objects
// FIXME: Split this idl/impl into AbstractResponse and Response.
[
// FIXME: Remove this legacy function after the required Chromium-side patch lands.
Constructor(optional Dictionary responseInitDict),
// FIXME: Add ctors for String, Stream, and ArrayBuffer/ArrayBufferView response bodies.
Constructor(Blob body, optional Dictionary responseInitDict),
RuntimeEnabled=ServiceWorker,
Exposed=ServiceWorker
] interface Response {
Expand Down
20 changes: 20 additions & 0 deletions Source/platform/exported/WebServiceWorkerResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
#include "config.h"
#include "public/platform/WebServiceWorkerResponse.h"

#include "platform/blob/BlobData.h"

namespace blink {

class WebServiceWorkerResponsePrivate : public RefCounted<WebServiceWorkerResponsePrivate> {
public:
unsigned short status;
WebString statusText;
HashMap<String, String> headers;
RefPtr<WebCore::BlobDataHandle> blobDataHandle;
};

WebServiceWorkerResponse::WebServiceWorkerResponse()
Expand Down Expand Up @@ -66,6 +69,13 @@ WebString WebServiceWorkerResponse::getHeader(const WebString& key) const
return m_private->headers.get(key);
}

WebString WebServiceWorkerResponse::blobUUID() const
{
if (!m_private->blobDataHandle)
return WebString();
return m_private->blobDataHandle->uuid();
}

void WebServiceWorkerResponse::setHeaders(const HashMap<String, String>& headers)
{
m_private->headers = headers;
Expand All @@ -76,4 +86,14 @@ const HashMap<String, String>& WebServiceWorkerResponse::headers() const
return m_private->headers;
}

void WebServiceWorkerResponse::setBlobDataHandle(PassRefPtr<WebCore::BlobDataHandle> blobDataHandle)
{
m_private->blobDataHandle = blobDataHandle;
}

PassRefPtr<WebCore::BlobDataHandle> WebServiceWorkerResponse::blobDataHandle() const
{
return m_private->blobDataHandle;
}

} // namespace blink
14 changes: 9 additions & 5 deletions public/platform/WebServiceWorkerResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
#include "wtf/Forward.h"
#include "wtf/HashMap.h"
#include "wtf/text/StringHash.h"
#endif

// FIXME: Remove this after chromium code is cleaned up.
#define NEW_SERVICE_WORKER_RESPONSE_INTERFACE
namespace WebCore {
class BlobDataHandle;
}
#endif

namespace blink {

Expand Down Expand Up @@ -49,12 +50,15 @@ class BLINK_PLATFORM_EXPORT WebServiceWorkerResponse {
WebVector<WebString> getHeaderKeys() const;
WebString getHeader(const WebString& key) const;

WebString blobUUID() const;

#if INSIDE_BLINK
void setHeaders(const HashMap<String, String>&);
const HashMap<String, String>& headers() const;
#endif

// FIXME: Eventually this should have additional methods such as for blob.
void setBlobDataHandle(PassRefPtr<WebCore::BlobDataHandle>);
PassRefPtr<WebCore::BlobDataHandle> blobDataHandle() const;
#endif

private:
WebPrivatePtr<WebServiceWorkerResponsePrivate> m_private;
Expand Down

0 comments on commit 18fe14c

Please sign in to comment.