Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions images/chromium-headless/image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ RUN --mount=type=cache,target=/var/cache/apt,sharing=locked,id=$CACHEIDPREFIX-ap
dbus-x11 \
xvfb \
x11-utils \
xclip \
xdotool \
fontconfig \
fonts-noto-cjk \
Expand Down
48 changes: 48 additions & 0 deletions server/cmd/api/api/computer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,54 @@ func clampPoints(points [][2]int, screenWidth, screenHeight int) {
}
}

func (s *ApiService) ReadClipboard(ctx context.Context, request oapi.ReadClipboardRequestObject) (oapi.ReadClipboardResponseObject, error) {
log := logger.FromContext(ctx)

s.inputMu.Lock()
defer s.inputMu.Unlock()

display := s.resolveDisplayFromEnv()
cmd := exec.CommandContext(ctx, "xclip", "-selection", "clipboard", "-o")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this included for both headful + headless images?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good catch! I added the xclip to the headless image! a35b3d1

cmd.Env = append(os.Environ(), fmt.Sprintf("DISPLAY=%s", display))
output, err := cmd.Output()
if err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) && exitErr.ExitCode() == 1 {
return oapi.ReadClipboard200JSONResponse{Text: ""}, nil
}
log.Error("xclip read failed", "err", err)
return oapi.ReadClipboard500JSONResponse{InternalErrorJSONResponse: oapi.InternalErrorJSONResponse{
Message: fmt.Sprintf("failed to read clipboard: %v", err)},
}, nil
}
return oapi.ReadClipboard200JSONResponse{Text: string(output)}, nil
}

func (s *ApiService) WriteClipboard(ctx context.Context, request oapi.WriteClipboardRequestObject) (oapi.WriteClipboardResponseObject, error) {
log := logger.FromContext(ctx)

s.inputMu.Lock()
defer s.inputMu.Unlock()

if request.Body == nil {
return oapi.WriteClipboard400JSONResponse{BadRequestErrorJSONResponse: oapi.BadRequestErrorJSONResponse{
Message: "request body is required"},
}, nil
}

display := s.resolveDisplayFromEnv()
cmd := exec.CommandContext(ctx, "xclip", "-selection", "clipboard")
cmd.Env = append(os.Environ(), fmt.Sprintf("DISPLAY=%s", display))
cmd.Stdin = strings.NewReader(request.Body.Text)
if err := cmd.Run(); err != nil {
log.Error("xclip write failed", "err", err)
return oapi.WriteClipboard500JSONResponse{InternalErrorJSONResponse: oapi.InternalErrorJSONResponse{
Message: fmt.Sprintf("failed to write to clipboard: %v", err)},
}, nil
}
return oapi.WriteClipboard200Response{}, nil
}

// generateRelativeSteps produces a sequence of relative steps that approximate a
// straight line from (0,0) to (dx,dy) using at most the provided number of
// steps. Each returned element is a pair {stepX, stepY}. The steps are
Expand Down
37 changes: 37 additions & 0 deletions server/e2e/e2e_chromium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,43 @@ func TestDisplayResolutionChange(t *testing.T) {
t.Log("all resolution changes verified successfully")
}

func TestClipboardHeadless(t *testing.T) {
t.Parallel()

if _, err := exec.LookPath("docker"); err != nil {
t.Skipf("docker not available: %v", err)
}

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel()

c := NewTestContainer(t, headlessImage)
require.NoError(t, c.Start(ctx, ContainerConfig{}), "failed to start container")
defer c.Stop(ctx)

require.NoError(t, c.WaitReady(ctx), "api not ready")

specResp, err := http.Get(c.APIBaseURL() + "/spec.yaml")
require.NoError(t, err)
specBody, _ := io.ReadAll(specResp.Body)
specResp.Body.Close()
require.True(t, strings.Contains(string(specBody), "/computer/clipboard/write"),
"API spec does not include clipboard routes - rebuild the image with: cd kernel-images/images/chromium-headless && docker build --no-cache -f image/Dockerfile -t %s ../..", headlessImage)

client, err := c.APIClient()
require.NoError(t, err, "failed to create API client")

writeResp, err := client.WriteClipboardWithResponse(ctx, instanceoapi.WriteClipboardRequest{Text: "e2e-clipboard-test"})
require.NoError(t, err, "WriteClipboard request failed")
require.Equal(t, http.StatusOK, writeResp.StatusCode(), "unexpected write status: %s body=%s", writeResp.Status(), string(writeResp.Body))

readResp, err := client.ReadClipboardWithResponse(ctx)
require.NoError(t, err, "ReadClipboard request failed")
require.Equal(t, http.StatusOK, readResp.StatusCode(), "unexpected read status: %s body=%s", readResp.Status(), string(readResp.Body))
require.NotNil(t, readResp.JSON200, "expected JSON200 response")
require.Equal(t, "e2e-clipboard-test", readResp.JSON200.Text, "clipboard content mismatch")
}

func TestExtensionUploadAndActivation(t *testing.T) {
t.Parallel()
ensurePlaywrightDeps(t)
Expand Down
Loading
Loading