summaryrefslogtreecommitdiffstats
path: root/widget/gtk/TaskbarProgress.cpp
blob: 396f39b5e7e543c2035c8827c26450913d514472 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "mozilla/Logging.h"

#include "TaskbarProgress.h"
#include "nsWindow.h"
#include "WidgetUtils.h"
#include "nsPIDOMWindow.h"

using mozilla::LogLevel;
static mozilla::LazyLogModule gGtkTaskbarProgressLog("nsIGtkTaskbarProgress");

/******************************************************************************
 * TaskbarProgress
 ******************************************************************************/

NS_IMPL_ISUPPORTS(TaskbarProgress, nsIGtkTaskbarProgress, nsITaskbarProgress)

TaskbarProgress::TaskbarProgress() : mPrimaryWindow(nullptr) {
  MOZ_LOG(gGtkTaskbarProgressLog, LogLevel::Info,
          ("%p TaskbarProgress()", this));
}

TaskbarProgress::~TaskbarProgress() {
  MOZ_LOG(gGtkTaskbarProgressLog, LogLevel::Info,
          ("%p ~TaskbarProgress()", this));
}

NS_IMETHODIMP
TaskbarProgress::SetProgressState(nsTaskbarProgressState aState,
                                  uint64_t aCurrentValue, uint64_t aMaxValue) {
  NS_ENSURE_ARG_RANGE(aState, 0, STATE_PAUSED);

  if (aState == STATE_NO_PROGRESS || aState == STATE_INDETERMINATE) {
    NS_ENSURE_TRUE(aCurrentValue == 0, NS_ERROR_INVALID_ARG);
    NS_ENSURE_TRUE(aMaxValue == 0, NS_ERROR_INVALID_ARG);
  }

  NS_ENSURE_TRUE((aCurrentValue <= aMaxValue), NS_ERROR_ILLEGAL_VALUE);

  // See TaskbarProgress::SetPrimaryWindow: if we're running in headless
  // mode, mPrimaryWindow will be null.
  if (!mPrimaryWindow) {
    return NS_OK;
  }

  gulong progress;

  if (aMaxValue == 0) {
    progress = 0;
  } else {
    // Rounding down to ensure we don't set to 'full' until the operation
    // is completely finished.
    progress = (gulong)(((double)aCurrentValue / aMaxValue) * 100.0);
  }

  // Check if the resultant value is the same as the previous call, and
  // ignore this update if it is.

  if (progress == mCurrentProgress) {
    return NS_OK;
  }

  mCurrentProgress = progress;

  MOZ_LOG(gGtkTaskbarProgressLog, LogLevel::Debug,
          ("GtkTaskbarProgress::SetProgressState progress: %lu", progress));

  mPrimaryWindow->SetProgress(progress);

  return NS_OK;
}

NS_IMETHODIMP
TaskbarProgress::SetPrimaryWindow(mozIDOMWindowProxy* aWindow) {
  NS_ENSURE_TRUE(aWindow != nullptr, NS_ERROR_ILLEGAL_VALUE);

  auto* parent = nsPIDOMWindowOuter::From(aWindow);
  RefPtr<nsIWidget> widget =
      mozilla::widget::WidgetUtils::DOMWindowToWidget(parent);

  // Only nsWindows have a native window, HeadlessWidgets do not.  Stop here if
  // the window does not have one.
  if (!widget->GetNativeData(NS_NATIVE_WINDOW)) {
    return NS_OK;
  }

  mPrimaryWindow = static_cast<nsWindow*>(widget.get());

  // Clear our current progress.  We get a forced update from the
  // DownloadsTaskbar after returning from this function - zeroing out our
  // progress will make sure the new window gets the property set on it
  // immediately, rather than waiting for the progress value to change (which
  // could be a while depending on size.)
  mCurrentProgress = 0;

  MOZ_LOG(gGtkTaskbarProgressLog, LogLevel::Debug,
          ("GtkTaskbarProgress::SetPrimaryWindow window: %p",
           mPrimaryWindow.get()));

  return NS_OK;
}