File: annotations_manager.cc

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 (174 lines) | stat: -rw-r--r-- 5,348 bytes parent folder | download | duplicates (9)
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
// 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.

#include "components/fuchsia_component_support/annotations_manager.h"

#include <lib/fpromise/promise.h>

#include <set>
#include <string>
#include <string_view>
#include <tuple>
#include <utility>

#include "base/check.h"
#include "base/fuchsia/fuchsia_logging.h"
#include "base/fuchsia/mem_buffer_util.h"
#include "base/logging.h"
#include "base/memory/raw_ref.h"
#include "base/strings/string_number_conversions.h"

namespace fuchsia_component_support {

namespace {
using AnnotationKeySet =
    std::set<fuchsia::element::AnnotationKey, AnnotationKeyCompare>;
}  // namespace

fuchsia::element::Annotation MakeAnnotation(std::string_view key,
                                            std::string_view value) {
  fuchsia::element::Annotation result;
  result.key.namespace_ = "global";
  result.key.value = std::string(key);

  constexpr size_t kArbitraryTextBufferThreshold = 128;
  if (value.size() <= kArbitraryTextBufferThreshold) {
    result.value.set_text(std::string(value));
  } else {
    result.value.set_buffer(base::MemBufferFromString(value, /*name=*/key));
  }

  return result;
}

fuchsia::element::Annotation MakeBoolAnnotation(std::string_view key,
                                                bool value) {
  static constexpr std::string_view kTrue("true");
  static constexpr std::string_view kFalse("false");
  return MakeAnnotation(key, value ? kTrue : kFalse);
}

fuchsia::element::Annotation MakeIntAnnotation(std::string_view key,
                                               int value) {
  return MakeAnnotation(key, base::NumberToString(value));
}

bool AnnotationKeyCompare::operator()(
    const fuchsia::element::AnnotationKey& key1,
    const fuchsia::element::AnnotationKey& key2) const {
  return std::tie(key1.namespace_, key1.value) <
         std::tie(key2.namespace_, key2.value);
}

class AnnotationsManager::ControllerImpl
    : public fuchsia::element::AnnotationController {
 public:
  explicit ControllerImpl(AnnotationsManager& manager) : manager_(manager) {}
  ~ControllerImpl() override = default;

  // fuchsia::element::AnnotationController implementation.
  void UpdateAnnotations(
      std::vector<fuchsia::element::Annotation> annotations_to_set,
      std::vector<fuchsia::element::AnnotationKey> annotations_to_delete,
      UpdateAnnotationsCallback callback) override {
    DVLOG(2) << __func__;
    if (manager_->UpdateAnnotations(std::move(annotations_to_set),
                                    std::move(annotations_to_delete))) {
      callback(fpromise::ok());
    } else {
      callback(fpromise::error(
          fuchsia::element::UpdateAnnotationsError::INVALID_ARGS));
    }
  }
  void GetAnnotations(GetAnnotationsCallback callback) override {
    DVLOG(2) << __func__;
    callback(fpromise::ok(manager_->GetAnnotations()));
  }
  void WatchAnnotations(WatchAnnotationsCallback callback) override {
    DVLOG(2) << __func__;

    if (have_changes_) {
      have_changes_ = false;
      callback(fpromise::ok(manager_->GetAnnotations()));
      return;
    }

    if (on_annotations_changed_) {
      manager_->bindings_.CloseBinding(this, ZX_ERR_BAD_STATE);
      return;
    }

    on_annotations_changed_ = std::move(callback);
  }

  void OnAnnotationsChanged() {
    have_changes_ = true;
    if (on_annotations_changed_) {
      WatchAnnotations(std::exchange(on_annotations_changed_, {}));
    }
  }

 private:
  const raw_ref<AnnotationsManager> manager_;

  // Initially true so that the first call to WatchAnnotations() will
  // return results immediately.
  bool have_changes_ = true;

  WatchAnnotationsCallback on_annotations_changed_;
};

AnnotationsManager::AnnotationsManager() = default;

AnnotationsManager::~AnnotationsManager() = default;

bool AnnotationsManager::UpdateAnnotations(
    std::vector<fuchsia::element::Annotation> to_set,
    std::vector<fuchsia::element::AnnotationKey> to_delete) {
  AnnotationKeySet changed;
  for (const auto& key : to_delete) {
    annotations_.erase(key);
    auto [it, inserted] = changed.insert(key);
    if (!inserted) {
      return false;
    }
  }
  for (auto& annotation : to_set) {
    annotations_.insert_or_assign(
        fuchsia::element::AnnotationKey(annotation.key),
        std::move(annotation.value));
    auto [it, inserted] = changed.insert(annotation.key);
    if (!inserted) {
      return false;
    }
  }

  if (!changed.empty()) {
    for (auto& binding : bindings_.bindings()) {
      binding->impl()->OnAnnotationsChanged();
    }
  }

  return true;
}

std::vector<fuchsia::element::Annotation> AnnotationsManager::GetAnnotations()
    const {
  std::vector<fuchsia::element::Annotation> result;
  result.reserve(annotations_.size());
  for (const auto& [key, value] : annotations_) {
    result.push_back(fuchsia::element::Annotation{.key = key});
    zx_status_t status = value.Clone(&result.back().value);
    ZX_CHECK(status == ZX_OK, status);
  }
  return result;
}

void AnnotationsManager::Connect(
    fidl::InterfaceRequest<fuchsia::element::AnnotationController> request) {
  bindings_.AddBinding(std::make_unique<ControllerImpl>(*this),
                       std::move(request));
}

}  // namespace fuchsia_component_support