File: isolated_web_app_source.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 (566 lines) | stat: -rw-r--r-- 18,390 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
// Copyright 2024 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_WEB_APPLICATIONS_ISOLATED_WEB_APPS_ISOLATED_WEB_APP_SOURCE_H_
#define CHROME_BROWSER_WEB_APPLICATIONS_ISOLATED_WEB_APPS_ISOLATED_WEB_APP_SOURCE_H_

#include <iosfwd>
#include <optional>
#include <type_traits>
#include <variant>

#include "base/files/file_path.h"
#include "base/types/expected.h"
#include "base/values.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_storage_location.h"
#include "components/web_package/signed_web_bundles/signed_web_bundle_id.h"
#include "url/origin.h"

namespace web_app {

// The classes defined in this file represent the source of an Isolated Web App.
// There are two kinds of sources: Bundle-based sources and proxy-based sources.
// These classes allow code that only deals with bundles, only deals with
// proxies, only deals with dev mode apps, etc., to be written in a type-safe
// way without having to resort to `CHECK`s or error handling for unsupported
// types of sources.
//
// Note: Not all of these forward declarations are technically required, but
// they make it easier to get an overview of the available classes.

// A source representing either a bundle or a proxy.
class IwaSource;

// A source representing either a bundle or a proxy, with additional information
// about whether the source is a dev mode or prod mode source.
class IwaSourceWithMode;
class IwaSourceDevMode;
class IwaSourceProdMode;  // can never be a proxy

// A source representing either a bundle or a proxy, with additional information
// about whether the source is a dev mode or prod mode source, as well as
// information about a file operation for bundle-based sources. The available
// file operations differ depending on whether it is a dev mode or prod mode
// source.
class IwaSourceWithModeAndFileOp;
class IwaSourceDevModeWithFileOp;
class IwaSourceProdModeWithFileOp;  // can never be a proxy

// Bundle-based sources:
class IwaSourceBundle;
class IwaSourceBundleWithMode;
class IwaSourceBundleDevMode;
class IwaSourceBundleProdMode;
class IwaSourceBundleWithModeAndFileOp;
class IwaSourceBundleDevModeWithFileOp;
class IwaSourceBundleProdModeWithFileOp;

// Proxy-based sources (there is just a single class here, because proxies are
// always dev mode and do not have a file operation):
class IwaSourceProxy;

class IwaSourceProxy {
 public:
  // `explicit_bundle_id` will be used as bundle id for this Web App,
  // instead of randomly generated one. Must be type proxy.
  explicit IwaSourceProxy(url::Origin proxy_url,
                          std::optional<web_package::SignedWebBundleId>
                              explicit_bundle_id = std::nullopt);
  ~IwaSourceProxy();

  IwaSourceProxy(const IwaSourceProxy&);
  IwaSourceProxy& operator=(const IwaSourceProxy&);
  IwaSourceProxy(IwaSourceProxy&&);
  IwaSourceProxy& operator=(IwaSourceProxy&&);

  bool operator==(const IwaSourceProxy&) const;

  const url::Origin proxy_url() const { return proxy_url_; }
  const std::optional<web_package::SignedWebBundleId>& explicit_bundle_id()
      const {
    return explicit_bundle_id_;
  }
  bool dev_mode() const { return true; }

  base::Value ToDebugValue() const;

 private:
  url::Origin proxy_url_;
  // Optionally a bundle_id can be provided. Otherwise
  // this IWA would have a randomly generated one.
  std::optional<web_package::SignedWebBundleId> explicit_bundle_id_;
};
std::ostream& operator<<(std::ostream& os, const IwaSourceProxy& source);

enum class IwaSourceBundleModeAndFileOp {
  kDevModeCopy,
  kDevModeMove,

  kProdModeCopy,
  kProdModeMove,
  // References are not allowed outside of dev mode.
};
std::ostream& operator<<(std::ostream& os,
                         IwaSourceBundleModeAndFileOp bundle_mode_and_file_op);

enum class IwaSourceBundleDevFileOp {
  kCopy,
  kMove,
};
std::ostream& operator<<(std::ostream& os, IwaSourceBundleDevFileOp file_op);

enum class IwaSourceBundleProdFileOp {
  kCopy,
  kMove,
  // References are not allowed outside of dev mode.
};
std::ostream& operator<<(std::ostream& os, IwaSourceBundleProdFileOp file_op);

namespace internal {

class IwaSourceBundleBase {
 public:
  explicit IwaSourceBundleBase(base::FilePath);
  ~IwaSourceBundleBase();

  bool operator==(const IwaSourceBundleBase&) const;

  const base::FilePath& path() const { return path_; }

 protected:
  base::FilePath path_;
};

}  // namespace internal

class IwaSourceBundle : public internal::IwaSourceBundleBase {
 public:
  explicit IwaSourceBundle(base::FilePath);
  ~IwaSourceBundle();

  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSourceBundle(IwaSourceBundleWithMode other);
  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSourceBundle(IwaSourceBundleWithModeAndFileOp other);

  bool operator==(const IwaSourceBundle&) const;

  [[nodiscard]] IwaSourceBundleWithModeAndFileOp WithModeAndFileOp(
      IwaSourceBundleModeAndFileOp mode_and_file_op) const;
  [[nodiscard]] IwaSourceBundleDevModeWithFileOp WithDevModeFileOp(
      IwaSourceBundleDevFileOp file_op) const;
  [[nodiscard]] IwaSourceBundleProdModeWithFileOp WithProdModeFileOp(
      IwaSourceBundleProdFileOp file_op) const;

  base::Value ToDebugValue() const;
};
std::ostream& operator<<(std::ostream& os, const IwaSourceBundle& source);

class IwaSourceBundleWithMode : public internal::IwaSourceBundleBase {
 public:
  friend class IwaSourceBundle;

  IwaSourceBundleWithMode(base::FilePath path, bool dev_mode);
  ~IwaSourceBundleWithMode();

  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSourceBundleWithMode(IwaSourceBundleDevMode other);
  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSourceBundleWithMode(IwaSourceBundleProdMode other);
  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSourceBundleWithMode(
      IwaSourceBundleWithModeAndFileOp other);

  bool operator==(const IwaSourceBundleWithMode&) const;

  // Depending on whether `this` is a dev mode or prod mode source, returns a
  // source with either the `prod_file_op` or `dev_file_op` file operation.
  [[nodiscard]] IwaSourceBundleWithModeAndFileOp WithFileOp(
      IwaSourceBundleProdFileOp prod_file_op,
      IwaSourceBundleDevFileOp dev_file_op) const;

  bool dev_mode() const { return dev_mode_; }

  base::Value ToDebugValue() const;

 protected:
  bool dev_mode_;
};
std::ostream& operator<<(std::ostream& os,
                         const IwaSourceBundleWithMode& source);

class IwaSourceBundleDevMode : public internal::IwaSourceBundleBase {
 public:
  friend class IwaSourceBundleWithMode;

  explicit IwaSourceBundleDevMode(base::FilePath path);
  ~IwaSourceBundleDevMode();

  bool operator==(const IwaSourceBundleDevMode&) const;

  [[nodiscard]] IwaSourceBundleDevModeWithFileOp WithFileOp(
      IwaSourceBundleDevFileOp) const;

  base::Value ToDebugValue() const;
};
std::ostream& operator<<(std::ostream& os,
                         const IwaSourceBundleDevMode& source);

class IwaSourceBundleProdMode : public internal::IwaSourceBundleBase {
 public:
  friend class IwaSourceBundleWithMode;

  explicit IwaSourceBundleProdMode(base::FilePath path);
  ~IwaSourceBundleProdMode();

  bool operator==(const IwaSourceBundleProdMode&) const;

  [[nodiscard]] IwaSourceBundleProdModeWithFileOp WithFileOp(
      IwaSourceBundleProdFileOp) const;

  base::Value ToDebugValue() const;
};
std::ostream& operator<<(std::ostream& os,
                         const IwaSourceBundleProdMode& source);

class IwaSourceBundleWithModeAndFileOp : public internal::IwaSourceBundleBase {
 public:
  friend class IwaSourceBundle;
  friend class IwaSourceBundleWithMode;

  using ModeAndFileOp = IwaSourceBundleModeAndFileOp;

  IwaSourceBundleWithModeAndFileOp(base::FilePath path,
                                   ModeAndFileOp mode_and_file_op);
  ~IwaSourceBundleWithModeAndFileOp();

  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSourceBundleWithModeAndFileOp(
      IwaSourceBundleDevModeWithFileOp other);
  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSourceBundleWithModeAndFileOp(
      IwaSourceBundleProdModeWithFileOp other);

  bool operator==(const IwaSourceBundleWithModeAndFileOp&) const;

  bool dev_mode() const;
  ModeAndFileOp mode_and_file_op() const { return mode_and_file_op_; }

  base::Value ToDebugValue() const;

 protected:
  ModeAndFileOp mode_and_file_op_;
};
std::ostream& operator<<(std::ostream& os,
                         const IwaSourceBundleWithModeAndFileOp& source);

class IwaSourceBundleDevModeWithFileOp : public IwaSourceBundleDevMode {
 public:
  friend class IwaSourceBundleWithModeAndFileOp;

  using FileOp = IwaSourceBundleDevFileOp;

  IwaSourceBundleDevModeWithFileOp(base::FilePath path, FileOp file_op);
  ~IwaSourceBundleDevModeWithFileOp();

  bool operator==(const IwaSourceBundleDevModeWithFileOp&) const;

  FileOp file_op() const { return file_op_; }

  base::Value ToDebugValue() const;

 protected:
  FileOp file_op_;
};
std::ostream& operator<<(std::ostream& os,
                         const IwaSourceBundleDevModeWithFileOp& source);

class IwaSourceBundleProdModeWithFileOp : public IwaSourceBundleProdMode {
 public:
  friend class IwaSourceBundleWithModeAndFileOp;

  using FileOp = IwaSourceBundleProdFileOp;

  IwaSourceBundleProdModeWithFileOp(base::FilePath path, FileOp file_op);
  ~IwaSourceBundleProdModeWithFileOp();

  bool operator==(const IwaSourceBundleProdModeWithFileOp&) const;

  FileOp file_op() const { return file_op_; }

  base::Value ToDebugValue() const;

 protected:
  FileOp file_op_;
};
std::ostream& operator<<(std::ostream& os,
                         const IwaSourceBundleProdModeWithFileOp& source);

class IwaSource {
 public:
  using Variant = std::variant<IwaSourceBundle, IwaSourceProxy>;

  template <typename V>
  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSource(V variant_value)
    requires(std::is_constructible_v<Variant, V>)
      : variant_(std::move(variant_value)) {}

  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSource(IwaSourceWithMode other);
  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSource(IwaSourceWithModeAndFileOp other);

  IwaSource(const IwaSource&);
  IwaSource& operator=(const IwaSource&);

  ~IwaSource();

  bool operator==(const IwaSource&) const;

  const Variant& variant() const { return variant_; }

  base::Value ToDebugValue() const;

 private:
  Variant variant_;
};
std::ostream& operator<<(std::ostream& os, const IwaSource& source);

class IwaSourceWithMode {
 public:
  friend class IwaSource;

  using Variant = std::variant<IwaSourceBundleWithMode, IwaSourceProxy>;

  static IwaSourceWithMode FromStorageLocation(
      const base::FilePath& profile_dir,
      const IsolatedWebAppStorageLocation& storage_location);

  template <typename V>
  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSourceWithMode(V variant_value)
    requires(std::is_constructible_v<Variant, V>)
      : variant_(std::move(variant_value)) {}

  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSourceWithMode(IwaSourceDevMode other);
  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSourceWithMode(IwaSourceProdMode other);
  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSourceWithMode(IwaSourceWithModeAndFileOp other);

  IwaSourceWithMode(const IwaSourceWithMode& other);
  IwaSourceWithMode& operator=(const IwaSourceWithMode& other);

  ~IwaSourceWithMode();

  bool operator==(const IwaSourceWithMode&) const;

  // Depending on whether `this` is a dev mode or prod mode source, returns a
  // source with either the `prod_file_op` or `dev_file_op` file operation.
  [[nodiscard]] IwaSourceWithModeAndFileOp WithFileOp(
      IwaSourceBundleProdFileOp prod_file_op,
      IwaSourceBundleDevFileOp dev_file_op) const;

  bool dev_mode() const;
  const Variant& variant() const { return variant_; }

  base::Value ToDebugValue() const;

 private:
  Variant variant_;
};
std::ostream& operator<<(std::ostream& os, const IwaSourceWithMode& source);

class IwaSourceDevMode {
 public:
  friend class IwaSourceWithMode;

  using Variant = std::variant<IwaSourceBundleDevMode, IwaSourceProxy>;

  // Attempt to convert the provided `storage_location` into an instance of
  // `IwaSourceDevMode`. Will fail with an unexpected if the storage location is
  // not a dev mode storage location.
  static base::expected<IwaSourceDevMode, std::monostate> FromStorageLocation(
      const base::FilePath& profile_dir,
      const IsolatedWebAppStorageLocation& storage_location);

  template <typename V>
  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSourceDevMode(V variant_value)
    requires(std::is_constructible_v<Variant, V>)
      : variant_(std::move(variant_value)) {}

  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSourceDevMode(IwaSourceDevModeWithFileOp other);

  IwaSourceDevMode(const IwaSourceDevMode& other);
  IwaSourceDevMode& operator=(const IwaSourceDevMode& other);

  ~IwaSourceDevMode();

  bool operator==(const IwaSourceDevMode&) const;

  [[nodiscard]] IwaSourceDevModeWithFileOp WithFileOp(
      IwaSourceBundleDevFileOp file_op) const;

  const Variant& variant() const { return variant_; }

  base::Value ToDebugValue() const;

 private:
  Variant variant_;
};
std::ostream& operator<<(std::ostream& os, const IwaSourceDevMode& source);

class IwaSourceProdMode {
 public:
  friend class IwaSourceWithMode;

  // Even though there is just one type in the variant, we use a variant for
  // consistency.
  using Variant = std::variant<IwaSourceBundleProdMode>;

  // Attempt to convert the provided `storage_location` into an instance of
  // `IwaSourceProdMode`. Will fail with an unexpected if the storage location
  // is not a prod mode storage location.
  static base::expected<IwaSourceProdMode, std::monostate> FromStorageLocation(
      const base::FilePath& profile_dir,
      const IsolatedWebAppStorageLocation& storage_location);

  template <typename V>
  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSourceProdMode(V variant_value)
    requires(std::is_constructible_v<Variant, V>)
      : variant_(std::move(variant_value)) {}

  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSourceProdMode(IwaSourceProdModeWithFileOp other);

  IwaSourceProdMode(const IwaSourceProdMode& other);
  IwaSourceProdMode& operator=(const IwaSourceProdMode& other);

  ~IwaSourceProdMode();

  bool operator==(const IwaSourceProdMode&) const;

  [[nodiscard]] IwaSourceProdModeWithFileOp WithFileOp(
      IwaSourceBundleProdFileOp file_op) const;

  const Variant& variant() const { return variant_; }

  base::Value ToDebugValue() const;

 private:
  Variant variant_;
};
std::ostream& operator<<(std::ostream& os, const IwaSourceProdMode& source);

class IwaSourceWithModeAndFileOp {
 public:
  friend class IwaSource;
  friend class IwaSourceWithMode;

  using Variant =
      std::variant<IwaSourceBundleWithModeAndFileOp, IwaSourceProxy>;

  template <typename V>
  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSourceWithModeAndFileOp(V variant_value)
    requires(std::is_constructible_v<Variant, V>)
      : variant_(std::move(variant_value)) {}

  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSourceWithModeAndFileOp(IwaSourceDevModeWithFileOp other);
  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSourceWithModeAndFileOp(IwaSourceProdModeWithFileOp other);

  IwaSourceWithModeAndFileOp(const IwaSourceWithModeAndFileOp& other);
  IwaSourceWithModeAndFileOp& operator=(
      const IwaSourceWithModeAndFileOp& other);

  ~IwaSourceWithModeAndFileOp();

  bool operator==(const IwaSourceWithModeAndFileOp&) const;

  bool dev_mode() const;
  const Variant& variant() const { return variant_; }

  base::Value ToDebugValue() const;

 private:
  Variant variant_;
};
std::ostream& operator<<(std::ostream& os,
                         const IwaSourceWithModeAndFileOp& source);

class IwaSourceDevModeWithFileOp {
 public:
  friend class IwaSourceDevMode;
  friend class IwaSourceWithModeAndFileOp;

  using Variant =
      std::variant<IwaSourceBundleDevModeWithFileOp, IwaSourceProxy>;

  template <typename V>
  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSourceDevModeWithFileOp(V variant_value)
    requires(std::is_constructible_v<Variant, V>)
      : variant_(std::move(variant_value)) {}

  IwaSourceDevModeWithFileOp(const IwaSourceDevModeWithFileOp& other);
  IwaSourceDevModeWithFileOp& operator=(
      const IwaSourceDevModeWithFileOp& other);

  ~IwaSourceDevModeWithFileOp();

  bool operator==(const IwaSourceDevModeWithFileOp&) const;

  const Variant& variant() const { return variant_; }

  base::Value ToDebugValue() const;

 private:
  Variant variant_;
};
std::ostream& operator<<(std::ostream& os,
                         const IwaSourceDevModeWithFileOp& source);

class IwaSourceProdModeWithFileOp {
 public:
  friend class IwaSourceProdMode;
  friend class IwaSourceWithModeAndFileOp;

  // Even though there is just one type in the variant, we use a variant for
  // consistency.
  using Variant = std::variant<IwaSourceBundleProdModeWithFileOp>;

  template <typename V>
  // NOLINTNEXTLINE(google-explicit-constructor)
  /* implicit */ IwaSourceProdModeWithFileOp(V variant_value)
    requires(std::is_constructible_v<Variant, V>)
      : variant_(std::move(variant_value)) {}

  IwaSourceProdModeWithFileOp(const IwaSourceProdModeWithFileOp& other);
  IwaSourceProdModeWithFileOp& operator=(
      const IwaSourceProdModeWithFileOp& other);

  ~IwaSourceProdModeWithFileOp();

  bool operator==(const IwaSourceProdModeWithFileOp&) const;

  const Variant& variant() const { return variant_; }

  base::Value ToDebugValue() const;

 private:
  Variant variant_;
};
std::ostream& operator<<(std::ostream& os,
                         const IwaSourceProdModeWithFileOp& source);

}  // namespace web_app

#endif  // CHROME_BROWSER_WEB_APPLICATIONS_ISOLATED_WEB_APPS_ISOLATED_WEB_APP_SOURCE_H_