2 * Copyright (C) 2009 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.
31 #ifndef WorkerThreadableLoader_h
32 #define WorkerThreadableLoader_h
34 #include "core/loader/ThreadableLoader.h"
35 #include "core/loader/ThreadableLoaderClient.h"
36 #include "core/loader/ThreadableLoaderClientWrapper.h"
37 #include "platform/heap/Handle.h"
38 #include "platform/weborigin/Referrer.h"
39 #include "wtf/PassOwnPtr.h"
40 #include "wtf/PassRefPtr.h"
41 #include "wtf/RefPtr.h"
42 #include "wtf/Threading.h"
43 #include "wtf/text/WTFString.h"
48 class ResourceRequest
;
49 class WorkerGlobalScope
;
50 class WorkerLoaderProxy
;
51 struct CrossThreadResourceRequestData
;
53 class WorkerThreadableLoader final
: public ThreadableLoader
, private ThreadableLoaderClientWrapper::ResourceTimingClient
{
54 WTF_MAKE_FAST_ALLOCATED(WorkerThreadableLoader
);
56 static void loadResourceSynchronously(WorkerGlobalScope
&, const ResourceRequest
&, ThreadableLoaderClient
&, const ThreadableLoaderOptions
&, const ResourceLoaderOptions
&);
57 static PassRefPtr
<WorkerThreadableLoader
> create(WorkerGlobalScope
& workerGlobalScope
, PassRefPtr
<ThreadableLoaderClientWrapper
> clientWrapper
, PassOwnPtr
<ThreadableLoaderClient
> clientBridge
, const ResourceRequest
& request
, const ThreadableLoaderOptions
& options
, const ResourceLoaderOptions
& resourceLoaderOptions
)
59 return adoptRef(new WorkerThreadableLoader(workerGlobalScope
, clientWrapper
, clientBridge
, request
, options
, resourceLoaderOptions
));
62 ~WorkerThreadableLoader() override
;
64 void overrideTimeout(unsigned long timeout
) override
;
66 void cancel() override
;
69 // Creates a loader on the main thread and bridges communication between
70 // the main thread and the worker context's thread where WorkerThreadableLoader runs.
72 // Regarding the bridge and lifetimes of items used in callbacks, there are a few cases:
74 // all cases. All tasks posted from the worker context's thread are ok because
75 // the last task posted always is "mainThreadDestroy", so MainThreadBridge is
76 // around for all tasks that use it on the main thread.
78 // case 1. worker.terminate is called.
79 // In this case, no more tasks are posted from the worker object's thread to the worker
80 // context's thread -- WorkerGlobalScopeProxy implementation enforces this.
82 // case 2. xhr gets aborted and the worker context continues running.
83 // The ThreadableLoaderClientWrapper has the underlying client cleared, so no more calls
84 // go through it. All tasks posted from the worker object's thread to the worker context's
85 // thread do "ThreadableLoaderClientWrapper::ref" (automatically inside of the cross thread copy
86 // done in createCrossThreadTask), so the ThreadableLoaderClientWrapper instance is there until all
87 // tasks are executed.
88 class MainThreadBridge final
: public ThreadableLoaderClient
{
90 // All executed on the worker context's thread.
91 MainThreadBridge(PassRefPtr
<ThreadableLoaderClientWrapper
>, PassOwnPtr
<ThreadableLoaderClient
>, PassRefPtr
<WorkerLoaderProxy
>, const ResourceRequest
&, const ThreadableLoaderOptions
&, const ResourceLoaderOptions
&, const ReferrerPolicy
, const String
& outgoingReferrer
);
92 void overrideTimeout(unsigned long timeoutMilliseconds
);
97 // Executed on the worker context's thread.
98 void clearClientWrapper();
100 // All executed on the main thread.
101 void mainThreadDestroy(ExecutionContext
*);
102 ~MainThreadBridge() override
;
104 void mainThreadCreateLoader(PassOwnPtr
<CrossThreadResourceRequestData
>, ThreadableLoaderOptions
, ResourceLoaderOptions
, const ReferrerPolicy
, const String
& outgoingReferrer
, ExecutionContext
*);
105 void mainThreadOverrideTimeout(unsigned long timeoutMilliseconds
, ExecutionContext
*);
106 void mainThreadCancel(ExecutionContext
*);
107 void didSendData(unsigned long long bytesSent
, unsigned long long totalBytesToBeSent
) override
;
108 void didReceiveResponse(unsigned long identifier
, const ResourceResponse
&, PassOwnPtr
<WebDataConsumerHandle
>) override
;
109 void didReceiveData(const char*, unsigned dataLength
) override
;
110 void didDownloadData(int dataLength
) override
;
111 void didReceiveCachedMetadata(const char*, int dataLength
) override
;
112 void didFinishLoading(unsigned long identifier
, double finishTime
) override
;
113 void didFail(const ResourceError
&) override
;
114 void didFailAccessControlCheck(const ResourceError
&) override
;
115 void didFailRedirectCheck() override
;
116 void didReceiveResourceTiming(const ResourceTimingInfo
&) override
;
118 // Only to be used on the main thread.
119 RefPtr
<ThreadableLoader
> m_mainThreadLoader
;
120 OwnPtr
<ThreadableLoaderClient
> m_clientBridge
;
122 // ThreadableLoaderClientWrapper is to be used on the worker context thread.
123 // The ref counting is done on either thread.
124 RefPtr
<ThreadableLoaderClientWrapper
> m_workerClientWrapper
;
126 // Used on the worker context thread.
127 RefPtr
<WorkerLoaderProxy
> m_loaderProxy
;
130 WorkerThreadableLoader(WorkerGlobalScope
&, PassRefPtr
<ThreadableLoaderClientWrapper
>, PassOwnPtr
<ThreadableLoaderClient
>, const ResourceRequest
&, const ThreadableLoaderOptions
&, const ResourceLoaderOptions
&);
132 void didReceiveResourceTiming(const ResourceTimingInfo
&) override
;
134 RefPtrWillBePersistent
<WorkerGlobalScope
> m_workerGlobalScope
;
135 RefPtr
<ThreadableLoaderClientWrapper
> m_workerClientWrapper
;
136 MainThreadBridge
& m_bridge
;
141 #endif // WorkerThreadableLoader_h