• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 validateControlRequestWithoutResponse(message) {
9    var messageValidator = new internal.Validator(message);
10    var error = messageValidator.validateMessageIsRequestWithoutResponse();
11    if (error != internal.validationError.NONE) {
12      throw error;
13    }
14
15    if (message.getName() != mojo.pipeControl.kRunOrClosePipeMessageId) {
16      throw new Error("Control message name is not kRunOrClosePipeMessageId");
17    }
18
19    // Validate payload.
20    error = mojo.pipeControl.RunOrClosePipeMessageParams.validate(
21        messageValidator, message.getHeaderNumBytes());
22    if (error != internal.validationError.NONE) {
23      throw error;
24    }
25  }
26
27  function runOrClosePipe(message, delegate) {
28    var reader = new internal.MessageReader(message);
29    var runOrClosePipeMessageParams = reader.decodeStruct(
30        mojo.pipeControl.RunOrClosePipeMessageParams);
31    var event = runOrClosePipeMessageParams.input
32        .peerAssociatedEndpointClosedEvent;
33    return delegate.onPeerAssociatedEndpointClosed(event.id,
34        event.disconnectReason);
35  }
36
37  function isPipeControlMessage(message) {
38    return !internal.isValidInterfaceId(message.getInterfaceId());
39  }
40
41  function PipeControlMessageHandler(delegate) {
42    this.delegate_ = delegate;
43  }
44
45  PipeControlMessageHandler.prototype.accept = function(message) {
46    validateControlRequestWithoutResponse(message);
47    return runOrClosePipe(message, this.delegate_);
48  };
49
50  internal.PipeControlMessageHandler = PipeControlMessageHandler;
51  internal.isPipeControlMessage = isPipeControlMessage;
52})();
53