|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "nsBaseClipboard.h" |
|
7 |
|
8 #include "nsIClipboardOwner.h" |
|
9 #include "nsCOMPtr.h" |
|
10 #include "nsXPCOM.h" |
|
11 #include "nsISupportsPrimitives.h" |
|
12 |
|
13 nsBaseClipboard::nsBaseClipboard() |
|
14 { |
|
15 mClipboardOwner = nullptr; |
|
16 mTransferable = nullptr; |
|
17 mIgnoreEmptyNotification = false; |
|
18 mEmptyingForSetData = false; |
|
19 } |
|
20 |
|
21 nsBaseClipboard::~nsBaseClipboard() |
|
22 { |
|
23 EmptyClipboard(kSelectionClipboard); |
|
24 EmptyClipboard(kGlobalClipboard); |
|
25 EmptyClipboard(kFindClipboard); |
|
26 } |
|
27 |
|
28 NS_IMPL_ISUPPORTS(nsBaseClipboard, nsIClipboard) |
|
29 |
|
30 /** |
|
31 * Sets the transferable object |
|
32 * |
|
33 */ |
|
34 NS_IMETHODIMP nsBaseClipboard::SetData(nsITransferable * aTransferable, nsIClipboardOwner * anOwner, |
|
35 int32_t aWhichClipboard) |
|
36 { |
|
37 NS_ASSERTION ( aTransferable, "clipboard given a null transferable" ); |
|
38 |
|
39 if (aTransferable == mTransferable && anOwner == mClipboardOwner) |
|
40 return NS_OK; |
|
41 bool selectClipPresent; |
|
42 SupportsSelectionClipboard(&selectClipPresent); |
|
43 bool findClipPresent; |
|
44 SupportsFindClipboard(&findClipPresent); |
|
45 if ( !selectClipPresent && !findClipPresent && aWhichClipboard != kGlobalClipboard ) |
|
46 return NS_ERROR_FAILURE; |
|
47 |
|
48 mEmptyingForSetData = true; |
|
49 EmptyClipboard(aWhichClipboard); |
|
50 mEmptyingForSetData = false; |
|
51 |
|
52 mClipboardOwner = anOwner; |
|
53 if ( anOwner ) |
|
54 NS_ADDREF(mClipboardOwner); |
|
55 |
|
56 mTransferable = aTransferable; |
|
57 |
|
58 nsresult rv = NS_ERROR_FAILURE; |
|
59 |
|
60 if ( mTransferable ) { |
|
61 NS_ADDREF(mTransferable); |
|
62 if (!mPrivacyHandler) { |
|
63 rv = NS_NewClipboardPrivacyHandler(getter_AddRefs(mPrivacyHandler)); |
|
64 NS_ENSURE_SUCCESS(rv, rv); |
|
65 } |
|
66 rv = mPrivacyHandler->PrepareDataForClipboard(mTransferable); |
|
67 NS_ENSURE_SUCCESS(rv, rv); |
|
68 rv = SetNativeClipboardData(aWhichClipboard); |
|
69 } |
|
70 |
|
71 return rv; |
|
72 } |
|
73 |
|
74 /** |
|
75 * Gets the transferable object |
|
76 * |
|
77 */ |
|
78 NS_IMETHODIMP nsBaseClipboard::GetData(nsITransferable * aTransferable, int32_t aWhichClipboard) |
|
79 { |
|
80 NS_ASSERTION ( aTransferable, "clipboard given a null transferable" ); |
|
81 |
|
82 bool selectClipPresent; |
|
83 SupportsSelectionClipboard(&selectClipPresent); |
|
84 bool findClipPresent; |
|
85 SupportsFindClipboard(&findClipPresent); |
|
86 if ( !selectClipPresent && !findClipPresent && aWhichClipboard != kGlobalClipboard ) |
|
87 return NS_ERROR_FAILURE; |
|
88 |
|
89 if ( aTransferable ) |
|
90 return GetNativeClipboardData(aTransferable, aWhichClipboard); |
|
91 |
|
92 return NS_ERROR_FAILURE; |
|
93 } |
|
94 |
|
95 NS_IMETHODIMP nsBaseClipboard::EmptyClipboard(int32_t aWhichClipboard) |
|
96 { |
|
97 bool selectClipPresent; |
|
98 SupportsSelectionClipboard(&selectClipPresent); |
|
99 bool findClipPresent; |
|
100 SupportsFindClipboard(&findClipPresent); |
|
101 if ( !selectClipPresent && !findClipPresent && aWhichClipboard != kGlobalClipboard ) |
|
102 return NS_ERROR_FAILURE; |
|
103 |
|
104 if (mIgnoreEmptyNotification) |
|
105 return NS_OK; |
|
106 |
|
107 if ( mClipboardOwner ) { |
|
108 mClipboardOwner->LosingOwnership(mTransferable); |
|
109 NS_RELEASE(mClipboardOwner); |
|
110 } |
|
111 |
|
112 NS_IF_RELEASE(mTransferable); |
|
113 |
|
114 return NS_OK; |
|
115 } |
|
116 |
|
117 NS_IMETHODIMP |
|
118 nsBaseClipboard::HasDataMatchingFlavors(const char** aFlavorList, |
|
119 uint32_t aLength, |
|
120 int32_t aWhichClipboard, |
|
121 bool* outResult) |
|
122 { |
|
123 *outResult = true; // say we always do. |
|
124 return NS_OK; |
|
125 } |
|
126 |
|
127 NS_IMETHODIMP |
|
128 nsBaseClipboard::SupportsSelectionClipboard(bool* _retval) |
|
129 { |
|
130 *_retval = false; // we don't support the selection clipboard by default. |
|
131 return NS_OK; |
|
132 } |
|
133 |
|
134 NS_IMETHODIMP |
|
135 nsBaseClipboard::SupportsFindClipboard(bool* _retval) |
|
136 { |
|
137 *_retval = false; // we don't support the find clipboard by default. |
|
138 return NS_OK; |
|
139 } |