File: shortcut.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (112 lines) | stat: -rw-r--r-- 3,764 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
// 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_SERVICES_APP_SERVICE_PUBLIC_CPP_SHORTCUT_SHORTCUT_H_
#define COMPONENTS_SERVICES_APP_SERVICE_PUBLIC_CPP_SHORTCUT_SHORTCUT_H_

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

#include "base/component_export.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/stack_allocated.h"
#include "base/types/strong_alias.h"
#include "components/services/app_service/public/cpp/icon_types.h"
#include "components/services/app_service/public/cpp/macros.h"

namespace apps {

using ShortcutId = base::StrongAlias<class ShortcutIdTag, std::string>;

// Where the shortcut was created from.
enum class ShortcutSource {
  kUnknown = 0,
  kUser = 1,    // Created by the user and managed by sync. This includes any
                // shortcuts created by syncing between devices.
  kPolicy = 2,  // Created by organization policy.
  kDefault = 3  // Created by default.
};

COMPONENT_EXPORT(SHORTCUT)
std::ostream& operator<<(std::ostream& os, ShortcutSource v);

struct COMPONENT_EXPORT(SHORTCUT) Shortcut {
  Shortcut(const std::string& host_app_id, const std::string& local_id);

  Shortcut(const Shortcut&) = delete;
  Shortcut& operator=(const Shortcut&) = delete;
  Shortcut(Shortcut&&) = delete;
  Shortcut& operator=(Shortcut&&) = delete;

  bool operator==(const Shortcut&) const;

  ~Shortcut();

  std::unique_ptr<Shortcut> Clone() const;

  // Example output:
  //
  // shortcut_id: 2
  // - shortcut_name: Launch
  // - source: Source::kUser
  // - host_app_id: app_1
  // - local_id: shortcut_1
  // - allow_removal: true
  std::string ToString() const;
  // Name of the shortcut.
  std::optional<std::string> name;
  // Shortcut creation source.
  ShortcutSource shortcut_source = ShortcutSource::kUnknown;

  // 'host_app_id' and 'local_id' should not be changeable after creation.
  // The host app of the shortcut.
  const std::string host_app_id;
  // The locally unique identifier for the shortcut within an app. This id would
  // be used to launch the shortcut or load shortcut icon from the app.
  const std::string local_id;

  // Represents the unique identifier for a shortcut. This identifier should be
  // unique within a profile, and stable across different user sessions.
  // 'shortcut_id' is generated from the hash of 'host_app_id' and 'local_id',
  // these value should not be updated separately.
  const ShortcutId shortcut_id;

  // Represents what icon should be loaded for this shortcut, icon key will
  // change if the icon has been updated from the publisher.
  std::optional<IconKey> icon_key;

  // Whether the shortcut publisher allows the shortcut to be removed by user.
  std::optional<bool> allow_removal;
};

// A view class to reduce the risk of lifetime issues by preventing
// long-term storage on the heap.
class COMPONENT_EXPORT(SHORTCUT) ShortcutView {
 public:
  explicit ShortcutView(const Shortcut* shortcut) : shortcut_(shortcut) {}
  const Shortcut* operator->() const { return shortcut_.get(); }
  explicit operator bool() const { return shortcut_; }

 private:
  const raw_ptr<const Shortcut> shortcut_;
  STACK_ALLOCATED();
};

using ShortcutPtr = std::unique_ptr<Shortcut>;
using Shortcuts = std::vector<ShortcutPtr>;

// Creates a deep copy of `source_shortcuts`.
COMPONENT_EXPORT(SHORTCUT)
Shortcuts CloneShortcuts(const Shortcuts& source_shortcuts);

COMPONENT_EXPORT(SHORTCUT)
ShortcutId GenerateShortcutId(const std::string& host_app_id,
                              const std::string& local_id);

}  // namespace apps

#endif  // COMPONENTS_SERVICES_APP_SERVICE_PUBLIC_CPP_SHORTCUT_SHORTCUT_H_