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 library http_server_mojom; | 5 library http_server_mojom; |
6 import 'dart:async'; | 6 import 'dart:async'; |
7 import 'package:mojo/bindings.dart' as bindings; | 7 import 'package:mojo/bindings.dart' as bindings; |
8 import 'package:mojo/core.dart' as core; | 8 import 'package:mojo/core.dart' as core; |
9 import 'package:mojo/mojo/bindings/types/service_describer.mojom.dart' as servic
e_describer; | 9 import 'package:mojo/mojo/bindings/types/service_describer.mojom.dart' as servic
e_describer; |
10 import 'package:mojo_services/http_server/http_request.mojom.dart' as http_reque
st_mojom; | 10 import 'package:mojo_services/http_server/http_request.mojom.dart' as http_reque
st_mojom; |
11 import 'package:mojo_services/http_server/http_response.mojom.dart' as http_resp
onse_mojom; | 11 import 'package:mojo_services/http_server/http_response.mojom.dart' as http_resp
onse_mojom; |
12 | 12 |
13 | 13 |
14 | 14 |
15 class _HttpServerSetHandlerParams extends bindings.Struct { | 15 class _HttpServerSetHandlerParams extends bindings.Struct { |
16 static const List<bindings.StructDataHeader> kVersions = const [ | 16 static const List<bindings.StructDataHeader> kVersions = const [ |
17 const bindings.StructDataHeader(24, 0) | 17 const bindings.StructDataHeader(24, 0) |
18 ]; | 18 ]; |
19 String pattern = null; | 19 String pattern = null; |
20 HttpHandlerInterface handler = null; | 20 HttpHandlerInterface handler = null; |
21 | 21 |
22 _HttpServerSetHandlerParams() : super(kVersions.last.size); | 22 _HttpServerSetHandlerParams() : super(kVersions.last.size); |
23 | 23 |
| 24 _HttpServerSetHandlerParams.init( |
| 25 String this.pattern, |
| 26 HttpHandlerInterface this.handler |
| 27 ) : super(kVersions.last.size); |
| 28 |
24 static _HttpServerSetHandlerParams deserialize(bindings.Message message) { | 29 static _HttpServerSetHandlerParams deserialize(bindings.Message message) { |
25 var decoder = new bindings.Decoder(message); | 30 var decoder = new bindings.Decoder(message); |
26 var result = decode(decoder); | 31 var result = decode(decoder); |
27 if (decoder.excessHandles != null) { | 32 if (decoder.excessHandles != null) { |
28 decoder.excessHandles.forEach((h) => h.close()); | 33 decoder.excessHandles.forEach((h) => h.close()); |
29 } | 34 } |
30 return result; | 35 return result; |
31 } | 36 } |
32 | 37 |
33 static _HttpServerSetHandlerParams decode(bindings.Decoder decoder0) { | 38 static _HttpServerSetHandlerParams decode(bindings.Decoder decoder0) { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 | 102 |
98 | 103 |
99 class HttpServerSetHandlerResponseParams extends bindings.Struct { | 104 class HttpServerSetHandlerResponseParams extends bindings.Struct { |
100 static const List<bindings.StructDataHeader> kVersions = const [ | 105 static const List<bindings.StructDataHeader> kVersions = const [ |
101 const bindings.StructDataHeader(16, 0) | 106 const bindings.StructDataHeader(16, 0) |
102 ]; | 107 ]; |
103 bool success = false; | 108 bool success = false; |
104 | 109 |
105 HttpServerSetHandlerResponseParams() : super(kVersions.last.size); | 110 HttpServerSetHandlerResponseParams() : super(kVersions.last.size); |
106 | 111 |
| 112 HttpServerSetHandlerResponseParams.init( |
| 113 bool this.success |
| 114 ) : super(kVersions.last.size); |
| 115 |
107 static HttpServerSetHandlerResponseParams deserialize(bindings.Message message
) { | 116 static HttpServerSetHandlerResponseParams deserialize(bindings.Message message
) { |
108 var decoder = new bindings.Decoder(message); | 117 var decoder = new bindings.Decoder(message); |
109 var result = decode(decoder); | 118 var result = decode(decoder); |
110 if (decoder.excessHandles != null) { | 119 if (decoder.excessHandles != null) { |
111 decoder.excessHandles.forEach((h) => h.close()); | 120 decoder.excessHandles.forEach((h) => h.close()); |
112 } | 121 } |
113 return result; | 122 return result; |
114 } | 123 } |
115 | 124 |
116 static HttpServerSetHandlerResponseParams decode(bindings.Decoder decoder0) { | 125 static HttpServerSetHandlerResponseParams decode(bindings.Decoder decoder0) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 } | 177 } |
169 | 178 |
170 | 179 |
171 class _HttpServerGetPortParams extends bindings.Struct { | 180 class _HttpServerGetPortParams extends bindings.Struct { |
172 static const List<bindings.StructDataHeader> kVersions = const [ | 181 static const List<bindings.StructDataHeader> kVersions = const [ |
173 const bindings.StructDataHeader(8, 0) | 182 const bindings.StructDataHeader(8, 0) |
174 ]; | 183 ]; |
175 | 184 |
176 _HttpServerGetPortParams() : super(kVersions.last.size); | 185 _HttpServerGetPortParams() : super(kVersions.last.size); |
177 | 186 |
| 187 _HttpServerGetPortParams.init( |
| 188 ) : super(kVersions.last.size); |
| 189 |
178 static _HttpServerGetPortParams deserialize(bindings.Message message) { | 190 static _HttpServerGetPortParams deserialize(bindings.Message message) { |
179 var decoder = new bindings.Decoder(message); | 191 var decoder = new bindings.Decoder(message); |
180 var result = decode(decoder); | 192 var result = decode(decoder); |
181 if (decoder.excessHandles != null) { | 193 if (decoder.excessHandles != null) { |
182 decoder.excessHandles.forEach((h) => h.close()); | 194 decoder.excessHandles.forEach((h) => h.close()); |
183 } | 195 } |
184 return result; | 196 return result; |
185 } | 197 } |
186 | 198 |
187 static _HttpServerGetPortParams decode(bindings.Decoder decoder0) { | 199 static _HttpServerGetPortParams decode(bindings.Decoder decoder0) { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 | 239 |
228 | 240 |
229 class HttpServerGetPortResponseParams extends bindings.Struct { | 241 class HttpServerGetPortResponseParams extends bindings.Struct { |
230 static const List<bindings.StructDataHeader> kVersions = const [ | 242 static const List<bindings.StructDataHeader> kVersions = const [ |
231 const bindings.StructDataHeader(16, 0) | 243 const bindings.StructDataHeader(16, 0) |
232 ]; | 244 ]; |
233 int port = 0; | 245 int port = 0; |
234 | 246 |
235 HttpServerGetPortResponseParams() : super(kVersions.last.size); | 247 HttpServerGetPortResponseParams() : super(kVersions.last.size); |
236 | 248 |
| 249 HttpServerGetPortResponseParams.init( |
| 250 int this.port |
| 251 ) : super(kVersions.last.size); |
| 252 |
237 static HttpServerGetPortResponseParams deserialize(bindings.Message message) { | 253 static HttpServerGetPortResponseParams deserialize(bindings.Message message) { |
238 var decoder = new bindings.Decoder(message); | 254 var decoder = new bindings.Decoder(message); |
239 var result = decode(decoder); | 255 var result = decode(decoder); |
240 if (decoder.excessHandles != null) { | 256 if (decoder.excessHandles != null) { |
241 decoder.excessHandles.forEach((h) => h.close()); | 257 decoder.excessHandles.forEach((h) => h.close()); |
242 } | 258 } |
243 return result; | 259 return result; |
244 } | 260 } |
245 | 261 |
246 static HttpServerGetPortResponseParams decode(bindings.Decoder decoder0) { | 262 static HttpServerGetPortResponseParams decode(bindings.Decoder decoder0) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 | 315 |
300 | 316 |
301 class _HttpHandlerHandleRequestParams extends bindings.Struct { | 317 class _HttpHandlerHandleRequestParams extends bindings.Struct { |
302 static const List<bindings.StructDataHeader> kVersions = const [ | 318 static const List<bindings.StructDataHeader> kVersions = const [ |
303 const bindings.StructDataHeader(16, 0) | 319 const bindings.StructDataHeader(16, 0) |
304 ]; | 320 ]; |
305 http_request_mojom.HttpRequest request = null; | 321 http_request_mojom.HttpRequest request = null; |
306 | 322 |
307 _HttpHandlerHandleRequestParams() : super(kVersions.last.size); | 323 _HttpHandlerHandleRequestParams() : super(kVersions.last.size); |
308 | 324 |
| 325 _HttpHandlerHandleRequestParams.init( |
| 326 http_request_mojom.HttpRequest this.request |
| 327 ) : super(kVersions.last.size); |
| 328 |
309 static _HttpHandlerHandleRequestParams deserialize(bindings.Message message) { | 329 static _HttpHandlerHandleRequestParams deserialize(bindings.Message message) { |
310 var decoder = new bindings.Decoder(message); | 330 var decoder = new bindings.Decoder(message); |
311 var result = decode(decoder); | 331 var result = decode(decoder); |
312 if (decoder.excessHandles != null) { | 332 if (decoder.excessHandles != null) { |
313 decoder.excessHandles.forEach((h) => h.close()); | 333 decoder.excessHandles.forEach((h) => h.close()); |
314 } | 334 } |
315 return result; | 335 return result; |
316 } | 336 } |
317 | 337 |
318 static _HttpHandlerHandleRequestParams decode(bindings.Decoder decoder0) { | 338 static _HttpHandlerHandleRequestParams decode(bindings.Decoder decoder0) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 | 391 |
372 | 392 |
373 class HttpHandlerHandleRequestResponseParams extends bindings.Struct { | 393 class HttpHandlerHandleRequestResponseParams extends bindings.Struct { |
374 static const List<bindings.StructDataHeader> kVersions = const [ | 394 static const List<bindings.StructDataHeader> kVersions = const [ |
375 const bindings.StructDataHeader(16, 0) | 395 const bindings.StructDataHeader(16, 0) |
376 ]; | 396 ]; |
377 http_response_mojom.HttpResponse response = null; | 397 http_response_mojom.HttpResponse response = null; |
378 | 398 |
379 HttpHandlerHandleRequestResponseParams() : super(kVersions.last.size); | 399 HttpHandlerHandleRequestResponseParams() : super(kVersions.last.size); |
380 | 400 |
| 401 HttpHandlerHandleRequestResponseParams.init( |
| 402 http_response_mojom.HttpResponse this.response |
| 403 ) : super(kVersions.last.size); |
| 404 |
381 static HttpHandlerHandleRequestResponseParams deserialize(bindings.Message mes
sage) { | 405 static HttpHandlerHandleRequestResponseParams deserialize(bindings.Message mes
sage) { |
382 var decoder = new bindings.Decoder(message); | 406 var decoder = new bindings.Decoder(message); |
383 var result = decode(decoder); | 407 var result = decode(decoder); |
384 if (decoder.excessHandles != null) { | 408 if (decoder.excessHandles != null) { |
385 decoder.excessHandles.forEach((h) => h.close()); | 409 decoder.excessHandles.forEach((h) => h.close()); |
386 } | 410 } |
387 return result; | 411 return result; |
388 } | 412 } |
389 | 413 |
390 static HttpHandlerHandleRequestResponseParams decode(bindings.Decoder decoder0
) { | 414 static HttpHandlerHandleRequestResponseParams decode(bindings.Decoder decoder0
) { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 Map toJson() { | 462 Map toJson() { |
439 throw new bindings.MojoCodecError( | 463 throw new bindings.MojoCodecError( |
440 'Object containing handles cannot be encoded to JSON.'); | 464 'Object containing handles cannot be encoded to JSON.'); |
441 } | 465 } |
442 } | 466 } |
443 | 467 |
444 const int _httpServerMethodSetHandlerName = 0; | 468 const int _httpServerMethodSetHandlerName = 0; |
445 const int _httpServerMethodGetPortName = 1; | 469 const int _httpServerMethodGetPortName = 1; |
446 | 470 |
447 class _HttpServerServiceDescription implements service_describer.ServiceDescript
ion { | 471 class _HttpServerServiceDescription implements service_describer.ServiceDescript
ion { |
448 dynamic getTopLevelInterface([Function responseFactory]) => | 472 void getTopLevelInterface(Function responder) { |
449 responseFactory(null); | 473 responder(null); |
| 474 } |
450 | 475 |
451 dynamic getTypeDefinition(String typeKey, [Function responseFactory]) => | 476 void getTypeDefinition(String typeKey, Function responder) { |
452 responseFactory(null); | 477 responder(null); |
| 478 } |
453 | 479 |
454 dynamic getAllTypeDefinitions([Function responseFactory]) => | 480 void getAllTypeDefinitions(Function responder) { |
455 responseFactory(null); | 481 responder(null); |
| 482 } |
456 } | 483 } |
457 | 484 |
458 abstract class HttpServer { | 485 abstract class HttpServer { |
459 static const String serviceName = null; | 486 static const String serviceName = null; |
460 | 487 |
461 static service_describer.ServiceDescription _cachedServiceDescription; | 488 static service_describer.ServiceDescription _cachedServiceDescription; |
462 static service_describer.ServiceDescription get serviceDescription { | 489 static service_describer.ServiceDescription get serviceDescription { |
463 if (_cachedServiceDescription == null) { | 490 if (_cachedServiceDescription == null) { |
464 _cachedServiceDescription = new _HttpServerServiceDescription(); | 491 _cachedServiceDescription = new _HttpServerServiceDescription(); |
465 } | 492 } |
466 return _cachedServiceDescription; | 493 return _cachedServiceDescription; |
467 } | 494 } |
468 | 495 |
469 static HttpServerProxy connectToService( | 496 static HttpServerProxy connectToService( |
470 bindings.ServiceConnector s, String url, [String serviceName]) { | 497 bindings.ServiceConnector s, String url, [String serviceName]) { |
471 HttpServerProxy p = new HttpServerProxy.unbound(); | 498 HttpServerProxy p = new HttpServerProxy.unbound(); |
472 String name = serviceName ?? HttpServer.serviceName; | 499 String name = serviceName ?? HttpServer.serviceName; |
473 if ((name == null) || name.isEmpty) { | 500 if ((name == null) || name.isEmpty) { |
474 throw new core.MojoApiError( | 501 throw new core.MojoApiError( |
475 "If an interface has no ServiceName, then one must be provided."); | 502 "If an interface has no ServiceName, then one must be provided."); |
476 } | 503 } |
477 s.connectToService(url, p, name); | 504 s.connectToService(url, p, name); |
478 return p; | 505 return p; |
479 } | 506 } |
480 dynamic setHandler(String pattern,HttpHandlerInterface handler,[Function respo
nseFactory = null]); | 507 void setHandler(String pattern,HttpHandlerInterface handler,void callback(bool
success)); |
481 dynamic getPort([Function responseFactory = null]); | 508 void getPort(void callback(int port)); |
482 } | 509 } |
483 | 510 |
484 abstract class HttpServerInterface | 511 abstract class HttpServerInterface |
485 implements bindings.MojoInterface<HttpServer>, | 512 implements bindings.MojoInterface<HttpServer>, |
486 HttpServer { | 513 HttpServer { |
487 factory HttpServerInterface([HttpServer impl]) => | 514 factory HttpServerInterface([HttpServer impl]) => |
488 new HttpServerStub.unbound(impl); | 515 new HttpServerStub.unbound(impl); |
489 | 516 |
490 factory HttpServerInterface.fromEndpoint( | 517 factory HttpServerInterface.fromEndpoint( |
491 core.MojoMessagePipeEndpoint endpoint, | 518 core.MojoMessagePipeEndpoint endpoint, |
(...skipping 29 matching lines...) Expand all Loading... |
521 | 548 |
522 void handleResponse(bindings.ServiceMessage message) { | 549 void handleResponse(bindings.ServiceMessage message) { |
523 switch (message.header.type) { | 550 switch (message.header.type) { |
524 case _httpServerMethodSetHandlerName: | 551 case _httpServerMethodSetHandlerName: |
525 var r = HttpServerSetHandlerResponseParams.deserialize( | 552 var r = HttpServerSetHandlerResponseParams.deserialize( |
526 message.payload); | 553 message.payload); |
527 if (!message.header.hasRequestId) { | 554 if (!message.header.hasRequestId) { |
528 proxyError("Expected a message with a valid request Id."); | 555 proxyError("Expected a message with a valid request Id."); |
529 return; | 556 return; |
530 } | 557 } |
531 Completer c = completerMap[message.header.requestId]; | 558 Function callback = callbackMap[message.header.requestId]; |
532 if (c == null) { | 559 if (callback == null) { |
533 proxyError( | 560 proxyError( |
534 "Message had unknown request Id: ${message.header.requestId}"); | 561 "Message had unknown request Id: ${message.header.requestId}"); |
535 return; | 562 return; |
536 } | 563 } |
537 completerMap.remove(message.header.requestId); | 564 callbackMap.remove(message.header.requestId); |
538 if (c.isCompleted) { | 565 callback(r.success ); |
539 proxyError("Response completer already completed"); | |
540 return; | |
541 } | |
542 c.complete(r); | |
543 break; | 566 break; |
544 case _httpServerMethodGetPortName: | 567 case _httpServerMethodGetPortName: |
545 var r = HttpServerGetPortResponseParams.deserialize( | 568 var r = HttpServerGetPortResponseParams.deserialize( |
546 message.payload); | 569 message.payload); |
547 if (!message.header.hasRequestId) { | 570 if (!message.header.hasRequestId) { |
548 proxyError("Expected a message with a valid request Id."); | 571 proxyError("Expected a message with a valid request Id."); |
549 return; | 572 return; |
550 } | 573 } |
551 Completer c = completerMap[message.header.requestId]; | 574 Function callback = callbackMap[message.header.requestId]; |
552 if (c == null) { | 575 if (callback == null) { |
553 proxyError( | 576 proxyError( |
554 "Message had unknown request Id: ${message.header.requestId}"); | 577 "Message had unknown request Id: ${message.header.requestId}"); |
555 return; | 578 return; |
556 } | 579 } |
557 completerMap.remove(message.header.requestId); | 580 callbackMap.remove(message.header.requestId); |
558 if (c.isCompleted) { | 581 callback(r.port ); |
559 proxyError("Response completer already completed"); | |
560 return; | |
561 } | |
562 c.complete(r); | |
563 break; | 582 break; |
564 default: | 583 default: |
565 proxyError("Unexpected message type: ${message.header.type}"); | 584 proxyError("Unexpected message type: ${message.header.type}"); |
566 close(immediate: true); | 585 close(immediate: true); |
567 break; | 586 break; |
568 } | 587 } |
569 } | 588 } |
570 | 589 |
571 @override | 590 @override |
572 String toString() { | 591 String toString() { |
(...skipping 24 matching lines...) Expand all Loading... |
597 return newMockedProxy; | 616 return newMockedProxy; |
598 } | 617 } |
599 | 618 |
600 static HttpServerProxy newFromEndpoint( | 619 static HttpServerProxy newFromEndpoint( |
601 core.MojoMessagePipeEndpoint endpoint) { | 620 core.MojoMessagePipeEndpoint endpoint) { |
602 assert(endpoint.setDescription("For HttpServerProxy")); | 621 assert(endpoint.setDescription("For HttpServerProxy")); |
603 return new HttpServerProxy.fromEndpoint(endpoint); | 622 return new HttpServerProxy.fromEndpoint(endpoint); |
604 } | 623 } |
605 | 624 |
606 | 625 |
607 dynamic setHandler(String pattern,HttpHandlerInterface handler,[Function respo
nseFactory = null]) { | 626 void setHandler(String pattern,HttpHandlerInterface handler,void callback(bool
success)) { |
608 if (impl != null) { | 627 if (impl != null) { |
609 return new Future(() => impl.setHandler(pattern,handler,_HttpServerStubCon
trol._httpServerSetHandlerResponseParamsFactory)); | 628 impl.setHandler(pattern,handler,callback); |
| 629 return; |
610 } | 630 } |
611 var params = new _HttpServerSetHandlerParams(); | 631 var params = new _HttpServerSetHandlerParams(); |
612 params.pattern = pattern; | 632 params.pattern = pattern; |
613 params.handler = handler; | 633 params.handler = handler; |
614 return ctrl.sendMessageWithRequestId( | 634 ctrl.sendMessageWithRequestId( |
615 params, | 635 params, |
616 _httpServerMethodSetHandlerName, | 636 _httpServerMethodSetHandlerName, |
617 -1, | 637 -1, |
618 bindings.MessageHeader.kMessageExpectsResponse); | 638 bindings.MessageHeader.kMessageExpectsResponse, |
| 639 callback); |
619 } | 640 } |
620 dynamic getPort([Function responseFactory = null]) { | 641 void getPort(void callback(int port)) { |
621 if (impl != null) { | 642 if (impl != null) { |
622 return new Future(() => impl.getPort(_HttpServerStubControl._httpServerGet
PortResponseParamsFactory)); | 643 impl.getPort(callback); |
| 644 return; |
623 } | 645 } |
624 var params = new _HttpServerGetPortParams(); | 646 var params = new _HttpServerGetPortParams(); |
625 return ctrl.sendMessageWithRequestId( | 647 ctrl.sendMessageWithRequestId( |
626 params, | 648 params, |
627 _httpServerMethodGetPortName, | 649 _httpServerMethodGetPortName, |
628 -1, | 650 -1, |
629 bindings.MessageHeader.kMessageExpectsResponse); | 651 bindings.MessageHeader.kMessageExpectsResponse, |
| 652 callback); |
630 } | 653 } |
631 } | 654 } |
632 | 655 |
633 class _HttpServerStubControl | 656 class _HttpServerStubControl |
634 extends bindings.StubMessageHandler | 657 extends bindings.StubMessageHandler |
635 implements bindings.StubControl<HttpServer> { | 658 implements bindings.StubControl<HttpServer> { |
636 HttpServer _impl; | 659 HttpServer _impl; |
637 | 660 |
638 _HttpServerStubControl.fromEndpoint( | 661 _HttpServerStubControl.fromEndpoint( |
639 core.MojoMessagePipeEndpoint endpoint, [HttpServer impl]) | 662 core.MojoMessagePipeEndpoint endpoint, [HttpServer impl]) |
640 : super.fromEndpoint(endpoint, autoBegin: impl != null) { | 663 : super.fromEndpoint(endpoint, autoBegin: impl != null) { |
641 _impl = impl; | 664 _impl = impl; |
642 } | 665 } |
643 | 666 |
644 _HttpServerStubControl.fromHandle( | 667 _HttpServerStubControl.fromHandle( |
645 core.MojoHandle handle, [HttpServer impl]) | 668 core.MojoHandle handle, [HttpServer impl]) |
646 : super.fromHandle(handle, autoBegin: impl != null) { | 669 : super.fromHandle(handle, autoBegin: impl != null) { |
647 _impl = impl; | 670 _impl = impl; |
648 } | 671 } |
649 | 672 |
650 _HttpServerStubControl.unbound([this._impl]) : super.unbound(); | 673 _HttpServerStubControl.unbound([this._impl]) : super.unbound(); |
651 | 674 |
652 String get serviceName => HttpServer.serviceName; | 675 String get serviceName => HttpServer.serviceName; |
653 | 676 |
654 | 677 |
655 static HttpServerSetHandlerResponseParams _httpServerSetHandlerResponseParamsF
actory(bool success) { | 678 Function _httpServerSetHandlerResponseParamsResponder( |
656 var result = new HttpServerSetHandlerResponseParams(); | 679 int requestId) { |
657 result.success = success; | 680 return (bool success) { |
658 return result; | 681 var result = new HttpServerSetHandlerResponseParams(); |
| 682 result.success = success; |
| 683 sendResponse(buildResponseWithId( |
| 684 result, |
| 685 _httpServerMethodSetHandlerName, |
| 686 requestId, |
| 687 bindings.MessageHeader.kMessageIsResponse)); |
| 688 }; |
659 } | 689 } |
660 static HttpServerGetPortResponseParams _httpServerGetPortResponseParamsFactory
(int port) { | 690 Function _httpServerGetPortResponseParamsResponder( |
661 var result = new HttpServerGetPortResponseParams(); | 691 int requestId) { |
662 result.port = port; | 692 return (int port) { |
663 return result; | 693 var result = new HttpServerGetPortResponseParams(); |
| 694 result.port = port; |
| 695 sendResponse(buildResponseWithId( |
| 696 result, |
| 697 _httpServerMethodGetPortName, |
| 698 requestId, |
| 699 bindings.MessageHeader.kMessageIsResponse)); |
| 700 }; |
664 } | 701 } |
665 | 702 |
666 dynamic handleMessage(bindings.ServiceMessage message) { | 703 void handleMessage(bindings.ServiceMessage message) { |
667 if (bindings.ControlMessageHandler.isControlMessage(message)) { | 704 if (bindings.ControlMessageHandler.isControlMessage(message)) { |
668 return bindings.ControlMessageHandler.handleMessage(this, | 705 bindings.ControlMessageHandler.handleMessage( |
669 0, | 706 this, 0, message); |
670 message); | 707 return; |
671 } | 708 } |
672 if (_impl == null) { | 709 if (_impl == null) { |
673 throw new core.MojoApiError("$this has no implementation set"); | 710 throw new core.MojoApiError("$this has no implementation set"); |
674 } | 711 } |
675 switch (message.header.type) { | 712 switch (message.header.type) { |
676 case _httpServerMethodSetHandlerName: | 713 case _httpServerMethodSetHandlerName: |
677 var params = _HttpServerSetHandlerParams.deserialize( | 714 var params = _HttpServerSetHandlerParams.deserialize( |
678 message.payload); | 715 message.payload); |
679 var response = _impl.setHandler(params.pattern,params.handler,_httpServe
rSetHandlerResponseParamsFactory); | 716 _impl.setHandler(params.pattern, params.handler, _httpServerSetHandlerRe
sponseParamsResponder(message.header.requestId)); |
680 if (response is Future) { | |
681 return response.then((response) { | |
682 if (response != null) { | |
683 return buildResponseWithId( | |
684 response, | |
685 _httpServerMethodSetHandlerName, | |
686 message.header.requestId, | |
687 bindings.MessageHeader.kMessageIsResponse); | |
688 } | |
689 }); | |
690 } else if (response != null) { | |
691 return buildResponseWithId( | |
692 response, | |
693 _httpServerMethodSetHandlerName, | |
694 message.header.requestId, | |
695 bindings.MessageHeader.kMessageIsResponse); | |
696 } | |
697 break; | 717 break; |
698 case _httpServerMethodGetPortName: | 718 case _httpServerMethodGetPortName: |
699 var response = _impl.getPort(_httpServerGetPortResponseParamsFactory); | 719 _impl.getPort(_httpServerGetPortResponseParamsResponder(message.header.r
equestId)); |
700 if (response is Future) { | |
701 return response.then((response) { | |
702 if (response != null) { | |
703 return buildResponseWithId( | |
704 response, | |
705 _httpServerMethodGetPortName, | |
706 message.header.requestId, | |
707 bindings.MessageHeader.kMessageIsResponse); | |
708 } | |
709 }); | |
710 } else if (response != null) { | |
711 return buildResponseWithId( | |
712 response, | |
713 _httpServerMethodGetPortName, | |
714 message.header.requestId, | |
715 bindings.MessageHeader.kMessageIsResponse); | |
716 } | |
717 break; | 720 break; |
718 default: | 721 default: |
719 throw new bindings.MojoCodecError("Unexpected message name"); | 722 throw new bindings.MojoCodecError("Unexpected message name"); |
720 break; | 723 break; |
721 } | 724 } |
722 return null; | |
723 } | 725 } |
724 | 726 |
725 HttpServer get impl => _impl; | 727 HttpServer get impl => _impl; |
726 set impl(HttpServer d) { | 728 set impl(HttpServer d) { |
727 if (d == null) { | 729 if (d == null) { |
728 throw new core.MojoApiError("$this: Cannot set a null implementation"); | 730 throw new core.MojoApiError("$this: Cannot set a null implementation"); |
729 } | 731 } |
730 if (isBound && (_impl == null)) { | 732 if (isBound && (_impl == null)) { |
731 beginHandlingEvents(); | 733 beginHandlingEvents(); |
732 } | 734 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
766 core.MojoHandle handle, [HttpServer impl]) | 768 core.MojoHandle handle, [HttpServer impl]) |
767 : super(new _HttpServerStubControl.fromHandle(handle, impl)); | 769 : super(new _HttpServerStubControl.fromHandle(handle, impl)); |
768 | 770 |
769 static HttpServerStub newFromEndpoint( | 771 static HttpServerStub newFromEndpoint( |
770 core.MojoMessagePipeEndpoint endpoint) { | 772 core.MojoMessagePipeEndpoint endpoint) { |
771 assert(endpoint.setDescription("For HttpServerStub")); | 773 assert(endpoint.setDescription("For HttpServerStub")); |
772 return new HttpServerStub.fromEndpoint(endpoint); | 774 return new HttpServerStub.fromEndpoint(endpoint); |
773 } | 775 } |
774 | 776 |
775 | 777 |
776 dynamic setHandler(String pattern,HttpHandlerInterface handler,[Function respo
nseFactory = null]) { | 778 void setHandler(String pattern,HttpHandlerInterface handler,void callback(bool
success)) { |
777 return impl.setHandler(pattern,handler,responseFactory); | 779 return impl.setHandler(pattern,handler,callback); |
778 } | 780 } |
779 dynamic getPort([Function responseFactory = null]) { | 781 void getPort(void callback(int port)) { |
780 return impl.getPort(responseFactory); | 782 return impl.getPort(callback); |
781 } | 783 } |
782 } | 784 } |
783 | 785 |
784 const int _httpHandlerMethodHandleRequestName = 0; | 786 const int _httpHandlerMethodHandleRequestName = 0; |
785 | 787 |
786 class _HttpHandlerServiceDescription implements service_describer.ServiceDescrip
tion { | 788 class _HttpHandlerServiceDescription implements service_describer.ServiceDescrip
tion { |
787 dynamic getTopLevelInterface([Function responseFactory]) => | 789 void getTopLevelInterface(Function responder) { |
788 responseFactory(null); | 790 responder(null); |
| 791 } |
789 | 792 |
790 dynamic getTypeDefinition(String typeKey, [Function responseFactory]) => | 793 void getTypeDefinition(String typeKey, Function responder) { |
791 responseFactory(null); | 794 responder(null); |
| 795 } |
792 | 796 |
793 dynamic getAllTypeDefinitions([Function responseFactory]) => | 797 void getAllTypeDefinitions(Function responder) { |
794 responseFactory(null); | 798 responder(null); |
| 799 } |
795 } | 800 } |
796 | 801 |
797 abstract class HttpHandler { | 802 abstract class HttpHandler { |
798 static const String serviceName = null; | 803 static const String serviceName = null; |
799 | 804 |
800 static service_describer.ServiceDescription _cachedServiceDescription; | 805 static service_describer.ServiceDescription _cachedServiceDescription; |
801 static service_describer.ServiceDescription get serviceDescription { | 806 static service_describer.ServiceDescription get serviceDescription { |
802 if (_cachedServiceDescription == null) { | 807 if (_cachedServiceDescription == null) { |
803 _cachedServiceDescription = new _HttpHandlerServiceDescription(); | 808 _cachedServiceDescription = new _HttpHandlerServiceDescription(); |
804 } | 809 } |
805 return _cachedServiceDescription; | 810 return _cachedServiceDescription; |
806 } | 811 } |
807 | 812 |
808 static HttpHandlerProxy connectToService( | 813 static HttpHandlerProxy connectToService( |
809 bindings.ServiceConnector s, String url, [String serviceName]) { | 814 bindings.ServiceConnector s, String url, [String serviceName]) { |
810 HttpHandlerProxy p = new HttpHandlerProxy.unbound(); | 815 HttpHandlerProxy p = new HttpHandlerProxy.unbound(); |
811 String name = serviceName ?? HttpHandler.serviceName; | 816 String name = serviceName ?? HttpHandler.serviceName; |
812 if ((name == null) || name.isEmpty) { | 817 if ((name == null) || name.isEmpty) { |
813 throw new core.MojoApiError( | 818 throw new core.MojoApiError( |
814 "If an interface has no ServiceName, then one must be provided."); | 819 "If an interface has no ServiceName, then one must be provided."); |
815 } | 820 } |
816 s.connectToService(url, p, name); | 821 s.connectToService(url, p, name); |
817 return p; | 822 return p; |
818 } | 823 } |
819 dynamic handleRequest(http_request_mojom.HttpRequest request,[Function respons
eFactory = null]); | 824 void handleRequest(http_request_mojom.HttpRequest request,void callback(http_r
esponse_mojom.HttpResponse response)); |
820 } | 825 } |
821 | 826 |
822 abstract class HttpHandlerInterface | 827 abstract class HttpHandlerInterface |
823 implements bindings.MojoInterface<HttpHandler>, | 828 implements bindings.MojoInterface<HttpHandler>, |
824 HttpHandler { | 829 HttpHandler { |
825 factory HttpHandlerInterface([HttpHandler impl]) => | 830 factory HttpHandlerInterface([HttpHandler impl]) => |
826 new HttpHandlerStub.unbound(impl); | 831 new HttpHandlerStub.unbound(impl); |
827 | 832 |
828 factory HttpHandlerInterface.fromEndpoint( | 833 factory HttpHandlerInterface.fromEndpoint( |
829 core.MojoMessagePipeEndpoint endpoint, | 834 core.MojoMessagePipeEndpoint endpoint, |
(...skipping 29 matching lines...) Expand all Loading... |
859 | 864 |
860 void handleResponse(bindings.ServiceMessage message) { | 865 void handleResponse(bindings.ServiceMessage message) { |
861 switch (message.header.type) { | 866 switch (message.header.type) { |
862 case _httpHandlerMethodHandleRequestName: | 867 case _httpHandlerMethodHandleRequestName: |
863 var r = HttpHandlerHandleRequestResponseParams.deserialize( | 868 var r = HttpHandlerHandleRequestResponseParams.deserialize( |
864 message.payload); | 869 message.payload); |
865 if (!message.header.hasRequestId) { | 870 if (!message.header.hasRequestId) { |
866 proxyError("Expected a message with a valid request Id."); | 871 proxyError("Expected a message with a valid request Id."); |
867 return; | 872 return; |
868 } | 873 } |
869 Completer c = completerMap[message.header.requestId]; | 874 Function callback = callbackMap[message.header.requestId]; |
870 if (c == null) { | 875 if (callback == null) { |
871 proxyError( | 876 proxyError( |
872 "Message had unknown request Id: ${message.header.requestId}"); | 877 "Message had unknown request Id: ${message.header.requestId}"); |
873 return; | 878 return; |
874 } | 879 } |
875 completerMap.remove(message.header.requestId); | 880 callbackMap.remove(message.header.requestId); |
876 if (c.isCompleted) { | 881 callback(r.response ); |
877 proxyError("Response completer already completed"); | |
878 return; | |
879 } | |
880 c.complete(r); | |
881 break; | 882 break; |
882 default: | 883 default: |
883 proxyError("Unexpected message type: ${message.header.type}"); | 884 proxyError("Unexpected message type: ${message.header.type}"); |
884 close(immediate: true); | 885 close(immediate: true); |
885 break; | 886 break; |
886 } | 887 } |
887 } | 888 } |
888 | 889 |
889 @override | 890 @override |
890 String toString() { | 891 String toString() { |
(...skipping 24 matching lines...) Expand all Loading... |
915 return newMockedProxy; | 916 return newMockedProxy; |
916 } | 917 } |
917 | 918 |
918 static HttpHandlerProxy newFromEndpoint( | 919 static HttpHandlerProxy newFromEndpoint( |
919 core.MojoMessagePipeEndpoint endpoint) { | 920 core.MojoMessagePipeEndpoint endpoint) { |
920 assert(endpoint.setDescription("For HttpHandlerProxy")); | 921 assert(endpoint.setDescription("For HttpHandlerProxy")); |
921 return new HttpHandlerProxy.fromEndpoint(endpoint); | 922 return new HttpHandlerProxy.fromEndpoint(endpoint); |
922 } | 923 } |
923 | 924 |
924 | 925 |
925 dynamic handleRequest(http_request_mojom.HttpRequest request,[Function respons
eFactory = null]) { | 926 void handleRequest(http_request_mojom.HttpRequest request,void callback(http_r
esponse_mojom.HttpResponse response)) { |
926 if (impl != null) { | 927 if (impl != null) { |
927 return new Future(() => impl.handleRequest(request,_HttpHandlerStubControl
._httpHandlerHandleRequestResponseParamsFactory)); | 928 impl.handleRequest(request,callback); |
| 929 return; |
928 } | 930 } |
929 var params = new _HttpHandlerHandleRequestParams(); | 931 var params = new _HttpHandlerHandleRequestParams(); |
930 params.request = request; | 932 params.request = request; |
931 return ctrl.sendMessageWithRequestId( | 933 ctrl.sendMessageWithRequestId( |
932 params, | 934 params, |
933 _httpHandlerMethodHandleRequestName, | 935 _httpHandlerMethodHandleRequestName, |
934 -1, | 936 -1, |
935 bindings.MessageHeader.kMessageExpectsResponse); | 937 bindings.MessageHeader.kMessageExpectsResponse, |
| 938 callback); |
936 } | 939 } |
937 } | 940 } |
938 | 941 |
939 class _HttpHandlerStubControl | 942 class _HttpHandlerStubControl |
940 extends bindings.StubMessageHandler | 943 extends bindings.StubMessageHandler |
941 implements bindings.StubControl<HttpHandler> { | 944 implements bindings.StubControl<HttpHandler> { |
942 HttpHandler _impl; | 945 HttpHandler _impl; |
943 | 946 |
944 _HttpHandlerStubControl.fromEndpoint( | 947 _HttpHandlerStubControl.fromEndpoint( |
945 core.MojoMessagePipeEndpoint endpoint, [HttpHandler impl]) | 948 core.MojoMessagePipeEndpoint endpoint, [HttpHandler impl]) |
946 : super.fromEndpoint(endpoint, autoBegin: impl != null) { | 949 : super.fromEndpoint(endpoint, autoBegin: impl != null) { |
947 _impl = impl; | 950 _impl = impl; |
948 } | 951 } |
949 | 952 |
950 _HttpHandlerStubControl.fromHandle( | 953 _HttpHandlerStubControl.fromHandle( |
951 core.MojoHandle handle, [HttpHandler impl]) | 954 core.MojoHandle handle, [HttpHandler impl]) |
952 : super.fromHandle(handle, autoBegin: impl != null) { | 955 : super.fromHandle(handle, autoBegin: impl != null) { |
953 _impl = impl; | 956 _impl = impl; |
954 } | 957 } |
955 | 958 |
956 _HttpHandlerStubControl.unbound([this._impl]) : super.unbound(); | 959 _HttpHandlerStubControl.unbound([this._impl]) : super.unbound(); |
957 | 960 |
958 String get serviceName => HttpHandler.serviceName; | 961 String get serviceName => HttpHandler.serviceName; |
959 | 962 |
960 | 963 |
961 static HttpHandlerHandleRequestResponseParams _httpHandlerHandleRequestRespons
eParamsFactory(http_response_mojom.HttpResponse response) { | 964 Function _httpHandlerHandleRequestResponseParamsResponder( |
962 var result = new HttpHandlerHandleRequestResponseParams(); | 965 int requestId) { |
963 result.response = response; | 966 return (http_response_mojom.HttpResponse response) { |
964 return result; | 967 var result = new HttpHandlerHandleRequestResponseParams(); |
| 968 result.response = response; |
| 969 sendResponse(buildResponseWithId( |
| 970 result, |
| 971 _httpHandlerMethodHandleRequestName, |
| 972 requestId, |
| 973 bindings.MessageHeader.kMessageIsResponse)); |
| 974 }; |
965 } | 975 } |
966 | 976 |
967 dynamic handleMessage(bindings.ServiceMessage message) { | 977 void handleMessage(bindings.ServiceMessage message) { |
968 if (bindings.ControlMessageHandler.isControlMessage(message)) { | 978 if (bindings.ControlMessageHandler.isControlMessage(message)) { |
969 return bindings.ControlMessageHandler.handleMessage(this, | 979 bindings.ControlMessageHandler.handleMessage( |
970 0, | 980 this, 0, message); |
971 message); | 981 return; |
972 } | 982 } |
973 if (_impl == null) { | 983 if (_impl == null) { |
974 throw new core.MojoApiError("$this has no implementation set"); | 984 throw new core.MojoApiError("$this has no implementation set"); |
975 } | 985 } |
976 switch (message.header.type) { | 986 switch (message.header.type) { |
977 case _httpHandlerMethodHandleRequestName: | 987 case _httpHandlerMethodHandleRequestName: |
978 var params = _HttpHandlerHandleRequestParams.deserialize( | 988 var params = _HttpHandlerHandleRequestParams.deserialize( |
979 message.payload); | 989 message.payload); |
980 var response = _impl.handleRequest(params.request,_httpHandlerHandleRequ
estResponseParamsFactory); | 990 _impl.handleRequest(params.request, _httpHandlerHandleRequestResponsePar
amsResponder(message.header.requestId)); |
981 if (response is Future) { | |
982 return response.then((response) { | |
983 if (response != null) { | |
984 return buildResponseWithId( | |
985 response, | |
986 _httpHandlerMethodHandleRequestName, | |
987 message.header.requestId, | |
988 bindings.MessageHeader.kMessageIsResponse); | |
989 } | |
990 }); | |
991 } else if (response != null) { | |
992 return buildResponseWithId( | |
993 response, | |
994 _httpHandlerMethodHandleRequestName, | |
995 message.header.requestId, | |
996 bindings.MessageHeader.kMessageIsResponse); | |
997 } | |
998 break; | 991 break; |
999 default: | 992 default: |
1000 throw new bindings.MojoCodecError("Unexpected message name"); | 993 throw new bindings.MojoCodecError("Unexpected message name"); |
1001 break; | 994 break; |
1002 } | 995 } |
1003 return null; | |
1004 } | 996 } |
1005 | 997 |
1006 HttpHandler get impl => _impl; | 998 HttpHandler get impl => _impl; |
1007 set impl(HttpHandler d) { | 999 set impl(HttpHandler d) { |
1008 if (d == null) { | 1000 if (d == null) { |
1009 throw new core.MojoApiError("$this: Cannot set a null implementation"); | 1001 throw new core.MojoApiError("$this: Cannot set a null implementation"); |
1010 } | 1002 } |
1011 if (isBound && (_impl == null)) { | 1003 if (isBound && (_impl == null)) { |
1012 beginHandlingEvents(); | 1004 beginHandlingEvents(); |
1013 } | 1005 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1047 core.MojoHandle handle, [HttpHandler impl]) | 1039 core.MojoHandle handle, [HttpHandler impl]) |
1048 : super(new _HttpHandlerStubControl.fromHandle(handle, impl)); | 1040 : super(new _HttpHandlerStubControl.fromHandle(handle, impl)); |
1049 | 1041 |
1050 static HttpHandlerStub newFromEndpoint( | 1042 static HttpHandlerStub newFromEndpoint( |
1051 core.MojoMessagePipeEndpoint endpoint) { | 1043 core.MojoMessagePipeEndpoint endpoint) { |
1052 assert(endpoint.setDescription("For HttpHandlerStub")); | 1044 assert(endpoint.setDescription("For HttpHandlerStub")); |
1053 return new HttpHandlerStub.fromEndpoint(endpoint); | 1045 return new HttpHandlerStub.fromEndpoint(endpoint); |
1054 } | 1046 } |
1055 | 1047 |
1056 | 1048 |
1057 dynamic handleRequest(http_request_mojom.HttpRequest request,[Function respons
eFactory = null]) { | 1049 void handleRequest(http_request_mojom.HttpRequest request,void callback(http_r
esponse_mojom.HttpResponse response)) { |
1058 return impl.handleRequest(request,responseFactory); | 1050 return impl.handleRequest(request,callback); |
1059 } | 1051 } |
1060 } | 1052 } |
1061 | 1053 |
1062 | 1054 |
1063 | 1055 |
OLD | NEW |