Search code examples
bashgoogle-chromegoogle-chrome-extensiongoogle-chrome-appcrosh

Creating file using crosh from Chrome extension [terminalPrivate.sendInput]


I'm attempting to interface with the Chrome OS "crosh" terminal through a Chrome extension. I'm using Secure Shell's dev-id to get access to chrome.terminalPrivate. From my initial attempt, I'm able to start a crosh process and bash shell. However, I'm trying to create a file in the ~/Downloads directory and that doesn't seem to be working. The file is never created as far as I can tell.

Here is the code that I've put together so far (I used this code from the Chromium developers as a starting point):

// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var shellCommand = 'shell\n';
var croshName = 'crosh';

window.onload = function() {
  Crosh(null);
  commandTest();
}

function Crosh(argv) {
  this.argv_ = argv;
  this.io = null;
  this.keyboard_ = false;
  this.pid_ = -1;
}

function commandTest() {
  chrome.terminalPrivate.onProcessOutput.addListener(processListener);

  chrome.terminalPrivate.openTerminalProcess(croshName, (pid) => {
    if (pid < 0) {
      window.alert("error!");
    }

    this.pid_ = pid;

    var cmd1 = 'shell\n';
    var cmd2 = 'touch ~/Downloads/test.txt\n';

    chrome.terminalPrivate.sendInput(pid, cmd1,
      function (r1) {
        window.alert(r1);
      }
    );

    chrome.terminalPrivate.sendInput(pid, cmd2,
      function (r2) {
        window.alert(r2);
      }
    );

    chrome.terminalPrivate.closeTerminalProcess(
      this.pid_,
      function(result) {
        window.alert(result);
      }
    );
  });
}

function processListener(pid, type, text){
  window.alert(text);
}

Thanks for your help!


Solution

  • i answered in the chromium-hterm group where you posted this question too

    it looks like you're trying to shoe horn a slow event driven process into a fast synchronous code path. you need to change the model to react on events rather than shove input down the pipe when the other side isn't ready yet.

    i.e. delete all the sendInput calls from openTerminalProcess and move all the logic to processListener. that needs to check the output in "text" and then decide what to send next.

    basically you need to implement an ad-hoc parser akin to expect.