Skip to content

Commit fadce2f

Browse files
author
Alexander Zvegintsev
committedMay 8, 2024
8307779: Relax the java.awt.Robot specification
Reviewed-by: prr, iris, andrew Backport-of: 21aa057fafd88f4dd677bbf7c99a7eabb6b18409
1 parent c2c9e7f commit fadce2f

File tree

1 file changed

+79
-8
lines changed

1 file changed

+79
-8
lines changed
 

Diff for: ‎src/java.desktop/share/classes/java/awt/Robot.java

+79-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -68,6 +68,43 @@
6868
* <p>
6969
* Applications that use Robot for purposes other than self-testing should
7070
* handle these error conditions gracefully.
71+
* <p>
72+
* Platforms and desktop environments may impose restrictions or limitations
73+
* on the access required to implement all functionality in the Robot class.
74+
* For example:
75+
* <ul>
76+
* <li> preventing access to the contents of any part of a desktop
77+
* or Window on the desktop that is not owned by the running application.</li>
78+
* <li> treating window decorations as non-owned content.</li>
79+
* <li> ignoring or limiting specific requests to manipulate windows.</li>
80+
* <li> ignoring or limiting specific requests for Robot generated (synthesized)
81+
* events related to keyboard and mouse etc.</li>
82+
* <li> requiring specific or global permissions to any access to window
83+
* contents, even application owned content,or to perform even limited
84+
* synthesizing of events.</li>
85+
* </ul>
86+
*
87+
* The Robot API specification requires that approvals for these be granted
88+
* for full operation.
89+
* If they are not granted, the API will be degraded as discussed here.
90+
* Relevant specific API methods may document more specific limitations
91+
* and requirements.
92+
* Depending on the policies of the desktop environment,
93+
* the approvals mentioned above may:
94+
* <ul>
95+
* <li>be required every time</li>
96+
* <li>or persistent for the lifetime of an application,</li>
97+
* <li>or persistent across multiple user desktop sessions</li>
98+
* <li>be fine-grained permissions</li>
99+
* <li>be associated with a specific binary application,
100+
* or a class of binary applications.</li>
101+
* </ul>
102+
*
103+
* When such approvals need to given interactively, it may impede the normal
104+
* operation of the application until approved, and if approval is denied
105+
* or not possible, or cannot be made persistent then it will degrade
106+
* the functionality of this class and in turn any part of the operation
107+
* of the application which is dependent on it.
71108
*
72109
* @author Robi Khan
73110
* @since 1.3
@@ -189,6 +226,11 @@ private static void checkIsScreenDevice(GraphicsDevice device) {
189226

190227
/**
191228
* Moves mouse pointer to given screen coordinates.
229+
* <p>
230+
* The mouse pointer may not visually move on some platforms,
231+
* while the subsequent mousePress and mouseRelease can be
232+
* delivered to the correct location
233+
*
192234
* @param x X position
193235
* @param y Y position
194236
*/
@@ -383,8 +425,22 @@ private static void checkKeycodeArgument(int keycode) {
383425

384426
/**
385427
* Returns the color of a pixel at the given screen coordinates.
428+
* <p>
429+
* If the desktop environment requires that permissions be granted
430+
* to capture screen content, and the required permissions are not granted,
431+
* then a {@code SecurityException} may be thrown,
432+
* or the content of the returned {@code Color} is undefined.
433+
* </p>
434+
* @apiNote It is recommended to avoid calling this method on
435+
* the AWT Event Dispatch Thread since screen capture may be a lengthy
436+
* operation, particularly if acquiring permissions is needed and involves
437+
* user interaction.
438+
*
386439
* @param x X position of pixel
387440
* @param y Y position of pixel
441+
* @throws SecurityException if {@code readDisplayPixels} permission
442+
* is not granted, or access to the screen is denied
443+
* by the desktop environment
388444
* @return Color of the pixel
389445
*/
390446
public synchronized Color getPixelColor(int x, int y) {
@@ -395,12 +451,25 @@ public synchronized Color getPixelColor(int x, int y) {
395451
}
396452

397453
/**
398-
* Creates an image containing pixels read from the screen. This image does
399-
* not include the mouse cursor.
454+
* Creates an image containing pixels read from the screen.
455+
* <p>
456+
* If the desktop environment requires that permissions be granted
457+
* to capture screen content, and the required permissions are not granted,
458+
* then a {@code SecurityException} may be thrown,
459+
* or the contents of the returned {@code BufferedImage} are undefined.
460+
* </p>
461+
* @apiNote It is recommended to avoid calling this method on
462+
* the AWT Event Dispatch Thread since screen capture may be a lengthy
463+
* operation, particularly if acquiring permissions is needed and involves
464+
* user interaction.
465+
*
400466
* @param screenRect Rect to capture in screen coordinates
401467
* @return The captured image
402-
* @throws IllegalArgumentException if {@code screenRect} width and height are not greater than zero
403-
* @throws SecurityException if {@code readDisplayPixels} permission is not granted
468+
* @throws IllegalArgumentException if {@code screenRect} width and height
469+
* are not greater than zero
470+
* @throws SecurityException if {@code readDisplayPixels} permission
471+
* is not granted, or access to the screen is denied
472+
* by the desktop environment
404473
* @see SecurityManager#checkPermission
405474
* @see AWTPermission
406475
*/
@@ -410,7 +479,6 @@ public synchronized BufferedImage createScreenCapture(Rectangle screenRect) {
410479

411480
/**
412481
* Creates an image containing pixels read from the screen.
413-
* This image does not include the mouse cursor.
414482
* This method can be used in case there is a scaling transform
415483
* from user space to screen (device) space.
416484
* Typically this means that the display is a high resolution screen,
@@ -443,8 +511,11 @@ public synchronized BufferedImage createScreenCapture(Rectangle screenRect) {
443511
* }</pre>
444512
* @param screenRect Rect to capture in screen coordinates
445513
* @return The captured image
446-
* @throws IllegalArgumentException if {@code screenRect} width and height are not greater than zero
447-
* @throws SecurityException if {@code readDisplayPixels} permission is not granted
514+
* @throws IllegalArgumentException if {@code screenRect} width and height
515+
* are not greater than zero
516+
* @throws SecurityException if {@code readDisplayPixels} permission
517+
* is not granted, or access to the screen is denied
518+
* by the desktop environment
448519
* @see SecurityManager#checkPermission
449520
* @see AWTPermission
450521
*

0 commit comments

Comments
 (0)