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