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

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

Issue 2676443005: Add interface versioning. Methods queryVersion and requireVersion. (Closed)
Patch Set: Validate payload. 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
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/js/codec",
yzshen1 2017/02/13 23:44:23 alphabetically, please.
wangjimmy 2017/02/14 00:06:06 Done.
7 "mojo/public/interfaces/bindings/interface_control_messages.mojom",
8 "mojo/public/js/validator",
9 ], function(codec, controlMessages, 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 * Accept the response message from the receiver and decode.
yzshen1 2017/02/13 23:44:23 Accept -> Accepts; decode -> decodes. Because the
wangjimmy 2017/02/14 00:06:06 Done.
55 * the message struct to RunResponseMessageParams.
yzshen1 2017/02/13 23:44:23 I guess this is part of the sentence of the previo
wangjimmy 2017/02/14 00:06:06 Done.
56 * .
yzshen1 2017/02/13 23:44:23 Please remove "."
wangjimmy 2017/02/14 00:06:06 Done.
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

Powered by Google App Engine
This is Rietveld 408576698