File: holding_space_keyed_service.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 (239 lines) | stat: -rw-r--r-- 9,840 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
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
// 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_KEYED_SERVICE_H_
#define CHROME_BROWSER_UI_ASH_HOLDING_SPACE_HOLDING_SPACE_KEYED_SERVICE_H_

#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "ash/public/cpp/holding_space/holding_space_image.h"
#include "ash/public/cpp/holding_space/holding_space_metrics.h"
#include "ash/public/cpp/holding_space/holding_space_model.h"
#include "ash/public/cpp/holding_space/holding_space_progress.h"
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "chrome/browser/profiles/profile_observer.h"
#include "chrome/browser/ui/ash/holding_space/holding_space_client_impl.h"
#include "chrome/browser/ui/ash/thumbnail_loader/thumbnail_loader.h"
#include "chromeos/dbus/power/power_manager_client.h"
#include "components/account_id/account_id.h"
#include "components/keyed_service/core/keyed_service.h"
#include "url/gurl.h"

class GURL;

namespace base {
class FilePath;
}  // namespace base

namespace user_prefs {
class PrefRegistrySyncable;
}  // namespace user_prefs

namespace storage {
class FileSystemURL;
}  // namespace storage

namespace ash {

class HoldingSpaceDownloadsDelegate;
class HoldingSpaceKeyedServiceDelegate;
class HoldingSpaceSuggestionsDelegate;

// Browser context keyed service that:
// *   Manages the temporary holding space per-profile data model.
// *   Serves as an entry point to add holding space items from Chrome.
class HoldingSpaceKeyedService : public KeyedService,
                                 public ProfileObserver,
                                 public chromeos::PowerManagerClient::Observer {
 public:
  HoldingSpaceKeyedService(Profile* profile, const AccountId& account_id);
  HoldingSpaceKeyedService(const HoldingSpaceKeyedService& other) = delete;
  HoldingSpaceKeyedService& operator=(const HoldingSpaceKeyedService& other) =
      delete;
  ~HoldingSpaceKeyedService() override;

  // Registers profile preferences for holding space.
  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);

  // ProfileObserver override
  void OnProfileInitializationComplete(Profile* profile) override;

  // Adds multiple pinned file items identified by the provided file system
  // URLs. NOTE: No-op if the service has not been initialized.
  void AddPinnedFiles(
      const std::vector<storage::FileSystemURL>& file_system_urls);

  // Removes multiple pinned file items identified by the provided file system
  // URLs. NOTE: No-ops if:
  // 1. The specified files are not present in the holding space; OR
  // 2. The service has not been initialized.
  void RemovePinnedFiles(
      const std::vector<storage::FileSystemURL>& file_system_urls);

  // Returns whether the holding space contains a pinned file identified by a
  // file system URL.
  bool ContainsPinnedFile(const storage::FileSystemURL& file_system_url) const;

  // Returns the list of pinned files in the holding space. It returns the files
  // files system URLs as GURLs.
  std::vector<GURL> GetPinnedFiles() const;

  // Refreshes suggestions. Note that this intentionally does *not* invalidate
  // the file suggest service's item suggest cache which is too expensive for
  // holding space to invalidate.
  void RefreshSuggestions();

  // Removes suggestions associated with the specified `absolute_file_paths`.
  void RemoveSuggestions(
      const std::vector<base::FilePath>& absolute_file_paths);

  // Replaces the existing suggestions with `suggestions`. The order among
  // `suggestions` is respected, which means that if a suggestion A is in front
  // of a suggestion B in the given array, after calling this function, the
  // suggestion view of A is in front of the view of B. `suggestions` can be
  // empty. In this case, all the existing suggestions are cleared.
  // NOTE: No-op if the service has not been initialized.
  void SetSuggestions(
      const std::vector<std::pair<HoldingSpaceItem::Type, base::FilePath>>&
          suggestions);

  // Adds the specified `item` to the holding space model. Returns the id of the
  // added holding space item. Returns an empty string if the item is not added
  // for either of the following reasons:
  // 1. The `item` is a duplicate.
  // 2. The service has not been initialized.
  const std::string& AddItem(std::unique_ptr<HoldingSpaceItem> item);

  // Adds an item of the specified `type` backed by the provided absolute
  // `file_path` to the holding space model. Returns the id of the added
  // holding space item. Returns an empty string if the item is not added
  // for either of the following reasons:
  // 1. The item to add is a duplicate.
  // 2. The service has not been initialized.
  const std::string& AddItemOfType(
      HoldingSpaceItem::Type type,
      const base::FilePath& file_path,
      const HoldingSpaceProgress& progress = HoldingSpaceProgress(),
      HoldingSpaceImage::PlaceholderImageSkiaResolver
          placeholder_image_skia_resolver = base::NullCallback());

  // Returns whether a holding space item exists for the specified `id`.
  bool ContainsItem(const std::string& id) const;

  // Returns an object which, upon its destruction, performs an atomic update to
  // the holding space item associated with the specified `id`. Returns
  // `nullptr` if the service has not been initialized.
  std::unique_ptr<HoldingSpaceModel::ScopedItemUpdate> UpdateItem(
      const std::string& id);

  // Removes all holding space items directly from the model.
  // NOTE: No-op if the service has not been initialized.
  void RemoveAll();

  // Removes the holding space item with the specified `id` from the model.
  // NOTE: No-op if the service has not been initialized.
  void RemoveItem(const std::string& id);

  // Attempts to mark the specified holding space `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>
  OpenItemWhenComplete(const HoldingSpaceItem* item);

  // Returns the `profile_` associated with this service.
  Profile* profile() { return profile_; }

  HoldingSpaceClient* client() { return &holding_space_client_; }

  const HoldingSpaceModel* model_for_testing() const {
    return &holding_space_model_;
  }

  ThumbnailLoader* thumbnail_loader_for_testing() { return &thumbnail_loader_; }

 private:
  // KeyedService:
  void Shutdown() override;

  // PowerManagerClient::Observer
  void SuspendImminent(power_manager::SuspendImminent::Reason reason) override;
  void SuspendDone(base::TimeDelta sleep_duration) override;

  // Adds multiple `items` to the holding space model. `allow_duplicates`
  // indicates whether an item should be added to the model if it is duplicate
  // to an existing item. Returns the ids of the added holding space items or
  // empty strings where items were not added due to de-duplication checks.
  // NOTE: This function can be called during service initialization.
  std::vector<std::reference_wrapper<const std::string>> AddItems(
      std::vector<std::unique_ptr<HoldingSpaceItem>> items,
      bool allow_duplicates);

  // Invoked when the associated profile is ready.
  void OnProfileReady();

  // Creates and initializes holding space delegates. Called when the associated
  // profile finishes initialization, or when device suspend ends (the delegates
  // are shutdown during suspend).
  void InitializeDelegates();

  // Shuts down and destroys existing holding space delegates. Called on
  // profile shutdown, or when device suspend starts.
  void ShutdownDelegates();

  // Invoked when holding space persistence has been restored. Adds
  // `restored_items` to the holding space model and notifies delegates.
  void OnPersistenceRestored(
      std::vector<std::unique_ptr<HoldingSpaceItem>> restored_items);

  // Pin a drive file for offline access.
  void MakeDriveItemAvailableOffline(
      const storage::FileSystemURL& file_system_url);

  // Returns whether the service has been initialized. The service is considered
  // initialized if:
  // 1. All delegates have been created.
  // 2. Persistence restoration has been completed.
  bool IsInitialized() const;

  // Creates an item of the specified `type` backed by the provided absolute
  // `file_path`. Returns an empty unique pointer if the file url cannot be
  // resolved.
  std::unique_ptr<HoldingSpaceItem> CreateItemOfType(
      HoldingSpaceItem::Type type,
      const base::FilePath& file_path,
      const HoldingSpaceProgress& progress,
      HoldingSpaceImage::PlaceholderImageSkiaResolver
          placeholder_image_skia_resolver);

  const raw_ptr<Profile> profile_;
  const AccountId account_id_;

  HoldingSpaceClientImpl holding_space_client_;
  HoldingSpaceModel holding_space_model_;

  ThumbnailLoader thumbnail_loader_;

  // The `HoldingSpaceKeyedService` owns a collection of `delegates_` which are
  // each tasked with an independent area of responsibility on behalf of the
  // service. They operate autonomously of one another.
  std::vector<std::unique_ptr<HoldingSpaceKeyedServiceDelegate>> delegates_;

  // The delegate, owned by `delegates_`, responsible for downloads.
  raw_ptr<HoldingSpaceDownloadsDelegate> downloads_delegate_ = nullptr;

  // The delegate, owned by `delegates_`, responsible for suggestions.
  raw_ptr<HoldingSpaceSuggestionsDelegate> suggestions_delegate_ = nullptr;

  base::ScopedObservation<Profile, ProfileObserver> profile_observer_{this};

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

}  // namespace ash

#endif  // CHROME_BROWSER_UI_ASH_HOLDING_SPACE_HOLDING_SPACE_KEYED_SERVICE_H_