1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// found in the LICENSE file.
4f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef MOJO_BINDINGS_JS_WAITING_CALLBACK_H_
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define MOJO_BINDINGS_JS_WAITING_CALLBACK_H_
7f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
8a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "gin/handle.h"
9f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "gin/runner.h"
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "gin/wrappable.h"
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "mojo/bindings/js/handle.h"
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "mojo/bindings/js/handle_close_observer.h"
130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "mojo/public/c/environment/async_waiter.h"
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "mojo/public/cpp/system/core.h"
15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
16f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace mojo {
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace js {
18f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass WaitingCallback : public gin::Wrappable<WaitingCallback>,
201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                        public gin::HandleCloseObserver {
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) public:
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static gin::WrapperInfo kWrapperInfo;
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Creates a new WaitingCallback.
25a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  static gin::Handle<WaitingCallback> Create(
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      v8::Isolate* isolate,
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      v8::Handle<v8::Function> callback,
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      gin::Handle<gin::HandleWrapper> handle_wrapper,
29f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      MojoHandleSignals signals);
30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Cancels the callback. Does nothing if a callback is not pending. This is
32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // implicitly invoked from the destructor but can be explicitly invoked as
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // necessary.
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void Cancel();
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
36f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) private:
371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  WaitingCallback(v8::Isolate* isolate,
381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  v8::Handle<v8::Function> callback,
391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  gin::Handle<gin::HandleWrapper> handle_wrapper);
40f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual ~WaitingCallback();
41f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Callback from MojoAsyncWaiter. |closure| is the WaitingCallback.
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  static void CallOnHandleReady(void* closure, MojoResult result);
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Invoked from CallOnHandleReady() (CallOnHandleReady() must be static).
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void OnHandleReady(MojoResult result);
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Invoked by the HandleWrapper if the handle is closed while this wait is
491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // still in progress.
501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual void OnWillCloseHandle() OVERRIDE;
511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::WeakPtr<gin::Runner> runner_;
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MojoAsyncWaitID wait_id_;
54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  gin::HandleWrapper* handle_wrapper_;
561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
57f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(WaitingCallback);
58f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)};
59f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
60f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // namespace js
61f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // namespace mojo
62f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif  // MOJO_BINDINGS_JS_WAITING_CALLBACK_H_
64