2009-01-30 00:13:17 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-01-30 00:13:17 -08:00
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
2009-01-30 00:13:17 -08:00
|
|
|
#include "nsClipboardPrivacyHandler.h"
|
|
|
|
#include "nsITransferable.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIClipboard.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsLiteralString.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsXPCOM.h"
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 09:59:13 -07:00
|
|
|
#include "mozilla/Services.h"
|
2009-01-30 00:13:17 -08:00
|
|
|
|
2011-04-24 20:10:12 -07:00
|
|
|
#if defined(XP_WIN)
|
2009-12-02 20:08:39 -08:00
|
|
|
#include <ole2.h>
|
|
|
|
#endif
|
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2009-01-30 00:13:17 -08:00
|
|
|
#define NS_MOZ_DATA_FROM_PRIVATEBROWSING "application/x-moz-private-browsing"
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS2(nsClipboardPrivacyHandler, nsIObserver, nsISupportsWeakReference)
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsClipboardPrivacyHandler::Init()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 09:59:13 -07:00
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (!observerService)
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-06-26 08:41:12 -07:00
|
|
|
return observerService->AddObserver(this, "last-pb-context-exited", true);
|
2009-01-30 00:13:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare the transferable object to be inserted into the clipboard
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
nsresult
|
|
|
|
nsClipboardPrivacyHandler::PrepareDataForClipboard(nsITransferable * aTransferable)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aTransferable, "clipboard given a null transferable");
|
|
|
|
|
2012-06-26 08:41:12 -07:00
|
|
|
bool isPrivateData = false;
|
|
|
|
aTransferable->GetIsPrivateData(&isPrivateData);
|
|
|
|
|
2009-01-30 00:13:17 -08:00
|
|
|
nsresult rv = NS_OK;
|
2012-06-26 08:41:12 -07:00
|
|
|
if (isPrivateData) {
|
2009-01-30 00:13:17 -08:00
|
|
|
nsCOMPtr<nsISupportsPRBool> data = do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID);
|
|
|
|
if (data) {
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = data->SetData(true);
|
2009-01-30 00:13:17 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-03-17 13:40:41 -07:00
|
|
|
// Ignore the error code of AddDataFlavor, since we might have added
|
|
|
|
// this flavor before. If this call really fails, so will the next
|
|
|
|
// one (SetTransferData).
|
|
|
|
aTransferable->AddDataFlavor(NS_MOZ_DATA_FROM_PRIVATEBROWSING);
|
2009-01-30 00:13:17 -08:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
rv = aTransferable->SetTransferData(NS_MOZ_DATA_FROM_PRIVATEBROWSING, data, sizeof(bool));
|
2009-01-30 00:13:17 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsClipboardPrivacyHandler::Observe(nsISupports *aSubject, char const *aTopic, PRUnichar const *aData)
|
|
|
|
{
|
2012-06-26 08:41:12 -07:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIClipboard> clipboard =
|
|
|
|
do_GetService("@mozilla.org/widget/clipboard;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
const char * flavors[] = { NS_MOZ_DATA_FROM_PRIVATEBROWSING };
|
|
|
|
bool haveFlavors;
|
|
|
|
rv = clipboard->HasDataMatchingFlavors(flavors,
|
|
|
|
ArrayLength(flavors),
|
|
|
|
nsIClipboard::kGlobalClipboard,
|
|
|
|
&haveFlavors);
|
|
|
|
if (NS_SUCCEEDED(rv) && haveFlavors) {
|
2011-04-24 20:10:12 -07:00
|
|
|
#if defined(XP_WIN)
|
2012-06-26 08:41:12 -07:00
|
|
|
// Workaround for bug 518412. On Windows 7 x64, there is a bug
|
|
|
|
// in handling clipboard data without any formats between
|
|
|
|
// 32-bit/64-bit boundaries, which could lead Explorer to crash.
|
|
|
|
// We work around the problem by clearing the clipboard using
|
|
|
|
// the usual Win32 API.
|
|
|
|
NS_ENSURE_TRUE(SUCCEEDED(::OleSetClipboard(NULL)), NS_ERROR_FAILURE);
|
2009-12-02 20:08:39 -08:00
|
|
|
#else
|
2012-06-26 08:41:12 -07:00
|
|
|
// Empty the native clipboard by copying an empty transferable
|
|
|
|
nsCOMPtr<nsITransferable> nullData =
|
|
|
|
do_CreateInstance("@mozilla.org/widget/transferable;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-07-30 07:20:58 -07:00
|
|
|
nullData->Init(nullptr);
|
|
|
|
rv = clipboard->SetData(nullData, nullptr,
|
2012-06-26 08:41:12 -07:00
|
|
|
nsIClipboard::kGlobalClipboard);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-12-02 20:08:39 -08:00
|
|
|
#endif
|
2009-01-30 00:13:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewClipboardPrivacyHandler(nsClipboardPrivacyHandler ** aHandler)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_PRECONDITION(aHandler != nullptr, "null ptr");
|
2009-01-30 00:13:17 -08:00
|
|
|
if (!aHandler)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
*aHandler = new nsClipboardPrivacyHandler();
|
|
|
|
|
|
|
|
NS_ADDREF(*aHandler);
|
|
|
|
nsresult rv = (*aHandler)->Init();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
NS_RELEASE(*aHandler);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|