tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

RemotePrintJobChild.cpp (5274B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "RemotePrintJobChild.h"
      8 
      9 #include "mozilla/SpinEventLoopUntil.h"
     10 #include "nsPagePrintTimer.h"
     11 #include "nsPrintJob.h"
     12 #include "private/pprio.h"
     13 
     14 namespace mozilla {
     15 namespace layout {
     16 
     17 NS_IMPL_ISUPPORTS(RemotePrintJobChild, nsIWebProgressListener)
     18 
     19 RemotePrintJobChild::RemotePrintJobChild() = default;
     20 
     21 nsresult RemotePrintJobChild::InitializePrint(const nsString& aDocumentTitle,
     22                                              const int32_t& aStartPage,
     23                                              const int32_t& aEndPage) {
     24  // Print initialization can sometimes display a dialog in the parent, so we
     25  // need to spin a nested event loop until initialization completes.
     26  (void)SendInitializePrint(aDocumentTitle, aStartPage, aEndPage);
     27  mozilla::SpinEventLoopUntil("RemotePrintJobChild::InitializePrint"_ns,
     28                              [&]() { return mPrintInitialized; });
     29 
     30  return mInitializationResult;
     31 }
     32 
     33 mozilla::ipc::IPCResult RemotePrintJobChild::RecvPrintInitializationResult(
     34    const nsresult& aRv, const mozilla::ipc::FileDescriptor& aFd) {
     35  mPrintInitialized = true;
     36  mInitializationResult = aRv;
     37  if (NS_SUCCEEDED(aRv)) {
     38    SetNextPageFD(aFd);
     39  }
     40  return IPC_OK();
     41 }
     42 
     43 PRFileDesc* RemotePrintJobChild::GetNextPageFD() {
     44  MOZ_ASSERT(!mDestroyed);
     45  MOZ_ASSERT(mNextPageFD);
     46  PRFileDesc* fd = mNextPageFD;
     47  mNextPageFD = nullptr;
     48  return fd;
     49 }
     50 
     51 void RemotePrintJobChild::SetNextPageFD(
     52    const mozilla::ipc::FileDescriptor& aFd) {
     53  MOZ_ASSERT(!mDestroyed);
     54  auto handle = aFd.ClonePlatformHandle();
     55  mNextPageFD = PR_ImportFile(PROsfd(handle.release()));
     56 }
     57 
     58 void RemotePrintJobChild::ProcessPage(const IntSize& aSizeInPoints,
     59                                      nsTArray<uint64_t>&& aDeps) {
     60  MOZ_ASSERT(mPagePrintTimer);
     61 
     62  mPagePrintTimer->WaitForRemotePrint();
     63  if (!mDestroyed) {
     64    (void)SendProcessPage(aSizeInPoints.width, aSizeInPoints.height,
     65                          std::move(aDeps));
     66  }
     67 }
     68 
     69 mozilla::ipc::IPCResult RemotePrintJobChild::RecvPageProcessed(
     70    const mozilla::ipc::FileDescriptor& aFd) {
     71  MOZ_ASSERT(mPagePrintTimer);
     72  SetNextPageFD(aFd);
     73 
     74  mPagePrintTimer->RemotePrintFinished();
     75  return IPC_OK();
     76 }
     77 
     78 mozilla::ipc::IPCResult RemotePrintJobChild::RecvAbortPrint(
     79    const nsresult& aRv) {
     80  MOZ_ASSERT(mPrintJob);
     81 
     82  mPrintJob->CleanupOnFailure(aRv, true);
     83  return IPC_OK();
     84 }
     85 
     86 void RemotePrintJobChild::SetPagePrintTimer(nsPagePrintTimer* aPagePrintTimer) {
     87  MOZ_ASSERT(!mDestroyed);
     88  MOZ_ASSERT(aPagePrintTimer);
     89 
     90  mPagePrintTimer = aPagePrintTimer;
     91 }
     92 
     93 void RemotePrintJobChild::SetPrintJob(nsPrintJob* aPrintJob) {
     94  MOZ_ASSERT(!mDestroyed);
     95  MOZ_ASSERT(aPrintJob);
     96 
     97  mPrintJob = aPrintJob;
     98 }
     99 
    100 // nsIWebProgressListener
    101 
    102 NS_IMETHODIMP
    103 RemotePrintJobChild::OnStateChange(nsIWebProgress* aProgress,
    104                                   nsIRequest* aRequest, uint32_t aStateFlags,
    105                                   nsresult aStatus) {
    106  // `RemotePrintJobParent` emits its own state change events based on its
    107  // own progress & the actor lifecycle, so any forwarded event here would get
    108  // ignored.
    109  return NS_OK;
    110 }
    111 
    112 NS_IMETHODIMP
    113 RemotePrintJobChild::OnProgressChange(nsIWebProgress* aProgress,
    114                                      nsIRequest* aRequest,
    115                                      int32_t aCurSelfProgress,
    116                                      int32_t aMaxSelfProgress,
    117                                      int32_t aCurTotalProgress,
    118                                      int32_t aMaxTotalProgress) {
    119  if (!mDestroyed) {
    120    (void)SendProgressChange(aCurSelfProgress, aMaxSelfProgress,
    121                             aCurTotalProgress, aMaxTotalProgress);
    122  }
    123 
    124  return NS_OK;
    125 }
    126 
    127 NS_IMETHODIMP
    128 RemotePrintJobChild::OnLocationChange(nsIWebProgress* aProgress,
    129                                      nsIRequest* aRequest, nsIURI* aURI,
    130                                      uint32_t aFlags) {
    131  return NS_OK;
    132 }
    133 
    134 NS_IMETHODIMP
    135 RemotePrintJobChild::OnStatusChange(nsIWebProgress* aProgress,
    136                                    nsIRequest* aRequest, nsresult aStatus,
    137                                    const char16_t* aMessage) {
    138  if (NS_SUCCEEDED(mInitializationResult) && !mDestroyed) {
    139    (void)SendStatusChange(aStatus);
    140  }
    141 
    142  return NS_OK;
    143 }
    144 
    145 NS_IMETHODIMP
    146 RemotePrintJobChild::OnSecurityChange(nsIWebProgress* aProgress,
    147                                      nsIRequest* aRequest, uint32_t aState) {
    148  return NS_OK;
    149 }
    150 
    151 NS_IMETHODIMP
    152 RemotePrintJobChild::OnContentBlockingEvent(nsIWebProgress* aProgress,
    153                                            nsIRequest* aRequest,
    154                                            uint32_t aEvent) {
    155  return NS_OK;
    156 }
    157 
    158 // End of nsIWebProgressListener
    159 
    160 RemotePrintJobChild::~RemotePrintJobChild() = default;
    161 
    162 void RemotePrintJobChild::ActorDestroy(ActorDestroyReason aWhy) {
    163  mPagePrintTimer = nullptr;
    164  mPrintJob = nullptr;
    165 
    166  mDestroyed = true;
    167 }
    168 
    169 }  // namespace layout
    170 }  // namespace mozilla