File: associated_interface_provider.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (118 lines) | stat: -rw-r--r-- 4,001 bytes parent folder | download | duplicates (7)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"

#include <map>

#include "base/containers/contains.h"
#include "base/no_destructor.h"
#include "base/task/single_thread_task_runner.h"
#include "mojo/public/cpp/bindings/associated_receiver.h"

namespace blink {

class AssociatedInterfaceProvider::LocalProvider
    : public mojom::AssociatedInterfaceProvider {
 public:
  using Binder =
      base::RepeatingCallback<void(mojo::ScopedInterfaceEndpointHandle)>;

  explicit LocalProvider(
      scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
    associated_interface_provider_receiver_.Bind(
        remote_.BindNewEndpointAndPassDedicatedReceiver(),
        std::move(task_runner));
  }
  LocalProvider(const LocalProvider&) = delete;
  LocalProvider& operator=(const LocalProvider&) = delete;

  ~LocalProvider() override = default;

  void SetBinderForName(const std::string& name, const Binder& binder) {
    binders_[name] = binder;
  }

  void ResetBinderForName(const std::string& name) { binders_.erase(name); }

  bool HasInterface(const std::string& name) const {
    return base::Contains(binders_, name);
  }

  void GetInterface(const std::string& name,
                    mojo::ScopedInterfaceEndpointHandle handle) {
    return remote_->GetAssociatedInterface(
        name, mojo::PendingAssociatedReceiver<mojom::AssociatedInterface>(
                  std::move(handle)));
  }

 private:
  // mojom::AssociatedInterfaceProvider:
  void GetAssociatedInterface(
      const std::string& name,
      mojo::PendingAssociatedReceiver<mojom::AssociatedInterface> receiver)
      override {
    auto it = binders_.find(name);
    if (it != binders_.end()) {
      it->second.Run(receiver.PassHandle());
    }
  }

  std::map<std::string, Binder> binders_;
  mojo::AssociatedReceiver<mojom::AssociatedInterfaceProvider>
      associated_interface_provider_receiver_{this};
  mojo::AssociatedRemote<mojom::AssociatedInterfaceProvider> remote_;
};

AssociatedInterfaceProvider::AssociatedInterfaceProvider(
    mojo::PendingAssociatedRemote<mojom::AssociatedInterfaceProvider> proxy,
    scoped_refptr<base::SingleThreadTaskRunner> task_runner)
    : proxy_(std::move(proxy), task_runner),
      task_runner_(std::move(task_runner)) {
  DCHECK(proxy_.is_bound());
}

AssociatedInterfaceProvider::AssociatedInterfaceProvider(
    scoped_refptr<base::SingleThreadTaskRunner> task_runner)
    : local_provider_(std::make_unique<LocalProvider>(task_runner)),
      task_runner_(std::move(task_runner)) {}

AssociatedInterfaceProvider::~AssociatedInterfaceProvider() = default;

void AssociatedInterfaceProvider::GetInterface(
    const std::string& name,
    mojo::ScopedInterfaceEndpointHandle handle) {
  if (local_provider_ && (local_provider_->HasInterface(name) || !proxy_)) {
    local_provider_->GetInterface(name, std::move(handle));
    return;
  }
  DCHECK(proxy_);
  proxy_->GetAssociatedInterface(
      name, mojo::PendingAssociatedReceiver<mojom::AssociatedInterface>(
                std::move(handle)));
}

void AssociatedInterfaceProvider::OverrideBinderForTesting(
    const std::string& name,
    const base::RepeatingCallback<void(mojo::ScopedInterfaceEndpointHandle)>&
        binder) {
  if (binder) {
    if (!local_provider_) {
      local_provider_ = std::make_unique<LocalProvider>(task_runner_);
    }
    local_provider_->SetBinderForName(name, binder);
  } else if (local_provider_) {
    local_provider_->ResetBinderForName(name);
  }
}

AssociatedInterfaceProvider*
AssociatedInterfaceProvider::GetEmptyAssociatedInterfaceProvider() {
  static base::NoDestructor<AssociatedInterfaceProvider>
      associated_interface_provider(
          base::SingleThreadTaskRunner::GetCurrentDefault());
  return associated_interface_provider.get();
}

}  // namespace blink