Etienne Membrives | 7349134 | 2015-01-16 23:45:50 +0100 | [diff] [blame] | 1 | // Copyright 2013 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 | #include "python_system_impl_helper.h" |
| 6 | |
| 7 | #include <Python.h> |
| 8 | |
| 9 | #include "base/bind.h" |
| 10 | #include "base/location.h" |
James Robinson | 94ade6b | 2015-08-25 13:02:06 -0700 | [diff] [blame] | 11 | #include "mojo/message_pump/message_pump_mojo.h" |
Etienne Membrives | 7349134 | 2015-01-16 23:45:50 +0100 | [diff] [blame] | 12 | #include "mojo/public/python/src/common.h" |
| 13 | |
| 14 | namespace { |
| 15 | class QuitCurrentRunLoop : public mojo::Closure::Runnable { |
| 16 | public: |
| 17 | void Run() const override { |
| 18 | base::MessageLoop::current()->Quit(); |
| 19 | } |
| 20 | |
| 21 | static mojo::Closure::Runnable* NewInstance() { |
| 22 | return new QuitCurrentRunLoop(); |
| 23 | } |
| 24 | }; |
| 25 | |
| 26 | } // namespace |
Etienne Membrives | f1d4833 | 2015-02-06 11:35:08 +0100 | [diff] [blame] | 27 | namespace services { |
Etienne Membrives | 7349134 | 2015-01-16 23:45:50 +0100 | [diff] [blame] | 28 | namespace python { |
Etienne Membrives | f1d4833 | 2015-02-06 11:35:08 +0100 | [diff] [blame] | 29 | namespace content_handler { |
Etienne Membrives | 7349134 | 2015-01-16 23:45:50 +0100 | [diff] [blame] | 30 | |
Etienne Membrives | f1d4833 | 2015-02-06 11:35:08 +0100 | [diff] [blame] | 31 | PythonRunLoop::PythonRunLoop() |
| 32 | : loop_(mojo::common::MessagePumpMojo::Create()) { |
Etienne Membrives | 7349134 | 2015-01-16 23:45:50 +0100 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | PythonRunLoop::~PythonRunLoop() { |
| 36 | } |
| 37 | |
| 38 | void PythonRunLoop::Run() { |
| 39 | loop_.Run(); |
| 40 | } |
| 41 | |
| 42 | void PythonRunLoop::RunUntilIdle() { |
| 43 | loop_.RunUntilIdle(); |
| 44 | } |
| 45 | |
| 46 | void PythonRunLoop::Quit() { |
| 47 | loop_.Quit(); |
| 48 | } |
| 49 | |
| 50 | void PythonRunLoop::PostDelayedTask(PyObject* callable, MojoTimeTicks delay) { |
Etienne Membrives | f1d4833 | 2015-02-06 11:35:08 +0100 | [diff] [blame] | 51 | mojo::Closure::Runnable* quit_runnable = |
| 52 | mojo::python::NewRunnableFromCallable(callable, loop_.QuitClosure()); |
Etienne Membrives | 7349134 | 2015-01-16 23:45:50 +0100 | [diff] [blame] | 53 | |
| 54 | loop_.PostDelayedTask( |
| 55 | FROM_HERE, base::Bind(&mojo::Closure::Run, |
| 56 | base::Owned(new mojo::Closure(quit_runnable))), |
| 57 | base::TimeDelta::FromMicroseconds(delay)); |
| 58 | } |
| 59 | |
Etienne Membrives | f1d4833 | 2015-02-06 11:35:08 +0100 | [diff] [blame] | 60 | mojo::python::PythonAsyncWaiter* NewAsyncWaiter() { |
| 61 | return new mojo::python::PythonAsyncWaiter( |
Etienne Membrives | 7349134 | 2015-01-16 23:45:50 +0100 | [diff] [blame] | 62 | mojo::Closure(QuitCurrentRunLoop::NewInstance())); |
| 63 | } |
| 64 | |
Etienne Membrives | f1d4833 | 2015-02-06 11:35:08 +0100 | [diff] [blame] | 65 | } // namespace content_handler |
Etienne Membrives | 7349134 | 2015-01-16 23:45:50 +0100 | [diff] [blame] | 66 | } // namespace python |
Etienne Membrives | f1d4833 | 2015-02-06 11:35:08 +0100 | [diff] [blame] | 67 | } // namespace services |