OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef WEBSOCKET_BLOB_RECEIVER_H_ |
| 6 #define WEBSOCKET_BLOB_RECEIVER_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 #include <iosfwd> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/files/file.h" |
| 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/weak_ptr.h" |
| 17 #include "content/common/content_export.h" |
| 18 |
| 19 namespace net { |
| 20 class FileStream; |
| 21 class DrainableIOBuffer; |
| 22 } |
| 23 |
| 24 namespace storage { |
| 25 class BlobDataHandle; |
| 26 class BlobStorageContext; |
| 27 class ShareableFileReference; |
| 28 } |
| 29 |
| 30 namespace content { |
| 31 |
| 32 // Reads data from a WebSocket and writes it to a Blob for later access by the |
| 33 // renderer. |
| 34 class CONTENT_EXPORT WebSocketBlobReceiver { |
| 35 public: |
| 36 // Interface to be implemented by the client of WebSocketBlobReceiver. None of |
| 37 // these methods will be called after the WebSocketBlobReceiver object has |
| 38 // been destroyed. |
| 39 class Client { |
| 40 public: |
| 41 Client() = default; |
| 42 virtual ~Client() = default; |
| 43 |
| 44 // Signals that a Blob has successfully been created. Will be called |
| 45 // asynchronously after WebSocketBlobReceiver::Finish() returns. The |
| 46 // owner of the WebSocketBlobReceiver object should delete it after this is |
| 47 // called. |blob_data_handle| must be copied to keep the Blob alive. |
| 48 virtual void BlobCreated( |
| 49 scoped_ptr<storage::BlobDataHandle> blob_data_handle, |
| 50 uint64_t size) = 0; |
| 51 |
| 52 // Signals asynchronously that Blob creation failed. Synchronous failures |
| 53 // are indicated by AppendData() or Finish() returning net error codes. |
| 54 // May be called any time after Start() returns. |
| 55 virtual void BlobFailed(int net_error_code) = 0; |
| 56 |
| 57 // Indicates that up to |quota| additional bytes of data may be passed to |
| 58 // OnNewData(). May be called any time between Start() and Finish(). |
| 59 virtual void AddFlowControlQuota(size_t quota) = 0; |
| 60 |
| 61 private: |
| 62 DISALLOW_COPY_AND_ASSIGN(Client); |
| 63 }; |
| 64 |
| 65 // |blob_storage_context| must out-live this object. |client| will be |
| 66 // destroyed when this object is. |
| 67 WebSocketBlobReceiver(scoped_ptr<Client> client, |
| 68 storage::BlobStorageContext* blob_storage_context); |
| 69 ~WebSocketBlobReceiver(); |
| 70 |
| 71 // Prepares to create the Blob. Calls AddFlowControlQuota() asychronously when |
| 72 // it is ready to accept data. Calls BlobFailed() asynchronously on error. |
| 73 void Start(); |
| 74 |
| 75 // Adds data to the Blob at the end. It is reasonable to call this for small |
| 76 // amounts of data: a new BlobDataItem is not created for each call. May call |
| 77 // AddFlowControlQuota() synchronously or asynchronously when it is ready for |
| 78 // more data. Returns a net::Error code on synchronous error, net::OK on |
| 79 // synchronous success, or net::ERR_IO_PENDING. |data| must not be larger than |
| 80 // the unused bytes passed to client_->AddFlowControlQuota() so far. |
| 81 int AppendData(const std::vector<char>& data); |
| 82 |
| 83 // Indicates that AppendData() will not be called again and that the Blob |
| 84 // should be finalised. Returns a net::Error code on synchronous failure. |
| 85 // BlobCreated() or BlobFailed() will be called asynchronously after this |
| 86 // method returns net::ERR_IO_PENDING. |
| 87 int Finish(); |
| 88 |
| 89 bool finish_called() const { return finish_called_; } |
| 90 |
| 91 private: |
| 92 class FileInfoHelper; |
| 93 |
| 94 enum class State { |
| 95 NONE = 0, |
| 96 CREATE_FILE, |
| 97 CREATE_FILE_COMPLETE, |
| 98 SEND_QUOTA, |
| 99 WRITE, |
| 100 WRITE_COMPLETE, |
| 101 GET_INFO, |
| 102 GET_INFO_COMPLETE, |
| 103 }; |
| 104 |
| 105 // Needed to make CHECK_EQ(), etc. work |
| 106 friend std::ostream& operator<<(std::ostream& os, State state); |
| 107 |
| 108 int DoLoop(int result); |
| 109 void DoLoopAsync(int result); // May destroy |this|. |
| 110 int DoCreateFile(); |
| 111 int DoCreateFileComplete(int result); |
| 112 int DoSendQuota(); |
| 113 int DoWrite(); |
| 114 int DoWriteComplete(int result); |
| 115 int DoGetInfo(); |
| 116 int DoGetInfoComplete(int result); |
| 117 |
| 118 void OnWriteComplete(int result); |
| 119 |
| 120 void DidCreateTemporaryFileStream( |
| 121 base::File::Error error_code, |
| 122 scoped_ptr<net::FileStream> file_stream, |
| 123 storage::ShareableFileReference* deletable_file); |
| 124 |
| 125 void PrepareWrite(const std::vector<char>& data); |
| 126 |
| 127 void DidGetInfo(bool result, const base::File::Info& info); |
| 128 |
| 129 void DoSendQuotaAsync(size_t pending_quota); |
| 130 |
| 131 const scoped_ptr<Client> client_; |
| 132 storage::BlobStorageContext* blob_storage_context_; |
| 133 scoped_ptr<net::FileStream> file_stream_; |
| 134 scoped_refptr<storage::ShareableFileReference> deletable_file_; |
| 135 const scoped_refptr<net::DrainableIOBuffer> io_buffer_; |
| 136 std::vector<char> pending_data_; |
| 137 size_t pending_quota_; |
| 138 State next_state_ = State::NONE; |
| 139 bool io_in_progress_ = false; |
| 140 bool finish_called_ = false; |
| 141 base::WeakPtrFactory<WebSocketBlobReceiver> weak_factory_; |
| 142 |
| 143 DISALLOW_COPY_AND_ASSIGN(WebSocketBlobReceiver); |
| 144 }; |
| 145 |
| 146 } // namespace content |
| 147 |
| 148 #endif |
OLD | NEW |