File: fake_nearby_process_manager.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 (115 lines) | stat: -rw-r--r-- 4,229 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
// Copyright 2024 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/data_migration/testing/fake_nearby_process_manager.h"

#include "base/notimplemented.h"
#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/test/bind.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "chromeos/ash/components/data_migration/constants.h"
#include "chromeos/ash/services/nearby/public/mojom/nearby_decoder.mojom.h"
#include "chromeos/ash/services/nearby/public/mojom/nearby_presence.mojom.h"
#include "chromeos/ash/services/nearby/public/mojom/quick_start_decoder.mojom.h"
#include "mojo/public/cpp/bindings/pending_remote.h"

namespace data_migration {
namespace {

using NearbyConnectionsMojom = ::nearby::connections::mojom::NearbyConnections;

class FakeNearbyProcessReference
    : public ash::nearby::NearbyProcessManager::NearbyProcessReference {
 public:
  explicit FakeNearbyProcessReference(
      mojo::SharedRemote<NearbyConnectionsMojom> nearby_connections)
      : nearby_connections_(std::move(nearby_connections)) {}
  FakeNearbyProcessReference(const FakeNearbyProcessReference&) = delete;
  FakeNearbyProcessReference& operator=(const FakeNearbyProcessReference&) =
      delete;
  ~FakeNearbyProcessReference() override = default;

  const mojo::SharedRemote<NearbyConnectionsMojom>& GetNearbyConnections()
      const override {
    return nearby_connections_;
  }

  // None of the below are used for data migration.
  const mojo::SharedRemote<ash::nearby::presence::mojom::NearbyPresence>&
  GetNearbyPresence() const override {
    NOTIMPLEMENTED();
    return nearby_presence_;
  }

  const mojo::SharedRemote<::sharing::mojom::NearbySharingDecoder>&
  GetNearbySharingDecoder() const override {
    NOTIMPLEMENTED();
    return nearby_sharing_decoder_;
  }

  const mojo::SharedRemote<ash::quick_start::mojom::QuickStartDecoder>&
  GetQuickStartDecoder() const override {
    NOTIMPLEMENTED();
    return quick_start_decoder_;
  }

 private:
  const mojo::SharedRemote<NearbyConnectionsMojom> nearby_connections_;
  const mojo::SharedRemote<ash::nearby::presence::mojom::NearbyPresence>
      nearby_presence_;
  const mojo::SharedRemote<::sharing::mojom::NearbySharingDecoder>
      nearby_sharing_decoder_;
  const mojo::SharedRemote<ash::quick_start::mojom::QuickStartDecoder>
      quick_start_decoder_;
};

}  // namespace

FakeNearbyProcessManager::FakeNearbyProcessManager(
    std::string_view remote_endpoint_id)
    : fake_nearby_connections_(std::move(remote_endpoint_id)),
      receiver_(&fake_nearby_connections_) {
  InitializeProcess();
}

FakeNearbyProcessManager::~FakeNearbyProcessManager() {
  if (remote_.is_bound()) {
    // Flushes any pending NearbyConnections mojo calls that the test may have
    // made. Since the mojo call made below uses the same message pipe as that
    // used during tests and mojo guarantees the ordering of calls made on the
    // same message pipe, any pending mojo calls made during the test are
    // guaranteed to be completed before the test exits. Failure to do so can
    // result in memory leaks reported by LSAN; mojo leaks memory internally if
    // the request's message loop is destroyed before a mojo call completes.
    base::RunLoop run_loop;
    remote_->StopAllEndpoints(
        kServiceId,
        base::BindLambdaForTesting(
            [&run_loop](FakeNearbyConnections::Status) { run_loop.Quit(); }));
    run_loop.Run();
  }
}

std::unique_ptr<ash::nearby::NearbyProcessManager::NearbyProcessReference>
FakeNearbyProcessManager::GetNearbyProcessReference(
    NearbyProcessStoppedCallback on_process_stopped_callback) {
  if (!remote_.is_bound() || !receiver_.is_bound()) {
    InitializeProcess();
  }
  return std::make_unique<FakeNearbyProcessReference>(remote_);
}

void FakeNearbyProcessManager::ShutDownProcess() {
  receiver_.reset();
  remote_.reset();
}

void FakeNearbyProcessManager::InitializeProcess() {
  ShutDownProcess();
  remote_.Bind(receiver_.BindNewPipeAndPassRemote(),
               base::SequencedTaskRunner::GetCurrentDefault());
}

}  // namespace data_migration