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(function() { 6 var internal = mojo.internal; 7 8 function constructRunOrClosePipeMessage(runOrClosePipeInput) { 9 var runOrClosePipeMessageParams = new 10 mojo.interfaceControl.RunOrClosePipeMessageParams(); 11 runOrClosePipeMessageParams.input = runOrClosePipeInput; 12 13 var messageName = mojo.interfaceControl.kRunOrClosePipeMessageId; 14 var payloadSize = 15 mojo.interfaceControl.RunOrClosePipeMessageParams.encodedSize; 16 var builder = new internal.MessageV0Builder(messageName, payloadSize); 17 builder.encodeStruct(mojo.interfaceControl.RunOrClosePipeMessageParams, 18 runOrClosePipeMessageParams); 19 var message = builder.finish(); 20 return message; 21 } 22 23 function validateControlResponse(message) { 24 var messageValidator = new internal.Validator(message); 25 var error = messageValidator.validateMessageIsResponse(); 26 if (error != internal.validationError.NONE) { 27 throw error; 28 } 29 30 if (message.getName() != mojo.interfaceControl.kRunMessageId) { 31 throw new Error("Control message name is not kRunMessageId"); 32 } 33 34 // Validate payload. 35 error = mojo.interfaceControl.RunResponseMessageParams.validate( 36 messageValidator, message.getHeaderNumBytes()); 37 if (error != internal.validationError.NONE) { 38 throw error; 39 } 40 } 41 42 function acceptRunResponse(message) { 43 validateControlResponse(message); 44 45 var reader = new internal.MessageReader(message); 46 var runResponseMessageParams = reader.decodeStruct( 47 mojo.interfaceControl.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 = mojo.interfaceControl.kRunMessageId; 63 var payloadSize = mojo.interfaceControl.RunMessageParams.encodedSize; 64 // |requestID| is set to 0, but is later properly set by Router. 65 var builder = new internal.MessageV1Builder(messageName, 66 payloadSize, internal.kMessageExpectsResponse, 0); 67 builder.encodeStruct(mojo.interfaceControl.RunMessageParams, 68 runMessageParams); 69 var message = builder.finish(); 70 71 return receiver.acceptAndExpectResponse(message).then(acceptRunResponse); 72 } 73 74 function ControlMessageProxy(receiver) { 75 this.receiver_ = receiver; 76 } 77 78 ControlMessageProxy.prototype.queryVersion = function() { 79 var runMessageParams = new mojo.interfaceControl.RunMessageParams(); 80 runMessageParams.input = new mojo.interfaceControl.RunInput(); 81 runMessageParams.input.queryVersion = 82 new mojo.interfaceControl.QueryVersion(); 83 84 return sendRunMessage(this.receiver_, runMessageParams).then(function( 85 runResponseMessageParams) { 86 return runResponseMessageParams.output.queryVersionResult.version; 87 }); 88 }; 89 90 ControlMessageProxy.prototype.requireVersion = function(version) { 91 var runOrClosePipeInput = new mojo.interfaceControl.RunOrClosePipeInput(); 92 runOrClosePipeInput.requireVersion = 93 new mojo.interfaceControl.RequireVersion({'version': version}); 94 var message = constructRunOrClosePipeMessage(runOrClosePipeInput); 95 this.receiver_.accept(message); 96 }; 97 98 internal.ControlMessageProxy = ControlMessageProxy; 99})(); 100