1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Ehsan Akhgari.
19 : * Portions created by the Initial Developer are Copyright (C) 2009
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Ehsan Akhgari <ehsan.akhgari@gmail.com> (Original author)
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "mozilla/Util.h"
40 :
41 : #include "nsClipboardPrivacyHandler.h"
42 : #include "nsITransferable.h"
43 : #include "nsISupportsPrimitives.h"
44 : #include "nsIObserverService.h"
45 : #include "nsIClipboard.h"
46 : #include "nsComponentManagerUtils.h"
47 : #include "nsServiceManagerUtils.h"
48 : #include "nsLiteralString.h"
49 : #include "nsNetCID.h"
50 : #include "nsXPCOM.h"
51 : #include "mozilla/Services.h"
52 :
53 : #if defined(XP_WIN)
54 : #include <ole2.h>
55 : #endif
56 :
57 : using namespace mozilla;
58 :
59 : #define NS_MOZ_DATA_FROM_PRIVATEBROWSING "application/x-moz-private-browsing"
60 :
61 0 : NS_IMPL_ISUPPORTS2(nsClipboardPrivacyHandler, nsIObserver, nsISupportsWeakReference)
62 :
63 : nsresult
64 0 : nsClipboardPrivacyHandler::Init()
65 : {
66 : nsCOMPtr<nsIObserverService> observerService =
67 0 : mozilla::services::GetObserverService();
68 0 : if (!observerService)
69 0 : return NS_ERROR_FAILURE;
70 0 : return observerService->AddObserver(this, NS_PRIVATE_BROWSING_SWITCH_TOPIC,
71 0 : true);
72 : }
73 :
74 : /**
75 : * Prepare the transferable object to be inserted into the clipboard
76 : *
77 : */
78 : nsresult
79 0 : nsClipboardPrivacyHandler::PrepareDataForClipboard(nsITransferable * aTransferable)
80 : {
81 0 : NS_ASSERTION(aTransferable, "clipboard given a null transferable");
82 :
83 0 : nsresult rv = NS_OK;
84 0 : if (InPrivateBrowsing()) {
85 0 : nsCOMPtr<nsISupportsPRBool> data = do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID);
86 0 : if (data) {
87 0 : rv = data->SetData(true);
88 0 : NS_ENSURE_SUCCESS(rv, rv);
89 :
90 0 : rv = aTransferable->AddDataFlavor(NS_MOZ_DATA_FROM_PRIVATEBROWSING);
91 0 : NS_ENSURE_SUCCESS(rv, rv);
92 :
93 0 : rv = aTransferable->SetTransferData(NS_MOZ_DATA_FROM_PRIVATEBROWSING, data, sizeof(bool));
94 0 : NS_ENSURE_SUCCESS(rv, rv);
95 : }
96 : }
97 :
98 0 : return rv;
99 : }
100 :
101 : NS_IMETHODIMP
102 0 : nsClipboardPrivacyHandler::Observe(nsISupports *aSubject, char const *aTopic, PRUnichar const *aData)
103 : {
104 0 : if (NS_LITERAL_STRING(NS_PRIVATE_BROWSING_LEAVE).Equals(aData)) {
105 : nsresult rv;
106 : nsCOMPtr<nsIClipboard> clipboard =
107 0 : do_GetService("@mozilla.org/widget/clipboard;1", &rv);
108 0 : NS_ENSURE_SUCCESS(rv, rv);
109 :
110 0 : const char * flavors[] = { NS_MOZ_DATA_FROM_PRIVATEBROWSING };
111 : bool haveFlavors;
112 0 : rv = clipboard->HasDataMatchingFlavors(flavors,
113 : ArrayLength(flavors),
114 : nsIClipboard::kGlobalClipboard,
115 0 : &haveFlavors);
116 0 : if (NS_SUCCEEDED(rv) && haveFlavors) {
117 : #if defined(XP_WIN)
118 : // Workaround for bug 518412. On Windows 7 x64, there is a bug
119 : // in handling clipboard data without any formats between
120 : // 32-bit/64-bit boundaries, which could lead Explorer to crash.
121 : // We work around the problem by clearing the clipboard using
122 : // the usual Win32 API.
123 : NS_ENSURE_TRUE(SUCCEEDED(::OleSetClipboard(NULL)), NS_ERROR_FAILURE);
124 : #else
125 : // Empty the native clipboard by copying an empty transferable
126 : nsCOMPtr<nsITransferable> nullData =
127 0 : do_CreateInstance("@mozilla.org/widget/transferable;1", &rv);
128 0 : NS_ENSURE_SUCCESS(rv, rv);
129 0 : rv = clipboard->SetData(nullData, nsnull,
130 0 : nsIClipboard::kGlobalClipboard);
131 0 : NS_ENSURE_SUCCESS(rv, rv);
132 : #endif
133 : }
134 : }
135 :
136 0 : return NS_OK;
137 : }
138 :
139 : bool
140 0 : nsClipboardPrivacyHandler::InPrivateBrowsing()
141 : {
142 0 : bool inPrivateBrowsingMode = false;
143 0 : if (!mPBService)
144 0 : mPBService = do_GetService(NS_PRIVATE_BROWSING_SERVICE_CONTRACTID);
145 0 : if (mPBService)
146 0 : mPBService->GetPrivateBrowsingEnabled(&inPrivateBrowsingMode);
147 0 : return inPrivateBrowsingMode;
148 : }
149 :
150 : nsresult
151 0 : NS_NewClipboardPrivacyHandler(nsClipboardPrivacyHandler ** aHandler)
152 : {
153 0 : NS_PRECONDITION(aHandler != nsnull, "null ptr");
154 0 : if (!aHandler)
155 0 : return NS_ERROR_NULL_POINTER;
156 :
157 0 : *aHandler = new nsClipboardPrivacyHandler();
158 0 : if (!*aHandler)
159 0 : return NS_ERROR_OUT_OF_MEMORY;
160 :
161 0 : NS_ADDREF(*aHandler);
162 0 : nsresult rv = (*aHandler)->Init();
163 0 : if (NS_FAILED(rv))
164 0 : NS_RELEASE(*aHandler);
165 :
166 0 : return rv;
167 : }
|