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

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

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

Powered by Google App Engine
This is Rietveld 408576698