OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "content/browser/loader/loader_test_service_impl.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "content/browser/loader/loader_test_service_context.h" |
| 10 #include "mojo/message_pump/handle_watcher.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 class LoaderTestServiceImpl::Job { |
| 15 public: |
| 16 Job(int id, |
| 17 mojo::ScopedDataPipeConsumerHandle reader, |
| 18 LoaderTestServiceImpl* service, |
| 19 const TransmitCallback& callback) |
| 20 : id_(id), |
| 21 reader_(std::move(reader)), |
| 22 read_size_(0), |
| 23 service_(service), |
| 24 callback_(callback) {} |
| 25 |
| 26 void Start() { StartWatching(); } |
| 27 |
| 28 void StartWatching() { |
| 29 handle_watcher_.Start(reader_.get(), MOJO_HANDLE_SIGNAL_READABLE, |
| 30 MOJO_DEADLINE_INDEFINITE, |
| 31 base::Bind(&Job::OnReadable, base::Unretained(this))); |
| 32 } |
| 33 |
| 34 void OnReadable(MojoResult) { |
| 35 // fprintf(stderr, "%s\n", __PRETTY_FUNCTION__); |
| 36 while (true) { |
| 37 const void* buffer = nullptr; |
| 38 uint32_t available; |
| 39 MojoResult result = mojo::BeginReadDataRaw( |
| 40 reader_.get(), &buffer, &available, MOJO_WRITE_DATA_FLAG_NONE); |
| 41 // fprintf(stderr, "result = %d\n", static_cast<int>(result)); |
| 42 // for (size_t i = 0; i < available; ++i) { |
| 43 // fprintf(stderr, "buffer[%zu] = %02x\n", i, ((const char*)buffer)[i]); |
| 44 // } |
| 45 if (result == MOJO_RESULT_OK) { |
| 46 mojo::EndReadDataRaw(reader_.get(), available); |
| 47 read_size_ += available; |
| 48 // fprintf(stderr, "read %zu\n", static_cast<size_t>(available)); |
| 49 } else if (result == MOJO_RESULT_SHOULD_WAIT) { |
| 50 StartWatching(); |
| 51 break; |
| 52 } else if (result == MOJO_RESULT_FAILED_PRECONDITION) { |
| 53 fprintf(stderr, "done: read %zu bytes\n", read_size_); |
| 54 callback_.Run(); |
| 55 service_->DeleteJob(id_); |
| 56 // |this| is deleted here. |
| 57 return; |
| 58 } else { |
| 59 service_->DeleteJob(id_); |
| 60 // |this| is deleted here. |
| 61 return; |
| 62 } |
| 63 } |
| 64 } |
| 65 |
| 66 private: |
| 67 int id_; |
| 68 mojo::ScopedDataPipeConsumerHandle reader_; |
| 69 mojo::common::HandleWatcher handle_watcher_; |
| 70 size_t read_size_; |
| 71 LoaderTestServiceImpl* service_; |
| 72 TransmitCallback callback_; |
| 73 }; |
| 74 |
| 75 LoaderTestServiceImpl::LoaderTestServiceImpl( |
| 76 mojo::InterfaceRequest<LoaderTestService> request, |
| 77 LoaderTestServiceContext* context) |
| 78 : binding_(this, std::move(request)), context_(context), job_id_(0) { |
| 79 fprintf(stderr, "%s\n", __PRETTY_FUNCTION__); |
| 80 binding_.set_connection_error_handler(base::Bind( |
| 81 &LoaderTestServiceImpl::OnConnectionError, base::Unretained(this))); |
| 82 } |
| 83 |
| 84 LoaderTestServiceImpl::~LoaderTestServiceImpl() = default; |
| 85 |
| 86 void LoaderTestServiceImpl::OnConnectionError() { |
| 87 fprintf(stderr, "%s\n", __PRETTY_FUNCTION__); |
| 88 context_->ServiceHadConnectionError(this); |
| 89 // |this| is deleted here. |
| 90 } |
| 91 |
| 92 void LoaderTestServiceImpl::Transmit(mojo::ScopedDataPipeConsumerHandle pipe, |
| 93 const TransmitCallback& callback) { |
| 94 fprintf(stderr, "%s\n", __PRETTY_FUNCTION__); |
| 95 int id = ++job_id_; |
| 96 auto i = jobs_.insert(std::make_pair( |
| 97 id, scoped_ptr<Job>(new Job(id, std::move(pipe), this, callback)))); |
| 98 i.first->second->Start(); |
| 99 } |
| 100 |
| 101 void LoaderTestServiceImpl::Hash(mojo::ScopedDataPipeConsumerHandle pipe, |
| 102 const HashCallback& callback) { |
| 103 callback.Run("hello"); |
| 104 } |
| 105 |
| 106 void LoaderTestServiceImpl::DeleteJob(int id) { |
| 107 jobs_.erase(id); |
| 108 } |
| 109 |
| 110 } // namespace content |
OLD | NEW |