| OLD | NEW |
| 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/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/single_thread_task_runner.h" |
| 13 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
| 15 #include "base/thread_task_runner_handle.h" |
| 14 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h" | 18 #include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h" |
| 17 | 19 |
| 18 namespace content { | 20 namespace content { |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 using blink::WebDataConsumerHandle; | 24 using blink::WebDataConsumerHandle; |
| 23 | 25 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 34 static const WebDataConsumerHandle::Result kShouldWait = | 36 static const WebDataConsumerHandle::Result kShouldWait = |
| 35 WebDataConsumerHandle::ShouldWait; | 37 WebDataConsumerHandle::ShouldWait; |
| 36 }; | 38 }; |
| 37 | 39 |
| 38 class ClientImpl final : public WebDataConsumerHandle::Client { | 40 class ClientImpl final : public WebDataConsumerHandle::Client { |
| 39 public: | 41 public: |
| 40 explicit ClientImpl(ReadDataOperationBase* operation) | 42 explicit ClientImpl(ReadDataOperationBase* operation) |
| 41 : operation_(operation) {} | 43 : operation_(operation) {} |
| 42 | 44 |
| 43 void didGetReadable() override { | 45 void didGetReadable() override { |
| 44 base::MessageLoop::current()->PostTask( | 46 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 45 FROM_HERE, | 47 FROM_HERE, base::Bind(&ReadDataOperationBase::ReadMore, |
| 46 base::Bind(&ReadDataOperationBase::ReadMore, | 48 base::Unretained(operation_))); |
| 47 base::Unretained(operation_))); | |
| 48 } | 49 } |
| 49 | 50 |
| 50 private: | 51 private: |
| 51 ReadDataOperationBase* operation_; | 52 ReadDataOperationBase* operation_; |
| 52 }; | 53 }; |
| 53 | 54 |
| 54 class ReadDataOperation : public ReadDataOperationBase { | 55 class ReadDataOperation : public ReadDataOperationBase { |
| 55 public: | 56 public: |
| 56 typedef WebDataConsumerHandle::Result Result; | 57 typedef WebDataConsumerHandle::Result Result; |
| 57 ReadDataOperation(mojo::ScopedDataPipeConsumerHandle handle, | 58 ReadDataOperation(mojo::ScopedDataPipeConsumerHandle handle, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 86 // Wait a while... | 87 // Wait a while... |
| 87 return; | 88 return; |
| 88 } | 89 } |
| 89 | 90 |
| 90 if (rv != kDone) { | 91 if (rv != kDone) { |
| 91 // Something is wrong. | 92 // Something is wrong. |
| 92 result_ = "error"; | 93 result_ = "error"; |
| 93 } | 94 } |
| 94 | 95 |
| 95 // The operation is done. | 96 // The operation is done. |
| 96 main_message_loop_->PostTask(FROM_HERE, on_done_); | 97 main_message_loop_->task_runner()->PostTask(FROM_HERE, on_done_); |
| 97 } | 98 } |
| 98 | 99 |
| 99 private: | 100 private: |
| 100 scoped_ptr<WebDataConsumerHandle> handle_; | 101 scoped_ptr<WebDataConsumerHandle> handle_; |
| 101 scoped_ptr<WebDataConsumerHandle::Client> client_; | 102 scoped_ptr<WebDataConsumerHandle::Client> client_; |
| 102 base::MessageLoop* main_message_loop_; | 103 base::MessageLoop* main_message_loop_; |
| 103 base::Closure on_done_; | 104 base::Closure on_done_; |
| 104 std::string result_; | 105 std::string result_; |
| 105 }; | 106 }; |
| 106 | 107 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 133 if (rv != kOk) | 134 if (rv != kOk) |
| 134 break; | 135 break; |
| 135 // In order to verify endRead, we read at most one byte for each time. | 136 // In order to verify endRead, we read at most one byte for each time. |
| 136 size_t read_size = std::max(static_cast<size_t>(1), size); | 137 size_t read_size = std::max(static_cast<size_t>(1), size); |
| 137 result_.insert(result_.size(), static_cast<const char*>(buffer), | 138 result_.insert(result_.size(), static_cast<const char*>(buffer), |
| 138 read_size); | 139 read_size); |
| 139 rv = handle_->endRead(read_size); | 140 rv = handle_->endRead(read_size); |
| 140 if (rv != kOk) { | 141 if (rv != kOk) { |
| 141 // Something is wrong. | 142 // Something is wrong. |
| 142 result_ = "error"; | 143 result_ = "error"; |
| 143 main_message_loop_->PostTask(FROM_HERE, on_done_); | 144 main_message_loop_->task_runner()->PostTask(FROM_HERE, on_done_); |
| 144 return; | 145 return; |
| 145 } | 146 } |
| 146 } | 147 } |
| 147 | 148 |
| 148 if (rv == kShouldWait) { | 149 if (rv == kShouldWait) { |
| 149 // Wait a while... | 150 // Wait a while... |
| 150 return; | 151 return; |
| 151 } | 152 } |
| 152 | 153 |
| 153 if (rv != kDone) { | 154 if (rv != kDone) { |
| 154 // Something is wrong. | 155 // Something is wrong. |
| 155 result_ = "error"; | 156 result_ = "error"; |
| 156 } | 157 } |
| 157 | 158 |
| 158 // The operation is done. | 159 // The operation is done. |
| 159 main_message_loop_->PostTask(FROM_HERE, on_done_); | 160 main_message_loop_->task_runner()->PostTask(FROM_HERE, on_done_); |
| 160 } | 161 } |
| 161 | 162 |
| 162 private: | 163 private: |
| 163 scoped_ptr<WebDataConsumerHandle> handle_; | 164 scoped_ptr<WebDataConsumerHandle> handle_; |
| 164 scoped_ptr<WebDataConsumerHandle::Client> client_; | 165 scoped_ptr<WebDataConsumerHandle::Client> client_; |
| 165 base::MessageLoop* main_message_loop_; | 166 base::MessageLoop* main_message_loop_; |
| 166 base::Closure on_done_; | 167 base::Closure on_done_; |
| 167 std::string result_; | 168 std::string result_; |
| 168 }; | 169 }; |
| 169 | 170 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 TEST_F(WebDataConsumerHandleImplTest, ReadData) { | 221 TEST_F(WebDataConsumerHandleImplTest, ReadData) { |
| 221 base::RunLoop run_loop; | 222 base::RunLoop run_loop; |
| 222 auto operation = make_scoped_ptr(new ReadDataOperation( | 223 auto operation = make_scoped_ptr(new ReadDataOperation( |
| 223 consumer_.Pass(), | 224 consumer_.Pass(), |
| 224 &message_loop_, | 225 &message_loop_, |
| 225 run_loop.QuitClosure())); | 226 run_loop.QuitClosure())); |
| 226 | 227 |
| 227 base::Thread t("DataConsumerHandle test thread"); | 228 base::Thread t("DataConsumerHandle test thread"); |
| 228 ASSERT_TRUE(t.Start()); | 229 ASSERT_TRUE(t.Start()); |
| 229 | 230 |
| 230 t.message_loop()->PostTask( | 231 t.task_runner()->PostTask(FROM_HERE, |
| 231 FROM_HERE, | 232 base::Bind(&ReadDataOperation::ReadData, |
| 232 base::Bind(&ReadDataOperation::ReadData, | 233 base::Unretained(operation.get()))); |
| 233 base::Unretained(operation.get()))); | |
| 234 | 234 |
| 235 std::string expected = ProduceData(24 * 1024); | 235 std::string expected = ProduceData(24 * 1024); |
| 236 producer_.reset(); | 236 producer_.reset(); |
| 237 | 237 |
| 238 run_loop.Run(); | 238 run_loop.Run(); |
| 239 t.Stop(); | 239 t.Stop(); |
| 240 | 240 |
| 241 EXPECT_EQ(expected, operation->result()); | 241 EXPECT_EQ(expected, operation->result()); |
| 242 } | 242 } |
| 243 | 243 |
| 244 TEST_F(WebDataConsumerHandleImplTest, TwoPhaseReadData) { | 244 TEST_F(WebDataConsumerHandleImplTest, TwoPhaseReadData) { |
| 245 base::RunLoop run_loop; | 245 base::RunLoop run_loop; |
| 246 auto operation = make_scoped_ptr(new TwoPhaseReadDataOperation( | 246 auto operation = make_scoped_ptr(new TwoPhaseReadDataOperation( |
| 247 consumer_.Pass(), | 247 consumer_.Pass(), |
| 248 &message_loop_, | 248 &message_loop_, |
| 249 run_loop.QuitClosure())); | 249 run_loop.QuitClosure())); |
| 250 | 250 |
| 251 base::Thread t("DataConsumerHandle test thread"); | 251 base::Thread t("DataConsumerHandle test thread"); |
| 252 ASSERT_TRUE(t.Start()); | 252 ASSERT_TRUE(t.Start()); |
| 253 | 253 |
| 254 t.message_loop()->PostTask( | 254 t.task_runner()->PostTask(FROM_HERE, |
| 255 FROM_HERE, | 255 base::Bind(&TwoPhaseReadDataOperation::ReadData, |
| 256 base::Bind(&TwoPhaseReadDataOperation::ReadData, | 256 base::Unretained(operation.get()))); |
| 257 base::Unretained(operation.get()))); | |
| 258 | 257 |
| 259 std::string expected = ProduceData(24 * 1024); | 258 std::string expected = ProduceData(24 * 1024); |
| 260 producer_.reset(); | 259 producer_.reset(); |
| 261 | 260 |
| 262 run_loop.Run(); | 261 run_loop.Run(); |
| 263 t.Stop(); | 262 t.Stop(); |
| 264 | 263 |
| 265 EXPECT_EQ(expected, operation->result()); | 264 EXPECT_EQ(expected, operation->result()); |
| 266 } | 265 } |
| 267 | 266 |
| 268 } // namespace | 267 } // namespace |
| 269 | 268 |
| 270 } // namespace content | 269 } // namespace content |
| OLD | NEW |