File: saved_desk_builder.h

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 (293 lines) | stat: -rw-r--r-- 10,937 bytes parent folder | download | duplicates (6)
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_DESKS_STORAGE_CORE_SAVED_DESK_BUILDER_H_
#define COMPONENTS_DESKS_STORAGE_CORE_SAVED_DESK_BUILDER_H_

#include <optional>
#include <string>
#include <vector>

#include "base/time/time.h"
#include "base/uuid.h"
#include "chromeos/ui/base/window_state_type.h"
#include "components/app_restore/app_launch_info.h"
#include "components/app_restore/restore_data.h"
#include "components/app_restore/window_info.h"
#include "components/services/app_service/public/cpp/app_launch_util.h"
#include "components/sync/protocol/workspace_desk_specifics.pb.h"
#include "components/tab_groups/tab_group_color.h"
#include "components/tab_groups/tab_group_info.h"
#include "ui/base/mojom/window_show_state.mojom-forward.h"
#include "ui/base/ui_base_types.h"
#include "ui/base/window_open_disposition.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/range/range.h"
#include "url/gurl.h"

namespace ash {
class DeskTemplate;
enum class DeskTemplateSource;
enum class DeskTemplateType;
}  // namespace ash

namespace desks_storage {

// This structure represents an app that has been built by a builder.
// Both the window_info struct and the launch info struct are needed by the
// AppRestoreClass to add a new app.
struct BuiltApp {
  enum Status {
    kOk,
    // All apps must have a window Id.
    kNoWindowId
  };

  BuiltApp(Status status,
           std::unique_ptr<app_restore::WindowInfo> window_info,
           std::unique_ptr<app_restore::AppLaunchInfo> launch_info);
  BuiltApp(BuiltApp&&);
  BuiltApp& operator=(BuiltApp&&);
  ~BuiltApp();

  // Any BuiltApp that does not have status kOk should be discarded.
  Status status;
  std::unique_ptr<app_restore::WindowInfo> window_info;
  std::unique_ptr<app_restore::AppLaunchInfo> launch_info;
};

// Generic App Builder class that contains the definitions for the
// base fields that can be set for every app.  Chrome Apps, SWAs
// and other unknown types can be generated via this class.
class SavedDeskGenericAppBuilder {
 public:
  SavedDeskGenericAppBuilder();
  SavedDeskGenericAppBuilder(const SavedDeskGenericAppBuilder&) = delete;
  SavedDeskGenericAppBuilder& operator=(const SavedDeskGenericAppBuilder&) =
      delete;
  SavedDeskGenericAppBuilder(SavedDeskGenericAppBuilder&&);
  SavedDeskGenericAppBuilder& operator=(SavedDeskGenericAppBuilder&&);
  ~SavedDeskGenericAppBuilder();

  // This method builds a generic app and populates a BuiltApp struct with
  // initial fields.
  BuiltApp Build();

  // Setters and Adders.
  SavedDeskGenericAppBuilder& SetAppId(std::string app_id_);
  SavedDeskGenericAppBuilder& SetWindowBound(gfx::Rect bounds);
  SavedDeskGenericAppBuilder& SetWindowState(chromeos::WindowStateType state);
  SavedDeskGenericAppBuilder& SetPreMinimizedWindowState(
      ui::mojom::WindowShowState state);
  SavedDeskGenericAppBuilder& SetZIndex(int index);
  SavedDeskGenericAppBuilder& SetWindowId(int window_id);
  SavedDeskGenericAppBuilder& SetDisplayId(int64_t display_id);
  SavedDeskGenericAppBuilder& SetLaunchContainer(
      apps::LaunchContainer container);
  SavedDeskGenericAppBuilder& SetWindowOpenDisposition(
      WindowOpenDisposition disposition);
  SavedDeskGenericAppBuilder& SetName(std::string name);
  SavedDeskGenericAppBuilder& SetSnapPercentage(int percentage);
  SavedDeskGenericAppBuilder& SetEventFlag(int32_t event_flag);

  // Apps are keyed by their ID, this returns the ID associated with this app
  // assuming it has been set.  There are some special app cases in which this
  // ID is a constant (i.e. a browser)  In the case where this is an app
  // with an assignable ID then a random one is assigned upon calling this
  // method.
  const std::string& GetAppId();

 private:
  std::optional<std::string> app_id_;
  std::optional<gfx::Rect> window_bounds_;
  std::optional<chromeos::WindowStateType> window_show_state_;
  std::optional<ui::mojom::WindowShowState> pre_minimized_window_show_state_;
  std::optional<int> z_index_;
  std::optional<int> window_id_;
  std::optional<int64_t> display_id_;
  std::optional<apps::LaunchContainer> launch_conatiner_;
  std::optional<WindowOpenDisposition> disposition_;
  std::optional<std::string> name_;
  std::optional<int> snap_percentage_;
  std::optional<int32_t> event_flag_;
};

// Builder for TabGroups.  Each instance represents a single tab group.
class SavedDeskTabGroupBuilder {
 public:
  enum TabGroupBuildStatus {
    kOk,
    // All fields must be set to build a TabGroup.
    kNotAllFieldsSet
  };

  // Caller will have to release the TabGroup from the unique_ptr contained
  // within.  This will help facilitate moving this result object around after
  // calling build.
  struct TabGroupWithStatus {
    TabGroupWithStatus(TabGroupBuildStatus status,
                       std::unique_ptr<tab_groups::TabGroupInfo> tab_group);
    ~TabGroupWithStatus();

    TabGroupBuildStatus status;
    std::unique_ptr<tab_groups::TabGroupInfo> tab_group;
  };

  SavedDeskTabGroupBuilder();
  SavedDeskTabGroupBuilder(const SavedDeskTabGroupBuilder&) = delete;
  SavedDeskTabGroupBuilder& operator=(const SavedDeskTabGroupBuilder&) = delete;
  SavedDeskTabGroupBuilder(SavedDeskTabGroupBuilder&&);
  ~SavedDeskTabGroupBuilder();

  // Setters and adders.
  SavedDeskTabGroupBuilder& SetRange(gfx::Range range);
  SavedDeskTabGroupBuilder& SetTitle(std::string title);
  SavedDeskTabGroupBuilder& SetColor(tab_groups::TabGroupColorId color);
  SavedDeskTabGroupBuilder& SetIsCollapsed(bool is_collapsed);

  // Returns a TabGroupWithStatus, pointer to the tab group will be nullptr
  // unless all fields associated with this struct are set.
  TabGroupWithStatus Build();

 private:
  std::optional<gfx::Range> range_;
  std::optional<std::string> title_;
  std::optional<tab_groups::TabGroupColorId> color_;
  std::optional<bool> is_collapsed_;
};

// Builder that constructs Browser representations, this can also be used to
// create PWAs.  To construct a PWA only use a single URL in the URLs field
// and set IsApp to true.  Requires a GenericAppBuilder with `window_id` set
// in order to correctly build.
class SavedDeskBrowserBuilder {
 public:
  SavedDeskBrowserBuilder();
  SavedDeskBrowserBuilder(const SavedDeskBrowserBuilder&) = delete;
  SavedDeskBrowserBuilder& operator=(const SavedDeskBrowserBuilder&) = delete;
  ~SavedDeskBrowserBuilder();

  BuiltApp Build();

  // Compose a generic builder to build with altered generic traits.
  SavedDeskBrowserBuilder& SetGenericBuilder(
      SavedDeskGenericAppBuilder& generic_builder);

  // setters and adders.
  SavedDeskBrowserBuilder& SetFirstNonPinnedTabIndex(int index);
  SavedDeskBrowserBuilder& SetActiveTabIndex(int index);
  SavedDeskBrowserBuilder& SetUrls(std::vector<GURL> urls);
  SavedDeskBrowserBuilder& AddTabGroupBuilder(
      SavedDeskTabGroupBuilder tab_group);

  // Indicates that this browser instance is a PWA.
  SavedDeskBrowserBuilder& SetIsApp(bool is_app);

  const std::string& GetAppId();

 private:
  SavedDeskGenericAppBuilder generic_builder_;

  std::optional<bool> is_app_;
  std::vector<SavedDeskTabGroupBuilder> tab_group_builders_;
  std::optional<int> active_tab_index_;
  std::optional<int> first_non_pinned_tab_index_;
  std::vector<GURL> urls_;
};

// Builder that constructs arc apps.  Requires a GenericAppBuilder with the
// `window_id` field set to construct properly.
class SavedDeskArcAppBuilder {
 public:
  SavedDeskArcAppBuilder();
  SavedDeskArcAppBuilder(const SavedDeskArcAppBuilder&) = delete;
  SavedDeskArcAppBuilder operator=(const SavedDeskArcAppBuilder&) = delete;
  ~SavedDeskArcAppBuilder();

  BuiltApp Build();

  // set composed generic builder for setting generic traits.
  SavedDeskArcAppBuilder& SetGenericBuilder(
      SavedDeskGenericAppBuilder& generic_builder);

  // setters and adders.
  SavedDeskArcAppBuilder& SetAppId(std::string app_id);
  SavedDeskArcAppBuilder& SetMinimumSize(gfx::Size size);
  SavedDeskArcAppBuilder& SetMaximumSize(gfx::Size size);
  SavedDeskArcAppBuilder& SetBoundsInRoot(gfx::Rect bounds_in_root);

 private:
  SavedDeskGenericAppBuilder generic_builder_;

  std::optional<std::string> app_id_;
  std::optional<gfx::Size> minimum_size_;
  std::optional<gfx::Size> maximum_size_;
  std::optional<gfx::Rect> bounds_in_root_;
};

// Helper class for building a saved desk for test.
class SavedDeskBuilder {
 public:
  SavedDeskBuilder();
  SavedDeskBuilder(const SavedDeskBuilder&) = delete;
  SavedDeskBuilder& operator=(const SavedDeskBuilder&) = delete;
  ~SavedDeskBuilder();

  // Builds a saved desk. This should only be called once per builder
  // instance.
  std::unique_ptr<ash::DeskTemplate> Build();

  // Sets saved desk UUID. If not set, the built desk will have a random UUID.
  SavedDeskBuilder& SetUuid(const std::string& uuid);

  // Sets saved desk name. If not set, the built desk will have a fixed name.
  SavedDeskBuilder& SetName(const std::string& name);

  // Sets saved desk type. If not set, the built desk will default to
  // DeskTemplate.
  SavedDeskBuilder& SetType(ash::DeskTemplateType desk_type);

  // Sets saved desk source. If not set, the built desk will default to kUser.
  SavedDeskBuilder& SetSource(ash::DeskTemplateSource desk_source);

  // Sets saved desk creation timestamp. If not set, the built desk will have
  // its creation timestamp set at the creation time of the SavedDeskBuilder.
  SavedDeskBuilder& SetCreatedTime(const base::Time& created_time);

  // Sets the saved desk updated timestamp.  If not set the built desk will
  // have its updated timestamp set to the same time as its created timestamp.
  SavedDeskBuilder& SetUpdatedTime(const base::Time& updated_time);

  // Sets the policy value if needed to define a policy template.
  SavedDeskBuilder& SetPolicyValue(const base::Value& value);

  // Sets whether or not the template should be launched on startup.
  SavedDeskBuilder& SetPolicyShouldLaunchOnStartup(
      bool should_launch_on_startup);

  // Adds an app window.
  SavedDeskBuilder& AddAppWindow(BuiltApp built_app);

 private:
  // additional state for whether or not to set updated time.  This prevents
  // some older tests from breaking.  Because we are not supposed to use a
  // null time in base::Time we will track whether or not we have set this
  // field via this variable.
  bool has_updated_time_ = false;

  // state for instantiating template
  base::Uuid desk_uuid_;
  std::string desk_name_;
  ash::DeskTemplateSource desk_source_;
  ash::DeskTemplateType desk_type_;
  base::Time created_time_;
  base::Time updated_time_;
  base::Value policy_value_;
  bool policy_should_launch_on_startup_ = false;
  std::vector<BuiltApp> built_apps_;
};

}  // namespace desks_storage

#endif  // COMPONENTS_DESKS_STORAGE_CORE_SAVED_DESK_BUILDER_H_