blob: 8b4bed2874206eda76320ff7b3cd27ad597e1a14 [file] [log] [blame]
Etienne Membrives73491342015-01-16 23:45:50 +01001// 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 Robinson94ade6b2015-08-25 13:02:06 -070011#include "mojo/message_pump/message_pump_mojo.h"
Etienne Membrives73491342015-01-16 23:45:50 +010012#include "mojo/public/python/src/common.h"
13
14namespace {
15class 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 Membrivesf1d48332015-02-06 11:35:08 +010027namespace services {
Etienne Membrives73491342015-01-16 23:45:50 +010028namespace python {
Etienne Membrivesf1d48332015-02-06 11:35:08 +010029namespace content_handler {
Etienne Membrives73491342015-01-16 23:45:50 +010030
Etienne Membrivesf1d48332015-02-06 11:35:08 +010031PythonRunLoop::PythonRunLoop()
32 : loop_(mojo::common::MessagePumpMojo::Create()) {
Etienne Membrives73491342015-01-16 23:45:50 +010033}
34
35PythonRunLoop::~PythonRunLoop() {
36}
37
38void PythonRunLoop::Run() {
39 loop_.Run();
40}
41
42void PythonRunLoop::RunUntilIdle() {
43 loop_.RunUntilIdle();
44}
45
46void PythonRunLoop::Quit() {
47 loop_.Quit();
48}
49
50void PythonRunLoop::PostDelayedTask(PyObject* callable, MojoTimeTicks delay) {
Etienne Membrivesf1d48332015-02-06 11:35:08 +010051 mojo::Closure::Runnable* quit_runnable =
52 mojo::python::NewRunnableFromCallable(callable, loop_.QuitClosure());
Etienne Membrives73491342015-01-16 23:45:50 +010053
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 Membrivesf1d48332015-02-06 11:35:08 +010060mojo::python::PythonAsyncWaiter* NewAsyncWaiter() {
61 return new mojo::python::PythonAsyncWaiter(
Etienne Membrives73491342015-01-16 23:45:50 +010062 mojo::Closure(QuitCurrentRunLoop::NewInstance()));
63}
64
Etienne Membrivesf1d48332015-02-06 11:35:08 +010065} // namespace content_handler
Etienne Membrives73491342015-01-16 23:45:50 +010066} // namespace python
Etienne Membrivesf1d48332015-02-06 11:35:08 +010067} // namespace services