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_WAITING_CALLBACK_H_ | 5 #ifndef MOJO_EDK_JS_WAITING_CALLBACK_H_ |
6 #define MOJO_EDK_JS_WAITING_CALLBACK_H_ | 6 #define MOJO_EDK_JS_WAITING_CALLBACK_H_ |
7 | 7 |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "gin/handle.h" | 10 #include "gin/handle.h" |
11 #include "gin/runner.h" | 11 #include "gin/runner.h" |
12 #include "gin/wrappable.h" | 12 #include "gin/wrappable.h" |
13 #include "mojo/edk/js/handle.h" | 13 #include "mojo/edk/js/handle.h" |
14 #include "mojo/edk/js/handle_close_observer.h" | 14 #include "mojo/edk/js/handle_close_observer.h" |
15 #include "mojo/message_pump/handle_watcher.h" | 15 #include "mojo/message_pump/handle_watcher.h" |
16 #include "mojo/public/cpp/system/core.h" | 16 #include "mojo/public/cpp/system/core.h" |
17 #include "mojo/public/cpp/system/watcher.h" | |
17 | 18 |
18 namespace mojo { | 19 namespace mojo { |
19 namespace edk { | 20 namespace edk { |
20 namespace js { | 21 namespace js { |
21 | 22 |
22 class WaitingCallback : public gin::Wrappable<WaitingCallback>, | 23 class WaitingCallback : public gin::Wrappable<WaitingCallback>, |
23 public HandleCloseObserver { | 24 public HandleCloseObserver { |
24 public: | 25 public: |
25 static gin::WrapperInfo kWrapperInfo; | 26 static gin::WrapperInfo kWrapperInfo; |
26 | 27 |
27 // Creates a new WaitingCallback. | 28 // Creates a new WaitingCallback. |
29 // | |
30 // If |one_shot| is true, the callback will only ever be called at most once. | |
31 // If false, the callback may be called any number of times until the | |
32 // WaitingCallback is explicitly cancelled. | |
28 static gin::Handle<WaitingCallback> Create( | 33 static gin::Handle<WaitingCallback> Create( |
29 v8::Isolate* isolate, | 34 v8::Isolate* isolate, |
30 v8::Handle<v8::Function> callback, | 35 v8::Handle<v8::Function> callback, |
31 gin::Handle<HandleWrapper> handle_wrapper, | 36 gin::Handle<HandleWrapper> handle_wrapper, |
32 MojoHandleSignals signals); | 37 MojoHandleSignals signals, |
38 bool one_shot); | |
33 | 39 |
34 // Cancels the callback. Does nothing if a callback is not pending. This is | 40 // Cancels the callback. Does nothing if a callback is not pending. This is |
35 // implicitly invoked from the destructor but can be explicitly invoked as | 41 // implicitly invoked from the destructor but can be explicitly invoked as |
36 // necessary. | 42 // necessary. |
37 void Cancel(); | 43 void Cancel(); |
38 | 44 |
39 private: | 45 private: |
40 WaitingCallback(v8::Isolate* isolate, | 46 WaitingCallback(v8::Isolate* isolate, |
41 v8::Handle<v8::Function> callback, | 47 v8::Handle<v8::Function> callback, |
42 gin::Handle<HandleWrapper> handle_wrapper); | 48 gin::Handle<HandleWrapper> handle_wrapper, |
49 bool one_shot); | |
43 ~WaitingCallback() override; | 50 ~WaitingCallback() override; |
44 | 51 |
45 // Callback from common::HandleWatcher. | 52 // Callback from common::HandleWatcher. |
46 void OnHandleReady(MojoResult result); | 53 void OnHandleReady(MojoResult result); |
47 | 54 |
48 // Invoked by the HandleWrapper if the handle is closed while this wait is | 55 // Invoked by the HandleWrapper if the handle is closed while this wait is |
49 // still in progress. | 56 // still in progress. |
50 void OnWillCloseHandle() override; | 57 void OnWillCloseHandle() override; |
51 | 58 |
52 void RemoveHandleCloseObserver(); | 59 void RemoveHandleCloseObserver(); |
53 void CallCallback(MojoResult result); | 60 void CallCallback(MojoResult result); |
54 | 61 |
62 // Indicates whether this is a one-shot callback or not. If so, it uses the | |
yzshen1
2016/03/08 22:37:25
It seems straightforward to use the new Watcher AP
| |
63 // deprecated HandleWatcher to wait for signals; otherwise it uses the new | |
64 // system Watcher API. | |
65 const bool one_shot_; | |
66 | |
55 base::WeakPtr<gin::Runner> runner_; | 67 base::WeakPtr<gin::Runner> runner_; |
56 | 68 |
57 common::HandleWatcher handle_watcher_; | 69 common::HandleWatcher handle_watcher_; |
70 Watcher watcher_; | |
58 | 71 |
59 HandleWrapper* handle_wrapper_; | 72 HandleWrapper* handle_wrapper_; |
60 base::WeakPtrFactory<WaitingCallback> weak_factory_; | 73 base::WeakPtrFactory<WaitingCallback> weak_factory_; |
61 | 74 |
62 DISALLOW_COPY_AND_ASSIGN(WaitingCallback); | 75 DISALLOW_COPY_AND_ASSIGN(WaitingCallback); |
63 }; | 76 }; |
64 | 77 |
65 } // namespace js | 78 } // namespace js |
66 } // namespace edk | 79 } // namespace edk |
67 } // namespace mojo | 80 } // namespace mojo |
68 | 81 |
69 #endif // MOJO_EDK_JS_WAITING_CALLBACK_H_ | 82 #endif // MOJO_EDK_JS_WAITING_CALLBACK_H_ |
OLD | NEW |