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 const storage::BlobDataHandle& blob_data_handle) = 0; |
| 50 |
| 51 // Signals asynchronously that Blob creation failed. Synchronous failures |
| 52 // are indicated by AppendData() or Finish() returning net error codes. |
| 53 // May be called any time after Start() returns. |
| 54 virtual void BlobFailed(int net_error_code) = 0; |
| 55 |
| 56 // Indicates that up to |quota| additional bytes of data may be passed to |
| 57 // OnNewData(). May be called any time between Start() and Finish(). |
| 58 virtual void AddFlowControlQuota(int64_t quota) = 0; |
| 59 |
| 60 private: |
| 61 DISALLOW_COPY_AND_ASSIGN(Client); |
| 62 }; |
| 63 |
| 64 // |blob_storage_context| must out-live this object. |client| will be |
| 65 // destroyed when this object is. |
| 66 WebSocketBlobReceiver(scoped_ptr<Client> client, |
| 67 storage::BlobStorageContext* blob_storage_context); |
| 68 ~WebSocketBlobReceiver(); |
| 69 |
| 70 // Prepares to create the Blob. Calls AddFlowControlQuota() asychronously when |
| 71 // it is ready to accept data. Calls BlobFailed() asynchronously on error. |
| 72 void Start(); |
| 73 |
| 74 // Adds data to the Blob at the end. It is reasonable to call this for small |
| 75 // amounts of data: a new BlobDataItem is not created for each call. May call |
| 76 // AddFlowControlQuota() synchronously or asynchronously when it is ready for |
| 77 // more data. Returns a net::Error code on synchronous error, net::OK on |
| 78 // synchronous success, or net::ERR_IO_PENDING. |data| must not be larger than |
| 79 // the unused bytes passed to client_->AddFlowControlQuota() so far. |
| 80 int AppendData(const std::vector<char>& data); |
| 81 |
| 82 // Indicates that AppendData() will not be called again and that the Blob |
| 83 // should be finalised. Returns a net::Error code on synchronous failure. |
| 84 // BlobCreated() or BlobFailed() will be called asynchronously after this |
| 85 // method returns net::ERR_IO_PENDING. |
| 86 int Finish(); |
| 87 |
| 88 private: |
| 89 class FileInfoHelper; |
| 90 |
| 91 enum class State { |
| 92 NONE = 0, |
| 93 CREATE_FILE, |
| 94 CREATE_FILE_COMPLETE, |
| 95 SEND_QUOTA, |
| 96 WRITE, |
| 97 WRITE_COMPLETE, |
| 98 GET_INFO, |
| 99 GET_INFO_COMPLETE, |
| 100 }; |
| 101 |
| 102 // Needed to make CHECK_EQ(), etc. work |
| 103 friend std::ostream& operator<<(std::ostream& os, State state); |
| 104 |
| 105 int DoLoop(int result); |
| 106 void DoLoopAsync(int result); |
| 107 int DoCreateFile(); |
| 108 int DoCreateFileComplete(int result); |
| 109 int DoSendQuota(); |
| 110 int DoWrite(); |
| 111 int DoWriteComplete(int result); |
| 112 int DoGetInfo(); |
| 113 int DoGetInfoComplete(int result); |
| 114 |
| 115 void OnWriteComplete(int result); |
| 116 |
| 117 void DidCreateTemporaryFileStream( |
| 118 base::File::Error error_code, |
| 119 scoped_ptr<net::FileStream> file_stream, |
| 120 storage::ShareableFileReference* deletable_file); |
| 121 |
| 122 void PrepareWrite(const std::vector<char>& data); |
| 123 |
| 124 void DidGetInfo(bool result, const base::File::Info& info); |
| 125 |
| 126 const scoped_ptr<Client> client_; |
| 127 storage::BlobStorageContext* blob_storage_context_; |
| 128 scoped_ptr<net::FileStream> file_stream_; |
| 129 scoped_refptr<storage::ShareableFileReference> deletable_file_; |
| 130 const scoped_refptr<net::DrainableIOBuffer> io_buffer_; |
| 131 std::vector<char> pending_data_; |
| 132 int64_t pending_quota_; |
| 133 State next_state_ = State::NONE; |
| 134 bool io_in_progress_ = false; |
| 135 bool finish_called_ = false; |
| 136 base::WeakPtrFactory<WebSocketBlobReceiver> weak_factory_; |
| 137 |
| 138 DISALLOW_COPY_AND_ASSIGN(WebSocketBlobReceiver); |
| 139 }; |
| 140 |
| 141 } // namespace content |
| 142 |
| 143 #endif |
OLD | NEW |