File: shortcut_update.cc

package info (click to toggle)
chromium 139.0.7258.154-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,129,716 kB
  • sloc: cpp: 35,100,894; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,921; 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,152; 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 (158 lines) | stat: -rw-r--r-- 5,016 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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/services/app_service/public/cpp/shortcut/shortcut_update.h"
#include <type_traits>

#include "base/check.h"
#include "base/check_op.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "components/services/app_service/public/cpp/icon_types.h"
#include "components/services/app_service/public/cpp/macros.h"
#include "components/services/app_service/public/cpp/shortcut/shortcut.h"

namespace apps {

ShortcutUpdate::ShortcutUpdate(const Shortcut* state, const Shortcut* delta)
    : state_(state), delta_(delta) {
  CHECK(state_ || delta_);
  if (state_ && delta_) {
    CHECK_EQ(state_->shortcut_id, delta->shortcut_id);
    CHECK_EQ(state_->host_app_id, delta->host_app_id);
    CHECK_EQ(state_->local_id, delta->local_id);
  }
}

bool ShortcutUpdate::operator==(const ShortcutUpdate& rhs) const {
  bool states_are_same = false;
  bool deltas_are_same = false;
  if (!this->state_ && !rhs.state_) {
    states_are_same = true;
  }
  if (this->state_ && rhs.state_) {
    states_are_same = *(this->state_) == *(rhs.state_);
  }

  if (!this->delta_ && !rhs.delta_) {
    deltas_are_same = true;
  }
  if (this->delta_ && rhs.delta_) {
    deltas_are_same = *(this->delta_) == *(rhs.delta_);
  }
  return states_are_same && deltas_are_same;
}

void ShortcutUpdate::Merge(Shortcut* state, const Shortcut* delta) {
  CHECK(state);
  if (!delta) {
    return;
  }

  if (delta->shortcut_id != state->shortcut_id) {
    LOG(ERROR) << "inconsistent shortcut_id: " << delta->shortcut_id << " vs "
               << state->shortcut_id;
    return;
  }

  if (delta->host_app_id != state->host_app_id) {
    LOG(ERROR) << "inconsistent host_app_id: " << delta->host_app_id << " vs "
               << state->host_app_id;
    return;
  }

  if (delta->local_id != state->local_id) {
    LOG(ERROR) << "inconsistent local_id: " << delta->local_id << " vs "
               << state->local_id;
    return;
  }

  SET_OPTIONAL_VALUE(name);
  SET_ENUM_VALUE(shortcut_source, ShortcutSource::kUnknown);

  state->icon_key = MergeIconKey(
      state && state->icon_key.has_value() ? &state->icon_key.value() : nullptr,
      delta && delta->icon_key.has_value() ? &delta->icon_key.value()
                                           : nullptr);

  SET_OPTIONAL_VALUE(allow_removal);
  // When adding new fields to the Shortcut struct, this function should also
  // be updated.
}

const ShortcutId& ShortcutUpdate::ShortcutId() const {
  return delta_ ? delta_->shortcut_id : state_->shortcut_id;
}

const std::string& ShortcutUpdate::HostAppId() const {
  return delta_ ? delta_->host_app_id : state_->host_app_id;
}

const std::string& ShortcutUpdate::LocalId() const {
  return delta_ ? delta_->local_id : state_->local_id;
}

const std::string& ShortcutUpdate::Name() const {
  GET_VALUE_WITH_FALLBACK(name, base::EmptyString())
}

bool ShortcutUpdate::NameChanged() const {
  RETURN_OPTIONAL_VALUE_CHANGED(name);
}

ShortcutSource ShortcutUpdate::ShortcutSource() const {
  GET_VALUE_WITH_DEFAULT_VALUE(shortcut_source, ShortcutSource::kUnknown);
}

bool ShortcutUpdate::ShortcutSourceChanged() const {
  IS_VALUE_CHANGED_WITH_DEFAULT_VALUE(shortcut_source,
                                      ShortcutSource::kUnknown);
}

std::optional<apps::IconKey> ShortcutUpdate::IconKey() const {
  return MergeIconKey(
      state_ && state_->icon_key.has_value() ? &state_->icon_key.value()
                                             : nullptr,
      delta_ && delta_->icon_key.has_value() ? &delta_->icon_key.value()
                                             : nullptr);
}

bool ShortcutUpdate::IconKeyChanged() const {
  if (!delta_ || !delta_->icon_key.has_value()) {
    return false;
  }
  if (!state_ || !state_->icon_key.has_value()) {
    return true;
  }
  return MergeIconKey(&(state_->icon_key.value()),
                      &(delta_->icon_key.value())) != state_->icon_key;
}

std::optional<bool> ShortcutUpdate::AllowRemoval() const {
  GET_VALUE_WITH_FALLBACK(allow_removal, std::nullopt)
}

bool ShortcutUpdate::AllowRemovalChanged() const {
  RETURN_OPTIONAL_VALUE_CHANGED(allow_removal);
}

bool ShortcutUpdate::ShortcutInitialized() const {
  return !state_ && delta_;
}

// For logging and debug purposes.
COMPONENT_EXPORT(SHORTCUT)
std::ostream& operator<<(std::ostream& out,
                         const ShortcutUpdate& shortcut_update) {
  out << "ShortcutId: " << shortcut_update.ShortcutId() << std::endl;
  out << "HostAppId: " << shortcut_update.HostAppId() << std::endl;
  out << "LocalId: " << shortcut_update.LocalId() << std::endl;
  out << "Name: " << shortcut_update.Name() << std::endl;
  out << "ShortcutSource: " << shortcut_update.ShortcutSource() << std::endl;
  out << "AllowRemoval: " << PRINT_OPTIONAL_BOOL(shortcut_update.AllowRemoval())
      << std::endl;
  return out;
}

}  // namespace apps