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