Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8335267: [XWayland] move screencast tokens from .awt to .java folder #166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
91 changes: 53 additions & 38 deletions src/java.desktop/unix/classes/sun/awt/screencast/TokenStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,35 +60,33 @@
* The restore token allows the ScreenCast session to be restored
* with previously granted screen access permissions.
*/
@SuppressWarnings("removal")
final class TokenStorage {

private TokenStorage() {}

private static final String REL_NAME =
".java/robot/screencast-tokens.properties";
private static final String REL_NAME_SECONDARY =
".awt/robot/screencast-tokens.properties";

private static final Properties PROPS = new Properties();
private static final Path PROPS_PATH;
private static final Path PROP_FILENAME;

@SuppressWarnings("removal")
private static void doPrivilegedRunnable(Runnable runnable) {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override
public Void run() {
runnable.run();
return null;
}
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
runnable.run();
return null;
});
}

static {
PROPS_PATH = AccessController.doPrivileged(new PrivilegedAction<Path>() {
@Override
public Path run() {
return setupPath();
}
});
@SuppressWarnings("removal")
Path propsPath = AccessController
.doPrivileged((PrivilegedAction<Path>) () -> setupPath());

PROPS_PATH = propsPath;

if (PROPS_PATH != null) {
PROP_FILENAME = PROPS_PATH.getFileName();
Expand All @@ -110,25 +108,32 @@ private static Path setupPath() {
}

Path path = Path.of(userHome, REL_NAME);
Path secondaryPath = Path.of(userHome, REL_NAME_SECONDARY);

boolean copyFromSecondary = !Files.isWritable(path)
&& Files.isWritable(secondaryPath);

Path workdir = path.getParent();

if (!Files.exists(workdir)) {
try {
Files.createDirectories(workdir);
} catch (Exception e) {
if (SCREENCAST_DEBUG) {
System.err.printf("Token storage: cannot create" +
" directory %s %s\n", workdir, e);
if (!Files.isWritable(path)) {
if (!Files.exists(workdir)) {
try {
Files.createDirectories(workdir);
} catch (Exception e) {
if (SCREENCAST_DEBUG) {
System.err.printf("Token storage: cannot create" +
" directory %s %s\n", workdir, e);
}
return null;
}
return null;
}
}

if (!Files.isWritable(workdir)) {
if (SCREENCAST_DEBUG) {
System.err.printf("Token storage: %s is not writable\n", workdir);
if (!Files.isWritable(workdir)) {
if (SCREENCAST_DEBUG) {
System.err.printf("Token storage: %s is not writable\n", workdir);
}
return null;
}
return null;
}

try {
Expand All @@ -145,7 +150,17 @@ private static Path setupPath() {
}
}

if (Files.exists(path)) {
if (copyFromSecondary) {
if (SCREENCAST_DEBUG) {
System.out.println("Token storage: copying from the secondary location "
+ secondaryPath);
}
synchronized (PROPS) {
if (readTokens(secondaryPath)) {
store(path, "copy from the secondary location");
}
}
} else if (Files.exists(path)) {
if (!setFilePermission(path)) {
return null;
}
Expand Down Expand Up @@ -302,7 +317,7 @@ private static void storeTokenFromNative(String oldToken,
}

if (changed) {
doPrivilegedRunnable(() -> store("save tokens"));
doPrivilegedRunnable(() -> store(PROPS_PATH, "save tokens"));
}
}
}
Expand All @@ -315,7 +330,7 @@ private static boolean readTokens(Path path) {
PROPS.clear();
PROPS.load(reader);
}
} catch (IOException e) {
} catch (IOException | IllegalArgumentException e) {
if (SCREENCAST_DEBUG) {
System.err.printf("""
Token storage: failed to load property file %s
Expand Down Expand Up @@ -410,7 +425,7 @@ static Set<TokenItem> getTokens(List<Rectangle> affectedScreenBounds) {
}

private static void removeMalformedRecords(Set<String> malformedRecords) {
if (!isWritable()
if (!isWritable(PROPS_PATH)
|| malformedRecords == null
|| malformedRecords.isEmpty()) {
return;
Expand All @@ -424,17 +439,17 @@ private static void removeMalformedRecords(Set<String> malformedRecords) {
}
}

store("remove malformed records");
store(PROPS_PATH, "remove malformed records");
}
}

private static void store(String failMsg) {
if (!isWritable()) {
private static void store(Path path, String failMsg) {
if (!isWritable(path)) {
return;
}

synchronized (PROPS) {
try (BufferedWriter writer = Files.newBufferedWriter(PROPS_PATH)) {
try (BufferedWriter writer = Files.newBufferedWriter(path)) {
PROPS.store(writer, null);
} catch (IOException e) {
if (SCREENCAST_DEBUG) {
Expand All @@ -445,13 +460,13 @@ private static void store(String failMsg) {
}
}

private static boolean isWritable() {
if (PROPS_PATH == null
|| (Files.exists(PROPS_PATH) && !Files.isWritable(PROPS_PATH))) {
private static boolean isWritable(Path path) {
if (path == null
|| (Files.exists(path) && !Files.isWritable(path))) {

if (SCREENCAST_DEBUG) {
System.err.printf(
"Token storage: %s is not writable\n", PROPS_PATH);
"Token storage: %s is not writable\n", path);
}
return false;
} else {
Expand Down