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

Side by Side Diff: content/child/web_data_consumer_handle_impl_unittest.cc

Issue 1164493008: Implement WebDataConsumerHandle::Reader. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 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
« no previous file with comments | « content/child/web_data_consumer_handle_impl.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/child/web_data_consumer_handle_impl.h" 5 #include "content/child/web_data_consumer_handle_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/location.h" 10 #include "base/location.h"
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 main_message_loop_(main_message_loop), 62 main_message_loop_(main_message_loop),
63 on_done_(on_done) {} 63 on_done_(on_done) {}
64 64
65 const std::string& result() const { return result_; } 65 const std::string& result() const { return result_; }
66 66
67 void ReadMore() override { ReadData(); } 67 void ReadMore() override { ReadData(); }
68 68
69 void ReadData() { 69 void ReadData() {
70 if (!client_) { 70 if (!client_) {
71 client_.reset(new ClientImpl(this)); 71 client_.reset(new ClientImpl(this));
72 handle_->registerClient(client_.get()); 72 reader_ = handle_->ObtainReader(client_.get());
73 } 73 }
74 74
75 Result rv = kOk; 75 Result rv = kOk;
76 size_t readSize = 0; 76 size_t readSize = 0;
77 77
78 while (true) { 78 while (true) {
79 char buffer[16]; 79 char buffer[16];
80 rv = handle_->read(&buffer, sizeof(buffer), kNone, &readSize); 80 rv = reader_->read(&buffer, sizeof(buffer), kNone, &readSize);
81 if (rv != kOk) 81 if (rv != kOk)
82 break; 82 break;
83 result_.insert(result_.size(), &buffer[0], readSize); 83 result_.insert(result_.size(), &buffer[0], readSize);
84 } 84 }
85 85
86 if (rv == kShouldWait) { 86 if (rv == kShouldWait) {
87 // Wait a while... 87 // Wait a while...
88 return; 88 return;
89 } 89 }
90 90
91 if (rv != kDone) { 91 if (rv != kDone) {
92 // Something is wrong. 92 // Something is wrong.
93 result_ = "error"; 93 result_ = "error";
94 } 94 }
95 95
96 // The operation is done. 96 // The operation is done.
97 reader_.reset();
97 main_message_loop_->task_runner()->PostTask(FROM_HERE, on_done_); 98 main_message_loop_->task_runner()->PostTask(FROM_HERE, on_done_);
98 } 99 }
99 100
100 private: 101 private:
101 scoped_ptr<WebDataConsumerHandle> handle_; 102 scoped_ptr<WebDataConsumerHandleImpl> handle_;
103 scoped_ptr<WebDataConsumerHandle::Reader> reader_;
102 scoped_ptr<WebDataConsumerHandle::Client> client_; 104 scoped_ptr<WebDataConsumerHandle::Client> client_;
103 base::MessageLoop* main_message_loop_; 105 base::MessageLoop* main_message_loop_;
104 base::Closure on_done_; 106 base::Closure on_done_;
105 std::string result_; 107 std::string result_;
106 }; 108 };
107 109
108 class TwoPhaseReadDataOperation : public ReadDataOperationBase { 110 class TwoPhaseReadDataOperation : public ReadDataOperationBase {
109 public: 111 public:
110 typedef WebDataConsumerHandle::Result Result; 112 typedef WebDataConsumerHandle::Result Result;
111 TwoPhaseReadDataOperation(mojo::ScopedDataPipeConsumerHandle handle, 113 TwoPhaseReadDataOperation(mojo::ScopedDataPipeConsumerHandle handle,
112 base::MessageLoop* main_message_loop, 114 base::MessageLoop* main_message_loop,
113 const base::Closure& on_done) 115 const base::Closure& on_done)
114 : handle_(new WebDataConsumerHandleImpl(handle.Pass())), 116 : handle_(new WebDataConsumerHandleImpl(handle.Pass())),
115 main_message_loop_(main_message_loop), on_done_(on_done) {} 117 main_message_loop_(main_message_loop), on_done_(on_done) {}
116 118
117 const std::string& result() const { return result_; } 119 const std::string& result() const { return result_; }
118 120
119 void ReadMore() override { 121 void ReadMore() override {
120 ReadData(); 122 ReadData();
121 } 123 }
122 124
123 void ReadData() { 125 void ReadData() {
124 if (!client_) { 126 if (!client_) {
125 client_.reset(new ClientImpl(this)); 127 client_.reset(new ClientImpl(this));
126 handle_->registerClient(client_.get()); 128 reader_ = handle_->ObtainReader(client_.get());
127 } 129 }
128 130
129 Result rv; 131 Result rv;
130 while (true) { 132 while (true) {
131 const void* buffer = nullptr; 133 const void* buffer = nullptr;
132 size_t size; 134 size_t size;
133 rv = handle_->beginRead(&buffer, kNone, &size); 135 rv = reader_->beginRead(&buffer, kNone, &size);
134 if (rv != kOk) 136 if (rv != kOk)
135 break; 137 break;
136 // In order to verify endRead, we read at most one byte for each time. 138 // In order to verify endRead, we read at most one byte for each time.
137 size_t read_size = std::max(static_cast<size_t>(1), size); 139 size_t read_size = std::max(static_cast<size_t>(1), size);
138 result_.insert(result_.size(), static_cast<const char*>(buffer), 140 result_.insert(result_.size(), static_cast<const char*>(buffer),
139 read_size); 141 read_size);
140 rv = handle_->endRead(read_size); 142 rv = reader_->endRead(read_size);
141 if (rv != kOk) { 143 if (rv != kOk) {
142 // Something is wrong. 144 // Something is wrong.
143 result_ = "error"; 145 result_ = "error";
144 main_message_loop_->task_runner()->PostTask(FROM_HERE, on_done_); 146 main_message_loop_->task_runner()->PostTask(FROM_HERE, on_done_);
145 return; 147 return;
146 } 148 }
147 } 149 }
148 150
149 if (rv == kShouldWait) { 151 if (rv == kShouldWait) {
150 // Wait a while... 152 // Wait a while...
151 return; 153 return;
152 } 154 }
153 155
154 if (rv != kDone) { 156 if (rv != kDone) {
155 // Something is wrong. 157 // Something is wrong.
156 result_ = "error"; 158 result_ = "error";
157 } 159 }
158 160
159 // The operation is done. 161 // The operation is done.
162 reader_.reset();
160 main_message_loop_->task_runner()->PostTask(FROM_HERE, on_done_); 163 main_message_loop_->task_runner()->PostTask(FROM_HERE, on_done_);
161 } 164 }
162 165
163 private: 166 private:
164 scoped_ptr<WebDataConsumerHandle> handle_; 167 scoped_ptr<WebDataConsumerHandleImpl> handle_;
168 scoped_ptr<WebDataConsumerHandle::Reader> reader_;
165 scoped_ptr<WebDataConsumerHandle::Client> client_; 169 scoped_ptr<WebDataConsumerHandle::Client> client_;
166 base::MessageLoop* main_message_loop_; 170 base::MessageLoop* main_message_loop_;
167 base::Closure on_done_; 171 base::Closure on_done_;
168 std::string result_; 172 std::string result_;
169 }; 173 };
170 174
171 class WebDataConsumerHandleImplTest : public ::testing::Test { 175 class WebDataConsumerHandleImplTest : public ::testing::Test {
172 public: 176 public:
173 typedef WebDataConsumerHandle::Result Result; 177 typedef WebDataConsumerHandle::Result Result;
174 178
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 264
261 run_loop.Run(); 265 run_loop.Run();
262 t.Stop(); 266 t.Stop();
263 267
264 EXPECT_EQ(expected, operation->result()); 268 EXPECT_EQ(expected, operation->result());
265 } 269 }
266 270
267 } // namespace 271 } // namespace
268 272
269 } // namespace content 273 } // namespace content
OLDNEW
« no previous file with comments | « content/child/web_data_consumer_handle_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698