| 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 "modules/fetch/DataConsumerHandleUtil.h" | 5 #include "modules/fetch/DataConsumerHandleUtil.h" |
| 6 | 6 |
| 7 #include "platform/blob/BlobData.h" | |
| 8 #include "public/platform/Platform.h" | |
| 9 #include "public/platform/WebTaskRunner.h" | |
| 10 #include "public/platform/WebThread.h" | |
| 11 #include "public/platform/WebTraceLocation.h" | |
| 12 #include "wtf/Functional.h" | |
| 13 #include "wtf/PtrUtil.h" | 7 #include "wtf/PtrUtil.h" |
| 14 #include <memory> | 8 #include <memory> |
| 15 | 9 |
| 16 namespace blink { | 10 namespace blink { |
| 17 | 11 |
| 18 using Result = WebDataConsumerHandle::Result; | 12 using Result = WebDataConsumerHandle::Result; |
| 19 | 13 |
| 20 namespace { | 14 namespace { |
| 21 | 15 |
| 22 class WaitingHandle final : public WebDataConsumerHandle { | 16 class WaitingHandle final : public WebDataConsumerHandle { |
| 23 private: | 17 private: |
| 24 class ReaderImpl final : public WebDataConsumerHandle::Reader { | 18 class ReaderImpl final : public WebDataConsumerHandle::Reader { |
| 25 public: | 19 public: |
| 26 Result beginRead(const void** buffer, | 20 Result beginRead(const void** buffer, |
| 27 WebDataConsumerHandle::Flags, | 21 WebDataConsumerHandle::Flags, |
| 28 size_t* available) override { | 22 size_t* available) override { |
| 29 *available = 0; | 23 *available = 0; |
| 30 *buffer = nullptr; | 24 *buffer = nullptr; |
| 31 return ShouldWait; | 25 return ShouldWait; |
| 32 } | 26 } |
| 33 Result endRead(size_t) override { return UnexpectedError; } | 27 Result endRead(size_t) override { return UnexpectedError; } |
| 34 }; | 28 }; |
| 35 std::unique_ptr<Reader> obtainReader(Client*) override { | 29 std::unique_ptr<Reader> obtainReader(Client*) override { |
| 36 return WTF::wrapUnique(new ReaderImpl); | 30 return WTF::wrapUnique(new ReaderImpl); |
| 37 } | 31 } |
| 38 | 32 |
| 39 const char* debugName() const override { return "WaitingHandle"; } | 33 const char* debugName() const override { return "WaitingHandle"; } |
| 40 }; | 34 }; |
| 41 | 35 |
| 42 class RepeatingReader final : public WebDataConsumerHandle::Reader { | |
| 43 public: | |
| 44 explicit RepeatingReader(Result result, WebDataConsumerHandle::Client* client) | |
| 45 : m_result(result), m_notifier(client) {} | |
| 46 | |
| 47 private: | |
| 48 Result beginRead(const void** buffer, | |
| 49 WebDataConsumerHandle::Flags, | |
| 50 size_t* available) override { | |
| 51 *available = 0; | |
| 52 *buffer = nullptr; | |
| 53 return m_result; | |
| 54 } | |
| 55 Result endRead(size_t) override { | |
| 56 return WebDataConsumerHandle::UnexpectedError; | |
| 57 } | |
| 58 | |
| 59 Result m_result; | |
| 60 NotifyOnReaderCreationHelper m_notifier; | |
| 61 }; | |
| 62 | |
| 63 class DoneHandle final : public WebDataConsumerHandle { | |
| 64 private: | |
| 65 std::unique_ptr<Reader> obtainReader(Client* client) override { | |
| 66 return WTF::wrapUnique(new RepeatingReader(Done, client)); | |
| 67 } | |
| 68 const char* debugName() const override { return "DoneHandle"; } | |
| 69 }; | |
| 70 | |
| 71 class UnexpectedErrorHandle final : public WebDataConsumerHandle { | |
| 72 private: | |
| 73 std::unique_ptr<Reader> obtainReader(Client* client) override { | |
| 74 return WTF::wrapUnique(new RepeatingReader(UnexpectedError, client)); | |
| 75 } | |
| 76 const char* debugName() const override { return "UnexpectedErrorHandle"; } | |
| 77 }; | |
| 78 | |
| 79 class WebToFetchDataConsumerHandleAdapter : public FetchDataConsumerHandle { | 36 class WebToFetchDataConsumerHandleAdapter : public FetchDataConsumerHandle { |
| 80 public: | 37 public: |
| 81 WebToFetchDataConsumerHandleAdapter( | 38 WebToFetchDataConsumerHandleAdapter( |
| 82 std::unique_ptr<WebDataConsumerHandle> handle) | 39 std::unique_ptr<WebDataConsumerHandle> handle) |
| 83 : m_handle(std::move(handle)) {} | 40 : m_handle(std::move(handle)) {} |
| 84 | 41 |
| 85 private: | 42 private: |
| 86 class ReaderImpl final : public FetchDataConsumerHandle::Reader { | 43 class ReaderImpl final : public FetchDataConsumerHandle::Reader { |
| 87 public: | 44 public: |
| 88 ReaderImpl(std::unique_ptr<WebDataConsumerHandle::Reader> reader) | 45 ReaderImpl(std::unique_ptr<WebDataConsumerHandle::Reader> reader) |
| (...skipping 26 matching lines...) Expand all Loading... |
| 115 | 72 |
| 116 std::unique_ptr<WebDataConsumerHandle> m_handle; | 73 std::unique_ptr<WebDataConsumerHandle> m_handle; |
| 117 }; | 74 }; |
| 118 | 75 |
| 119 } // namespace | 76 } // namespace |
| 120 | 77 |
| 121 std::unique_ptr<WebDataConsumerHandle> createWaitingDataConsumerHandle() { | 78 std::unique_ptr<WebDataConsumerHandle> createWaitingDataConsumerHandle() { |
| 122 return wrapUnique(new WaitingHandle); | 79 return wrapUnique(new WaitingHandle); |
| 123 } | 80 } |
| 124 | 81 |
| 125 std::unique_ptr<WebDataConsumerHandle> createDoneDataConsumerHandle() { | |
| 126 return wrapUnique(new DoneHandle); | |
| 127 } | |
| 128 | |
| 129 std::unique_ptr<WebDataConsumerHandle> | |
| 130 createUnexpectedErrorDataConsumerHandle() { | |
| 131 return wrapUnique(new UnexpectedErrorHandle); | |
| 132 } | |
| 133 | |
| 134 std::unique_ptr<FetchDataConsumerHandle> | 82 std::unique_ptr<FetchDataConsumerHandle> |
| 135 createFetchDataConsumerHandleFromWebHandle( | 83 createFetchDataConsumerHandleFromWebHandle( |
| 136 std::unique_ptr<WebDataConsumerHandle> handle) { | 84 std::unique_ptr<WebDataConsumerHandle> handle) { |
| 137 return wrapUnique(new WebToFetchDataConsumerHandleAdapter(std::move(handle))); | 85 return wrapUnique(new WebToFetchDataConsumerHandleAdapter(std::move(handle))); |
| 138 } | 86 } |
| 139 | 87 |
| 140 NotifyOnReaderCreationHelper::NotifyOnReaderCreationHelper( | |
| 141 WebDataConsumerHandle::Client* client) | |
| 142 : m_factory(this) { | |
| 143 if (!client) | |
| 144 return; | |
| 145 // Note we don't need thread safety here because this object is | |
| 146 // bound to the current thread. | |
| 147 Platform::current()->currentThread()->getWebTaskRunner()->postTask( | |
| 148 BLINK_FROM_HERE, | |
| 149 WTF::bind(&NotifyOnReaderCreationHelper::notify, | |
| 150 m_factory.createWeakPtr(), WTF::unretained(client))); | |
| 151 } | |
| 152 | |
| 153 void NotifyOnReaderCreationHelper::notify( | |
| 154 WebDataConsumerHandle::Client* client) { | |
| 155 // |client| dereference is safe here because: | |
| 156 // - |this| is owned by a reader, | |
| 157 // so the reader outlives |this|, and | |
| 158 // - |client| is the client of the reader, so |client| outlives the reader | |
| 159 // (guaranteed by the user of the reader), | |
| 160 // and therefore |client| outlives |this|. | |
| 161 client->didGetReadable(); | |
| 162 } | |
| 163 | |
| 164 } // namespace blink | 88 } // namespace blink |
| OLD | NEW |