Skip to content

Commit 163309f

Browse files
author
Alexander Zvegintsev
committedJul 18, 2024
8307779: Relax the java.awt.Robot specification
Reviewed-by: phh, prr Backport-of: 42eb684707287a41a9922519de6b08aff1d83eae
1 parent 1784030 commit 163309f

File tree

1 file changed

+79
-8
lines changed

1 file changed

+79
-8
lines changed
 

‎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, 2019, 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
@@ -180,6 +217,11 @@ private void checkIsScreenDevice(GraphicsDevice device) {
180217

181218
/**
182219
* Moves mouse pointer to given screen coordinates.
220+
* <p>
221+
* The mouse pointer may not visually move on some platforms,
222+
* while the subsequent mousePress and mouseRelease can be
223+
* delivered to the correct location
224+
*
183225
* @param x X position
184226
* @param y Y position
185227
*/
@@ -374,8 +416,22 @@ private void checkKeycodeArgument(int keycode) {
374416

375417
/**
376418
* Returns the color of a pixel at the given screen coordinates.
419+
* <p>
420+
* If the desktop environment requires that permissions be granted
421+
* to capture screen content, and the required permissions are not granted,
422+
* then a {@code SecurityException} may be thrown,
423+
* or the content of the returned {@code Color} is undefined.
424+
* </p>
425+
* @apiNote It is recommended to avoid calling this method on
426+
* the AWT Event Dispatch Thread since screen capture may be a lengthy
427+
* operation, particularly if acquiring permissions is needed and involves
428+
* user interaction.
429+
*
377430
* @param x X position of pixel
378431
* @param y Y position of pixel
432+
* @throws SecurityException if {@code readDisplayPixels} permission
433+
* is not granted, or access to the screen is denied
434+
* by the desktop environment
379435
* @return Color of the pixel
380436
*/
381437
public synchronized Color getPixelColor(int x, int y) {
@@ -386,12 +442,25 @@ public synchronized Color getPixelColor(int x, int y) {
386442
}
387443

388444
/**
389-
* Creates an image containing pixels read from the screen. This image does
390-
* not include the mouse cursor.
445+
* Creates an image containing pixels read from the screen.
446+
* <p>
447+
* If the desktop environment requires that permissions be granted
448+
* to capture screen content, and the required permissions are not granted,
449+
* then a {@code SecurityException} may be thrown,
450+
* or the contents of the returned {@code BufferedImage} are undefined.
451+
* </p>
452+
* @apiNote It is recommended to avoid calling this method on
453+
* the AWT Event Dispatch Thread since screen capture may be a lengthy
454+
* operation, particularly if acquiring permissions is needed and involves
455+
* user interaction.
456+
*
391457
* @param screenRect Rect to capture in screen coordinates
392458
* @return The captured image
393-
* @throws IllegalArgumentException if {@code screenRect} width and height are not greater than zero
394-
* @throws SecurityException if {@code readDisplayPixels} permission is not granted
459+
* @throws IllegalArgumentException if {@code screenRect} width and height
460+
* are not greater than zero
461+
* @throws SecurityException if {@code readDisplayPixels} permission
462+
* is not granted, or access to the screen is denied
463+
* by the desktop environment
395464
* @see SecurityManager#checkPermission
396465
* @see AWTPermission
397466
*/
@@ -401,7 +470,6 @@ public synchronized BufferedImage createScreenCapture(Rectangle screenRect) {
401470

402471
/**
403472
* Creates an image containing pixels read from the screen.
404-
* This image does not include the mouse cursor.
405473
* This method can be used in case there is a scaling transform
406474
* from user space to screen (device) space.
407475
* Typically this means that the display is a high resolution screen,
@@ -434,8 +502,11 @@ public synchronized BufferedImage createScreenCapture(Rectangle screenRect) {
434502
* }</pre>
435503
* @param screenRect Rect to capture in screen coordinates
436504
* @return The captured image
437-
* @throws IllegalArgumentException if {@code screenRect} width and height are not greater than zero
438-
* @throws SecurityException if {@code readDisplayPixels} permission is not granted
505+
* @throws IllegalArgumentException if {@code screenRect} width and height
506+
* are not greater than zero
507+
* @throws SecurityException if {@code readDisplayPixels} permission
508+
* is not granted, or access to the screen is denied
509+
* by the desktop environment
439510
* @see SecurityManager#checkPermission
440511
* @see AWTPermission
441512
*

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Jul 18, 2024

@openjdk-notifier[bot]
Please sign in to comment.