1/*
2 * Copyright (C) 2014 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "core/loader/WorkerLoaderClientBridgeSyncHelper.h"
33
34#include "core/workers/WorkerGlobalScope.h"
35#include "core/workers/WorkerLoaderProxy.h"
36#include "public/platform/WebWaitableEvent.h"
37#include "wtf/ArrayBuffer.h"
38#include "wtf/Functional.h"
39#include "wtf/MainThread.h"
40#include "wtf/OwnPtr.h"
41
42namespace blink {
43
44PassOwnPtr<WorkerLoaderClientBridgeSyncHelper> WorkerLoaderClientBridgeSyncHelper::create(ThreadableLoaderClient& client, PassOwnPtr<blink::WebWaitableEvent> event)
45{
46    return adoptPtr(new WorkerLoaderClientBridgeSyncHelper(client, event));
47}
48
49WorkerLoaderClientBridgeSyncHelper::~WorkerLoaderClientBridgeSyncHelper()
50{
51    ASSERT(isMainThread());
52    for (size_t i = 0; i < m_receivedData.size(); ++i)
53        delete m_receivedData[i];
54}
55
56void WorkerLoaderClientBridgeSyncHelper::run()
57{
58    // This must be called only after m_event is signalled.
59    ASSERT(m_done);
60    for (size_t i = 0; i < m_clientTasks.size(); ++i)
61        m_clientTasks[i]();
62}
63
64void WorkerLoaderClientBridgeSyncHelper::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
65{
66    ASSERT(isMainThread());
67    m_clientTasks.append(bind(&ThreadableLoaderClient::didSendData, &m_client, bytesSent, totalBytesToBeSent));
68}
69
70static void didReceiveResponseAdapter(ThreadableLoaderClient* client, unsigned long identifier, PassOwnPtr<CrossThreadResourceResponseData> responseData)
71{
72    OwnPtr<ResourceResponse> response(ResourceResponse::adopt(responseData));
73    client->didReceiveResponse(identifier, *response);
74}
75
76void WorkerLoaderClientBridgeSyncHelper::didReceiveResponse(unsigned long identifier, const ResourceResponse& response)
77{
78    ASSERT(isMainThread());
79    m_clientTasks.append(bind(&didReceiveResponseAdapter, &m_client, identifier, response.copyData()));
80}
81
82void WorkerLoaderClientBridgeSyncHelper::didReceiveData(const char* data, int dataLength)
83{
84    ASSERT(isMainThread());
85    Vector<char>* buffer = new Vector<char>(dataLength);
86    memcpy(buffer->data(), data, dataLength);
87    m_receivedData.append(buffer);
88    m_clientTasks.append(bind(&ThreadableLoaderClient::didReceiveData, &m_client, static_cast<const char*>(buffer->data()), dataLength));
89}
90
91void WorkerLoaderClientBridgeSyncHelper::didDownloadData(int dataLength)
92{
93    ASSERT(isMainThread());
94    m_clientTasks.append(bind(&ThreadableLoaderClient::didDownloadData, &m_client, dataLength));
95}
96
97void WorkerLoaderClientBridgeSyncHelper::didReceiveCachedMetadata(const char* data, int dataLength)
98{
99    ASSERT(isMainThread());
100    Vector<char>* buffer = new Vector<char>(dataLength);
101    memcpy(buffer->data(), data, dataLength);
102    m_receivedData.append(buffer);
103    m_clientTasks.append(bind(&ThreadableLoaderClient::didReceiveCachedMetadata, &m_client, static_cast<const char*>(buffer->data()), dataLength));
104}
105
106void WorkerLoaderClientBridgeSyncHelper::didFinishLoading(unsigned long identifier, double finishTime)
107{
108    ASSERT(isMainThread());
109    m_clientTasks.append(bind(&ThreadableLoaderClient::didFinishLoading, &m_client, identifier, finishTime));
110    m_done = true;
111    m_event->signal();
112}
113
114void WorkerLoaderClientBridgeSyncHelper::didFail(const ResourceError& error)
115{
116    ASSERT(isMainThread());
117    m_clientTasks.append(bind(&ThreadableLoaderClient::didFail, &m_client, error.copy()));
118    m_done = true;
119    m_event->signal();
120}
121
122void WorkerLoaderClientBridgeSyncHelper::didFailAccessControlCheck(const ResourceError& error)
123{
124    ASSERT(isMainThread());
125    m_clientTasks.append(bind(&ThreadableLoaderClient::didFailAccessControlCheck, &m_client, error.copy()));
126    m_done = true;
127    m_event->signal();
128}
129
130void WorkerLoaderClientBridgeSyncHelper::didFailRedirectCheck()
131{
132    ASSERT(isMainThread());
133    m_clientTasks.append(bind(&ThreadableLoaderClient::didFailRedirectCheck, &m_client));
134    m_done = true;
135    m_event->signal();
136}
137
138WorkerLoaderClientBridgeSyncHelper::WorkerLoaderClientBridgeSyncHelper(ThreadableLoaderClient& client, PassOwnPtr<blink::WebWaitableEvent> event)
139    : m_done(false)
140    , m_client(client)
141    , m_event(event)
142{
143    ASSERT(m_event);
144}
145
146} // namespace blink
147