File: fake_mojo_service_manager.h

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 (97 lines) | stat: -rw-r--r-- 4,187 bytes parent folder | download | duplicates (8)
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
// 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 CHROMEOS_ASH_COMPONENTS_MOJO_SERVICE_MANAGER_FAKE_MOJO_SERVICE_MANAGER_H_
#define CHROMEOS_ASH_COMPONENTS_MOJO_SERVICE_MANAGER_FAKE_MOJO_SERVICE_MANAGER_H_

#include <string>
#include <vector>

#include "base/component_export.h"
#include "chromeos/ash/components/mojo_service_manager/mojom/mojo_service_manager.mojom.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/remote_set.h"

namespace ash::mojo_service_manager {

// Provides fake implementation of the service manager for testing. It also
// overrides the global service manager connection and reset it when destructed.
//
// The behaviors are different from the real service:
// * No permission checking. A fake identity can be set when binding the mojo
//   remote. It will be used as the identity of owner or requester.
// * Register always succeeds, except that the services has already been
//   registered.
// * Request always succeeds. Timeout is ignored (always wait forever).
// * Query returns "not found" if the service is not yet registered / requested,
//   otherwise returns the state of the service.
// * The ServiceObserver can receive all the event (no permission checking).
//
class COMPONENT_EXPORT(CHROMEOS_MOJO_SERVICE_MANAGER) FakeMojoServiceManager
    : public chromeos::mojo_service_manager::mojom::ServiceManager {
 public:
  FakeMojoServiceManager();
  FakeMojoServiceManager(FakeMojoServiceManager&) = delete;
  FakeMojoServiceManager& operator=(FakeMojoServiceManager&) = delete;
  ~FakeMojoServiceManager() override;

  // Adds a new pipe and pass the pending remote. The identity of remote will
  // be bound to |security_context|.
  mojo::PendingRemote<chromeos::mojo_service_manager::mojom::ServiceManager>
  AddNewPipeAndPassRemote(const std::string& security_context);

 private:
  // Keeps all the objects related to a mojo service.
  struct ServiceState {
    ServiceState();
    ~ServiceState();

    // The pending requests to be sent after the service is available.
    std::vector<
        std::pair<chromeos::mojo_service_manager::mojom::ProcessIdentityPtr,
                  mojo::ScopedMessagePipeHandle>>
        pending_requests;
    // The owner of the service.
    chromeos::mojo_service_manager::mojom::ProcessIdentityPtr owner;
    // The mojo remote to the service provider.
    mojo::Remote<chromeos::mojo_service_manager::mojom::ServiceProvider>
        service_provider;
  };

  // chromeos::mojo_service_manager::mojom::ServiceManager overrides.
  void Register(const std::string& service_name,
                mojo::PendingRemote<
                    chromeos::mojo_service_manager::mojom::ServiceProvider>
                    service_provider) override;
  void Request(const std::string& service_name,
               std::optional<base::TimeDelta> timeout,
               mojo::ScopedMessagePipeHandle receiver) override;
  void Query(const std::string& service_name, QueryCallback callback) override;
  void AddServiceObserver(
      mojo::PendingRemote<
          chromeos::mojo_service_manager::mojom::ServiceObserver> observer)
      override;

  // Handles disconnection from service providers.
  void ServiceProviderDisconnectHandler(const std::string& service_name);

  // Sends service event to all the observers.
  void SendServiceEvent(
      chromeos::mojo_service_manager::mojom::ServiceEventPtr event);

  // The receiver set to provide the fake service manager.
  mojo::ReceiverSet<chromeos::mojo_service_manager::mojom::ServiceManager,
                    chromeos::mojo_service_manager::mojom::ProcessIdentityPtr>
      receiver_set_;
  // The map of the service name to the service state.
  std::map<std::string, ServiceState> service_map_;
  // The remote set for the service observer.
  mojo::RemoteSet<chromeos::mojo_service_manager::mojom::ServiceObserver>
      service_observers_;
};

}  // namespace ash::mojo_service_manager

#endif  // CHROMEOS_ASH_COMPONENTS_MOJO_SERVICE_MANAGER_FAKE_MOJO_SERVICE_MANAGER_H_