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#ifndef WorkerThreadableLoader_h 32#define WorkerThreadableLoader_h 33 34#include "ThreadableLoader.h" 35#include "ThreadableLoaderClient.h" 36#include "ThreadableLoaderClientWrapper.h" 37 38#include <wtf/PassOwnPtr.h> 39#include <wtf/PassRefPtr.h> 40#include <wtf/RefCounted.h> 41#include <wtf/RefPtr.h> 42#include <wtf/Threading.h> 43#include <wtf/text/WTFString.h> 44 45namespace WebCore { 46 47 class ResourceError; 48 class ResourceRequest; 49 class WorkerGlobalScope; 50 class WorkerLoaderProxy; 51 struct CrossThreadResourceResponseData; 52 struct CrossThreadResourceRequestData; 53 54 class WorkerThreadableLoader : public RefCounted<WorkerThreadableLoader>, public ThreadableLoader { 55 WTF_MAKE_FAST_ALLOCATED; 56 public: 57 static void loadResourceSynchronously(WorkerGlobalScope*, const ResourceRequest&, ThreadableLoaderClient&, const ThreadableLoaderOptions&); 58 static PassRefPtr<WorkerThreadableLoader> create(WorkerGlobalScope* workerGlobalScope, ThreadableLoaderClient* client, const String& taskMode, const ResourceRequest& request, const ThreadableLoaderOptions& options) 59 { 60 return adoptRef(new WorkerThreadableLoader(workerGlobalScope, client, taskMode, request, options)); 61 } 62 63 ~WorkerThreadableLoader(); 64 65 virtual void cancel() override; 66 67 bool done() const { return m_workerClientWrapper->done(); } 68 69 using RefCounted<WorkerThreadableLoader>::ref; 70 using RefCounted<WorkerThreadableLoader>::deref; 71 72 protected: 73 virtual void refThreadableLoader() override { ref(); } 74 virtual void derefThreadableLoader() override { deref(); } 75 76 private: 77 // Creates a loader on the main thread and bridges communication between 78 // the main thread and the worker context's thread where WorkerThreadableLoader runs. 79 // 80 // Regarding the bridge and lifetimes of items used in callbacks, there are a few cases: 81 // 82 // all cases. All tasks posted from the worker context's thread are ok because 83 // the last task posted always is "mainThreadDestroy", so MainThreadBridge is 84 // around for all tasks that use it on the main thread. 85 // 86 // case 1. worker.terminate is called. 87 // In this case, no more tasks are posted from the worker object's thread to the worker 88 // context's thread -- WorkerGlobalScopeProxy implementation enforces this. 89 // 90 // case 2. xhr gets aborted and the worker context continues running. 91 // The ThreadableLoaderClientWrapper has the underlying client cleared, so no more calls 92 // go through it. All tasks posted from the worker object's thread to the worker context's 93 // thread do "ThreadableLoaderClientWrapper::ref" (automatically inside of the cross thread copy 94 // done in CrossThreadTask), so the ThreadableLoaderClientWrapper instance is there until all 95 // tasks are executed. 96 class MainThreadBridge : public ThreadableLoaderClient { 97 public: 98 // All executed on the worker context's thread. 99 MainThreadBridge(PassRefPtr<ThreadableLoaderClientWrapper>, WorkerLoaderProxy&, const String& taskMode, const ResourceRequest&, const ThreadableLoaderOptions&, const String& outgoingReferrer); 100 void cancel(); 101 void destroy(); 102 103 private: 104 // Executed on the worker context's thread. 105 void clearClientWrapper(); 106 107 // All executed on the main thread. 108 static void mainThreadDestroy(ScriptExecutionContext&, MainThreadBridge*); 109 ~MainThreadBridge(); 110 111 static void mainThreadCreateLoader(ScriptExecutionContext&, MainThreadBridge*, PassOwnPtr<CrossThreadResourceRequestData>, ThreadableLoaderOptions, const String& outgoingReferrer); 112 static void mainThreadCancel(ScriptExecutionContext&, MainThreadBridge*); 113 virtual void didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent) override; 114 virtual void didReceiveResponse(unsigned long identifier, const ResourceResponse&) override; 115 virtual void didReceiveData(const char*, int dataLength) override; 116 virtual void didFinishLoading(unsigned long identifier, double finishTime) override; 117 virtual void didFail(const ResourceError&) override; 118 virtual void didFailAccessControlCheck(const ResourceError&) override; 119 virtual void didFailRedirectCheck() override; 120 121 // Only to be used on the main thread. 122 RefPtr<ThreadableLoader> m_mainThreadLoader; 123 124 // ThreadableLoaderClientWrapper is to be used on the worker context thread. 125 // The ref counting is done on either thread. 126 RefPtr<ThreadableLoaderClientWrapper> m_workerClientWrapper; 127 128 // May be used on either thread. 129 WorkerLoaderProxy& m_loaderProxy; 130 131 // For use on the main thread. 132 String m_taskMode; 133 }; 134 135 WorkerThreadableLoader(WorkerGlobalScope*, ThreadableLoaderClient*, const String& taskMode, const ResourceRequest&, const ThreadableLoaderOptions&); 136 137 RefPtr<WorkerGlobalScope> m_workerGlobalScope; 138 RefPtr<ThreadableLoaderClientWrapper> m_workerClientWrapper; 139 MainThreadBridge& m_bridge; 140 }; 141 142} // namespace WebCore 143 144#endif // WorkerThreadableLoader_h 145