Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: mojo/public/js/lib/control_message_proxy.js

Issue 2676443005: Add interface versioning. Methods queryVersion and requireVersion. (Closed)
Patch Set: Expect the result inside the error handler for test. Code formatting and address codereview comment… Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/js/lib/control_message_handler.js ('k') | mojo/public/js/router.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 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 define("mojo/public/js/lib/control_message_proxy", [
6 "mojo/public/interfaces/bindings/interface_control_messages.mojom",
7 "mojo/public/js/codec",
8 "mojo/public/js/validator",
9 ], function(controlMessages, codec, validator) {
10
11 var Validator = validator.Validator;
12
13 function sendRunOrClosePipeMessage(receiver, runOrClosePipeMessageParams) {
14 var messageName = controlMessages.kRunOrClosePipeMessageId;
15 var payloadSize = controlMessages.RunOrClosePipeMessageParams.encodedSize;
16 var builder = new codec.MessageBuilder(messageName, payloadSize);
17 builder.encodeStruct(controlMessages.RunOrClosePipeMessageParams,
18 runOrClosePipeMessageParams);
19 var message = builder.finish();
20 receiver.accept(message);
21 }
22
23 function validateControlResponse(message) {
24 var messageValidator = new Validator(message);
25 var error = messageValidator.validateMessageIsResponse();
26 if (error != validator.validationError.NONE) {
27 throw error;
28 }
29
30 if (message.getName() != controlMessages.kRunMessageId) {
31 throw new Error("Control message name is not kRunMessageId");
32 }
33
34 // Validate payload.
35 error = controlMessages.RunResponseMessageParams.validate(
36 messageValidator, message.getHeaderNumBytes());
37 if (error != validator.validationError.NONE) {
38 throw error;
39 }
40 }
41
42 function acceptRunResponse(message) {
43 validateControlResponse(message);
44
45 var reader = new codec.MessageReader(message);
46 var runResponseMessageParams = reader.decodeStruct(
47 controlMessages.RunResponseMessageParams);
48
49 return Promise.resolve(runResponseMessageParams);
50 }
51
52 /**
53 * Sends the given run message through the receiver.
54 * Accepts the response message from the receiver and decodes the message
55 * struct to RunResponseMessageParams.
56 *
57 * @param {Router} receiver.
58 * @param {RunMessageParams} runMessageParams to be sent via a message.
59 * @return {Promise} that resolves to a RunResponseMessageParams.
60 */
61 function sendRunMessage(receiver, runMessageParams) {
62 var messageName = controlMessages.kRunMessageId;
63 var payloadSize = controlMessages.RunMessageParams.encodedSize;
64 // |requestID| is set to 0, but is later properly set by Router.
65 var builder = new codec.MessageWithRequestIDBuilder(messageName,
66 payloadSize, codec.kMessageExpectsResponse, 0);
67 builder.encodeStruct(controlMessages.RunMessageParams, runMessageParams);
68 var message = builder.finish();
69
70 return receiver.acceptAndExpectResponse(message).then(acceptRunResponse);
71 }
72
73 function ControlMessageProxy(receiver) {
74 this.receiver = receiver;
75 }
76
77 ControlMessageProxy.prototype.queryVersion = function() {
78 var runMessageParams = new controlMessages.RunMessageParams();
79 runMessageParams.input = new controlMessages.RunInput();
80 runMessageParams.input.query_version = new controlMessages.QueryVersion();
81
82 return sendRunMessage(this.receiver, runMessageParams).then(function(
83 runResponseMessageParams) {
84 return runResponseMessageParams.output.query_version_result.version;
85 });
86 };
87
88 ControlMessageProxy.prototype.requireVersion = function(version) {
89 var runOrClosePipeMessageParams = new
90 controlMessages.RunOrClosePipeMessageParams();
91 runOrClosePipeMessageParams.input = new
92 controlMessages.RunOrClosePipeInput();
93 runOrClosePipeMessageParams.input.require_version = new
94 controlMessages.RequireVersion({'version': version});
95 sendRunOrClosePipeMessage(this.receiver, runOrClosePipeMessageParams);
96 };
97
98 var exports = {};
99 exports.ControlMessageProxy = ControlMessageProxy;
100
101 return exports;
102 });
OLDNEW
« no previous file with comments | « mojo/public/js/lib/control_message_handler.js ('k') | mojo/public/js/router.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698