File: fake_mojo_service_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 (152 lines) | stat: -rw-r--r-- 5,832 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
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
// 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 "chromeos/ash/components/mojo_service_manager/fake_mojo_service_manager.h"

#include <utility>

#include "base/check.h"
#include "chromeos/ash/components/mojo_service_manager/connection.h"

namespace ash::mojo_service_manager {

// The security context of ash-chrome. This should be the default security
// context for the global connection.
constexpr char kAshSecurityContext[] = "u:r:cros_browser:s0";

namespace mojom = ::chromeos::mojo_service_manager::mojom;

FakeMojoServiceManager::ServiceState::ServiceState() = default;

FakeMojoServiceManager::ServiceState::~ServiceState() = default;

FakeMojoServiceManager::FakeMojoServiceManager() {
  SetServiceManagerRemoteForTesting(
      AddNewPipeAndPassRemote(kAshSecurityContext));
}

FakeMojoServiceManager::~FakeMojoServiceManager() {
  // Reset the connection before the fake service manager so the disconnect
  // handler won't be triggered.
  ResetServiceManagerConnection();
}

mojo::PendingRemote<mojom::ServiceManager>
FakeMojoServiceManager::AddNewPipeAndPassRemote(
    const std::string& security_context) {
  mojo::PendingRemote<mojom::ServiceManager> remote;
  receiver_set_.Add(this, remote.InitWithNewPipeAndPassReceiver(),
                    mojom::ProcessIdentity::New(security_context, 0, 0, 0));
  return remote;
}

void FakeMojoServiceManager::Register(
    const std::string& service_name,
    mojo::PendingRemote<mojom::ServiceProvider> service_provider) {
  auto it = service_map_.find(service_name);
  if (it == service_map_.end()) {
    auto [it_new, success] = service_map_.try_emplace(service_name);
    CHECK(success);
    it = it_new;
  }

  ServiceState& service_state = it->second;
  if (service_state.service_provider.is_bound()) {
    service_provider.ResetWithReason(
        static_cast<uint32_t>(mojom::ErrorCode::kServiceAlreadyRegistered),
        "The service: " + service_name + " has already been registered.");
    return;
  }
  service_state.service_provider.Bind(std::move(service_provider));
  service_state.service_provider.set_disconnect_handler(
      base::BindOnce(&FakeMojoServiceManager::ServiceProviderDisconnectHandler,
                     base::Unretained(this), service_name));

  const mojom::ProcessIdentityPtr& identity = receiver_set_.current_context();
  service_state.owner = identity.Clone();
  SendServiceEvent(mojom::ServiceEvent::New(
      mojom::ServiceEvent::Type::kRegistered, service_name, identity.Clone()));

  auto pending_requests = std::move(service_state.pending_requests);
  for (auto& [requester, receiver] : pending_requests) {
    // If a receiver become invalid before being posted, don't send it because
    // the mojo will complain about sending invalid handles and reset the
    // connection of service provider.
    if (!receiver.is_valid()) {
      continue;
    }
    service_state.service_provider->Request(std::move(requester),
                                            std::move(receiver));
  }
}

void FakeMojoServiceManager::Request(const std::string& service_name,
                                     std::optional<base::TimeDelta> /*timeout*/,
                                     mojo::ScopedMessagePipeHandle receiver) {
  auto it = service_map_.find(service_name);
  if (it == service_map_.end()) {
    auto [it_new, success] = service_map_.try_emplace(service_name);
    CHECK(success);
    it = it_new;
  }

  ServiceState& service_state = it->second;
  const mojom::ProcessIdentityPtr& identity = receiver_set_.current_context();
  if (service_state.service_provider.is_bound()) {
    service_state.service_provider->Request(identity.Clone(),
                                            std::move(receiver));
    return;
  }
  service_state.pending_requests.emplace_back(identity.Clone(),
                                              std::move(receiver));
}

void FakeMojoServiceManager::Query(const std::string& service_name,
                                   QueryCallback callback) {
  auto it = service_map_.find(service_name);
  if (it == service_map_.end()) {
    std::move(callback).Run(mojom::ErrorOrServiceState::NewError(
        mojom::Error::New(mojom::ErrorCode::kServiceNotFound,
                          "Cannot find service: " + service_name)));
    return;
  }

  const ServiceState& service_state = it->second;
  mojom::ServiceStatePtr state =
      service_state.service_provider.is_bound()
          ? mojom::ServiceState::NewRegisteredState(
                mojom::RegisteredServiceState::New(
                    /*owner=*/service_state.owner.Clone()))
          : mojom::ServiceState::NewUnregisteredState(
                mojom::UnregisteredServiceState::New());
  std::move(callback).Run(
      mojom::ErrorOrServiceState::NewState(std::move(state)));
}

void FakeMojoServiceManager::AddServiceObserver(
    mojo::PendingRemote<mojom::ServiceObserver> observer) {
  service_observers_.Add(std::move(observer));
}

void FakeMojoServiceManager::ServiceProviderDisconnectHandler(
    const std::string& service_name) {
  auto it = service_map_.find(service_name);
  CHECK(it != service_map_.end());
  ServiceState& service_state = it->second;
  service_state.service_provider.reset();
  mojom::ProcessIdentityPtr dispatcher;
  dispatcher.Swap(&service_state.owner);
  SendServiceEvent(
      mojom::ServiceEvent::New(mojom::ServiceEvent::Type::kUnRegistered,
                               service_name, std::move(dispatcher)));
}

void FakeMojoServiceManager::SendServiceEvent(mojom::ServiceEventPtr event) {
  for (const mojo::Remote<mojom::ServiceObserver>& remote :
       service_observers_) {
    remote->OnServiceEvent(event.Clone());
  }
}

}  // namespace ash::mojo_service_manager