Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 "nsPrintProgress.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "nsIBaseWindow.h" |
michael@0 | 9 | #include "nsISupportsArray.h" |
michael@0 | 10 | #include "nsXPCOM.h" |
michael@0 | 11 | #include "nsISupportsPrimitives.h" |
michael@0 | 12 | #include "nsIComponentManager.h" |
michael@0 | 13 | |
michael@0 | 14 | NS_IMPL_ADDREF(nsPrintProgress) |
michael@0 | 15 | NS_IMPL_RELEASE(nsPrintProgress) |
michael@0 | 16 | |
michael@0 | 17 | NS_INTERFACE_MAP_BEGIN(nsPrintProgress) |
michael@0 | 18 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIPrintStatusFeedback) |
michael@0 | 19 | NS_INTERFACE_MAP_ENTRY(nsIPrintProgress) |
michael@0 | 20 | NS_INTERFACE_MAP_ENTRY(nsIPrintStatusFeedback) |
michael@0 | 21 | NS_INTERFACE_MAP_ENTRY(nsIWebProgressListener) |
michael@0 | 22 | NS_INTERFACE_MAP_END_THREADSAFE |
michael@0 | 23 | |
michael@0 | 24 | |
michael@0 | 25 | nsPrintProgress::nsPrintProgress() |
michael@0 | 26 | { |
michael@0 | 27 | m_closeProgress = false; |
michael@0 | 28 | m_processCanceled = false; |
michael@0 | 29 | m_pendingStateFlags = -1; |
michael@0 | 30 | m_pendingStateValue = NS_OK; |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | nsPrintProgress::~nsPrintProgress() |
michael@0 | 34 | { |
michael@0 | 35 | (void)ReleaseListeners(); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | /* void openProgressDialog (in nsIDOMWindow parent, in string dialogURL, in nsISupports parameters); */ |
michael@0 | 39 | NS_IMETHODIMP nsPrintProgress::OpenProgressDialog(nsIDOMWindow *parent, |
michael@0 | 40 | const char *dialogURL, |
michael@0 | 41 | nsISupports *parameters, |
michael@0 | 42 | nsIObserver *openDialogObserver, |
michael@0 | 43 | bool *notifyOnOpen) |
michael@0 | 44 | { |
michael@0 | 45 | m_observer = openDialogObserver; |
michael@0 | 46 | |
michael@0 | 47 | nsresult rv = NS_ERROR_FAILURE; |
michael@0 | 48 | |
michael@0 | 49 | if (m_dialog) |
michael@0 | 50 | return NS_ERROR_ALREADY_INITIALIZED; |
michael@0 | 51 | |
michael@0 | 52 | if (!dialogURL || !*dialogURL) |
michael@0 | 53 | return NS_ERROR_INVALID_ARG; |
michael@0 | 54 | |
michael@0 | 55 | if (parent) |
michael@0 | 56 | { |
michael@0 | 57 | // Set up window.arguments[0]... |
michael@0 | 58 | nsCOMPtr<nsISupportsArray> array; |
michael@0 | 59 | rv = NS_NewISupportsArray(getter_AddRefs(array)); |
michael@0 | 60 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 61 | |
michael@0 | 62 | nsCOMPtr<nsISupportsInterfacePointer> ifptr = |
michael@0 | 63 | do_CreateInstance(NS_SUPPORTS_INTERFACE_POINTER_CONTRACTID, &rv); |
michael@0 | 64 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 65 | |
michael@0 | 66 | ifptr->SetData(static_cast<nsIPrintProgress*>(this)); |
michael@0 | 67 | ifptr->SetDataIID(&NS_GET_IID(nsIPrintProgress)); |
michael@0 | 68 | |
michael@0 | 69 | array->AppendElement(ifptr); |
michael@0 | 70 | |
michael@0 | 71 | array->AppendElement(parameters); |
michael@0 | 72 | |
michael@0 | 73 | // Open the dialog. |
michael@0 | 74 | nsCOMPtr<nsIDOMWindow> newWindow; |
michael@0 | 75 | rv = parent->OpenDialog(NS_ConvertASCIItoUTF16(dialogURL), |
michael@0 | 76 | NS_LITERAL_STRING("_blank"), |
michael@0 | 77 | NS_LITERAL_STRING("chrome,titlebar,dependent,centerscreen"), |
michael@0 | 78 | array, getter_AddRefs(newWindow)); |
michael@0 | 79 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 80 | *notifyOnOpen = true; |
michael@0 | 81 | } |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | return rv; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | /* void closeProgressDialog (in boolean forceClose); */ |
michael@0 | 88 | NS_IMETHODIMP nsPrintProgress::CloseProgressDialog(bool forceClose) |
michael@0 | 89 | { |
michael@0 | 90 | m_closeProgress = true; |
michael@0 | 91 | // XXX Casting bool to nsresult |
michael@0 | 92 | return OnStateChange(nullptr, nullptr, nsIWebProgressListener::STATE_STOP, |
michael@0 | 93 | static_cast<nsresult>(forceClose)); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | /* nsIPrompt GetPrompter (); */ |
michael@0 | 97 | NS_IMETHODIMP nsPrintProgress::GetPrompter(nsIPrompt **_retval) |
michael@0 | 98 | { |
michael@0 | 99 | NS_ENSURE_ARG_POINTER(_retval); |
michael@0 | 100 | *_retval = nullptr; |
michael@0 | 101 | |
michael@0 | 102 | if (! m_closeProgress && m_dialog) |
michael@0 | 103 | return m_dialog->GetPrompter(_retval); |
michael@0 | 104 | |
michael@0 | 105 | return NS_ERROR_FAILURE; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | /* attribute boolean processCanceledByUser; */ |
michael@0 | 109 | NS_IMETHODIMP nsPrintProgress::GetProcessCanceledByUser(bool *aProcessCanceledByUser) |
michael@0 | 110 | { |
michael@0 | 111 | NS_ENSURE_ARG_POINTER(aProcessCanceledByUser); |
michael@0 | 112 | *aProcessCanceledByUser = m_processCanceled; |
michael@0 | 113 | return NS_OK; |
michael@0 | 114 | } |
michael@0 | 115 | NS_IMETHODIMP nsPrintProgress::SetProcessCanceledByUser(bool aProcessCanceledByUser) |
michael@0 | 116 | { |
michael@0 | 117 | m_processCanceled = aProcessCanceledByUser; |
michael@0 | 118 | OnStateChange(nullptr, nullptr, nsIWebProgressListener::STATE_STOP, NS_OK); |
michael@0 | 119 | return NS_OK; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | /* void RegisterListener (in nsIWebProgressListener listener); */ |
michael@0 | 123 | NS_IMETHODIMP nsPrintProgress::RegisterListener(nsIWebProgressListener * listener) |
michael@0 | 124 | { |
michael@0 | 125 | if (!listener) //Nothing to do with a null listener! |
michael@0 | 126 | return NS_OK; |
michael@0 | 127 | |
michael@0 | 128 | m_listenerList.AppendObject(listener); |
michael@0 | 129 | if (m_closeProgress || m_processCanceled) |
michael@0 | 130 | listener->OnStateChange(nullptr, nullptr, |
michael@0 | 131 | nsIWebProgressListener::STATE_STOP, NS_OK); |
michael@0 | 132 | else |
michael@0 | 133 | { |
michael@0 | 134 | listener->OnStatusChange(nullptr, nullptr, NS_OK, m_pendingStatus.get()); |
michael@0 | 135 | if (m_pendingStateFlags != -1) |
michael@0 | 136 | listener->OnStateChange(nullptr, nullptr, m_pendingStateFlags, m_pendingStateValue); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | return NS_OK; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | /* void UnregisterListener (in nsIWebProgressListener listener); */ |
michael@0 | 143 | NS_IMETHODIMP nsPrintProgress::UnregisterListener(nsIWebProgressListener *listener) |
michael@0 | 144 | { |
michael@0 | 145 | if (listener) |
michael@0 | 146 | m_listenerList.RemoveObject(listener); |
michael@0 | 147 | |
michael@0 | 148 | return NS_OK; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | /* void doneIniting (); */ |
michael@0 | 152 | NS_IMETHODIMP nsPrintProgress::DoneIniting() |
michael@0 | 153 | { |
michael@0 | 154 | if (m_observer) { |
michael@0 | 155 | m_observer->Observe(nullptr, nullptr, nullptr); |
michael@0 | 156 | } |
michael@0 | 157 | return NS_OK; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long aStateFlags, in nsresult aStatus); */ |
michael@0 | 161 | NS_IMETHODIMP nsPrintProgress::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, uint32_t aStateFlags, nsresult aStatus) |
michael@0 | 162 | { |
michael@0 | 163 | m_pendingStateFlags = aStateFlags; |
michael@0 | 164 | m_pendingStateValue = aStatus; |
michael@0 | 165 | |
michael@0 | 166 | uint32_t count = m_listenerList.Count(); |
michael@0 | 167 | for (uint32_t i = count - 1; i < count; i --) |
michael@0 | 168 | { |
michael@0 | 169 | nsCOMPtr<nsIWebProgressListener> progressListener = m_listenerList.SafeObjectAt(i); |
michael@0 | 170 | if (progressListener) |
michael@0 | 171 | progressListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | return NS_OK; |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */ |
michael@0 | 178 | NS_IMETHODIMP nsPrintProgress::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, int32_t aCurSelfProgress, int32_t aMaxSelfProgress, int32_t aCurTotalProgress, int32_t aMaxTotalProgress) |
michael@0 | 179 | { |
michael@0 | 180 | uint32_t count = m_listenerList.Count(); |
michael@0 | 181 | for (uint32_t i = count - 1; i < count; i --) |
michael@0 | 182 | { |
michael@0 | 183 | nsCOMPtr<nsIWebProgressListener> progressListener = m_listenerList.SafeObjectAt(i); |
michael@0 | 184 | if (progressListener) |
michael@0 | 185 | progressListener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress); |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | return NS_OK; |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location, in unsigned long aFlags); */ |
michael@0 | 192 | NS_IMETHODIMP nsPrintProgress::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location, uint32_t aFlags) |
michael@0 | 193 | { |
michael@0 | 194 | return NS_OK; |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */ |
michael@0 | 198 | NS_IMETHODIMP nsPrintProgress::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const char16_t *aMessage) |
michael@0 | 199 | { |
michael@0 | 200 | if (aMessage && *aMessage) |
michael@0 | 201 | m_pendingStatus = aMessage; |
michael@0 | 202 | |
michael@0 | 203 | uint32_t count = m_listenerList.Count(); |
michael@0 | 204 | for (uint32_t i = count - 1; i < count; i --) |
michael@0 | 205 | { |
michael@0 | 206 | nsCOMPtr<nsIWebProgressListener> progressListener = m_listenerList.SafeObjectAt(i); |
michael@0 | 207 | if (progressListener) |
michael@0 | 208 | progressListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage); |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | return NS_OK; |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */ |
michael@0 | 215 | NS_IMETHODIMP nsPrintProgress::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, uint32_t state) |
michael@0 | 216 | { |
michael@0 | 217 | return NS_OK; |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | nsresult nsPrintProgress::ReleaseListeners() |
michael@0 | 221 | { |
michael@0 | 222 | m_listenerList.Clear(); |
michael@0 | 223 | return NS_OK; |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | NS_IMETHODIMP nsPrintProgress::ShowStatusString(const char16_t *status) |
michael@0 | 227 | { |
michael@0 | 228 | return OnStatusChange(nullptr, nullptr, NS_OK, status); |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | /* void startMeteors (); */ |
michael@0 | 232 | NS_IMETHODIMP nsPrintProgress::StartMeteors() |
michael@0 | 233 | { |
michael@0 | 234 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 235 | } |
michael@0 | 236 | |
michael@0 | 237 | /* void stopMeteors (); */ |
michael@0 | 238 | NS_IMETHODIMP nsPrintProgress::StopMeteors() |
michael@0 | 239 | { |
michael@0 | 240 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | /* void showProgress (in long percent); */ |
michael@0 | 244 | NS_IMETHODIMP nsPrintProgress::ShowProgress(int32_t percent) |
michael@0 | 245 | { |
michael@0 | 246 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | /* [noscript] void setDocShell (in nsIDocShell shell, in nsIDOMWindow window); */ |
michael@0 | 250 | NS_IMETHODIMP nsPrintProgress::SetDocShell(nsIDocShell *shell, nsIDOMWindow *window) |
michael@0 | 251 | { |
michael@0 | 252 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | /* void closeWindow (); */ |
michael@0 | 256 | NS_IMETHODIMP nsPrintProgress::CloseWindow() |
michael@0 | 257 | { |
michael@0 | 258 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 259 | } |
michael@0 | 260 |