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