• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_EDK_JS_WAITING_CALLBACK_H_
6 #define MOJO_EDK_JS_WAITING_CALLBACK_H_
7 
8 #include "base/macros.h"
9 #include "base/memory/weak_ptr.h"
10 #include "gin/handle.h"
11 #include "gin/runner.h"
12 #include "gin/wrappable.h"
13 #include "mojo/edk/js/handle.h"
14 #include "mojo/public/cpp/system/core.h"
15 #include "mojo/public/cpp/system/watcher.h"
16 
17 namespace mojo {
18 namespace edk {
19 namespace js {
20 
21 class WaitingCallback : public gin::Wrappable<WaitingCallback> {
22  public:
23   static gin::WrapperInfo kWrapperInfo;
24 
25   // Creates a new WaitingCallback.
26   //
27   // If |one_shot| is true, the callback will only ever be called at most once.
28   // If false, the callback may be called any number of times until the
29   // WaitingCallback is explicitly cancelled.
30   static gin::Handle<WaitingCallback> Create(
31       v8::Isolate* isolate,
32       v8::Handle<v8::Function> callback,
33       gin::Handle<HandleWrapper> handle_wrapper,
34       MojoHandleSignals signals,
35       bool one_shot);
36 
37   // Cancels the callback. Does nothing if a callback is not pending. This is
38   // implicitly invoked from the destructor but can be explicitly invoked as
39   // necessary.
40   void Cancel();
41 
42  private:
43   WaitingCallback(v8::Isolate* isolate,
44                   v8::Handle<v8::Function> callback,
45                   bool one_shot);
46   ~WaitingCallback() override;
47 
48   // Callback from the Watcher.
49   void OnHandleReady(MojoResult result);
50 
51   // Indicates whether this is a one-shot callback or not. If so, it uses the
52   // deprecated HandleWatcher to wait for signals; otherwise it uses the new
53   // system Watcher API.
54   const bool one_shot_;
55 
56   base::WeakPtr<gin::Runner> runner_;
57   Watcher watcher_;
58   base::WeakPtrFactory<WaitingCallback> weak_factory_;
59 
60   DISALLOW_COPY_AND_ASSIGN(WaitingCallback);
61 };
62 
63 }  // namespace js
64 }  // namespace edk
65 }  // namespace mojo
66 
67 #endif  // MOJO_EDK_JS_WAITING_CALLBACK_H_
68