summaryrefslogtreecommitdiffstats
path: root/widget/ClipboardWriteRequestParent.cpp
blob: 751befbe050a166d7c530d481ceecd84a3cbbdf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

#include "mozilla/ClipboardWriteRequestParent.h"

#include "mozilla/dom/ContentParent.h"
#include "mozilla/net/CookieJarSettings.h"
#include "nsComponentManagerUtils.h"
#include "nsIClipboard.h"
#include "nsITransferable.h"
#include "nsWidgetsCID.h"

static NS_DEFINE_CID(kCClipboardCID, NS_CLIPBOARD_CID);

using mozilla::dom::ContentParent;
using mozilla::ipc::IPCResult;

namespace mozilla {

NS_IMPL_ISUPPORTS(ClipboardWriteRequestParent, nsIAsyncClipboardRequestCallback)

ClipboardWriteRequestParent::ClipboardWriteRequestParent(
    ContentParent* aManager)
    : mManager(aManager) {}

ClipboardWriteRequestParent::~ClipboardWriteRequestParent() = default;

nsresult ClipboardWriteRequestParent::Init(const int32_t& aClipboardType) {
  nsCOMPtr<nsIClipboard> clipboard(do_GetService(kCClipboardCID));
  if (!clipboard) {
    Unused << PClipboardWriteRequestParent::Send__delete__(this,
                                                           NS_ERROR_FAILURE);
    return NS_ERROR_FAILURE;
  }

  nsresult rv = clipboard->AsyncSetData(aClipboardType, this,
                                        getter_AddRefs(mAsyncSetClipboardData));
  if (NS_FAILED(rv)) {
    Unused << PClipboardWriteRequestParent::Send__delete__(this, rv);
    return rv;
  }

  return NS_OK;
}

NS_IMETHODIMP ClipboardWriteRequestParent::OnComplete(nsresult aResult) {
  nsCOMPtr<nsIAsyncSetClipboardData> clipboardData =
      std::move(mAsyncSetClipboardData);
  if (clipboardData) {
    Unused << PClipboardWriteRequestParent::Send__delete__(this, aResult);
  }
  return NS_OK;
}

IPCResult ClipboardWriteRequestParent::RecvSetData(
    const IPCTransferable& aTransferable) {
  if (!mManager->ValidatePrincipal(
          aTransferable.requestingPrincipal(),
          {ContentParent::ValidatePrincipalOptions::AllowNullPtr,
           ContentParent::ValidatePrincipalOptions::AllowExpanded,
           ContentParent::ValidatePrincipalOptions::AllowSystem})) {
    ContentParent::LogAndAssertFailedPrincipalValidationInfo(
        aTransferable.requestingPrincipal(), __func__);
  }

  if (!mAsyncSetClipboardData) {
    return IPC_OK();
  }

  nsresult rv = NS_ERROR_FAILURE;
  nsCOMPtr<nsITransferable> trans =
      do_CreateInstance("@mozilla.org/widget/transferable;1", &rv);
  if (NS_FAILED(rv)) {
    mAsyncSetClipboardData->Abort(rv);
    return IPC_OK();
  }

  trans->Init(nullptr);
  rv = nsContentUtils::IPCTransferableToTransferable(
      aTransferable, true /* aAddDataFlavor */, trans,
      true /* aFilterUnknownFlavors */);
  if (NS_FAILED(rv)) {
    mAsyncSetClipboardData->Abort(rv);
    return IPC_OK();
  }

  mAsyncSetClipboardData->SetData(trans, nullptr);
  return IPC_OK();
}

IPCResult ClipboardWriteRequestParent::Recv__delete__(nsresult aReason) {
#ifndef FUZZING_SNAPSHOT
  MOZ_DIAGNOSTIC_ASSERT(NS_FAILED(aReason));
#endif
  nsCOMPtr<nsIAsyncSetClipboardData> clipboardData =
      std::move(mAsyncSetClipboardData);
  if (clipboardData) {
    clipboardData->Abort(aReason);
  }
  return IPC_OK();
}

void ClipboardWriteRequestParent::ActorDestroy(ActorDestroyReason aReason) {
  nsCOMPtr<nsIAsyncSetClipboardData> clipboardData =
      std::move(mAsyncSetClipboardData);
  if (clipboardData) {
    clipboardData->Abort(NS_ERROR_ABORT);
  }
}

}  // namespace mozilla