File: holding_space_downloads_delegate.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (118 lines) | stat: -rw-r--r-- 5,310 bytes parent folder | download | duplicates (5)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_ASH_HOLDING_SPACE_HOLDING_SPACE_DOWNLOADS_DELEGATE_H_
#define CHROME_BROWSER_UI_ASH_HOLDING_SPACE_HOLDING_SPACE_DOWNLOADS_DELEGATE_H_

#include <memory>
#include <set>
#include <string>
#include <vector>

#include "base/containers/unique_ptr_adapters.h"
#include "base/scoped_observation.h"
#include "chrome/browser/ash/arc/fileapi/arc_file_system_bridge.h"
#include "chrome/browser/download/notification/multi_profile_download_notifier.h"
#include "chrome/browser/ui/ash/holding_space/holding_space_keyed_service_delegate.h"

namespace content {
class DownloadManager;
}  // namespace content

namespace ash {

// A delegate of `HoldingSpaceKeyedService` tasked with monitoring the status of
// of downloads on its behalf.
class HoldingSpaceDownloadsDelegate
    : public HoldingSpaceKeyedServiceDelegate,
      public MultiProfileDownloadNotifier::Client,
      public arc::ArcFileSystemBridge::Observer {
 public:
  HoldingSpaceDownloadsDelegate(HoldingSpaceKeyedService* service,
                                HoldingSpaceModel* model);
  HoldingSpaceDownloadsDelegate(const HoldingSpaceDownloadsDelegate&) = delete;
  HoldingSpaceDownloadsDelegate& operator=(
      const HoldingSpaceDownloadsDelegate&) = delete;
  ~HoldingSpaceDownloadsDelegate() override;

  // Attempts to mark the download underlying the given `item` to open when
  // complete. Returns `std::nullopt` on success or the reason if the attempt
  // was not successful.
  std::optional<holding_space_metrics::ItemLaunchFailureReason>
  OpenWhenComplete(const HoldingSpaceItem* item);

 private:
  class InProgressDownload;

  // HoldingSpaceKeyedServiceDelegate:
  void OnPersistenceRestored() override;
  void OnHoldingSpaceItemsRemoved(
      const std::vector<const HoldingSpaceItem*>& items) override;

  // MultiProfileDownloadNotifier::Client:
  void OnManagerInitialized(content::DownloadManager* manager) override;
  void OnManagerGoingDown(content::DownloadManager* manager) override;
  void OnDownloadCreated(content::DownloadManager* manager,
                         download::DownloadItem* item) override;
  void OnDownloadUpdated(content::DownloadManager* manager,
                         download::DownloadItem* item) override;

  // arc::ArcFileSystemBridge::Observer:
  void OnMediaStoreUriAdded(
      const GURL& uri,
      const arc::mojom::MediaStoreMetadata& metadata) override;

  // Invoked when the specified `in_progress_download` is updated. If
  // `invalidate_image` is `true`, the image for the associated holding space
  // item will be explicitly invalidated. This is necessary if, for example, the
  // underlying download is transitioning to/from a dangerous or insecure state.
  void OnDownloadUpdated(InProgressDownload* in_progress_download,
                         bool invalidate_image);

  // Invoked when the specified `in_progress_download` is completed.
  void OnDownloadCompleted(InProgressDownload* in_progress_download);

  // Invoked when the specified `in_progress_download` fails. This may be due to
  // cancellation, interruption, or destruction of the underlying download.
  void OnDownloadFailed(const InProgressDownload* in_progress_download);

  // Invoked to erase the specified `in_progress_download` when it is no longer
  // needed either due to completion or failure of the underlying download.
  void EraseDownload(const InProgressDownload* in_progress_download);

  // Creates or updates the holding space item in the model associated with the
  // specified `in_progress_download`. If `invalidate_image` is `true`, the
  // image for the holding space item will be explicitly invalidated. This is
  // necessary if, for example, the underlying download is transitioning to/from
  // a dangerous or insecure state.
  void CreateOrUpdateHoldingSpaceItem(InProgressDownload* in_progress_download,
                                      bool invalidate_image);

  // Attempts to cancel/pause/resume the download underlying the given `item`.
  void Cancel(const HoldingSpaceItem* item, HoldingSpaceCommandId command_id);
  void Pause(const HoldingSpaceItem* item, HoldingSpaceCommandId command_id);
  void Resume(const HoldingSpaceItem* item, HoldingSpaceCommandId command_id);

  // The collection of currently in-progress downloads.
  std::set<std::unique_ptr<InProgressDownload>, base::UniquePtrComparator>
      in_progress_downloads_;

  base::ScopedObservation<arc::ArcFileSystemBridge,
                          arc::ArcFileSystemBridge::Observer>
      arc_file_system_bridge_observation_{this};

  // Notifies this delegate of download events created for the profile
  // associated with this delegate's service. If the incognito profile
  // integration feature is enabled, the delegate is also notified of download
  // events created for incognito profiles spawned from the service's main
  // profile.
  MultiProfileDownloadNotifier download_notifier_{
      this, /*wait_for_manager_initialization=*/true};

  base::WeakPtrFactory<HoldingSpaceDownloadsDelegate> weak_factory_{this};
};

}  // namespace ash

#endif  // CHROME_BROWSER_UI_ASH_HOLDING_SPACE_HOLDING_SPACE_DOWNLOADS_DELEGATE_H_