Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "mozilla/ArrayUtils.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "nsClipboardPrivacyHandler.h" |
michael@0 | 9 | #include "nsITransferable.h" |
michael@0 | 10 | #include "nsISupportsPrimitives.h" |
michael@0 | 11 | #include "nsIObserverService.h" |
michael@0 | 12 | #include "nsIClipboard.h" |
michael@0 | 13 | #include "nsComponentManagerUtils.h" |
michael@0 | 14 | #include "nsServiceManagerUtils.h" |
michael@0 | 15 | #include "nsLiteralString.h" |
michael@0 | 16 | #include "nsNetCID.h" |
michael@0 | 17 | #include "nsXPCOM.h" |
michael@0 | 18 | #include "mozilla/Services.h" |
michael@0 | 19 | |
michael@0 | 20 | #if defined(XP_WIN) |
michael@0 | 21 | #include <ole2.h> |
michael@0 | 22 | #endif |
michael@0 | 23 | |
michael@0 | 24 | using namespace mozilla; |
michael@0 | 25 | |
michael@0 | 26 | #define NS_MOZ_DATA_FROM_PRIVATEBROWSING "application/x-moz-private-browsing" |
michael@0 | 27 | |
michael@0 | 28 | NS_IMPL_ISUPPORTS(nsClipboardPrivacyHandler, nsIObserver, nsISupportsWeakReference) |
michael@0 | 29 | |
michael@0 | 30 | nsresult |
michael@0 | 31 | nsClipboardPrivacyHandler::Init() |
michael@0 | 32 | { |
michael@0 | 33 | nsCOMPtr<nsIObserverService> observerService = |
michael@0 | 34 | mozilla::services::GetObserverService(); |
michael@0 | 35 | if (!observerService) |
michael@0 | 36 | return NS_ERROR_FAILURE; |
michael@0 | 37 | return observerService->AddObserver(this, "last-pb-context-exited", true); |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | /** |
michael@0 | 41 | * Prepare the transferable object to be inserted into the clipboard |
michael@0 | 42 | * |
michael@0 | 43 | */ |
michael@0 | 44 | nsresult |
michael@0 | 45 | nsClipboardPrivacyHandler::PrepareDataForClipboard(nsITransferable * aTransferable) |
michael@0 | 46 | { |
michael@0 | 47 | NS_ASSERTION(aTransferable, "clipboard given a null transferable"); |
michael@0 | 48 | |
michael@0 | 49 | bool isPrivateData = false; |
michael@0 | 50 | aTransferable->GetIsPrivateData(&isPrivateData); |
michael@0 | 51 | |
michael@0 | 52 | nsresult rv = NS_OK; |
michael@0 | 53 | if (isPrivateData) { |
michael@0 | 54 | nsCOMPtr<nsISupportsPRBool> data = do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID); |
michael@0 | 55 | if (data) { |
michael@0 | 56 | rv = data->SetData(true); |
michael@0 | 57 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 58 | |
michael@0 | 59 | // Ignore the error code of AddDataFlavor, since we might have added |
michael@0 | 60 | // this flavor before. If this call really fails, so will the next |
michael@0 | 61 | // one (SetTransferData). |
michael@0 | 62 | aTransferable->AddDataFlavor(NS_MOZ_DATA_FROM_PRIVATEBROWSING); |
michael@0 | 63 | |
michael@0 | 64 | rv = aTransferable->SetTransferData(NS_MOZ_DATA_FROM_PRIVATEBROWSING, data, sizeof(bool)); |
michael@0 | 65 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 66 | } |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | return rv; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | NS_IMETHODIMP |
michael@0 | 73 | nsClipboardPrivacyHandler::Observe(nsISupports *aSubject, char const *aTopic, char16_t const *aData) |
michael@0 | 74 | { |
michael@0 | 75 | nsresult rv; |
michael@0 | 76 | nsCOMPtr<nsIClipboard> clipboard = |
michael@0 | 77 | do_GetService("@mozilla.org/widget/clipboard;1", &rv); |
michael@0 | 78 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 79 | |
michael@0 | 80 | const char * flavors[] = { NS_MOZ_DATA_FROM_PRIVATEBROWSING }; |
michael@0 | 81 | bool haveFlavors; |
michael@0 | 82 | rv = clipboard->HasDataMatchingFlavors(flavors, |
michael@0 | 83 | ArrayLength(flavors), |
michael@0 | 84 | nsIClipboard::kGlobalClipboard, |
michael@0 | 85 | &haveFlavors); |
michael@0 | 86 | if (NS_SUCCEEDED(rv) && haveFlavors) { |
michael@0 | 87 | #if defined(XP_WIN) |
michael@0 | 88 | // Workaround for bug 518412. On Windows 7 x64, there is a bug |
michael@0 | 89 | // in handling clipboard data without any formats between |
michael@0 | 90 | // 32-bit/64-bit boundaries, which could lead Explorer to crash. |
michael@0 | 91 | // We work around the problem by clearing the clipboard using |
michael@0 | 92 | // the usual Win32 API. |
michael@0 | 93 | NS_ENSURE_TRUE(SUCCEEDED(::OleSetClipboard(nullptr)), NS_ERROR_FAILURE); |
michael@0 | 94 | #else |
michael@0 | 95 | // Empty the native clipboard by copying an empty transferable |
michael@0 | 96 | nsCOMPtr<nsITransferable> nullData = |
michael@0 | 97 | do_CreateInstance("@mozilla.org/widget/transferable;1", &rv); |
michael@0 | 98 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 99 | nullData->Init(nullptr); |
michael@0 | 100 | rv = clipboard->SetData(nullData, nullptr, |
michael@0 | 101 | nsIClipboard::kGlobalClipboard); |
michael@0 | 102 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 103 | #endif |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | return NS_OK; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | nsresult |
michael@0 | 110 | NS_NewClipboardPrivacyHandler(nsClipboardPrivacyHandler ** aHandler) |
michael@0 | 111 | { |
michael@0 | 112 | NS_PRECONDITION(aHandler != nullptr, "null ptr"); |
michael@0 | 113 | if (!aHandler) |
michael@0 | 114 | return NS_ERROR_NULL_POINTER; |
michael@0 | 115 | |
michael@0 | 116 | *aHandler = new nsClipboardPrivacyHandler(); |
michael@0 | 117 | |
michael@0 | 118 | NS_ADDREF(*aHandler); |
michael@0 | 119 | nsresult rv = (*aHandler)->Init(); |
michael@0 | 120 | if (NS_FAILED(rv)) |
michael@0 | 121 | NS_RELEASE(*aHandler); |
michael@0 | 122 | |
michael@0 | 123 | return rv; |
michael@0 | 124 | } |