Skip to content
Permalink
Browse files

8254825: Monitoring available clipboard formats should be done via ne…

…w Windows APIs

Reviewed-by: serb
  • Loading branch information
xavery authored and mrserb committed Oct 26, 2020
1 parent de05b00 commit b4984336319adf8762f9812e28fb7f1ddecaee56
@@ -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
@@ -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)

@@ -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) {
@@ -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;
@@ -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
@@ -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:
@@ -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);
};
@@ -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: {
@@ -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
@@ -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 = "";
@@ -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")) {

1 comment on commit b498433

@bridgekeeper

This comment has been minimized.

Copy link

@bridgekeeper bridgekeeper bot commented on b498433 Oct 26, 2020

Please sign in to comment.