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/. */
7 #include "mozilla/Logging.h"
9 #include "TaskbarProgress.h"
11 #include "WidgetUtils.h"
12 #include "nsPIDOMWindow.h"
14 using mozilla::LogLevel
;
15 static mozilla::LazyLogModule
gGtkTaskbarProgressLog("nsIGtkTaskbarProgress");
17 /******************************************************************************
19 ******************************************************************************/
21 NS_IMPL_ISUPPORTS(TaskbarProgress
, nsIGtkTaskbarProgress
, nsITaskbarProgress
)
23 TaskbarProgress::TaskbarProgress() : mPrimaryWindow(nullptr) {
24 MOZ_LOG(gGtkTaskbarProgressLog
, LogLevel::Info
,
25 ("%p TaskbarProgress()", this));
28 TaskbarProgress::~TaskbarProgress() {
29 MOZ_LOG(gGtkTaskbarProgressLog
, LogLevel::Info
,
30 ("%p ~TaskbarProgress()", this));
34 TaskbarProgress::SetProgressState(nsTaskbarProgressState aState
,
35 uint64_t aCurrentValue
, uint64_t aMaxValue
) {
36 NS_ENSURE_ARG_RANGE(aState
, 0, STATE_PAUSED
);
38 if (aState
== STATE_NO_PROGRESS
|| aState
== STATE_INDETERMINATE
) {
39 NS_ENSURE_TRUE(aCurrentValue
== 0, NS_ERROR_INVALID_ARG
);
40 NS_ENSURE_TRUE(aMaxValue
== 0, NS_ERROR_INVALID_ARG
);
43 NS_ENSURE_TRUE((aCurrentValue
<= aMaxValue
), NS_ERROR_ILLEGAL_VALUE
);
45 // See TaskbarProgress::SetPrimaryWindow: if we're running in headless
46 // mode, mPrimaryWindow will be null.
47 if (!mPrimaryWindow
) {
56 // Rounding down to ensure we don't set to 'full' until the operation
57 // is completely finished.
58 progress
= (gulong
)(((double)aCurrentValue
/ aMaxValue
) * 100.0);
61 // Check if the resultant value is the same as the previous call, and
62 // ignore this update if it is.
64 if (progress
== mCurrentProgress
) {
68 mCurrentProgress
= progress
;
70 MOZ_LOG(gGtkTaskbarProgressLog
, LogLevel::Debug
,
71 ("GtkTaskbarProgress::SetProgressState progress: %lu", progress
));
73 mPrimaryWindow
->SetProgress(progress
);
79 TaskbarProgress::SetPrimaryWindow(mozIDOMWindowProxy
* aWindow
) {
80 NS_ENSURE_TRUE(aWindow
!= nullptr, NS_ERROR_ILLEGAL_VALUE
);
82 auto* parent
= nsPIDOMWindowOuter::From(aWindow
);
83 RefPtr
<nsIWidget
> widget
=
84 mozilla::widget::WidgetUtils::DOMWindowToWidget(parent
);
86 // Only nsWindows have a native window, HeadlessWidgets do not. Stop here if
87 // the window does not have one.
88 if (!widget
->GetNativeData(NS_NATIVE_WINDOW
)) {
92 mPrimaryWindow
= static_cast<nsWindow
*>(widget
.get());
94 // Clear our current progress. We get a forced update from the
95 // DownloadsTaskbar after returning from this function - zeroing out our
96 // progress will make sure the new window gets the property set on it
97 // immediately, rather than waiting for the progress value to change (which
98 // could be a while depending on size.)
101 MOZ_LOG(gGtkTaskbarProgressLog
, LogLevel::Debug
,
102 ("GtkTaskbarProgress::SetPrimaryWindow window: %p",
103 mPrimaryWindow
.get()));