| 1 | /* |
| 2 | * Copyright (C) 2009 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 | #pragma once |
| 32 | |
| 33 | #include "ThreadableLoaderClient.h" |
| 34 | #include <wtf/Ref.h> |
| 35 | #include <wtf/ThreadSafeRefCounted.h> |
| 36 | |
| 37 | namespace WebCore { |
| 38 | |
| 39 | class ThreadableLoaderClientWrapper : public ThreadSafeRefCounted<ThreadableLoaderClientWrapper> { |
| 40 | public: |
| 41 | static Ref<ThreadableLoaderClientWrapper> create(ThreadableLoaderClient& client, const String& initiator) |
| 42 | { |
| 43 | return adoptRef(*new ThreadableLoaderClientWrapper(client, initiator)); |
| 44 | } |
| 45 | |
| 46 | void clearClient() |
| 47 | { |
| 48 | m_done = true; |
| 49 | m_client = nullptr; |
| 50 | } |
| 51 | |
| 52 | bool done() const |
| 53 | { |
| 54 | return m_done; |
| 55 | } |
| 56 | |
| 57 | void didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent) |
| 58 | { |
| 59 | if (m_client) |
| 60 | m_client->didSendData(bytesSent, totalBytesToBeSent); |
| 61 | } |
| 62 | |
| 63 | void didReceiveResponse(unsigned long identifier, const ResourceResponse& response) |
| 64 | { |
| 65 | if (m_client) |
| 66 | m_client->didReceiveResponse(identifier, response); |
| 67 | } |
| 68 | |
| 69 | void didReceiveData(const char* data, int dataLength) |
| 70 | { |
| 71 | if (m_client) |
| 72 | m_client->didReceiveData(data, dataLength); |
| 73 | } |
| 74 | |
| 75 | void didFinishLoading(unsigned long identifier) |
| 76 | { |
| 77 | m_done = true; |
| 78 | if (m_client) |
| 79 | m_client->didFinishLoading(identifier); |
| 80 | } |
| 81 | |
| 82 | void didFail(const ResourceError& error) |
| 83 | { |
| 84 | m_done = true; |
| 85 | if (m_client) |
| 86 | m_client->didFail(error); |
| 87 | } |
| 88 | |
| 89 | void didReceiveAuthenticationCancellation(unsigned long identifier, const ResourceResponse& response) |
| 90 | { |
| 91 | if (m_client) |
| 92 | m_client->didReceiveResponse(identifier, response); |
| 93 | } |
| 94 | |
| 95 | const String& initiator() const { return m_initiator; } |
| 96 | |
| 97 | protected: |
| 98 | explicit ThreadableLoaderClientWrapper(ThreadableLoaderClient&, const String&); |
| 99 | |
| 100 | ThreadableLoaderClient* m_client; |
| 101 | String m_initiator; |
| 102 | bool m_done { false }; |
| 103 | }; |
| 104 | |
| 105 | inline ThreadableLoaderClientWrapper::ThreadableLoaderClientWrapper(ThreadableLoaderClient& client, const String& initiator) |
| 106 | : m_client(&client) |
| 107 | , m_initiator(initiator.isolatedCopy()) |
| 108 | { |
| 109 | } |
| 110 | |
| 111 | } // namespace WebCore |
| 112 | |