OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef BodyStreamBuffer_h | 5 #ifndef BodyStreamBuffer_h |
6 #define BodyStreamBuffer_h | 6 #define BodyStreamBuffer_h |
7 | 7 |
8 #include "core/dom/DOMException.h" | 8 #include "core/dom/DOMException.h" |
9 #include "modules/ModulesExport.h" | 9 #include "modules/ModulesExport.h" |
10 #include "modules/fetch/DataConsumerHandleUtil.h" | |
11 #include "modules/fetch/FetchDataConsumerHandle.h" | |
12 #include "modules/fetch/FetchDataLoader.h" | |
10 #include "platform/blob/BlobData.h" | 13 #include "platform/blob/BlobData.h" |
11 #include "platform/heap/Heap.h" | 14 #include "platform/heap/Heap.h" |
12 #include "public/platform/WebDataConsumerHandle.h" | 15 #include "public/platform/WebDataConsumerHandle.h" |
13 #include "wtf/Deque.h" | 16 #include "wtf/Deque.h" |
14 #include "wtf/RefPtr.h" | 17 #include "wtf/RefPtr.h" |
15 #include "wtf/text/WTFString.h" | 18 #include "wtf/text/WTFString.h" |
16 | 19 |
17 namespace blink { | 20 namespace blink { |
18 | 21 |
19 class DOMArrayBuffer; | 22 class DrainingBodyStreamBuffer; |
20 | 23 |
21 class MODULES_EXPORT BodyStreamBuffer final : public GarbageCollectedFinalized<B odyStreamBuffer> { | 24 class MODULES_EXPORT BodyStreamBuffer final : public GarbageCollectedFinalized<B odyStreamBuffer> { |
22 public: | 25 public: |
23 class Observer : public GarbageCollectedFinalized<Observer> { | 26 static BodyStreamBuffer* create(PassOwnPtr<FetchDataConsumerHandle> handle) { return new BodyStreamBuffer(handle); } |
27 static BodyStreamBuffer* createEmpty(); | |
28 | |
29 FetchDataConsumerHandle* handle() const; | |
30 PassOwnPtr<FetchDataConsumerHandle> releaseHandle(); | |
31 | |
32 class DrainingStreamNotificationClient : public GarbageCollectedMixin { | |
24 public: | 33 public: |
25 virtual ~Observer() { } | 34 virtual ~DrainingStreamNotificationClient() { } |
26 virtual void onWrite() = 0; | 35 // Called after FetchDataLoader::Client methods. |
27 virtual void onClose() = 0; | 36 virtual void didFetchDataLoadFinishedFromDrainingStream() = 0; |
28 virtual void onError() = 0; | |
29 DEFINE_INLINE_VIRTUAL_TRACE() { } | |
30 }; | 37 }; |
31 | 38 |
32 class Canceller : public GarbageCollected<Canceller> { | 39 DEFINE_INLINE_TRACE() |
33 public: | 40 { |
34 virtual void cancel() = 0; | 41 visitor->trace(m_fetchDataLoader); |
35 DEFINE_INLINE_VIRTUAL_TRACE() { } | 42 visitor->trace(m_drainingStreamNotificationClient); |
36 }; | 43 } |
37 | 44 |
38 class BlobHandleCreatorClient : public GarbageCollectedFinalized<BlobHandleC reatorClient> { | 45 void didFetchDataLoadFinished(); |
39 public: | |
40 virtual ~BlobHandleCreatorClient() { } | |
41 virtual void didCreateBlobHandle(PassRefPtr<BlobDataHandle>) = 0; | |
42 virtual void didFail(DOMException*) = 0; | |
43 DEFINE_INLINE_VIRTUAL_TRACE() { } | |
44 }; | |
45 explicit BodyStreamBuffer(Canceller*); | |
46 ~BodyStreamBuffer() { } | |
47 | |
48 PassRefPtr<DOMArrayBuffer> read(); | |
49 bool isClosed() const { return m_isClosed; } | |
50 bool hasError() const { return m_exception; } | |
51 DOMException* exception() const { return m_exception; } | |
52 | |
53 // Can't call after close() or error() was called. | |
54 void write(PassRefPtr<DOMArrayBuffer>); | |
55 // Can't call after close() or error() was called. | |
56 void close(); | |
57 // Can't call after close() or error() was called. | |
58 void error(DOMException*); | |
59 void cancel() { m_canceller->cancel(); } | |
60 | |
61 // This function registers an observer so it fails and returns false when an | |
62 // observer was already registered. | |
63 bool readAllAndCreateBlobHandle(const String& contentType, BlobHandleCreator Client*); | |
64 | |
65 // This function registers an observer so it fails and returns false when an | |
66 // observer was already registered. | |
67 bool startTee(BodyStreamBuffer* out1, BodyStreamBuffer* out2); | |
68 | |
69 // When an observer was registered this function fails and returns false. | |
70 bool registerObserver(Observer*); | |
71 void unregisterObserver(); | |
72 bool isObserverRegistered() const { return m_observer.get(); } | |
73 DECLARE_TRACE(); | |
74 | |
75 // Creates a BodyStreamBuffer from |handle| as the source. | |
76 // On failure, BodyStreamBuffer::error() is called with a NetworkError | |
77 // with |failureMessage|. | |
78 static BodyStreamBuffer* create(PassOwnPtr<WebDataConsumerHandle> /* handle */, const String& failureMessage); | |
79 | 46 |
80 private: | 47 private: |
81 Deque<RefPtr<DOMArrayBuffer>> m_queue; | 48 explicit BodyStreamBuffer(PassOwnPtr<FetchDataConsumerHandle> handle) : m_ha ndle(handle) { } |
82 bool m_isClosed; | 49 |
83 Member<DOMException> m_exception; | 50 void setDrainingStreamNotificationClient(DrainingStreamNotificationClient*); |
84 Member<Observer> m_observer; | 51 |
85 Member<Canceller> m_canceller; | 52 void startLoading(FetchDataLoader*, FetchDataLoader::Client*); |
53 // Call DrainingStreamNotificationClient. | |
54 void doDrainingStreamNotification(); | |
55 // Clear DrainingStreamNotificationClient without calling. | |
56 void clearDrainingStreamNotification(); | |
57 | |
58 friend class DrainingBodyStreamBuffer; | |
59 | |
60 OwnPtr<FetchDataConsumerHandle> m_handle; | |
61 Member<FetchDataLoader> m_fetchDataLoader; | |
62 Member<DrainingStreamNotificationClient> m_drainingStreamNotificationClient; | |
63 }; | |
64 | |
65 // DrainingBodyStreamBuffer wraps BodyStreamBuffer returned from | |
66 // Body::createDrainingStream() and calls DrainingStreamNotificationClient | |
67 // callbacks unless leakBuffer() is called: | |
68 // - If startLoading() is called, the callback is called after loading finished. | |
69 // - If drainAsBlobDataHandle() is called, the callback is called immediately. | |
70 // - If leakBuffer() is called, the callback is no longer called. | |
71 // Any calls to DrainingBodyStreamBuffer methods after a call to either of | |
72 // methods above is no-op. | |
73 // After calling one of the methods above, we don't have to keep | |
74 // DrainingBodyStreamBuffer alive. | |
75 // If DrainingBodyStreamBuffer is destructed before any of above is called, | |
76 // the callback is called at destruction. | |
77 class MODULES_EXPORT DrainingBodyStreamBuffer final { | |
78 public: | |
79 static PassOwnPtr<DrainingBodyStreamBuffer> create(BodyStreamBuffer* buffer, BodyStreamBuffer::DrainingStreamNotificationClient* client) | |
80 { | |
81 return adoptPtr(new DrainingBodyStreamBuffer(buffer, client)); | |
82 } | |
83 ~DrainingBodyStreamBuffer(); | |
84 void startLoading(FetchDataLoader*, FetchDataLoader::Client*); | |
85 BodyStreamBuffer* leakBuffer(); | |
horo
2015/07/07 06:34:46
nit: I prefer releaseBuffer.
hiroshige
2015/07/07 06:50:53
I prefer leakBuffer() because this function omits
| |
86 PassRefPtr<BlobDataHandle> drainAsBlobDataHandle(FetchDataConsumerHandle::Re ader::BlobSizePolicy); | |
87 | |
88 private: | |
89 DrainingBodyStreamBuffer(BodyStreamBuffer*, BodyStreamBuffer::DrainingStream NotificationClient*); | |
90 | |
91 Persistent<BodyStreamBuffer> m_buffer; | |
86 }; | 92 }; |
87 | 93 |
88 } // namespace blink | 94 } // namespace blink |
89 | 95 |
90 #endif // BodyStreamBuffer_h | 96 #endif // BodyStreamBuffer_h |
OLD | NEW |