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

Side by Side Diff: mojo/services/network/public/cpp/web_socket_write_queue.cc

Issue 1873463003: Remove mojo network service. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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 2014 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 #include "network/public/cpp/web_socket_write_queue.h"
6
7 #include <stdint.h>
8
9 #include "base/bind.h"
10 #include "base/logging.h"
11
12 namespace mojo {
13
14 struct WebSocketWriteQueue::Operation {
15 uint32_t num_bytes_;
16 base::Callback<void(const char*)> callback_;
17
18 const char* data_;
19 // Only initialized if the initial Write fails. This saves a copy in
20 // the common case.
21 std::vector<char> data_copy_;
22 };
23
24 WebSocketWriteQueue::WebSocketWriteQueue(DataPipeProducerHandle handle)
25 : handle_(handle), is_busy_(false), weak_factory_(this) {
26 }
27
28 WebSocketWriteQueue::~WebSocketWriteQueue() {
29 }
30
31 void WebSocketWriteQueue::Write(const char* data,
32 uint32_t num_bytes,
33 base::Callback<void(const char*)> callback) {
34 Operation* op = new Operation;
35 op->num_bytes_ = num_bytes;
36 op->callback_ = callback;
37 op->data_ = data;
38 queue_.push_back(op);
39
40 if (!is_busy_) {
41 is_busy_ = true;
42 // This call may reset |is_busy_| to false.
43 TryToWrite();
44 }
45
46 if (is_busy_) {
47 // If we have to wait, make a local copy of the data so we know it will
48 // live until we need it.
49 op->data_copy_.resize(num_bytes);
50 memcpy(&op->data_copy_[0], data, num_bytes);
51 op->data_ = &op->data_copy_[0];
52 }
53 }
54
55 void WebSocketWriteQueue::TryToWrite() {
56 DCHECK(is_busy_);
57 DCHECK(!queue_.empty());
58 do {
59 Operation* op = queue_[0];
60 uint32_t bytes_written = op->num_bytes_;
61 MojoResult result = WriteDataRaw(
62 handle_, op->data_, &bytes_written, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE);
63 if (result == MOJO_RESULT_SHOULD_WAIT) {
64 Wait();
65 return;
66 }
67
68 // Ensure |op| is deleted, whether or not |this| goes away.
69 scoped_ptr<Operation> op_deleter(op);
70 queue_.weak_erase(queue_.begin());
71
72 // http://crbug.com/490193 This should run callback as well. May need to
73 // change the callback signature.
74 if (result != MOJO_RESULT_OK)
75 return;
76
77 base::WeakPtr<WebSocketWriteQueue> self(weak_factory_.GetWeakPtr());
78
79 // This call may delete |this|. In that case, |self| will be invalidated.
80 // It may re-enter Write() too. Because |is_busy_| is true during the whole
81 // process, TryToWrite() won't be re-entered.
82 op->callback_.Run(op->data_);
83
84 if (!self)
85 return;
86 } while (!queue_.empty());
87 is_busy_ = false;
88 }
89
90 void WebSocketWriteQueue::Wait() {
91 DCHECK(is_busy_);
92 handle_watcher_.Start(handle_,
93 MOJO_HANDLE_SIGNAL_WRITABLE,
94 MOJO_DEADLINE_INDEFINITE,
95 base::Bind(&WebSocketWriteQueue::OnHandleReady,
96 base::Unretained(this)));
97 }
98
99 void WebSocketWriteQueue::OnHandleReady(MojoResult result) {
100 DCHECK(is_busy_);
101 TryToWrite();
102 }
103
104 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/services/network/public/cpp/web_socket_write_queue.h ('k') | mojo/services/network/public/interfaces/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698