Chromium Code Reviews| Index: examples/python/__mojo__.py |
| diff --git a/examples/python/__mojo__.py b/examples/python/__mojo__.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..15970efbe1d0892419c6cb662e7387c9d6b00716 |
| --- /dev/null |
| +++ b/examples/python/__mojo__.py |
| @@ -0,0 +1,64 @@ |
| +# Copyright 2015 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""Example python application implementing the Echo service.""" |
| + |
| +import logging |
| + |
| +import application_mojom |
| +import example_service_mojom |
| +import service_provider_mojom |
| +import shell_mojom |
| + |
| +import mojo_system |
| + |
| +class ApplicationImpl(application_mojom.Application): |
| + def __init__(self): |
| + self._providers = [] |
| + |
| + def Initialize(self, args): |
| + pass |
| + |
| + def AcceptConnection(self, requestor_url, provider): |
| + # We keep a reference to ServiceProviderImpl to ensure neither it nor |
| + # provider gets garbage collected. |
| + service_provider = ServiceProviderImpl(provider) |
| + service_provider.AddService(ExampleServiceImpl.manager.name, |
|
qsr
2015/01/14 18:08:41
You do not need to pass 2 parameters here, the cla
etiennej
2015/01/15 00:03:07
Done.
|
| + ExampleServiceImpl) |
| + provider.client = service_provider |
| + self._providers.append(provider) |
| + |
| + |
| +class ServiceProviderImpl(service_provider_mojom.ServiceProvider): |
| + def __init__(self, provider): |
| + self._provider = provider |
| + self._name_to_service_connector = {} |
| + |
| + def AddService(self, name, service_class): |
| + self._name_to_service_connector[name] = service_class |
| + |
| + def ConnectToService(self, interface_name, pipe): |
| + if interface_name in self._name_to_service_connector: |
| + service = self._name_to_service_connector[interface_name] |
| + service.manager.Bind(service(), pipe) |
| + else: |
| + logging.error("Unable to find service " + interface_name) |
| + |
| + |
| +class ExampleServiceImpl(example_service_mojom.ExampleService): |
| + def Ping(self, ping_value): |
| + self.client.Pong(ping_value) |
| + |
| + def RunCallback(self): |
| + return {} |
| + |
| +def MojoMain(shell_handle): |
| + """MojoMain is the entry point for a python Mojo module.""" |
| + loop = mojo_system.RunLoop() |
| + |
| + shell = shell_mojom.Shell.manager.Proxy(mojo_system.Handle(shell_handle)) |
| + shell.client = ApplicationImpl() |
| + shell.manager.AddOnErrorCallback(loop.Quit) |
| + |
| + loop.Run() |