Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 #ifndef MOJO_BINDINGS_JS_DRAIN_DATA_H_ | |
| 6 #define MOJO_BINDINGS_JS_DRAIN_DATA_H_ | |
| 7 | |
| 8 #include "base/memory/scoped_vector.h" | |
| 9 #include "gin/runner.h" | |
| 10 #include "mojo/public/c/environment/async_waiter.h" | |
| 11 #include "mojo/public/cpp/system/core.h" | |
| 12 #include "v8/include/v8.h" | |
| 13 | |
| 14 | |
|
abarth-chromium
2014/09/25 00:01:42
Extra blank line here.
hansmuller
2014/09/25 14:36:35
Done.
| |
| 15 namespace mojo { | |
| 16 namespace js { | |
| 17 | |
| 18 // This class is the implementation of the Mojo JavaScript core module's | |
| 19 // drainData() method. It is not intended to be used directly. The caller | |
| 20 // allocates a DrainData on the heap and returns GetPromise() to JS. The | |
| 21 // implementation deletes itself after reading as much data as possible | |
| 22 // and rejecting or resolving the Promise. | |
| 23 | |
| 24 class DrainData { | |
| 25 public: | |
| 26 // Starts waiting for data on the specified data pipe consumer handle. | |
| 27 // See WaitForData(). The constructor does not block. | |
| 28 DrainData(v8::Isolate* isolate, mojo::Handle handle); | |
| 29 | |
| 30 // Returns a Promise that will be settled when no more data can be read. | |
| 31 // Should be called just once on a newly allocated DrainData object. | |
| 32 v8::Handle<v8::Value> GetPromise(); | |
| 33 | |
| 34 private: | |
| 35 ~DrainData(); | |
| 36 | |
| 37 // Registers an "async waiter" that calls DataReady() via WaitCompleted(). | |
| 38 void WaitForData(); | |
| 39 static void WaitCompleted(void* self, MojoResult result) { | |
| 40 static_cast<DrainData*>(self)->DataReady(result); | |
| 41 } | |
| 42 | |
| 43 // Use ReadData() to read whatever is availble now on handle_ and save | |
| 44 // it in data_buffers_. | |
| 45 void DataReady(MojoResult result); | |
| 46 MojoResult ReadData(); | |
| 47 | |
| 48 // When the remote data pipe handle is closed, or an error occurs, deliver | |
| 49 // all of the buffered data to the JS Promise and then delete this. | |
| 50 void DeliverData(MojoResult result); | |
| 51 | |
| 52 typedef std::vector<char> DataBuffer; | |
| 53 | |
| 54 v8::Isolate* isolate_; | |
| 55 ScopedDataPipeConsumerHandle handle_; | |
| 56 MojoAsyncWaitID wait_id_; | |
| 57 base::WeakPtr<gin::Runner> runner_; | |
| 58 v8::UniquePersistent<v8::Promise::Resolver> resolver_; | |
| 59 ScopedVector<DataBuffer> data_buffers_; | |
| 60 }; | |
| 61 | |
| 62 } // namespace js | |
| 63 } // namespace mojo | |
| 64 | |
| 65 #endif // MOJO_BINDINGS_JS_DRAIN_DATA_H_ | |
| OLD | NEW |