Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: content/browser/renderer_host/websocket_blob_sender.h

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

Powered by Google App Engine
This is Rietveld 408576698