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 CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_BLOB_SENDER_H_ | |
6 #define CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_BLOB_SENDER_H_ | |
7 | |
8 #include <stdint.h> | |
9 #include <iosfwd> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/macros.h" | |
14 #include "base/memory/ref_counted.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "content/common/content_export.h" | |
17 #include "net/base/completion_callback.h" | |
18 #include "net/websockets/websocket_event_interface.h" | |
19 | |
20 namespace base { | |
21 class SingleThreadTaskRunner; | |
22 } | |
23 | |
24 namespace net { | |
25 class IOBuffer; | |
26 } | |
27 | |
28 namespace storage { | |
29 class BlobReader; | |
30 class BlobStorageContext; | |
31 class FileSystemContext; | |
32 } | |
33 | |
34 namespace content { | |
35 | |
36 class WebSocketHost; | |
37 | |
38 // Read the contents of a Blob and write it to a WebSocket. Single-use: a new | |
39 // object must be created each time a Blob is sent. Destroying the object | |
40 // cancels all pending operations. | |
41 class CONTENT_EXPORT WebSocketBlobSender final { | |
42 public: | |
43 // An abstraction of the WebSocketChannel this object will send frames to. | |
44 class Channel { | |
45 public: | |
46 using ChannelState = net::WebSocketEventInterface::ChannelState; | |
47 | |
48 Channel() {} | |
49 virtual ~Channel() {} | |
50 | |
51 // The currently available quota for sending. It must not decrease without | |
52 // SendFrame() being called. | |
53 virtual int GetSendQuota() const = 0; | |
dcheng
2016/01/26 22:09:32
Can this actually be negative? I'm guessing this i
Adam Rice
2016/01/27 01:52:57
I changed it to size_t, since that is the directio
| |
54 | |
55 // Send a binary frame. |fin| is true for the final frame of the message. | |
56 // |data| is the contents of the frame. data.size() must be less than | |
57 // GetSendQuota(). If this call returns CHANNEL_DELETED, WebSocketBlobSender | |
58 // will assume that it has been deleted and return without calling any | |
59 // callbacks or accessing any other member data. | |
60 virtual ChannelState SendFrame(bool fin, const std::vector<char>& data) = 0; | |
61 | |
62 private: | |
63 DISALLOW_COPY_AND_ASSIGN(Channel); | |
64 }; | |
65 | |
66 // |channel| will be destroyed when this object is. | |
67 explicit WebSocketBlobSender(scoped_ptr<Channel> channel); | |
68 ~WebSocketBlobSender(); | |
69 | |
70 // Checks that the blob identified by |uuid| exists, has the size | |
71 // |expected_size| and then starts sending it via |channel_|. Returns | |
72 // ERR_IO_PENDING to indicate that |callback| will be called later with the | |
73 // result. net::OK indicates synchronous success. Any other net error code | |
74 // indicates synchronous failure. This method may result in the destruction of | |
75 // the channel, in which case |*channel_state| will be set to CHANNEL_DELETED. | |
76 int Start(const std::string& uuid, | |
77 uint64_t expected_size, | |
78 storage::BlobStorageContext* context, | |
79 storage::FileSystemContext* file_system_context, | |
80 base::SingleThreadTaskRunner* file_task_runner, | |
81 net::WebSocketEventInterface::ChannelState* channel_state, | |
82 const net::CompletionCallback& callback); | |
83 | |
84 // Sends more data if the object was waiting for quota and the new value of | |
85 // GetSendQuota() is large enough. | |
86 void OnNewSendQuota(); | |
87 | |
88 uint64_t expected_size() const { return expected_size_; } | |
89 | |
90 // ActualSize() should only be called after completion: ie. Start() returned a | |
91 // value other than ERR_IO_PENDING or |callback_| has been called. | |
92 uint64_t ActualSize() const; | |
93 | |
94 private: | |
95 // State proceeds through READ_SIZE and READ_SIZE_COMPLETE, then | |
96 // loops WAIT_FOR_QUOTA -> WAIT_FOR_QUOTA_COMPLETE -> READ | |
97 // -> READ_COMPLETE -> WAIT_FOR_QUOTA until the Blob is completely | |
98 // sent. | |
99 enum class State { | |
100 NONE = 0, | |
101 READ_SIZE, | |
102 READ_SIZE_COMPLETE, | |
103 WAIT_FOR_QUOTA, | |
104 WAIT_FOR_QUOTA_COMPLETE, | |
105 READ, | |
106 READ_COMPLETE, | |
107 }; | |
108 | |
109 // This is needed to make DCHECK_EQ(), etc. compile. | |
110 friend std::ostream& operator<<(std::ostream& os, State state); | |
111 | |
112 void OnReadComplete(int rv); | |
113 void OnSizeCalculated(int rv); | |
114 // |channel_state| should point to CHANNEL_ALIVE when called. If it is | |
115 // CHANNEL_DELETED on return, the object has been deleted. | |
116 int DoLoop(int result, Channel::ChannelState* channel_state); | |
117 void DoLoopAsync(int result); | |
118 int DoReadSize(); | |
119 int DoReadSizeComplete(int result); | |
120 int DoWaitForQuota(); | |
121 int DoWaitForQuotaComplete(); | |
122 int DoRead(); | |
123 int DoReadComplete(int result, Channel::ChannelState* channel_state); | |
124 | |
125 State next_state_ = State::NONE; | |
126 uint64_t expected_size_ = 0; | |
127 uint64_t bytes_left_ = 0; | |
128 net::CompletionCallback callback_; | |
129 scoped_refptr<net::IOBuffer> buffer_; | |
130 scoped_ptr<storage::BlobReader> reader_; | |
131 const scoped_ptr<Channel> channel_; | |
132 | |
133 DISALLOW_COPY_AND_ASSIGN(WebSocketBlobSender); | |
134 }; | |
135 | |
136 } // namespace content | |
137 | |
138 #endif // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_BLOB_SENDER_H_ | |
OLD | NEW |