1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/xpwidgets/nsClipboardPrivacyHandler.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,124 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "mozilla/ArrayUtils.h" 1.10 + 1.11 +#include "nsClipboardPrivacyHandler.h" 1.12 +#include "nsITransferable.h" 1.13 +#include "nsISupportsPrimitives.h" 1.14 +#include "nsIObserverService.h" 1.15 +#include "nsIClipboard.h" 1.16 +#include "nsComponentManagerUtils.h" 1.17 +#include "nsServiceManagerUtils.h" 1.18 +#include "nsLiteralString.h" 1.19 +#include "nsNetCID.h" 1.20 +#include "nsXPCOM.h" 1.21 +#include "mozilla/Services.h" 1.22 + 1.23 +#if defined(XP_WIN) 1.24 +#include <ole2.h> 1.25 +#endif 1.26 + 1.27 +using namespace mozilla; 1.28 + 1.29 +#define NS_MOZ_DATA_FROM_PRIVATEBROWSING "application/x-moz-private-browsing" 1.30 + 1.31 +NS_IMPL_ISUPPORTS(nsClipboardPrivacyHandler, nsIObserver, nsISupportsWeakReference) 1.32 + 1.33 +nsresult 1.34 +nsClipboardPrivacyHandler::Init() 1.35 +{ 1.36 + nsCOMPtr<nsIObserverService> observerService = 1.37 + mozilla::services::GetObserverService(); 1.38 + if (!observerService) 1.39 + return NS_ERROR_FAILURE; 1.40 + return observerService->AddObserver(this, "last-pb-context-exited", true); 1.41 +} 1.42 + 1.43 +/** 1.44 + * Prepare the transferable object to be inserted into the clipboard 1.45 + * 1.46 + */ 1.47 +nsresult 1.48 +nsClipboardPrivacyHandler::PrepareDataForClipboard(nsITransferable * aTransferable) 1.49 +{ 1.50 + NS_ASSERTION(aTransferable, "clipboard given a null transferable"); 1.51 + 1.52 + bool isPrivateData = false; 1.53 + aTransferable->GetIsPrivateData(&isPrivateData); 1.54 + 1.55 + nsresult rv = NS_OK; 1.56 + if (isPrivateData) { 1.57 + nsCOMPtr<nsISupportsPRBool> data = do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID); 1.58 + if (data) { 1.59 + rv = data->SetData(true); 1.60 + NS_ENSURE_SUCCESS(rv, rv); 1.61 + 1.62 + // Ignore the error code of AddDataFlavor, since we might have added 1.63 + // this flavor before. If this call really fails, so will the next 1.64 + // one (SetTransferData). 1.65 + aTransferable->AddDataFlavor(NS_MOZ_DATA_FROM_PRIVATEBROWSING); 1.66 + 1.67 + rv = aTransferable->SetTransferData(NS_MOZ_DATA_FROM_PRIVATEBROWSING, data, sizeof(bool)); 1.68 + NS_ENSURE_SUCCESS(rv, rv); 1.69 + } 1.70 + } 1.71 + 1.72 + return rv; 1.73 +} 1.74 + 1.75 +NS_IMETHODIMP 1.76 +nsClipboardPrivacyHandler::Observe(nsISupports *aSubject, char const *aTopic, char16_t const *aData) 1.77 +{ 1.78 + nsresult rv; 1.79 + nsCOMPtr<nsIClipboard> clipboard = 1.80 + do_GetService("@mozilla.org/widget/clipboard;1", &rv); 1.81 + NS_ENSURE_SUCCESS(rv, rv); 1.82 + 1.83 + const char * flavors[] = { NS_MOZ_DATA_FROM_PRIVATEBROWSING }; 1.84 + bool haveFlavors; 1.85 + rv = clipboard->HasDataMatchingFlavors(flavors, 1.86 + ArrayLength(flavors), 1.87 + nsIClipboard::kGlobalClipboard, 1.88 + &haveFlavors); 1.89 + if (NS_SUCCEEDED(rv) && haveFlavors) { 1.90 +#if defined(XP_WIN) 1.91 + // Workaround for bug 518412. On Windows 7 x64, there is a bug 1.92 + // in handling clipboard data without any formats between 1.93 + // 32-bit/64-bit boundaries, which could lead Explorer to crash. 1.94 + // We work around the problem by clearing the clipboard using 1.95 + // the usual Win32 API. 1.96 + NS_ENSURE_TRUE(SUCCEEDED(::OleSetClipboard(nullptr)), NS_ERROR_FAILURE); 1.97 +#else 1.98 + // Empty the native clipboard by copying an empty transferable 1.99 + nsCOMPtr<nsITransferable> nullData = 1.100 + do_CreateInstance("@mozilla.org/widget/transferable;1", &rv); 1.101 + NS_ENSURE_SUCCESS(rv, rv); 1.102 + nullData->Init(nullptr); 1.103 + rv = clipboard->SetData(nullData, nullptr, 1.104 + nsIClipboard::kGlobalClipboard); 1.105 + NS_ENSURE_SUCCESS(rv, rv); 1.106 +#endif 1.107 + } 1.108 + 1.109 + return NS_OK; 1.110 +} 1.111 + 1.112 +nsresult 1.113 +NS_NewClipboardPrivacyHandler(nsClipboardPrivacyHandler ** aHandler) 1.114 +{ 1.115 + NS_PRECONDITION(aHandler != nullptr, "null ptr"); 1.116 + if (!aHandler) 1.117 + return NS_ERROR_NULL_POINTER; 1.118 + 1.119 + *aHandler = new nsClipboardPrivacyHandler(); 1.120 + 1.121 + NS_ADDREF(*aHandler); 1.122 + nsresult rv = (*aHandler)->Init(); 1.123 + if (NS_FAILED(rv)) 1.124 + NS_RELEASE(*aHandler); 1.125 + 1.126 + return rv; 1.127 +}