| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Example python application implementing the Echo service.""" | 5 """Example python application implementing the Echo service.""" |
| 6 | 6 |
| 7 import logging | 7 import logging |
| 8 | 8 |
| 9 import application_mojom | 9 import application_mojom |
| 10 import example_service_mojom | 10 import example_service_mojom |
| 11 import service_provider_mojom | 11 import service_provider_mojom |
| 12 import shell_mojom | 12 import shell_mojom |
| 13 | 13 |
| 14 import mojo_system | 14 import mojo_system |
| 15 | 15 |
| 16 class ApplicationImpl(application_mojom.Application): | 16 class ApplicationImpl(application_mojom.Application): |
| 17 def __init__(self): | 17 def __init__(self, app_request_handle): |
| 18 self._providers = [] | 18 self._providers = [] |
| 19 application_mojom.Application.manager.Bind(self, app_request_handle) |
| 19 | 20 |
| 20 def Initialize(self, args): | 21 def Initialize(self, shell, args): |
| 21 pass | 22 self.shell = shell |
| 22 | 23 |
| 23 def AcceptConnection(self, requestor_url, services, exposed_services): | 24 def AcceptConnection(self, requestor_url, services, exposed_services): |
| 24 # We keep a reference to ServiceProviderImpl to ensure neither it nor | 25 # We keep a reference to ServiceProviderImpl to ensure neither it nor |
| 25 # provider gets garbage collected. | 26 # provider gets garbage collected. |
| 26 service_provider = ServiceProviderImpl(services) | 27 service_provider = ServiceProviderImpl(services) |
| 27 service_provider.AddService(ExampleServiceImpl) | 28 service_provider.AddService(ExampleServiceImpl) |
| 28 services.Bind(service_provider) | 29 services.Bind(service_provider) |
| 29 self._providers.append(services) | 30 self._providers.append(services) |
| 31 # TODO(qsr): Handle exposed_services |
| 30 | 32 |
| 31 | 33 |
| 32 class ServiceProviderImpl(service_provider_mojom.ServiceProvider): | 34 class ServiceProviderImpl(service_provider_mojom.ServiceProvider): |
| 33 def __init__(self, provider): | 35 def __init__(self, provider): |
| 34 self._provider = provider | 36 self._provider = provider |
| 35 self._name_to_service_connector = {} | 37 self._name_to_service_connector = {} |
| 36 | 38 |
| 37 def AddService(self, service_class): | 39 def AddService(self, service_class): |
| 38 self._name_to_service_connector[service_class.manager.name] = service_class | 40 self._name_to_service_connector[service_class.manager.name] = service_class |
| 39 | 41 |
| 40 def ConnectToService(self, interface_name, pipe): | 42 def ConnectToService(self, interface_name, pipe): |
| 41 if interface_name in self._name_to_service_connector: | 43 if interface_name in self._name_to_service_connector: |
| 42 service = self._name_to_service_connector[interface_name] | 44 service = self._name_to_service_connector[interface_name] |
| 43 service.manager.Bind(service(), pipe) | 45 service.manager.Bind(service(), pipe) |
| 44 else: | 46 else: |
| 45 logging.error("Unable to find service " + interface_name) | 47 logging.error("Unable to find service " + interface_name) |
| 46 | 48 |
| 47 | 49 |
| 48 class ExampleServiceImpl(example_service_mojom.ExampleService): | 50 class ExampleServiceImpl(example_service_mojom.ExampleService): |
| 49 def Ping(self, ping_value): | 51 def Ping(self, ping_value): |
| 50 self.client.Pong(ping_value) | 52 self.client.Pong(ping_value) |
| 51 | 53 |
| 52 def RunCallback(self): | 54 def RunCallback(self): |
| 53 return {} | 55 return {} |
| 54 | 56 |
| 55 def MojoMain(shell_handle): | 57 def MojoMain(app_request_handle): |
| 56 """MojoMain is the entry point for a python Mojo module.""" | 58 """MojoMain is the entry point for a python Mojo module.""" |
| 57 loop = mojo_system.RunLoop() | 59 loop = mojo_system.RunLoop() |
| 58 | 60 |
| 59 shell = shell_mojom.Shell.manager.Proxy(mojo_system.Handle(shell_handle)) | 61 application = ApplicationImpl(mojo_system.Handle(app_request_handle)) |
| 60 shell.client = ApplicationImpl() | 62 application.manager.AddOnErrorCallback(loop.Quit) |
| 61 shell.manager.AddOnErrorCallback(loop.Quit) | |
| 62 | 63 |
| 63 loop.Run() | 64 loop.Run() |
| OLD | NEW |