liyujie
2025-08-28 d9927380ed7c8366f762049be9f3fee225860833
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
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
#include "lib/fidl/cpp/internal/stub_controller.h"
 
#include "lib/fidl/cpp/internal/logging.h"
#include "lib/fidl/cpp/internal/pending_response.h"
#include "lib/fidl/cpp/internal/weak_stub_controller.h"
 
namespace fidl {
namespace internal {
 
StubController::StubController() : weak_(nullptr), reader_(this) {}
 
StubController::~StubController() { InvalidateWeakIfNeeded(); }
 
zx_status_t StubController::Send(const fidl_type_t* type, Message message) {
  const char* error_msg = nullptr;
  zx_status_t status = message.Validate(type, &error_msg);
  if (status != ZX_OK) {
    FIDL_REPORT_ENCODING_ERROR(message, type, error_msg);
    return status;
  }
  return message.Write(reader_.channel().get(), 0);
}
 
zx_status_t StubController::OnMessage(Message message) {
  if (!message.has_header())
    return ZX_ERR_INVALID_ARGS;
  zx_txid_t txid = message.txid();
  WeakStubController* weak = nullptr;
  if (txid) {
    if (!weak_)
      weak_ = new WeakStubController(this);
    weak = weak_;
  }
  return stub_->Dispatch_(std::move(message), PendingResponse(txid, weak));
}
 
void StubController::OnChannelGone() { InvalidateWeakIfNeeded(); }
 
void StubController::InvalidateWeakIfNeeded() {
  if (!weak_)
    return;
  weak_->Invalidate();
  weak_->Release();
  weak_ = nullptr;
}
 
}  // namespace internal
}  // namespace fidl