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 https://mozilla.org/MPL/2.0/. */
6 
7 #include "mozilla/Logging.h"
8 
9 #include "TaskbarProgress.h"
10 #include "nsWindow.h"
11 #include "WidgetUtils.h"
12 #include "nsPIDOMWindow.h"
13 
14 using mozilla::LogLevel;
15 static mozilla::LazyLogModule gGtkTaskbarProgressLog("nsIGtkTaskbarProgress");
16 
17 /******************************************************************************
18  * TaskbarProgress
19  ******************************************************************************/
20 
NS_IMPL_ISUPPORTS(TaskbarProgress,nsIGtkTaskbarProgress,nsITaskbarProgress)21 NS_IMPL_ISUPPORTS(TaskbarProgress, nsIGtkTaskbarProgress, nsITaskbarProgress)
22 
23 TaskbarProgress::TaskbarProgress() : mPrimaryWindow(nullptr) {
24   MOZ_LOG(gGtkTaskbarProgressLog, LogLevel::Info,
25           ("%p TaskbarProgress()", this));
26 }
27 
~TaskbarProgress()28 TaskbarProgress::~TaskbarProgress() {
29   MOZ_LOG(gGtkTaskbarProgressLog, LogLevel::Info,
30           ("%p ~TaskbarProgress()", this));
31 }
32 
33 NS_IMETHODIMP
SetProgressState(nsTaskbarProgressState aState,uint64_t aCurrentValue,uint64_t aMaxValue)34 TaskbarProgress::SetProgressState(nsTaskbarProgressState aState,
35                                   uint64_t aCurrentValue, uint64_t aMaxValue) {
36 #ifdef MOZ_X11
37   NS_ENSURE_ARG_RANGE(aState, 0, STATE_PAUSED);
38 
39   if (aState == STATE_NO_PROGRESS || aState == STATE_INDETERMINATE) {
40     NS_ENSURE_TRUE(aCurrentValue == 0, NS_ERROR_INVALID_ARG);
41     NS_ENSURE_TRUE(aMaxValue == 0, NS_ERROR_INVALID_ARG);
42   }
43 
44   NS_ENSURE_TRUE((aCurrentValue <= aMaxValue), NS_ERROR_ILLEGAL_VALUE);
45 
46   // See TaskbarProgress::SetPrimaryWindow: if we're running in headless
47   // mode, mPrimaryWindow will be null.
48   if (!mPrimaryWindow) {
49     return NS_OK;
50   }
51 
52   gulong progress;
53 
54   if (aMaxValue == 0) {
55     progress = 0;
56   } else {
57     // Rounding down to ensure we don't set to 'full' until the operation
58     // is completely finished.
59     progress = (gulong)(((double)aCurrentValue / aMaxValue) * 100.0);
60   }
61 
62   // Check if the resultant value is the same as the previous call, and
63   // ignore this update if it is.
64 
65   if (progress == mCurrentProgress) {
66     return NS_OK;
67   }
68 
69   mCurrentProgress = progress;
70 
71   MOZ_LOG(gGtkTaskbarProgressLog, LogLevel::Debug,
72           ("GtkTaskbarProgress::SetProgressState progress: %lu", progress));
73 
74   mPrimaryWindow->SetProgress(progress);
75 #endif
76 
77   return NS_OK;
78 }
79 
80 NS_IMETHODIMP
SetPrimaryWindow(mozIDOMWindowProxy * aWindow)81 TaskbarProgress::SetPrimaryWindow(mozIDOMWindowProxy* aWindow) {
82   NS_ENSURE_TRUE(aWindow != nullptr, NS_ERROR_ILLEGAL_VALUE);
83 
84   auto* parent = nsPIDOMWindowOuter::From(aWindow);
85   RefPtr<nsIWidget> widget =
86       mozilla::widget::WidgetUtils::DOMWindowToWidget(parent);
87 
88   // Only nsWindows have a native window, HeadlessWidgets do not.  Stop here if
89   // the window does not have one.
90   if (!widget->GetNativeData(NS_NATIVE_WINDOW)) {
91     return NS_OK;
92   }
93 
94   mPrimaryWindow = static_cast<nsWindow*>(widget.get());
95 
96   // Clear our current progress.  We get a forced update from the
97   // DownloadsTaskbar after returning from this function - zeroing out our
98   // progress will make sure the new window gets the property set on it
99   // immediately, rather than waiting for the progress value to change (which
100   // could be a while depending on size.)
101   mCurrentProgress = 0;
102 
103   MOZ_LOG(gGtkTaskbarProgressLog, LogLevel::Debug,
104           ("GtkTaskbarProgress::SetPrimaryWindow window: %p",
105            mPrimaryWindow.get()));
106 
107   return NS_OK;
108 }
109