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_PUBLIC_PYTHON_SRC_COMMON_H | |
| 6 #define MOJO_PUBLIC_PYTHON_SRC_COMMON_H | |
| 7 | |
| 8 #include <Python.h> | |
| 9 | |
| 10 #include <map> | |
| 11 | |
| 12 #include "mojo/public/c/environment/async_waiter.h" | |
| 13 #include "mojo/public/cpp/bindings/callback.h" | |
| 14 #include "mojo/public/cpp/bindings/lib/shared_ptr.h" | |
| 15 #include "mojo/public/cpp/system/core.h" | |
| 16 | |
| 17 namespace mojo { | |
| 18 namespace python { | |
|
qsr
2014/12/12 09:09:53
Blank line after this.
etiennej
2014/12/12 10:58:25
Done.
| |
| 19 class ScopedGIL { | |
| 20 public: | |
| 21 ScopedGIL(); | |
| 22 | |
| 23 ~ScopedGIL(); | |
| 24 | |
| 25 private: | |
| 26 PyGILState_STATE state_; | |
| 27 | |
| 28 MOJO_DISALLOW_COPY_AND_ASSIGN(ScopedGIL); | |
| 29 }; | |
| 30 | |
| 31 enum ScopedPyRefAcquire { | |
| 32 kAcquire, | |
| 33 }; | |
| 34 | |
| 35 class ScopedPyRef { | |
| 36 public: | |
| 37 ScopedPyRef(PyObject* object); | |
| 38 ScopedPyRef(PyObject* object, ScopedPyRefAcquire); | |
| 39 | |
| 40 ~ScopedPyRef(); | |
| 41 | |
| 42 operator PyObject*() const; | |
| 43 | |
| 44 private: | |
| 45 PyObject* object_; | |
| 46 | |
| 47 MOJO_DISALLOW_COPY_AND_ASSIGN(ScopedPyRef); | |
| 48 }; | |
| 49 | |
| 50 | |
| 51 class PythonClosure : public mojo::Closure::Runnable { | |
| 52 public: | |
| 53 PythonClosure(PyObject* callable, const mojo::Closure& quit); | |
| 54 ~PythonClosure(); | |
| 55 | |
| 56 void Run() const override; | |
| 57 | |
| 58 private: | |
| 59 ScopedPyRef callable_; | |
| 60 const mojo::Closure quit_; | |
| 61 | |
| 62 MOJO_DISALLOW_COPY_AND_ASSIGN(PythonClosure); | |
| 63 }; | |
| 64 | |
| 65 // Create a mojo::Closure from a callable python object. | |
| 66 Closure::Runnable* NewRunnableFromClosure(PyObject* callable, | |
|
qsr
2014/12/12 09:09:53
Can you call this NewRunnableFromCallable instead
etiennej
2014/12/12 10:58:25
Done.
| |
| 67 const Closure& quit_closure); | |
| 68 | |
| 69 // AsyncWaiter for python, used to execute a python closure after waiting. If an | |
| 70 // error occurs while executing the closure, the current message loop will be | |
| 71 // exited. See |AsyncWaiter| in mojo/public/c/environment/async_waiter.h for | |
| 72 // more details. | |
| 73 class PythonAsyncWaiter { | |
| 74 public: | |
| 75 PythonAsyncWaiter(const mojo::Closure& quit_closure); | |
| 76 ~PythonAsyncWaiter(); | |
| 77 MojoAsyncWaitID AsyncWait(MojoHandle handle, | |
| 78 MojoHandleSignals signals, | |
| 79 MojoDeadline deadline, | |
| 80 PyObject* callable); | |
| 81 | |
| 82 void CancelWait(MojoAsyncWaitID wait_id); | |
| 83 | |
| 84 private: | |
| 85 class AsyncWaiterRunnable; | |
| 86 | |
| 87 typedef std::map<MojoAsyncWaitID, | |
| 88 internal::SharedPtr<mojo::Callback<void(MojoResult)> > > | |
| 89 CallbackMap; | |
| 90 | |
| 91 CallbackMap callbacks_; | |
| 92 const MojoAsyncWaiter* async_waiter_; | |
| 93 const mojo::Closure& quit_; | |
| 94 }; | |
| 95 | |
| 96 } // namespace python | |
| 97 } // namespace mojo | |
| 98 | |
| 99 #endif // MOJO_PUBLIC_PYTHON_SRC_COMMON_H_ | |
| 100 | |
| OLD | NEW |