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