2 * Copyright (C) 2014 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/loader/WorkerLoaderClientBridgeSyncHelper.h"
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"
44 PassOwnPtr<WorkerLoaderClientBridgeSyncHelper> WorkerLoaderClientBridgeSyncHelper::create(ThreadableLoaderClient& client, PassOwnPtr<blink::WebWaitableEvent> event)
46 return adoptPtr(new WorkerLoaderClientBridgeSyncHelper(client, event));
49 WorkerLoaderClientBridgeSyncHelper::~WorkerLoaderClientBridgeSyncHelper()
51 ASSERT(isMainThread());
52 for (size_t i = 0; i < m_receivedData.size(); ++i)
53 delete m_receivedData[i];
56 void WorkerLoaderClientBridgeSyncHelper::run()
58 // This must be called only after m_event is signalled.
60 for (size_t i = 0; i < m_clientTasks.size(); ++i)
64 void WorkerLoaderClientBridgeSyncHelper::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
66 ASSERT(isMainThread());
67 m_clientTasks.append(bind(&ThreadableLoaderClient::didSendData, &m_client, bytesSent, totalBytesToBeSent));
70 static void didReceiveResponseAdapter(ThreadableLoaderClient* client, unsigned long identifier, PassOwnPtr<CrossThreadResourceResponseData> responseData, PassOwnPtr<WebDataConsumerHandle> handle)
72 OwnPtr<ResourceResponse> response(ResourceResponse::adopt(responseData));
73 client->didReceiveResponse(identifier, *response, handle);
76 void WorkerLoaderClientBridgeSyncHelper::didReceiveResponse(unsigned long identifier, const ResourceResponse& response, PassOwnPtr<WebDataConsumerHandle> handle)
78 ASSERT(isMainThread());
79 m_clientTasks.append(bind(&didReceiveResponseAdapter, &m_client, identifier, response.copyData(), handle));
82 void WorkerLoaderClientBridgeSyncHelper::didReceiveData(const char* data, unsigned dataLength)
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));
91 void WorkerLoaderClientBridgeSyncHelper::didDownloadData(int dataLength)
93 ASSERT(isMainThread());
94 m_clientTasks.append(bind(&ThreadableLoaderClient::didDownloadData, &m_client, dataLength));
97 void WorkerLoaderClientBridgeSyncHelper::didReceiveCachedMetadata(const char* data, int dataLength)
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));
106 void WorkerLoaderClientBridgeSyncHelper::didFinishLoading(unsigned long identifier, double finishTime)
108 ASSERT(isMainThread());
109 m_clientTasks.append(bind(&ThreadableLoaderClient::didFinishLoading, &m_client, identifier, finishTime));
114 void WorkerLoaderClientBridgeSyncHelper::didFail(const ResourceError& error)
116 ASSERT(isMainThread());
117 m_clientTasks.append(bind(&ThreadableLoaderClient::didFail, &m_client, error.copy()));
122 void WorkerLoaderClientBridgeSyncHelper::didFailAccessControlCheck(const ResourceError& error)
124 ASSERT(isMainThread());
125 m_clientTasks.append(bind(&ThreadableLoaderClient::didFailAccessControlCheck, &m_client, error.copy()));
130 void WorkerLoaderClientBridgeSyncHelper::didFailRedirectCheck()
132 ASSERT(isMainThread());
133 m_clientTasks.append(bind(&ThreadableLoaderClient::didFailRedirectCheck, &m_client));
138 WorkerLoaderClientBridgeSyncHelper::WorkerLoaderClientBridgeSyncHelper(ThreadableLoaderClient& client, PassOwnPtr<blink::WebWaitableEvent> event)