| 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 "mojo/edk/js/drain_data.h" | 5 #include "mojo/edk/js/drain_data.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/bind.h" |
| 10 #include "gin/array_buffer.h" | 11 #include "gin/array_buffer.h" |
| 11 #include "gin/converter.h" | 12 #include "gin/converter.h" |
| 12 #include "gin/dictionary.h" | 13 #include "gin/dictionary.h" |
| 13 #include "gin/per_context_data.h" | 14 #include "gin/per_context_data.h" |
| 14 #include "gin/per_isolate_data.h" | 15 #include "gin/per_isolate_data.h" |
| 15 #include "mojo/public/cpp/environment/environment.h" | |
| 16 #include "mojo/public/cpp/system/core.h" | 16 #include "mojo/public/cpp/system/core.h" |
| 17 | 17 |
| 18 namespace mojo { | 18 namespace mojo { |
| 19 namespace edk { | 19 namespace edk { |
| 20 namespace js { | 20 namespace js { |
| 21 | 21 |
| 22 DrainData::DrainData(v8::Isolate* isolate, mojo::Handle handle) | 22 DrainData::DrainData(v8::Isolate* isolate, mojo::Handle handle) |
| 23 : isolate_(isolate), | 23 : isolate_(isolate), handle_(DataPipeConsumerHandle(handle.value())) { |
| 24 handle_(DataPipeConsumerHandle(handle.value())), | |
| 25 wait_id_(0) { | |
| 26 | |
| 27 v8::Handle<v8::Context> context(isolate_->GetCurrentContext()); | 24 v8::Handle<v8::Context> context(isolate_->GetCurrentContext()); |
| 28 runner_ = gin::PerContextData::From(context)->runner()->GetWeakPtr(); | 25 runner_ = gin::PerContextData::From(context)->runner()->GetWeakPtr(); |
| 29 | 26 |
| 30 WaitForData(); | 27 WaitForData(); |
| 31 } | 28 } |
| 32 | 29 |
| 33 v8::Handle<v8::Value> DrainData::GetPromise() { | 30 v8::Handle<v8::Value> DrainData::GetPromise() { |
| 34 CHECK(resolver_.IsEmpty()); | 31 CHECK(resolver_.IsEmpty()); |
| 35 v8::Handle<v8::Promise::Resolver> resolver( | 32 v8::Handle<v8::Promise::Resolver> resolver( |
| 36 v8::Promise::Resolver::New(isolate_)); | 33 v8::Promise::Resolver::New(isolate_)); |
| 37 resolver_.Reset(isolate_, resolver); | 34 resolver_.Reset(isolate_, resolver); |
| 38 return resolver->GetPromise(); | 35 return resolver->GetPromise(); |
| 39 } | 36 } |
| 40 | 37 |
| 41 DrainData::~DrainData() { | 38 DrainData::~DrainData() { |
| 42 if (wait_id_) | |
| 43 Environment::GetDefaultAsyncWaiter()->CancelWait(wait_id_); | |
| 44 resolver_.Reset(); | 39 resolver_.Reset(); |
| 45 } | 40 } |
| 46 | 41 |
| 47 void DrainData::WaitForData() { | 42 void DrainData::WaitForData() { |
| 48 wait_id_ = Environment::GetDefaultAsyncWaiter()->AsyncWait( | 43 handle_watcher_.Start( |
| 49 handle_.get().value(), | 44 handle_.get(), MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE, |
| 50 MOJO_HANDLE_SIGNAL_READABLE, | 45 base::Bind(&DrainData::DataReady, base::Unretained(this))); |
| 51 MOJO_DEADLINE_INDEFINITE, | |
| 52 &DrainData::WaitCompleted, | |
| 53 this); | |
| 54 } | 46 } |
| 55 | 47 |
| 56 void DrainData::DataReady(MojoResult result) { | 48 void DrainData::DataReady(MojoResult result) { |
| 57 wait_id_ = 0; | |
| 58 if (result != MOJO_RESULT_OK) { | 49 if (result != MOJO_RESULT_OK) { |
| 59 DeliverData(result); | 50 DeliverData(result); |
| 60 return; | 51 return; |
| 61 } | 52 } |
| 62 while (result == MOJO_RESULT_OK) { | 53 while (result == MOJO_RESULT_OK) { |
| 63 result = ReadData(); | 54 result = ReadData(); |
| 64 if (result == MOJO_RESULT_SHOULD_WAIT) | 55 if (result == MOJO_RESULT_SHOULD_WAIT) |
| 65 WaitForData(); | 56 WaitForData(); |
| 66 else if (result != MOJO_RESULT_OK) | 57 else if (result != MOJO_RESULT_OK) |
| 67 DeliverData(result); | 58 DeliverData(result); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 resolver->Resolve(settled_value); | 118 resolver->Resolve(settled_value); |
| 128 else | 119 else |
| 129 resolver->Reject(settled_value); | 120 resolver->Reject(settled_value); |
| 130 | 121 |
| 131 delete this; | 122 delete this; |
| 132 } | 123 } |
| 133 | 124 |
| 134 } // namespace js | 125 } // namespace js |
| 135 } // namespace edk | 126 } // namespace edk |
| 136 } // namespace mojo | 127 } // namespace mojo |
| OLD | NEW |