| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 """The metaclasses used by the mojo python bindings.""" | 5 """The metaclasses used by the mojo python bindings.""" |
| 6 | 6 |
| 7 import itertools | 7 import itertools |
| 8 import logging | 8 import logging |
| 9 import sys | 9 import sys |
| 10 | 10 |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 for base in bases: | 175 for base in bases: |
| 176 if isinstance(base, mcs): | 176 if isinstance(base, mcs): |
| 177 return type.__new__(mcs, name, bases, dictionary) | 177 return type.__new__(mcs, name, bases, dictionary) |
| 178 | 178 |
| 179 descriptor = dictionary.pop('DESCRIPTOR', {}) | 179 descriptor = dictionary.pop('DESCRIPTOR', {}) |
| 180 | 180 |
| 181 methods = [_MethodDescriptor(x) for x in descriptor.get('methods', [])] | 181 methods = [_MethodDescriptor(x) for x in descriptor.get('methods', [])] |
| 182 for method in methods: | 182 for method in methods: |
| 183 dictionary[method.name] = _NotImplemented | 183 dictionary[method.name] = _NotImplemented |
| 184 client_class_getter = descriptor.get('client', None) | 184 client_class_getter = descriptor.get('client', None) |
| 185 fully_qualified_name = descriptor['fully_qualified_name'] |
| 185 | 186 |
| 186 interface_manager = InterfaceManager(name, methods, client_class_getter) | 187 interface_manager = InterfaceManager(fully_qualified_name, methods, |
| 188 client_class_getter) |
| 187 dictionary.update({ | 189 dictionary.update({ |
| 188 'client': None, | 190 'client': None, |
| 189 'manager': None, | 191 'manager': None, |
| 190 '_interface_manager': interface_manager, | 192 '_interface_manager': interface_manager, |
| 191 }) | 193 }) |
| 192 | 194 |
| 193 interface_class = type.__new__(mcs, name, bases, dictionary) | 195 interface_class = type.__new__(mcs, name, bases, dictionary) |
| 194 interface_manager.interface_class = interface_class | 196 interface_manager.interface_class = interface_class |
| 195 return interface_class | 197 return interface_class |
| 196 | 198 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 224 self._handle = handle | 226 self._handle = handle |
| 225 | 227 |
| 226 def IsPending(self): | 228 def IsPending(self): |
| 227 return self._handle.IsValid() | 229 return self._handle.IsValid() |
| 228 | 230 |
| 229 def PassMessagePipe(self): | 231 def PassMessagePipe(self): |
| 230 result = self._handle | 232 result = self._handle |
| 231 self._handle = None | 233 self._handle = None |
| 232 return result | 234 return result |
| 233 | 235 |
| 236 def Bind(self, impl): |
| 237 type(impl).manager.Bind(impl, self.PassMessagePipe()) |
| 238 |
| 234 | 239 |
| 235 class InterfaceManager(object): | 240 class InterfaceManager(object): |
| 236 """ | 241 """ |
| 237 Manager for an interface class. The manager contains the operation that allows | 242 Manager for an interface class. The manager contains the operation that allows |
| 238 to bind an implementation to a pipe, or to generate a proxy for an interface | 243 to bind an implementation to a pipe, or to generate a proxy for an interface |
| 239 over a pipe. | 244 over a pipe. |
| 240 """ | 245 """ |
| 241 | 246 |
| 242 def __init__(self, name, methods, client_class_getter): | 247 def __init__(self, name, methods, client_class_getter): |
| 243 self.name = name | 248 self.name = name |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 retainer = _Retainer(router) | 280 retainer = _Retainer(router) |
| 276 def Cleanup(_): | 281 def Cleanup(_): |
| 277 retainer.release() | 282 retainer.release() |
| 278 error_handler.AddCallback(Cleanup) | 283 error_handler.AddCallback(Cleanup) |
| 279 | 284 |
| 280 if self.client_manager: | 285 if self.client_manager: |
| 281 impl.client = self.client_manager._InternalProxy(router, error_handler) | 286 impl.client = self.client_manager._InternalProxy(router, error_handler) |
| 282 | 287 |
| 283 # Give an instance manager to the implementation to allow it to close | 288 # Give an instance manager to the implementation to allow it to close |
| 284 # the connection. | 289 # the connection. |
| 285 impl.manager = InstanceManager(router) | 290 impl.manager = InstanceManager(router, error_handler) |
| 286 | 291 |
| 287 router.Start() | 292 router.Start() |
| 288 | 293 |
| 289 def _InternalProxy(self, router, error_handler): | 294 def _InternalProxy(self, router, error_handler): |
| 290 if not self._proxy_class: | 295 if not self._proxy_class: |
| 291 dictionary = { | 296 dictionary = { |
| 292 '__module__': __name__, | 297 '__module__': __name__, |
| 293 '__init__': _ProxyInit, | 298 '__init__': _ProxyInit, |
| 294 } | 299 } |
| 295 if self.client_manager: | 300 if self.client_manager: |
| 296 dictionary['client'] = property(_ProxyGetClient, _ProxySetClient) | 301 dictionary['client'] = property(_ProxyGetClient, _ProxySetClient) |
| 297 dictionary['manager'] = None | 302 dictionary['manager'] = None |
| 298 dictionary['_client_manager'] = self.client_manager | 303 dictionary['_client_manager'] = self.client_manager |
| 299 for method in self.methods: | 304 for method in self.methods: |
| 300 dictionary[method.name] = _ProxyMethodCall(method) | 305 dictionary[method.name] = _ProxyMethodCall(method) |
| 301 self._proxy_class = type('%sProxy' % self.name, | 306 self._proxy_class = type('%sProxy' % self.name, |
| 302 (self.interface_class, InterfaceProxy), | 307 (self.interface_class, InterfaceProxy), |
| 303 dictionary) | 308 dictionary) |
| 304 | 309 |
| 305 proxy = self._proxy_class(router, error_handler) | 310 proxy = self._proxy_class(router, error_handler) |
| 306 # Give an instance manager to the proxy to allow to close the connection. | 311 # Give an instance manager to the proxy to allow to close the connection. |
| 307 proxy.manager = InstanceManager(router) | 312 proxy.manager = InstanceManager(router, error_handler) |
| 308 return proxy | 313 return proxy |
| 309 | 314 |
| 310 def _Stub(self, impl): | 315 def _Stub(self, impl): |
| 311 if not self._stub_class: | 316 if not self._stub_class: |
| 312 accept_method = _StubAccept(self.methods) | 317 accept_method = _StubAccept(self.methods) |
| 313 dictionary = { | 318 dictionary = { |
| 314 '__module__': __name__, | 319 '__module__': __name__, |
| 315 '__init__': _StubInit, | 320 '__init__': _StubInit, |
| 316 'Accept': accept_method, | 321 'Accept': accept_method, |
| 317 'AcceptWithResponder': accept_method, | 322 'AcceptWithResponder': accept_method, |
| 318 } | 323 } |
| 319 self._stub_class = type('%sStub' % self.name, | 324 self._stub_class = type('%sStub' % self.name, |
| 320 (messaging.MessageReceiverWithResponder,), | 325 (messaging.MessageReceiverWithResponder,), |
| 321 dictionary) | 326 dictionary) |
| 322 return self._stub_class(impl) | 327 return self._stub_class(impl) |
| 323 | 328 |
| 324 | 329 |
| 325 class InstanceManager(object): | 330 class InstanceManager(object): |
| 326 """ | 331 """ |
| 327 Manager for the implementation of an interface or a proxy. The manager allows | 332 Manager for the implementation of an interface or a proxy. The manager allows |
| 328 to control the connection over the pipe. | 333 to control the connection over the pipe. |
| 329 """ | 334 """ |
| 330 def __init__(self, router): | 335 def __init__(self, router, error_handler): |
| 331 self.router = router | 336 self._router = router |
| 337 self._error_handler = error_handler |
| 332 | 338 |
| 333 def Close(self): | 339 def Close(self): |
| 334 self.router.Close() | 340 self._router.Close() |
| 335 | 341 |
| 336 def PassMessagePipe(self): | 342 def PassMessagePipe(self): |
| 337 return self.router.PassMessagePipe() | 343 return self._router.PassMessagePipe() |
| 344 |
| 345 def AddOnErrorCallback(self, callback): |
| 346 self._error_handler.AddCallback(lambda _: callback()) |
| 338 | 347 |
| 339 | 348 |
| 340 class _MethodDescriptor(object): | 349 class _MethodDescriptor(object): |
| 341 def __init__(self, descriptor): | 350 def __init__(self, descriptor): |
| 342 self.name = descriptor['name'] | 351 self.name = descriptor['name'] |
| 343 self.ordinal = descriptor['ordinal'] | 352 self.ordinal = descriptor['ordinal'] |
| 344 self.parameters_struct = _ConstructParameterStruct( | 353 self.parameters_struct = _ConstructParameterStruct( |
| 345 descriptor['parameters'], self.name, "Parameters") | 354 descriptor['parameters'], self.name, "Parameters") |
| 346 self.response_struct = _ConstructParameterStruct( | 355 self.response_struct = _ConstructParameterStruct( |
| 347 descriptor.get('responses'), self.name, "Responses") | 356 descriptor.get('responses'), self.name, "Responses") |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 logging.warning( | 569 logging.warning( |
| 561 'Error occured in accept method. Connection will be closed.') | 570 'Error occured in accept method. Connection will be closed.') |
| 562 if self.impl.manager: | 571 if self.impl.manager: |
| 563 self.impl.manager.Close() | 572 self.impl.manager.Close() |
| 564 return False | 573 return False |
| 565 return Accept | 574 return Accept |
| 566 | 575 |
| 567 | 576 |
| 568 def _NotImplemented(*_1, **_2): | 577 def _NotImplemented(*_1, **_2): |
| 569 raise NotImplementedError() | 578 raise NotImplementedError() |
| OLD | NEW |