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 validateControlRequestWithResponse(message) { 9 var messageValidator = new internal.Validator(message); 10 var error = messageValidator.validateMessageIsRequestExpectingResponse(); 11 if (error !== internal.validationError.NONE) { 12 throw error; 13 } 14 15 if (message.getName() != mojo.interfaceControl.kRunMessageId) { 16 throw new Error("Control message name is not kRunMessageId"); 17 } 18 19 // Validate payload. 20 error = mojo.interfaceControl.RunMessageParams.validate(messageValidator, 21 message.getHeaderNumBytes()); 22 if (error != internal.validationError.NONE) { 23 throw error; 24 } 25 } 26 27 function validateControlRequestWithoutResponse(message) { 28 var messageValidator = new internal.Validator(message); 29 var error = messageValidator.validateMessageIsRequestWithoutResponse(); 30 if (error != internal.validationError.NONE) { 31 throw error; 32 } 33 34 if (message.getName() != mojo.interfaceControl.kRunOrClosePipeMessageId) { 35 throw new Error("Control message name is not kRunOrClosePipeMessageId"); 36 } 37 38 // Validate payload. 39 error = mojo.interfaceControl.RunOrClosePipeMessageParams.validate( 40 messageValidator, message.getHeaderNumBytes()); 41 if (error != internal.validationError.NONE) { 42 throw error; 43 } 44 } 45 46 function runOrClosePipe(message, interfaceVersion) { 47 var reader = new internal.MessageReader(message); 48 var runOrClosePipeMessageParams = reader.decodeStruct( 49 mojo.interfaceControl.RunOrClosePipeMessageParams); 50 return interfaceVersion >= 51 runOrClosePipeMessageParams.input.requireVersion.version; 52 } 53 54 function run(message, responder, interfaceVersion) { 55 var reader = new internal.MessageReader(message); 56 var runMessageParams = 57 reader.decodeStruct(mojo.interfaceControl.RunMessageParams); 58 var runOutput = null; 59 60 if (runMessageParams.input.queryVersion) { 61 runOutput = new mojo.interfaceControl.RunOutput(); 62 runOutput.queryVersionResult = new 63 mojo.interfaceControl.QueryVersionResult( 64 {'version': interfaceVersion}); 65 } 66 67 var runResponseMessageParams = new 68 mojo.interfaceControl.RunResponseMessageParams(); 69 runResponseMessageParams.output = runOutput; 70 71 var messageName = mojo.interfaceControl.kRunMessageId; 72 var payloadSize = 73 mojo.interfaceControl.RunResponseMessageParams.encodedSize; 74 var requestID = reader.requestID; 75 var builder = new internal.MessageV1Builder(messageName, 76 payloadSize, internal.kMessageIsResponse, requestID); 77 builder.encodeStruct(mojo.interfaceControl.RunResponseMessageParams, 78 runResponseMessageParams); 79 responder.accept(builder.finish()); 80 return true; 81 } 82 83 function isInterfaceControlMessage(message) { 84 return message.getName() == mojo.interfaceControl.kRunMessageId || 85 message.getName() == mojo.interfaceControl.kRunOrClosePipeMessageId; 86 } 87 88 function ControlMessageHandler(interfaceVersion) { 89 this.interfaceVersion_ = interfaceVersion; 90 } 91 92 ControlMessageHandler.prototype.accept = function(message) { 93 validateControlRequestWithoutResponse(message); 94 return runOrClosePipe(message, this.interfaceVersion_); 95 }; 96 97 ControlMessageHandler.prototype.acceptWithResponder = function(message, 98 responder) { 99 validateControlRequestWithResponse(message); 100 return run(message, responder, this.interfaceVersion_); 101 }; 102 103 internal.ControlMessageHandler = ControlMessageHandler; 104 internal.isInterfaceControlMessage = isInterfaceControlMessage; 105})(); 106