1 /* 2 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.lwawt.macosx; 27 28 import java.awt.*; 29 import java.awt.datatransfer.*; 30 import java.io.IOException; 31 import java.io.NotSerializableException; 32 import java.util.*; 33 34 import sun.awt.datatransfer.*; 35 36 37 /** 38 * A class which interfaces with Cocoa's pasteboard in order to support 39 * data transfer via Clipboard operations. Most of the work is provided by 40 * sun.awt.datatransfer.DataTransferer. 41 */ 42 43 final class CClipboard extends SunClipboard { 44 45 public CClipboard(String name) { 46 super(name); 47 } 48 49 @Override 50 public long getID() { 51 return 0; 52 } 53 54 @Override 55 protected void clearNativeContext() { 56 // Leaving Empty, as WClipboard.clearNativeContext is empty as well. 57 } 58 59 @Override 60 protected void setContentsNative(Transferable contents) { 61 FlavorTable flavorMap = getDefaultFlavorTable(); 62 // Don't use delayed Clipboard rendering for the Transferable's data. 63 // If we did that, we would call Transferable.getTransferData on 64 // the Toolkit thread, which is a security hole. 65 // 66 // Get all of the target formats into which the Transferable can be 67 // translated. Then, for each format, translate the data and post 68 // it to the Clipboard. 69 DataTransferer dataTransferer = DataTransferer.getInstance(); 70 long[] formatArray = dataTransferer.getFormatsForTransferableAsArray(contents, flavorMap); 71 declareTypes(formatArray, this); 72 73 Map<Long, DataFlavor> formatMap = dataTransferer.getFormatsForTransferable(contents, flavorMap); 74 for (Map.Entry<Long, DataFlavor> entry : formatMap.entrySet()) { 75 long format = entry.getKey(); 76 DataFlavor flavor = entry.getValue(); 77 78 try { 79 byte[] bytes = DataTransferer.getInstance().translateTransferable(contents, flavor, format); 80 setData(bytes, format); 81 } catch (IOException e) { 82 // Fix 4696186: don't print exception if data with 83 // javaJVMLocalObjectMimeType failed to serialize. 84 // May remove this if-check when 5078787 is fixed. 85 if (!(flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType) && 86 e instanceof NotSerializableException)) { 87 e.printStackTrace(); 88 } 89 } 90 } 91 92 notifyChanged(); 93 } 94 95 @Override 96 protected native long[] getClipboardFormats(); 97 @Override 98 protected native byte[] getClipboardData(long format) throws IOException; 99 100 // 1.5 peer method 101 @Override 102 protected void unregisterClipboardViewerChecked() { 103 // no-op because we lack OS support. This requires 4048791, which requires 4048792 104 } 105 106 // 1.5 peer method 107 @Override 108 protected void registerClipboardViewerChecked() { 109 // no-op because we lack OS support. This requires 4048791, which requires 4048792 110 } 111 112 // 1.5 peer method 113 // no-op. This appears to be win32 specific. Filed 4048790 for investigation 114 //protected Transferable createLocaleTransferable(long[] formats) throws IOException; 115 116 private native void declareTypes(long[] formats, SunClipboard newOwner); 117 private native void setData(byte[] data, long format); 118 119 /** 120 * Invokes native check whether a change count on the general pasteboard is different 121 * than when we set it. The different count value means the current owner lost 122 * pasteboard ownership and someone else put data on the clipboard. 123 * @since 1.7 124 */ 125 native void checkPasteboard(); 126 127 /*** Native Callbacks ***/ 128 private void notifyLostOwnership() { 129 lostOwnershipImpl(); 130 } 131 132 private static void notifyChanged() { 133 CClipboard clipboard = (CClipboard) Toolkit.getDefaultToolkit().getSystemClipboard(); 134 if (!clipboard.areFlavorListenersRegistered()) { 135 return; 136 } 137 clipboard.checkChange(clipboard.getClipboardFormats()); 138 } 139 }