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
26package sun.lwawt.macosx;
27
28import java.awt.*;
29import java.awt.datatransfer.*;
30import java.io.IOException;
31import java.io.NotSerializableException;
32import java.util.*;
33
34import 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
43final 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    public synchronized Transferable getContents(Object requestor) {
61        checkPasteboardAndNotify();
62        return super.getContents(requestor);
63    }
64
65    @Override
66    protected synchronized Transferable getContextContents() {
67        checkPasteboardAndNotify();
68        return super.getContextContents();
69    }
70
71    @Override
72    protected void setContentsNative(Transferable contents) {
73        FlavorTable flavorMap = getDefaultFlavorTable();
74        // Don't use delayed Clipboard rendering for the Transferable's data.
75        // If we did that, we would call Transferable.getTransferData on
76        // the Toolkit thread, which is a security hole.
77        //
78        // Get all of the target formats into which the Transferable can be
79        // translated. Then, for each format, translate the data and post
80        // it to the Clipboard.
81        DataTransferer dataTransferer = DataTransferer.getInstance();
82        long[] formatArray = dataTransferer.getFormatsForTransferableAsArray(contents, flavorMap);
83        declareTypes(formatArray, this);
84
85        Map<Long, DataFlavor> formatMap = dataTransferer.getFormatsForTransferable(contents, flavorMap);
86        for (Map.Entry<Long, DataFlavor> entry : formatMap.entrySet()) {
87            long format = entry.getKey();
88            DataFlavor flavor = entry.getValue();
89
90            try {
91                byte[] bytes = DataTransferer.getInstance().translateTransferable(contents, flavor, format);
92                setData(bytes, format);
93            } catch (IOException e) {
94                // Fix 4696186: don't print exception if data with
95                // javaJVMLocalObjectMimeType failed to serialize.
96                // May remove this if-check when 5078787 is fixed.
97                if (!(flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType) &&
98                        e instanceof NotSerializableException)) {
99                    e.printStackTrace();
100                }
101            }
102        }
103
104        notifyChanged();
105    }
106
107    @Override
108    protected native long[] getClipboardFormats();
109    @Override
110    protected native byte[] getClipboardData(long format) throws IOException;
111
112    // 1.5 peer method
113    @Override
114    protected void unregisterClipboardViewerChecked() {
115        // no-op because we lack OS support. This requires 4048791, which requires 4048792
116    }
117
118    // 1.5 peer method
119    @Override
120    protected void registerClipboardViewerChecked()    {
121        // no-op because we lack OS support. This requires 4048791, which requires 4048792
122    }
123
124    // 1.5 peer method
125    // no-op. This appears to be win32 specific. Filed 4048790 for investigation
126    //protected Transferable createLocaleTransferable(long[] formats) throws IOException;
127
128    private native void declareTypes(long[] formats, SunClipboard newOwner);
129    private native void setData(byte[] data, long format);
130
131    void checkPasteboardAndNotify() {
132        if (checkPasteboardWithoutNotification()) {
133            notifyChanged();
134            lostOwnershipNow(null);
135        }
136    }
137
138    /**
139     * Invokes native check whether a change count on the general pasteboard is different
140     * than when we set it. The different count value means the current owner lost
141     * pasteboard ownership and someone else put data on the clipboard.
142     * @since 1.7
143     */
144    native boolean checkPasteboardWithoutNotification();
145
146    /*** Native Callbacks ***/
147    private void notifyLostOwnership() {
148        lostOwnershipImpl();
149    }
150
151    private static void notifyChanged() {
152        CClipboard clipboard = (CClipboard) Toolkit.getDefaultToolkit().getSystemClipboard();
153        if (!clipboard.areFlavorListenersRegistered()) {
154            return;
155        }
156        clipboard.checkChange(clipboard.getClipboardFormats());
157    }
158}
159