Skip to content

Commit

Permalink
8307779: Relax the java.awt.Robot specification
Browse files Browse the repository at this point in the history
Reviewed-by: prr, iris, lancea, andrew
Backport-of: 21aa057fafd88f4dd677bbf7c99a7eabb6b18409
  • Loading branch information
Alexander Zvegintsev committed May 16, 2024
1 parent 4f3b76f commit c0a876a
Showing 1 changed file with 75 additions and 5 deletions.
80 changes: 75 additions & 5 deletions jdk/src/share/classes/java/awt/Robot.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -61,6 +61,43 @@
* <p>
* Applications that use Robot for purposes other than self-testing should
* handle these error conditions gracefully.
* <p>
* Platforms and desktop environments may impose restrictions or limitations
* on the access required to implement all functionality in the Robot class.
* For example:
* <ul>
* <li> preventing access to the contents of any part of a desktop
* or Window on the desktop that is not owned by the running application.</li>
* <li> treating window decorations as non-owned content.</li>
* <li> ignoring or limiting specific requests to manipulate windows.</li>
* <li> ignoring or limiting specific requests for Robot generated (synthesized)
* events related to keyboard and mouse etc.</li>
* <li> requiring specific or global permissions to any access to window
* contents, even application owned content,or to perform even limited
* synthesizing of events.</li>
* </ul>
*
* The Robot API specification requires that approvals for these be granted
* for full operation.
* If they are not granted, the API will be degraded as discussed here.
* Relevant specific API methods may document more specific limitations
* and requirements.
* Depending on the policies of the desktop environment,
* the approvals mentioned above may:
* <ul>
* <li>be required every time</li>
* <li>or persistent for the lifetime of an application,</li>
* <li>or persistent across multiple user desktop sessions</li>
* <li>be fine-grained permissions</li>
* <li>be associated with a specific binary application,
* or a class of binary applications.</li>
* </ul>
*
* When such approvals need to given interactively, it may impede the normal
* operation of the application until approved, and if approval is denied
* or not possible, or cannot be made persistent then it will degrade
* the functionality of this class and in turn any part of the operation
* of the application which is dependent on it.
*
* @author Robi Khan
* @since 1.3
Expand Down Expand Up @@ -192,6 +229,11 @@ public void dispose() {

/**
* Moves mouse pointer to given screen coordinates.
* <p>
* The mouse pointer may not visually move on some platforms,
* while the subsequent mousePress and mouseRelease can be
* delivered to the correct location
*
* @param x X position
* @param y Y position
*/
Expand Down Expand Up @@ -386,22 +428,50 @@ private void checkKeycodeArgument(int keycode) {

/**
* Returns the color of a pixel at the given screen coordinates.
* <p>
* If the desktop environment requires that permissions be granted
* to capture screen content, and the required permissions are not granted,
* then a {@code SecurityException} may be thrown,
* or the content of the returned {@code Color} is undefined.
* </p>
* @apiNote It is recommended to avoid calling this method on
* the AWT Event Dispatch Thread since screen capture may be a lengthy
* operation, particularly if acquiring permissions is needed and involves
* user interaction.
*
* @param x X position of pixel
* @param y Y position of pixel
* @throws SecurityException if {@code readDisplayPixels} permission
* is not granted, or access to the screen is denied
* by the desktop environment
* @return Color of the pixel
*/
public synchronized Color getPixelColor(int x, int y) {
checkScreenCaptureAllowed();
Color color = new Color(peer.getRGBPixel(x, y));
return color;
}

/**
* Creates an image containing pixels read from the screen. This image does
* not include the mouse cursor.
* Creates an image containing pixels read from the screen.
* <p>
* If the desktop environment requires that permissions be granted
* to capture screen content, and the required permissions are not granted,
* then a {@code SecurityException} may be thrown,
* or the contents of the returned {@code BufferedImage} are undefined.
* </p>
* @apiNote It is recommended to avoid calling this method on
* the AWT Event Dispatch Thread since screen capture may be a lengthy
* operation, particularly if acquiring permissions is needed and involves
* user interaction.
*
* @param screenRect Rect to capture in screen coordinates
* @return The captured image
* @throws IllegalArgumentException if <code>screenRect</code> width and height are not greater than zero
* @throws SecurityException if <code>readDisplayPixels</code> permission is not granted
* @throws IllegalArgumentException if {@code screenRect} width and height
* are not greater than zero
* @throws SecurityException if {@code readDisplayPixels} permission
* is not granted, or access to the screen is denied
* by the desktop environment
* @see SecurityManager#checkPermission
* @see AWTPermission
*/
Expand Down

3 comments on commit c0a876a

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@azvegint
Copy link
Member

Choose a reason for hiding this comment

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

/backport jdk8u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on c0a876a Jul 16, 2024

Choose a reason for hiding this comment

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

@azvegint the backport was successfully created on the branch backport-azvegint-c0a876a5-master in my personal fork of openjdk/jdk8u-dev. To create a pull request with this backport targeting openjdk/jdk8u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit c0a876a5 from the openjdk/jdk8u-ri repository.

The commit being backported was authored by Alexander Zvegintsev on 16 May 2024 and was reviewed by Phil Race, Iris Clark, Lance Andersen and Andrew John Hughes.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk8u-dev:

$ git fetch https://github.com/openjdk-bots/jdk8u-dev.git backport-azvegint-c0a876a5-master:backport-azvegint-c0a876a5-master
$ git checkout backport-azvegint-c0a876a5-master
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk8u-dev.git backport-azvegint-c0a876a5-master

⚠️ @azvegint You are not yet a collaborator in my fork openjdk-bots/jdk8u-dev. An invite will be sent out and you need to accept it before you can proceed.

Please sign in to comment.