1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #include "mozilla/Unused.h"
6 #include "nsIPrintProgressParams.h"
7 #include "nsIWebProgressListener.h"
8 #include "PrintProgressDialogParent.h"
9 
10 using mozilla::Unused;
11 
12 namespace mozilla {
13 namespace embedding {
14 
NS_IMPL_ISUPPORTS(PrintProgressDialogParent,nsIObserver)15 NS_IMPL_ISUPPORTS(PrintProgressDialogParent, nsIObserver)
16 
17 PrintProgressDialogParent::PrintProgressDialogParent() : mActive(true) {}
18 
~PrintProgressDialogParent()19 PrintProgressDialogParent::~PrintProgressDialogParent() {}
20 
SetWebProgressListener(nsIWebProgressListener * aListener)21 void PrintProgressDialogParent::SetWebProgressListener(
22     nsIWebProgressListener* aListener) {
23   mWebProgressListener = aListener;
24 }
25 
SetPrintProgressParams(nsIPrintProgressParams * aParams)26 void PrintProgressDialogParent::SetPrintProgressParams(
27     nsIPrintProgressParams* aParams) {
28   mPrintProgressParams = aParams;
29 }
30 
RecvStateChange(const long & stateFlags,const nsresult & status)31 mozilla::ipc::IPCResult PrintProgressDialogParent::RecvStateChange(
32     const long& stateFlags, const nsresult& status) {
33   if (mWebProgressListener) {
34     mWebProgressListener->OnStateChange(nullptr, nullptr, stateFlags, status);
35   }
36   return IPC_OK();
37 }
38 
RecvProgressChange(const long & curSelfProgress,const long & maxSelfProgress,const long & curTotalProgress,const long & maxTotalProgress)39 mozilla::ipc::IPCResult PrintProgressDialogParent::RecvProgressChange(
40     const long& curSelfProgress, const long& maxSelfProgress,
41     const long& curTotalProgress, const long& maxTotalProgress) {
42   if (mWebProgressListener) {
43     mWebProgressListener->OnProgressChange(nullptr, nullptr, curSelfProgress,
44                                            maxSelfProgress, curTotalProgress,
45                                            maxTotalProgress);
46   }
47   return IPC_OK();
48 }
49 
RecvDocTitleChange(const nsString & newTitle)50 mozilla::ipc::IPCResult PrintProgressDialogParent::RecvDocTitleChange(
51     const nsString& newTitle) {
52   if (mPrintProgressParams) {
53     mPrintProgressParams->SetDocTitle(newTitle);
54   }
55   return IPC_OK();
56 }
57 
RecvDocURLChange(const nsString & newURL)58 mozilla::ipc::IPCResult PrintProgressDialogParent::RecvDocURLChange(
59     const nsString& newURL) {
60   if (mPrintProgressParams) {
61     mPrintProgressParams->SetDocURL(newURL);
62   }
63   return IPC_OK();
64 }
65 
ActorDestroy(ActorDestroyReason aWhy)66 void PrintProgressDialogParent::ActorDestroy(ActorDestroyReason aWhy) {
67   // If IPC actor is destroyed, we can't send to child via IPC.
68   mActive = false;
69 }
70 
Recv__delete__()71 mozilla::ipc::IPCResult PrintProgressDialogParent::Recv__delete__() {
72   // The child has requested that we tear down the connection, so we set a
73   // member to make sure we don't try to contact it after the fact.
74   mActive = false;
75   return IPC_OK();
76 }
77 
78 // nsIObserver
79 NS_IMETHODIMP
Observe(nsISupports * aSubject,const char * aTopic,const char16_t * aData)80 PrintProgressDialogParent::Observe(nsISupports* aSubject, const char* aTopic,
81                                    const char16_t* aData) {
82   if (mActive) {
83     if (aTopic) {
84       if (!strcmp(aTopic, "cancelled")) {
85         Unused << SendCancelledCurrentJob();
86         if (!mDialogOpenedSent) {
87           // We haven't already called SendDialogOpened, so call it now or it
88           // might never get sent and block the child from new printing
89           // requests. Also set mActive to false because we don't want to send
90           // it twice and our PrintProgressDialogChild will get deleted anyway.
91           Unused << SendDialogOpened();
92           mActive = false;
93         }
94       } else if (!strcmp(aTopic, "completed")) {
95         // Once printing is complete don't send any messages to the child.
96         mActive = false;
97       }
98     } else {
99       Unused << SendDialogOpened();
100       mDialogOpenedSent = true;
101     }
102   } else {
103     NS_WARNING(
104         "The print progress dialog finished opening, but communications "
105         "with the child have been closed.");
106   }
107 
108   return NS_OK;
109 }
110 
111 }  // namespace embedding
112 }  // namespace mozilla
113