Bug 1777562 [wpt PR 34663] - [FedCM] Rename FederatedCredential to IdentityCredential...
[gecko.git] / widget / gtk / TaskbarProgress.cpp
blob396f39b5e7e543c2035c8827c26450913d514472
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"
10 #include "nsWindow.h"
11 #include "WidgetUtils.h"
12 #include "nsPIDOMWindow.h"
14 using mozilla::LogLevel;
15 static mozilla::LazyLogModule gGtkTaskbarProgressLog("nsIGtkTaskbarProgress");
17 /******************************************************************************
18 * TaskbarProgress
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));
33 NS_IMETHODIMP
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) {
48 return NS_OK;
51 gulong progress;
53 if (aMaxValue == 0) {
54 progress = 0;
55 } else {
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) {
65 return NS_OK;
68 mCurrentProgress = progress;
70 MOZ_LOG(gGtkTaskbarProgressLog, LogLevel::Debug,
71 ("GtkTaskbarProgress::SetProgressState progress: %lu", progress));
73 mPrimaryWindow->SetProgress(progress);
75 return NS_OK;
78 NS_IMETHODIMP
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)) {
89 return NS_OK;
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.)
99 mCurrentProgress = 0;
101 MOZ_LOG(gGtkTaskbarProgressLog, LogLevel::Debug,
102 ("GtkTaskbarProgress::SetPrimaryWindow window: %p",
103 mPrimaryWindow.get()));
105 return NS_OK;