File: download_display_controller.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (362 lines) | stat: -rw-r--r-- 13,321 bytes parent folder | download | duplicates (3)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/download/bubble/download_display_controller.h"

#include "base/functional/bind.h"
#include "base/numerics/safe_conversions.h"
#include "base/power_monitor/power_monitor.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chrome/browser/download/bubble/download_bubble_display_info.h"
#include "chrome/browser/download/bubble/download_bubble_prefs.h"
#include "chrome/browser/download/bubble/download_bubble_ui_controller.h"
#include "chrome/browser/download/bubble/download_bubble_utils.h"
#include "chrome/browser/download/download_core_service.h"
#include "chrome/browser/download/download_core_service_factory.h"
#include "chrome/browser/download/download_item_model.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/download/download_ui_controller.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/download/download_item_mode.h"
#include "chrome/browser/ui/exclusive_access/exclusive_access_bubble_type.h"
#include "chrome/browser/ui/exclusive_access/exclusive_access_context.h"
#include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "components/download/public/common/download_danger_type.h"
#include "components/offline_items_collection/core/offline_item.h"
#include "components/offline_items_collection/core/offline_item_state.h"

#if BUILDFLAG(IS_MAC)
#include "chrome/browser/ui/fullscreen_util_mac.h"
#endif

namespace {

using DownloadIconActive = DownloadDisplay::IconActive;
using DownloadIconState = DownloadDisplay::IconState;
using DownloadUIModelPtr = DownloadUIModel::DownloadUIModelPtr;

// The amount of time for the toolbar icon to be visible after a download is
// completed.
constexpr base::TimeDelta kToolbarIconVisibilityTimeInterval =
    base::Minutes(60);

// The amount of time for the toolbar icon to stay active after a download is
// completed. If the download completed while full screen, the timer is started
// after user comes out of the full screen.
constexpr base::TimeDelta kToolbarIconActiveTimeInterval = base::Minutes(1);

// Whether there are no more in-progress downloads that are not paused, i.e.,
// whether all actively downloading items are done.
bool IsAllDone(const DownloadBubbleDisplayInfo& info) {
  return info.in_progress_count == info.paused_count;
}

// Whether the last download complete time is more recent than `interval` ago.
bool HasRecentCompleteDownload(base::TimeDelta interval,
                               base::Time last_complete_time) {
  if (last_complete_time.is_null()) {
    return false;
  }
  base::TimeDelta time_since_last_completion =
      base::Time::Now() - last_complete_time;
  return time_since_last_completion < interval;
}

// `profile` must be non-null. `app_id` can be null, signifying no app. This
// returns the most recent browser for the profile which matches the given app
// or lack thereof.
Browser* FindMostRecentBrowserForProfileMatchingWebApp(
    Profile* profile,
    const webapps::AppId* app_id) {
  for (Browser* browser : chrome::FindAllBrowsersWithProfile(profile)) {
    const webapps::AppId* browser_app_id = GetWebAppIdForBrowser(browser);
    bool app_ids_match =
        (!app_id && !browser_app_id) ||
        (app_id && browser_app_id && *app_id == *browser_app_id);
    if (!app_ids_match) {
      continue;
    }
    return browser;
  }
  return nullptr;
}

}  // namespace

DownloadDisplayController::DownloadDisplayController(
    DownloadDisplay* display,
    Browser* browser,
    DownloadBubbleUIController* bubble_controller)
    : display_(display),
      browser_(browser),
      bubble_controller_(bubble_controller) {
  bubble_controller_->SetDownloadDisplayController(this);
  // |display| can be null in tests.
  if (display) {
    MaybeShowButtonWhenCreated();
  }
  base::PowerMonitor::GetInstance()->AddPowerSuspendObserver(this);
}

DownloadDisplayController::~DownloadDisplayController() {
  base::PowerMonitor::GetInstance()->RemovePowerSuspendObserver(this);
}

void DownloadDisplayController::OnNewItem(bool show_animation) {
  if (!download::ShouldShowDownloadBubble(browser_->profile())) {
    return;
  }

  HandleAccessibleAlerts();
  UpdateButtonStateFromUpdateService();
  if (display_->ShouldShowExclusiveAccessBubble()) {
    fullscreen_notification_shown_ = true;
    // ExclusiveAccessContext can be null in tests.
    if (auto* context =
            browser_->GetFeatures().exclusive_access_manager()->context()) {
      context->UpdateExclusiveAccessBubble(
          {.has_download = true, .force_update = true}, base::NullCallback());
    }
  } else {
    DownloadDisplay::IconUpdateInfo updates;
    updates.show_animation = show_animation;
    display_->UpdateDownloadIcon(updates);
  }
}

void DownloadDisplayController::OnUpdatedItem(bool is_done,
                                              bool may_show_details) {
  if (!download::ShouldShowDownloadBubble(browser_->profile())) {
    return;
  }
  HandleAccessibleAlerts();
  const DownloadBubbleDisplayInfo& info = UpdateButtonStateFromUpdateService();
  bool will_show_details = may_show_details && is_done && IsAllDone(info);
  if (is_done) {
    ScheduleToolbarDisappearance(kToolbarIconVisibilityTimeInterval);
  }
  if (will_show_details && display_->IsFullscreenWithParentViewHidden()) {
    // If we would show the details, but the user is in fullscreen (and is
    // capable of exiting), we should instead show the details once the user
    // exits fullscreen.
    should_show_details_on_exit_fullscreen_ =
        display_->ShouldShowExclusiveAccessBubble();
    // Show the details if we are in immersive fullscreen.
    BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser_);
    will_show_details = browser_view && browser_view->IsImmersiveModeEnabled();
  }

  // At this point, we are possibly in fullscreen. If we're in immersive
  // fullscreen on macOS, it's OK to show the details bubble because the
  // toolbar is either visible or it can be made visible. However, if we're
  // in content/HTML fullscreen, the toolbar is not visible and we should not
  // show the bubble. So, check our fullscreen state here and avoid showing
  // the bubble if we're in content fullscreen.
#if BUILDFLAG(IS_MAC)
  will_show_details =
      will_show_details && !fullscreen_utils::IsInContentFullscreen(browser_);
#endif

  if (will_show_details) {
    display_->ShowDetails();
  }
}

void DownloadDisplayController::OnRemovedItem(const ContentId& id) {
  if (!download::ShouldShowDownloadBubble(browser_->profile())) {
    return;
  }
  UpdateButtonStateFromUpdateService();
}

void DownloadDisplayController::OnButtonPressed() {
  DownloadUIController* download_ui_controller =
      DownloadCoreServiceFactory::GetForBrowserContext(
          browser_->profile()->GetOriginalProfile())
          ->GetDownloadUIController();
  if (download_ui_controller) {
    download_ui_controller->OnButtonClicked();
  }
}

void DownloadDisplayController::HandleButtonPressed() {
  // If the current state is kComplete, set the icon to inactive because of the
  // user action.
  if (display_->GetIconState() == DownloadIconState::kComplete) {
    DownloadDisplay::IconUpdateInfo updates;
    updates.new_active = DownloadIconActive::kInactive;
    display_->UpdateDownloadIcon(updates);
  }
}

void DownloadDisplayController::ShowToolbarButton() {
  // If toolbar pinning is enabled Show should be called regardless of whether
  // the toolbar button is showing because it may be already showing due to
  // being pinned and should remain showing even if unpinned until
  // HideToolbarButton is called.
  display_->Enable();
  display_->Show();
}

void DownloadDisplayController::HideToolbarButton() {
  // TODO(chlily): This should only hide the bubble/button when it is not open.
  display_->Hide();
}

void DownloadDisplayController::HideBubble() {
  if (display_->IsShowingDetails()) {
    display_->HideDetails();
  }
}

void DownloadDisplayController::ListenToFullScreenChanges() {
  observation_.Observe(browser_->GetFeatures()
                           .exclusive_access_manager()
                           ->fullscreen_controller());
}

void DownloadDisplayController::OnFullscreenStateChanged() {
  if ((!fullscreen_notification_shown_ &&
       !should_show_details_on_exit_fullscreen_) ||
      display_->IsFullscreenWithParentViewHidden()) {
    return;
  }
  fullscreen_notification_shown_ = false;

  UpdateButtonStateFromUpdateService();
  if (download::ShouldShowDownloadBubble(browser_->profile()) &&
      should_show_details_on_exit_fullscreen_) {
    display_->ShowDetails();
    should_show_details_on_exit_fullscreen_ = false;
  }
}

void DownloadDisplayController::OnResume() {
  HandleAccessibleAlerts();
  UpdateButtonStateFromUpdateService();
}

void DownloadDisplayController::OpenSecuritySubpage(
    const offline_items_collection::ContentId& id) {
  display_->OpenSecuritySubpage(id);
}

void DownloadDisplayController::UpdateToolbarButtonState(
    const DownloadBubbleDisplayInfo& info,
    const DownloadDisplay::ProgressInfo& progress_info) {
  if (info.all_models_size == 0) {
    HideToolbarButton();
    return;
  }

  DownloadDisplay::IconUpdateInfo updates;

  if (info.in_progress_count > 0) {
    updates.new_state = DownloadIconState::kProgress;
    updates.new_active = info.paused_count < info.in_progress_count
                             ? DownloadIconActive::kActive
                             : DownloadIconActive::kInactive;
  } else {
    updates.new_state = DownloadIconState::kComplete;
    bool complete_unactioned =
        HasRecentCompleteDownload(kToolbarIconActiveTimeInterval,
                                  info.last_completed_time) &&
        info.has_unactioned;
    bool exited_fullscreen_owed_details =
        !display_->IsFullscreenWithParentViewHidden() &&
        should_show_details_on_exit_fullscreen_;
    if (complete_unactioned || exited_fullscreen_owed_details) {
      updates.new_active = DownloadIconActive::kActive;
      ScheduleToolbarInactive(kToolbarIconActiveTimeInterval);
    } else {
      updates.new_active = DownloadIconActive::kInactive;
    }
  }

  if (info.has_deep_scanning) {
    updates.new_state = DownloadIconState::kDeepScanning;
  }

  if (updates.new_state != DownloadIconState::kComplete ||
      HasRecentCompleteDownload(kToolbarIconVisibilityTimeInterval,
                                info.last_completed_time)) {
    ShowToolbarButton();
  }

  updates.new_progress = progress_info;

  display_->UpdateDownloadIcon(updates);
}

void DownloadDisplayController::UpdateDownloadIconToInactive() {
  DownloadDisplay::IconUpdateInfo updates;
  updates.new_active = DownloadIconActive::kInactive;
  display_->UpdateDownloadIcon(updates);
}

const DownloadBubbleDisplayInfo&
DownloadDisplayController::UpdateButtonStateFromUpdateService() {
  const DownloadBubbleDisplayInfo& info =
      bubble_controller_->update_service()->GetDisplayInfo(
          GetWebAppIdForBrowser(browser_));
  DownloadDisplay::ProgressInfo progress_info =
      bubble_controller_->update_service()->GetProgressInfo(
          GetWebAppIdForBrowser(browser_));

  UpdateToolbarButtonState(info, progress_info);

  return info;
}

void DownloadDisplayController::HandleAccessibleAlerts() {
  // Don't attempt to announce through more than one browser.
  const webapps::AppId* app_id = GetWebAppIdForBrowser(browser_);
  if (browser_ != FindMostRecentBrowserForProfileMatchingWebApp(
                      browser_->profile(), app_id)) {
    return;
  }
  for (const std::u16string& alert :
       bubble_controller_->update_service()
           ->TakeAccessibleAlertsForAnnouncement(app_id)) {
    display_->AnnounceAccessibleAlertNow(alert);
  }
}

void DownloadDisplayController::ScheduleToolbarDisappearance(
    base::TimeDelta interval) {
  icon_disappearance_timer_.Stop();
  icon_disappearance_timer_.Start(
      FROM_HERE, interval, this, &DownloadDisplayController::HideToolbarButton);
}

void DownloadDisplayController::ScheduleToolbarInactive(
    base::TimeDelta interval) {
  icon_inactive_timer_.Stop();
  icon_inactive_timer_.Start(
      FROM_HERE, interval, this,
      &DownloadDisplayController::UpdateDownloadIconToInactive);
}

void DownloadDisplayController::MaybeShowButtonWhenCreated() {
  if (!download::ShouldShowDownloadBubble(browser_->profile())) {
    return;
  }

  const DownloadBubbleDisplayInfo& info = UpdateButtonStateFromUpdateService();
  if (display_->IsShowing()) {
    base::Time disappearance_time =
        info.last_completed_time + kToolbarIconVisibilityTimeInterval;
    base::TimeDelta interval_until_disappearance =
        disappearance_time - base::Time::Now();
    // Avoid passing a negative time interval.
    ScheduleToolbarDisappearance(
        std::max(base::TimeDelta(), interval_until_disappearance));
  }
}