OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "modules/fetch/FetchBlobDataConsumerHandle.h" | 6 #include "modules/fetch/FetchBlobDataConsumerHandle.h" |
7 | 7 |
8 #include "core/dom/ActiveDOMObject.h" | |
9 #include "core/dom/CrossThreadTask.h" | |
10 #include "core/dom/ExecutionContext.h" | 8 #include "core/dom/ExecutionContext.h" |
11 #include "core/fetch/FetchInitiatorTypeNames.h" | 9 #include "core/fetch/FetchInitiatorTypeNames.h" |
12 #include "core/loader/ThreadableLoaderClient.h" | 10 #include "core/loader/ThreadableLoaderClient.h" |
13 #include "modules/fetch/CompositeDataConsumerHandle.h" | 11 #include "modules/fetch/CompositeDataConsumerHandle.h" |
| 12 #include "modules/fetch/CrossThreadHolder.h" |
14 #include "modules/fetch/DataConsumerHandleUtil.h" | 13 #include "modules/fetch/DataConsumerHandleUtil.h" |
15 #include "platform/Task.h" | |
16 #include "platform/blob/BlobRegistry.h" | 14 #include "platform/blob/BlobRegistry.h" |
17 #include "platform/blob/BlobURL.h" | 15 #include "platform/blob/BlobURL.h" |
18 #include "platform/network/ResourceRequest.h" | 16 #include "platform/network/ResourceRequest.h" |
19 #include "public/platform/Platform.h" | |
20 #include "public/platform/WebTraceLocation.h" | |
21 #include "wtf/Locker.h" | |
22 #include "wtf/ThreadingPrimitives.h" | |
23 | 17 |
24 namespace blink { | 18 namespace blink { |
25 | 19 |
26 using Result = FetchBlobDataConsumerHandle::Result; | 20 using Result = FetchBlobDataConsumerHandle::Result; |
27 | 21 |
28 namespace { | 22 namespace { |
29 | 23 |
30 // CrossThreadHolder<T> provides cross-thread access to |obj| of class T | |
31 // bound to the thread of |executionContext| where |obj| is created. | |
32 // - CrossThreadHolder<T> can be passed across threads. | |
33 // - |obj|'s methods are called on the thread of |executionContext| | |
34 // via CrossThreadHolder<T>::postTask(). | |
35 // - |obj| is destructed on the thread of |executionContext| | |
36 // when |executionContext| is stopped or | |
37 // CrossThreadHolder is destructed (earlier of them). | |
38 // Note: |obj|'s destruction can be slightly after CrossThreadHolder. | |
39 template<typename T> | |
40 class CrossThreadHolder { | |
41 public: | |
42 // Must be called on the thread where |obj| is created | |
43 // (== the thread of |executionContext|). | |
44 // The current thread must be attached to Oilpan. | |
45 static PassOwnPtr<CrossThreadHolder<T>> create(ExecutionContext* executionCo
ntext, PassOwnPtr<T> obj) | |
46 { | |
47 ASSERT(executionContext->isContextThread()); | |
48 return adoptPtr(new CrossThreadHolder(executionContext, obj)); | |
49 } | |
50 | |
51 // Can be called from any thread. | |
52 // Executes |task| with |obj| and |executionContext| on the thread of | |
53 // |executionContext|. | |
54 // NOTE: |task| might be silently ignored (i.e. not executed) and | |
55 // destructed (possibly on the calling thread or on the thread of | |
56 // |executionContext|) when |executionContext| is stopped or | |
57 // CrossThreadHolder is destructed. | |
58 void postTask(PassOwnPtr<WTF::Function<void(T*, ExecutionContext*)>> task) | |
59 { | |
60 MutexLocker locker(m_mutex->mutex()); | |
61 if (!m_bridge) { | |
62 // The bridge has already disappeared. | |
63 return; | |
64 } | |
65 m_bridge->executionContext()->postTask(FROM_HERE, createCrossThreadTask(
&Bridge::runTask, m_bridge.get(), task)); | |
66 } | |
67 | |
68 ~CrossThreadHolder() | |
69 { | |
70 MutexLocker locker(m_mutex->mutex()); | |
71 clearInternal(); | |
72 } | |
73 | |
74 private: | |
75 // Object graph: | |
76 // +------+ +-----------------+ | |
77 // T <-OwnPtr- |Bridge| ---------RawPtr--------> |CrossThreadHolder| | |
78 // | | <-CrossThreadPersistent- | | | |
79 // +------+ +-----------------+ | |
80 // | | | |
81 // +--RefPtr--> MutexWrapper <--RefPtr--+ | |
82 // The CrossThreadPersistent/RawPtr between CrossThreadHolder and Bridge | |
83 // are protected by MutexWrapper | |
84 // and cleared when CrossThreadHolder::clearInternal() is called, i.e.: | |
85 // [1] when |executionContext| is stopped, or | |
86 // [2] when CrossThreadHolder is destructed. | |
87 // Then Bridge is shortly garbage collected and T is destructed. | |
88 | |
89 class MutexWrapper : public ThreadSafeRefCounted<MutexWrapper> { | |
90 public: | |
91 static PassRefPtr<MutexWrapper> create() { return adoptRef(new MutexWrap
per()); } | |
92 Mutex& mutex() { return m_mutex; } | |
93 private: | |
94 MutexWrapper() = default; | |
95 Mutex m_mutex; | |
96 }; | |
97 | |
98 // All methods except for clearInternal() | |
99 // must be called on |executionContext|'s thread. | |
100 class Bridge | |
101 : public GarbageCollectedFinalized<Bridge> | |
102 , public ActiveDOMObject { | |
103 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(Bridge); | |
104 public: | |
105 Bridge(ExecutionContext* executionContext, PassOwnPtr<T> obj, PassRefPtr
<MutexWrapper> mutex, CrossThreadHolder* holder) | |
106 : ActiveDOMObject(executionContext) | |
107 , m_obj(obj) | |
108 , m_mutex(mutex) | |
109 , m_holder(holder) | |
110 { | |
111 suspendIfNeeded(); | |
112 } | |
113 | |
114 DEFINE_INLINE_TRACE() | |
115 { | |
116 ActiveDOMObject::trace(visitor); | |
117 } | |
118 | |
119 T* getObject() const { return m_obj.get(); } | |
120 | |
121 // Must be protected by |m_mutex|. | |
122 // Is called from CrossThreadHolder::clearInternal() and | |
123 // can be called on any thread. | |
124 void clearInternal() | |
125 { | |
126 // We don't clear |m_obj| here to destruct |m_obj| on the thread | |
127 // of |executionContext|. | |
128 m_holder = nullptr; | |
129 } | |
130 | |
131 void runTask(PassOwnPtr<WTF::Function<void(T*, ExecutionContext*)>> task
) | |
132 { | |
133 ASSERT(executionContext()->isContextThread()); | |
134 if (m_obj) | |
135 (*task)(m_obj.get(), executionContext()); | |
136 } | |
137 | |
138 private: | |
139 // ActiveDOMObject | |
140 void stop() override | |
141 { | |
142 ASSERT(executionContext()->isContextThread()); | |
143 { | |
144 MutexLocker locker(m_mutex->mutex()); | |
145 if (m_holder) | |
146 m_holder->clearInternal(); | |
147 ASSERT(!m_holder); | |
148 } | |
149 | |
150 // We have to destruct |*m_obj| here because destructing |*m_obj| | |
151 // in ~Bridge() might be too late when |executionContext| is | |
152 // stopped. | |
153 m_obj.clear(); | |
154 } | |
155 | |
156 | |
157 OwnPtr<T> m_obj; | |
158 // All accesses to |m_holder| must be protected by |m_mutex|. | |
159 RefPtr<MutexWrapper> m_mutex; | |
160 CrossThreadHolder* m_holder; | |
161 }; | |
162 | |
163 // Must be protected by |m_mutex|. | |
164 void clearInternal() | |
165 { | |
166 if (m_bridge) | |
167 m_bridge->clearInternal(); | |
168 m_bridge.clear(); | |
169 } | |
170 | |
171 CrossThreadHolder(ExecutionContext* executionContext, PassOwnPtr<T> obj) | |
172 : m_mutex(MutexWrapper::create()) | |
173 , m_bridge(new Bridge(executionContext, obj, m_mutex, this)) | |
174 { | |
175 } | |
176 | |
177 RefPtr<MutexWrapper> m_mutex; | |
178 // |m_bridge| is protected by |m_mutex|. | |
179 // |m_bridge| is cleared before the thread that allocated |*m_bridge| | |
180 // is stopped. | |
181 CrossThreadPersistent<Bridge> m_bridge; | |
182 }; | |
183 | |
184 // Object graph: | 24 // Object graph: |
185 // +-------------+ | 25 // +-------------+ |
186 // |ReaderContext| | 26 // |ReaderContext| |
187 // +-------------+ +-----------+ +---+ | | | 27 // +-------------+ +-----------+ +---+ | | |
188 // |LoaderContext|<-|CTH::Bridge|<->|CTH|<-| | | 28 // |LoaderContext|<-|CTH::Bridge|<->|CTH|<-| | |
189 // +-------------+ +-----------+ +---+ +-------------+ | 29 // +-------------+ +-----------+ +---+ +-------------+ |
190 // | | 30 // | |
191 // +--> ThreadableLoader | 31 // +--> ThreadableLoader |
192 // | 32 // |
193 // When the loader thread is stopped, CrossThreadHolder::Bridge and | 33 // When the loader thread is stopped, CrossThreadHolder::Bridge and |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 | 300 |
461 return adoptPtr(new FetchBlobDataConsumerHandle(executionContext, blobDataHa
ndle, new DefaultLoaderFactory)); | 301 return adoptPtr(new FetchBlobDataConsumerHandle(executionContext, blobDataHa
ndle, new DefaultLoaderFactory)); |
462 } | 302 } |
463 | 303 |
464 FetchDataConsumerHandle::Reader* FetchBlobDataConsumerHandle::obtainReaderIntern
al(Client* client) | 304 FetchDataConsumerHandle::Reader* FetchBlobDataConsumerHandle::obtainReaderIntern
al(Client* client) |
465 { | 305 { |
466 return m_readerContext->obtainReader(client).leakPtr(); | 306 return m_readerContext->obtainReader(client).leakPtr(); |
467 } | 307 } |
468 | 308 |
469 } // namespace blink | 309 } // namespace blink |
OLD | NEW |