Skip to content

Commit

Permalink
8254825: Monitoring available clipboard formats should be done via ne…
Browse files Browse the repository at this point in the history
…w Windows APIs

Reviewed-by: serb
  • Loading branch information
xavery authored and mrserb committed Oct 26, 2020
1 parent de05b00 commit b498433
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 39 deletions.
28 changes: 4 additions & 24 deletions src/java.desktop/windows/native/libawt/windows/awt_Clipboard.cpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2020, 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 @@ -42,9 +42,7 @@ jobject AwtClipboard::theCurrentClipboard;
BOOL AwtClipboard::isGettingOwnership = FALSE;

volatile jmethodID AwtClipboard::handleContentsChangedMID;
volatile BOOL AwtClipboard::skipInitialWmDrawClipboardMsg = TRUE;
volatile BOOL AwtClipboard::isClipboardViewerRegistered = FALSE;
volatile HWND AwtClipboard::hwndNextViewer = NULL;

#define GALLOCFLG (GMEM_DDESHARE | GMEM_MOVEABLE | GMEM_ZEROINIT)

Expand All @@ -59,27 +57,11 @@ void AwtClipboard::LostOwnership(JNIEnv *env) {
}
}

void AwtClipboard::WmChangeCbChain(WPARAM wParam, LPARAM lParam) {
if ((HWND)wParam == hwndNextViewer) {
hwndNextViewer = (HWND)lParam;
} else if (hwndNextViewer != NULL) {
::SendMessage(hwndNextViewer, WM_CHANGECBCHAIN, wParam, lParam);
}
}

void AwtClipboard::WmDrawClipboard(JNIEnv *env, WPARAM wParam, LPARAM lParam) {
if (skipInitialWmDrawClipboardMsg) {
// skipping the first contents change notification as it comes
// immediately after registering the clipboard viewer window
// and it is not caused by an actual contents change.
skipInitialWmDrawClipboardMsg = FALSE;
return;
}
void AwtClipboard::WmClipboardUpdate(JNIEnv *env) {
if (theCurrentClipboard != NULL) {
env->CallVoidMethod(theCurrentClipboard, handleContentsChangedMID);
DASSERT(!safe_ExceptionOccurred(env));
}
::SendMessage(hwndNextViewer, WM_DRAWCLIPBOARD, wParam, lParam);
}

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

hwndNextViewer = ::SetClipboardViewer(AwtToolkit::GetInstance().GetHWnd());
::AddClipboardFormatListener(AwtToolkit::GetInstance().GetHWnd());
isClipboardViewerRegistered = TRUE;
}

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

if (isClipboardViewerRegistered) {
::ChangeClipboardChain(AwtToolkit::GetInstance().GetHWnd(), AwtClipboard::hwndNextViewer);
AwtClipboard::hwndNextViewer = NULL;
::RemoveClipboardFormatListener(AwtToolkit::GetInstance().GetHWnd());
isClipboardViewerRegistered = FALSE;
skipInitialWmDrawClipboardMsg = TRUE;
}

CATCH_BAD_ALLOC;
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2003, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2020, 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 @@ -36,10 +36,7 @@
class AwtClipboard {
private:
static BOOL isGettingOwnership;
// handle to the next window in the clipboard viewer chain
static volatile HWND hwndNextViewer;
static volatile BOOL isClipboardViewerRegistered;
static volatile BOOL skipInitialWmDrawClipboardMsg;
static volatile jmethodID handleContentsChangedMID;

public:
Expand All @@ -57,8 +54,7 @@ class AwtClipboard {
}

static void LostOwnership(JNIEnv *env);
static void WmChangeCbChain(WPARAM wparam, LPARAM lparam);
static void WmDrawClipboard(JNIEnv *env, WPARAM wparam, LPARAM lparam);
static void WmClipboardUpdate(JNIEnv *env);
static void RegisterClipboardViewer(JNIEnv *env, jobject jclipboard);
static void UnregisterClipboardViewer(JNIEnv *env);
};
Expand Down
Expand Up @@ -1067,12 +1067,8 @@ LRESULT CALLBACK AwtToolkit::WndProc(HWND hWnd, UINT message,
AwtClipboard::LostOwnership((JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2));
return 0;
}
case WM_CHANGECBCHAIN: {
AwtClipboard::WmChangeCbChain(wParam, lParam);
return 0;
}
case WM_DRAWCLIPBOARD: {
AwtClipboard::WmDrawClipboard((JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2), wParam, lParam);
case WM_CLIPBOARDUPDATE: {
AwtClipboard::WmClipboardUpdate((JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2));
return 0;
}
case WM_AWT_LIST_SETMULTISELECT: {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2020, 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 @@ -83,7 +83,7 @@ public void flavorsChanged(FlavorEvent e) {
}
});

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

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

if (!flavorChangedMonitor.await(10, TimeUnit.SECONDS)) {
throw new RuntimeException("No LostOwnership event received.");
throw new RuntimeException("No FlavorsChanged event received.");
};

if (!content.equals("pong")) {
Expand Down

1 comment on commit b498433

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented on b498433 Oct 26, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.