xref: /aosp_15_r20/external/cronet/base/fuchsia/fidl_event_handler.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_FUCHSIA_FIDL_EVENT_HANDLER_H_
6 #define BASE_FUCHSIA_FIDL_EVENT_HANDLER_H_
7 
8 #include <lib/fidl/cpp/wire/client_base.h>
9 #include <lib/fidl/cpp/wire/status.h>
10 
11 #include <optional>
12 #include <string>
13 
14 #include "base/fuchsia/fuchsia_logging.h"
15 #include "base/functional/callback.h"
16 #include "base/location.h"
17 #include "base/strings/string_piece.h"
18 
19 namespace fidl {
20 template <typename Protocol>
21 class AsyncEventHandler;
22 }
23 
24 namespace base {
25 
26 // An implementation of `fidl::AsyncEventhandler` that simply LOGs an ERROR
27 // when `on_fidl_error` is called. The lifetime of an instance of this class
28 // needs to match the lifetime of the `fidl::Client` that it is used with.
29 template <typename Protocol>
30 class FidlErrorEventLogger : public fidl::AsyncEventHandler<Protocol> {
31  public:
32   explicit FidlErrorEventLogger(
33       std::string protocol_name = fidl::DiscoverableProtocolName<Protocol>)
protocol_name_(std::move (protocol_name))34       : protocol_name_(std::move(protocol_name)) {}
35 
on_fidl_error(fidl::UnbindInfo error)36   void on_fidl_error(fidl::UnbindInfo error) override {
37     LOG(ERROR) << protocol_name_ << " was disconnected with "
38                << error.status_string() << ".";
39   }
40 
41  private:
42   std::string protocol_name_;
43 };
44 
45 // An implementation of `fidl::AsyncEventhandler` that LOGs an ERROR and
46 // exits the process when `on_fidl_error` is called. The lifetime of an instance
47 // of this class needs to match the lifetime of the `fidl::Client` that it is
48 // used with.
49 template <typename Protocol>
50 class FidlErrorEventProcessExiter : public fidl::AsyncEventHandler<Protocol> {
51  public:
52   explicit FidlErrorEventProcessExiter(
53       std::string protocol_name = fidl::DiscoverableProtocolName<Protocol>)
protocol_name_(std::move (protocol_name))54       : protocol_name_(std::move(protocol_name)) {}
55 
on_fidl_error(fidl::UnbindInfo error)56   void on_fidl_error(fidl::UnbindInfo error) override {
57     base::LogFidlErrorAndExitProcess(FROM_HERE, protocol_name_)(error.status());
58   }
59 
60  private:
61   std::string protocol_name_;
62 };
63 
64 // An implementation of `fidl::AsyncEventHandler` that invokes the
65 // caller-supplied callback when `on_fidl_error` is called. The lifetime of an
66 // instance of this class needs to match the lifetime of the `fidl::Client` that
67 // it is used with.
68 template <typename Protocol>
69 class FidlErrorEventHandler : public fidl::AsyncEventHandler<Protocol> {
70  public:
71   using OnFidlErrorCallback = base::RepeatingCallback<void(fidl::UnbindInfo)>;
72 
73   FidlErrorEventHandler() = delete;
FidlErrorEventHandler(OnFidlErrorCallback on_fidl_error_callback)74   explicit FidlErrorEventHandler(OnFidlErrorCallback on_fidl_error_callback)
75       : on_fidl_error_callback_(std::move(on_fidl_error_callback)) {}
76 
on_fidl_error(fidl::UnbindInfo error)77   void on_fidl_error(fidl::UnbindInfo error) override {
78     on_fidl_error_callback_.Run(error);
79   }
80 
81  private:
82   OnFidlErrorCallback on_fidl_error_callback_;
83 };
84 
85 }  // namespace base
86 
87 #endif  // BASE_FUCHSIA_FIDL_EVENT_HANDLER_H_
88