File: desks_client.h

package info (click to toggle)
chromium 139.0.7258.127-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,096 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 (300 lines) | stat: -rw-r--r-- 12,862 bytes parent folder | download | duplicates (7)
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
// 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.

#ifndef CHROME_BROWSER_UI_ASH_DESKS_DESKS_CLIENT_H_
#define CHROME_BROWSER_UI_ASH_DESKS_DESKS_CLIENT_H_

#include <map>
#include <memory>

#include "ash/public/cpp/session/session_observer.h"
#include "ash/wm/desks/desks_controller.h"
#include "base/containers/flat_map.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "base/types/expected.h"
#include "base/uuid.h"
#include "base/values.h"
#include "components/desks_storage/core/desk_model.h"
#include "components/sessions/core/session_id.h"

class DesksTemplatesAppLaunchHandler;
class Profile;

namespace ash {
class Desk;
class DeskTemplate;
class DesksController;
}  // namespace ash

namespace aura {
class Window;
}  // namespace aura

namespace desks_storage {
class DeskModel;
class LocalDeskDataManager;
class DeskModelWrapper;
}  // namespace desks_storage

// Class to handle all Desks in-browser functionalities. Will call into
// ash::DesksController to do actual desk related operations.
class DesksClient : public ash::SessionObserver {
 public:
  DesksClient();
  DesksClient(const DesksClient&) = delete;
  DesksClient& operator=(const DesksClient&) = delete;
  ~DesksClient() override;

  static DesksClient* Get();

  enum class DeskActionError {
    // Unknown error.
    kUnknownError = 0,
    // Storage error.
    kStorageError = 1,
    // Therer is no active profile.
    kNoCurrentUserError = 2,
    // Either the profile is not valid or there is not an active profile.
    kBadProfileError = 3,
    // The resource cannot be found.
    kResourceNotFoundError = 4,
    // The identifier is not valid.
    kInvalidIdError = 5,
    // The desks are currently being modified.
    kDesksBeingModifiedError = 6,
    // The desk count requirement not met.
    kDesksCountCheckFailedError = 7,
    kMaxValue = kDesksCountCheckFailedError,
  };

  // ash::SessionObserver:
  void OnActiveUserSessionChanged(const AccountId& account_id) override;

  // TODO: Change the callback to accept a ash::DeskTemplate* type parameter
  // later when DesksTemplatesClient (or DesksController) hooks up with storage
  // and can hold an in-memory captured desk template instance.
  using CaptureActiveDeskAndSaveTemplateCallback =
      base::OnceCallback<void(std::optional<DeskActionError> result,
                              std::unique_ptr<ash::DeskTemplate>)>;

  // Captures the active desk and saves it as template or saved desk for later
  // use. If such desk can be saved, `callback` will be invoked with
  // `std::nullopt` as the `result` with the pointer to the captured desk
  // template, otherwise, `callback` will be invoked with an `DeskActionError`
  // error as the `result` and a nullptr for desk template.
  void CaptureActiveDeskAndSaveTemplate(
      CaptureActiveDeskAndSaveTemplateCallback callback,
      ash::DeskTemplateType template_type);

  // Captures the active desk without saving it. If such desk can be saved,
  // `callback` will be invoked with `std::nullopt` as the `result` with the
  // pointer to the captured desk template, otherwise, `callback` will be
  // invoked with an `DeskActionError` error as the `result` and a nullptr for
  // desk template.
  virtual void CaptureActiveDesk(
      CaptureActiveDeskAndSaveTemplateCallback callback,
      ash::DeskTemplateType template_type);

  using DeleteDeskTemplateCallback =
      base::OnceCallback<void(std::optional<DeskActionError> result)>;
  // Deletes a saved desk template from storage. If the template can't be
  // deleted, |callback| will be invoked with the error code.
  // If it can be deleted successfully, or there is no such |template_uuid|
  // to be removed,|callback| will be invoked with the success result code.
  // TODO(crbug.com/1286515): This will be removed with the extension. Avoid
  // further uses of this method.
  void DeleteDeskTemplate(const base::Uuid& template_uuid,
                          DeleteDeskTemplateCallback callback);

  using GetDeskTemplatesCallback =
      base::OnceCallback<void(std::optional<DeskActionError> result,
                              const std::vector<raw_ptr<const ash::DeskTemplate,
                                                        VectorExperimental>>&)>;
  // Returns the current available saved desk templates.
  // TODO(crbug.com/1286515): This will be removed with the extension. Avoid
  // further uses of this method.
  void GetDeskTemplates(GetDeskTemplatesCallback callback);

  // Returns the current available desks.
  virtual base::expected<std::vector<const ash::Desk*>, DeskActionError>
  GetAllDesks();

  using GetTemplateJsonCallback =
      base::OnceCallback<void(std::optional<DeskActionError> result,
                              const base::Value& template_json)>;
  // Takes in |uuid| and fetches the stringified json representation of a
  // desk template.
  void GetTemplateJson(const base::Uuid& uuid,
                       Profile* profile,
                       GetTemplateJsonCallback callback);

  using LaunchDeskCallback =
      base::OnceCallback<void(std::optional<DeskActionError> result,
                              const base::Uuid& desk_uuid)>;
  // Launches the desk template with `template_uuid` as a new desk.
  // `template_uuid` should be the unique id for an existing desk template. If
  // no such id can be found or we are at the max desk limit (currently is 8)
  // so can't create new desk for the desk template, `callback` will be invoked
  // with a the error code. If `customized_desk_name` is provided, desk name
  // will be set to `customized_desk_name` or `customized_desk_name ({counter})`
  // to resolve naming conflicts. Otherwise, desk name will be set to auto
  // generated name.
  // TODO(crbug.com/1286515): This will be removed with the extension. Avoid
  // further uses of this method.
  virtual void LaunchDeskTemplate(
      const base::Uuid& template_uuid,
      LaunchDeskCallback callback,
      const std::u16string& customized_desk_name = std::u16string());

  // Launches an empty new desk. Desk name will be set to `customized_desk_name`
  // variant if it's provided, otherwise will be set to auto generated name.
  base::expected<const base::Uuid, DeskActionError> LaunchEmptyDesk(
      const std::u16string& customized_desk_name = std::u16string());

  using ErrorHandlingCallBack =
      base::OnceCallback<void(std::optional<DeskActionError> result)>;
  // Remove a desk, close all windows if `close_type` set to kCloseAllWindows,
  // otherwise combine the windows to the active desk to the left. Provide
  // a notification allowing the user to undo the removal if `close_type` is
  // set to `kCloseAllWindowsAndWait`
  virtual std::optional<DesksClient::DeskActionError> RemoveDesk(
      const base::Uuid& desk_uuid,
      ash::DeskCloseType close_type);

  // Uses `app_launch_handler_` to launch apps from the restore data found in
  // `desk_template`.
  virtual void LaunchAppsFromTemplate(
      std::unique_ptr<ash::DeskTemplate> desk_template);

  // Returns either the local desk storage backend or Chrome sync desk storage
  // backend depending on the feature flag DeskTemplateSync.
  desks_storage::DeskModel* GetDeskModel();

  // Sets the preconfigured desk template.
  void SetPolicyPreconfiguredTemplate(const AccountId& account_id,
                                      std::unique_ptr<std::string> data);
  void RemovePolicyPreconfiguredTemplate(const AccountId& account_id);

  // Notifies launch performance trackers that an app has been moved rather
  // than launched.
  void NotifyMovedSingleInstanceApp(int32_t window_id);

  // Set the property of showing on all-desk or not to a window.
  std::optional<DesksClient::DeskActionError>
  SetAllDeskPropertyByBrowserSessionId(SessionID browser_session_id,
                                       bool all_desk);

  // Returns the UUID of active desk.
  virtual base::Uuid GetActiveDesk();

  // Retrieves desk by its UUID.
  virtual base::expected<const ash::Desk*, DesksClient::DeskActionError>
  GetDeskByID(const base::Uuid& desk_uuid) const;

  // Switches to the target desk, returns error string if operation fails.
  std::optional<DesksClient::DeskActionError> SwitchDesk(
      const base::Uuid& desk_uuid);

 private:
  class LaunchPerformanceTracker;
  class DeskEventObserver;
  friend class DesksClientTest;
  friend class ScopedDesksTemplatesAppLaunchHandlerSetter;

  // Launches DeskTemplate after retrieval from storage.
  void OnGetTemplateForDeskLaunch(
      LaunchDeskCallback callback,
      std::u16string customized_desk_name,
      desks_storage::DeskModel::GetEntryByUuidStatus status,
      std::unique_ptr<ash::DeskTemplate> saved_desk);

  // Callback function that allows the |CaptureActiveDeskAndSaveTemplate|
  // |callback| to be called as a |desks_storage::AddOrUpdateEntryCallback|.
  void OnCaptureActiveDeskAndSaveTemplate(
      CaptureActiveDeskAndSaveTemplateCallback callback,
      desks_storage::DeskModel::AddOrUpdateEntryStatus status,
      std::unique_ptr<ash::DeskTemplate> desk_template);

  // Callback function that allows for the |DeleteDeskTemplateCallback| to be
  // called as a |desks_storage::DeleteEntryCallback|
  void OnDeleteDeskTemplate(DeleteDeskTemplateCallback callback,
                            desks_storage::DeskModel::DeleteEntryStatus status);
  // Callback function that is run after a saved desk called and moved from
  // library.
  void OnRecallSavedDesk(DesksClient::LaunchDeskCallback callback,
                         const base::Uuid& desk_id,
                         desks_storage::DeskModel::DeleteEntryStatus status);

  // Callback function that is called once the DesksController has captured the
  // active desk as a template. Invokes |callback| with |desk_template| as an
  // argument.
  void OnCapturedDeskTemplate(CaptureActiveDeskAndSaveTemplateCallback callback,
                              std::optional<DesksClient::DeskActionError> error,
                              std::unique_ptr<ash::DeskTemplate> desk_template);

  // Callback function that handles the JSON representation of a specific
  // template.
  void OnGetTemplateJson(DesksClient::GetTemplateJsonCallback callback,
                         desks_storage::DeskModel::GetTemplateJsonStatus status,
                         const base::Value& json_representation);

  // Callback function that clears the data associated with a specific launch.
  void OnLaunchComplete(int32_t launch_id);

  // Called by a launch performance tracker when it has completed monitoring the
  // launch of a template.
  void RemoveLaunchPerformanceTracker(const base::Uuid& tracker_uuid);

  // Get the pointer to the window by `browser_session_id`.
  aura::Window* GetWindowByBrowserSessionId(SessionID browser_session_id);

  // Creates a new desk and switch to it. If `customized_desk_name` is
  // provided, desk name will be `customized_desk_name` or `customized_desk_name
  // ({counter})` to resolve naming conflicts. CanCreateDesks() must be checked
  // before calling this.
  const ash::Desk* CreateEmptyDeskAndActivate(
      const std::u16string& customized_desk_name);

  // Convenience pointer to ash::DesksController. Guaranteed to be not null for
  // the duration of `this`.
  const raw_ptr<ash::DesksController> desks_controller_;

  raw_ptr<Profile> active_profile_ = nullptr;

  // Maps launch id to a launch handler.
  std::map<int32_t, std::unique_ptr<DesksTemplatesAppLaunchHandler>>
      app_launch_handlers_;

  // A test only template for testing `LaunchDeskTemplate`.
  std::unique_ptr<ash::DeskTemplate> launch_template_for_test_;

  // Local desks storage backend for desk templates.
  std::unique_ptr<desks_storage::LocalDeskDataManager>
      desk_templates_storage_manager_;

  // Local desks storage backend for save and recall desks.
  std::unique_ptr<desks_storage::LocalDeskDataManager>
      save_and_recall_desks_storage_manager_;

  // Wrapper desk model to house both desk types backend storage.
  std::unique_ptr<desks_storage::DeskModelWrapper> saved_desk_storage_manager_;

  // The stored JSON values of preconfigured desk templates
  base::flat_map<AccountId, std::string> preconfigured_desk_templates_json_;

  // Mapping of template ids that are being launched to their launch performance
  // trackers.
  base::flat_map<base::Uuid, std::unique_ptr<LaunchPerformanceTracker>>
      template_ids_to_launch_performance_trackers_;

  // Monitors desk events.
  std::unique_ptr<DeskEventObserver> desk_event_observer_;

  base::WeakPtrFactory<DesksClient> weak_ptr_factory_{this};
};

#endif  // CHROME_BROWSER_UI_ASH_DESKS_DESKS_CLIENT_H_