Skip to content

Commit b498433

Browse files
lighterowlmrserb
authored andcommitted
8254825: Monitoring available clipboard formats should be done via new Windows APIs
Reviewed-by: serb
1 parent de05b00 commit b498433

File tree

4 files changed

+11
-39
lines changed

4 files changed

+11
-39
lines changed

src/java.desktop/windows/native/libawt/windows/awt_Clipboard.cpp

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2020, 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
@@ -42,9 +42,7 @@ jobject AwtClipboard::theCurrentClipboard;
4242
BOOL AwtClipboard::isGettingOwnership = FALSE;
4343

4444
volatile jmethodID AwtClipboard::handleContentsChangedMID;
45-
volatile BOOL AwtClipboard::skipInitialWmDrawClipboardMsg = TRUE;
4645
volatile BOOL AwtClipboard::isClipboardViewerRegistered = FALSE;
47-
volatile HWND AwtClipboard::hwndNextViewer = NULL;
4846

4947
#define GALLOCFLG (GMEM_DDESHARE | GMEM_MOVEABLE | GMEM_ZEROINIT)
5048

@@ -59,27 +57,11 @@ void AwtClipboard::LostOwnership(JNIEnv *env) {
5957
}
6058
}
6159

62-
void AwtClipboard::WmChangeCbChain(WPARAM wParam, LPARAM lParam) {
63-
if ((HWND)wParam == hwndNextViewer) {
64-
hwndNextViewer = (HWND)lParam;
65-
} else if (hwndNextViewer != NULL) {
66-
::SendMessage(hwndNextViewer, WM_CHANGECBCHAIN, wParam, lParam);
67-
}
68-
}
69-
70-
void AwtClipboard::WmDrawClipboard(JNIEnv *env, WPARAM wParam, LPARAM lParam) {
71-
if (skipInitialWmDrawClipboardMsg) {
72-
// skipping the first contents change notification as it comes
73-
// immediately after registering the clipboard viewer window
74-
// and it is not caused by an actual contents change.
75-
skipInitialWmDrawClipboardMsg = FALSE;
76-
return;
77-
}
60+
void AwtClipboard::WmClipboardUpdate(JNIEnv *env) {
7861
if (theCurrentClipboard != NULL) {
7962
env->CallVoidMethod(theCurrentClipboard, handleContentsChangedMID);
8063
DASSERT(!safe_ExceptionOccurred(env));
8164
}
82-
::SendMessage(hwndNextViewer, WM_DRAWCLIPBOARD, wParam, lParam);
8365
}
8466

8567
void AwtClipboard::RegisterClipboardViewer(JNIEnv *env, jobject jclipboard) {
@@ -96,18 +78,16 @@ void AwtClipboard::RegisterClipboardViewer(JNIEnv *env, jobject jclipboard) {
9678
env->GetMethodID(cls, "handleContentsChanged", "()V");
9779
DASSERT(AwtClipboard::handleContentsChangedMID != NULL);
9880

99-
hwndNextViewer = ::SetClipboardViewer(AwtToolkit::GetInstance().GetHWnd());
81+
::AddClipboardFormatListener(AwtToolkit::GetInstance().GetHWnd());
10082
isClipboardViewerRegistered = TRUE;
10183
}
10284

10385
void AwtClipboard::UnregisterClipboardViewer(JNIEnv *env) {
10486
TRY;
10587

10688
if (isClipboardViewerRegistered) {
107-
::ChangeClipboardChain(AwtToolkit::GetInstance().GetHWnd(), AwtClipboard::hwndNextViewer);
108-
AwtClipboard::hwndNextViewer = NULL;
89+
::RemoveClipboardFormatListener(AwtToolkit::GetInstance().GetHWnd());
10990
isClipboardViewerRegistered = FALSE;
110-
skipInitialWmDrawClipboardMsg = TRUE;
11191
}
11292

11393
CATCH_BAD_ALLOC;

src/java.desktop/windows/native/libawt/windows/awt_Clipboard.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2003, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2020, 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
@@ -36,10 +36,7 @@
3636
class AwtClipboard {
3737
private:
3838
static BOOL isGettingOwnership;
39-
// handle to the next window in the clipboard viewer chain
40-
static volatile HWND hwndNextViewer;
4139
static volatile BOOL isClipboardViewerRegistered;
42-
static volatile BOOL skipInitialWmDrawClipboardMsg;
4340
static volatile jmethodID handleContentsChangedMID;
4441

4542
public:
@@ -57,8 +54,7 @@ class AwtClipboard {
5754
}
5855

5956
static void LostOwnership(JNIEnv *env);
60-
static void WmChangeCbChain(WPARAM wparam, LPARAM lparam);
61-
static void WmDrawClipboard(JNIEnv *env, WPARAM wparam, LPARAM lparam);
57+
static void WmClipboardUpdate(JNIEnv *env);
6258
static void RegisterClipboardViewer(JNIEnv *env, jobject jclipboard);
6359
static void UnregisterClipboardViewer(JNIEnv *env);
6460
};

src/java.desktop/windows/native/libawt/windows/awt_Toolkit.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,12 +1067,8 @@ LRESULT CALLBACK AwtToolkit::WndProc(HWND hWnd, UINT message,
10671067
AwtClipboard::LostOwnership((JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2));
10681068
return 0;
10691069
}
1070-
case WM_CHANGECBCHAIN: {
1071-
AwtClipboard::WmChangeCbChain(wParam, lParam);
1072-
return 0;
1073-
}
1074-
case WM_DRAWCLIPBOARD: {
1075-
AwtClipboard::WmDrawClipboard((JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2), wParam, lParam);
1070+
case WM_CLIPBOARDUPDATE: {
1071+
AwtClipboard::WmClipboardUpdate((JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2));
10761072
return 0;
10771073
}
10781074
case WM_AWT_LIST_SETMULTISELECT: {

test/jdk/java/awt/datatransfer/ClipboardInterVMTest/ClipboardInterVMTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2020, 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
@@ -83,7 +83,7 @@ public void flavorsChanged(FlavorEvent e) {
8383
}
8484
});
8585

86-
System.out.println("Starting external clipborad modifier...");
86+
System.out.println("Starting external clipboard modifier...");
8787
new Thread(() -> runTest(ClipboardInterVMTest.class.getCanonicalName(), "pong")).start();
8888

8989
String content = "";
@@ -106,7 +106,7 @@ public void flavorsChanged(FlavorEvent e) {
106106
};
107107

108108
if (!flavorChangedMonitor.await(10, TimeUnit.SECONDS)) {
109-
throw new RuntimeException("No LostOwnership event received.");
109+
throw new RuntimeException("No FlavorsChanged event received.");
110110
};
111111

112112
if (!content.equals("pong")) {

0 commit comments

Comments
 (0)