• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 namespace fidl {
19 template <typename Protocol>
20 class AsyncEventHandler;
21 }
22 
23 namespace base {
24 
25 // An implementation of `fidl::AsyncEventhandler` that simply LOGs an ERROR
26 // when `on_fidl_error` is called. The lifetime of an instance of this class
27 // needs to match the lifetime of the `fidl::Client` that it is used with.
28 template <typename Protocol>
29 class FidlErrorEventLogger : public fidl::AsyncEventHandler<Protocol> {
30  public:
31   explicit FidlErrorEventLogger(
32       std::string protocol_name = fidl::DiscoverableProtocolName<Protocol>)
protocol_name_(std::move (protocol_name))33       : protocol_name_(std::move(protocol_name)) {}
34 
on_fidl_error(fidl::UnbindInfo error)35   void on_fidl_error(fidl::UnbindInfo error) override {
36     LOG(ERROR) << protocol_name_ << " was disconnected with "
37                << error.status_string() << ".";
38   }
39 
40  private:
41   std::string protocol_name_;
42 };
43 
44 // An implementation of `fidl::AsyncEventhandler` that LOGs an ERROR and
45 // exits the process when `on_fidl_error` is called. The lifetime of an instance
46 // of this class needs to match the lifetime of the `fidl::Client` that it is
47 // used with.
48 template <typename Protocol>
49 class FidlErrorEventProcessExiter : public fidl::AsyncEventHandler<Protocol> {
50  public:
51   explicit FidlErrorEventProcessExiter(
52       std::string protocol_name = fidl::DiscoverableProtocolName<Protocol>)
protocol_name_(std::move (protocol_name))53       : protocol_name_(std::move(protocol_name)) {}
54 
on_fidl_error(fidl::UnbindInfo error)55   void on_fidl_error(fidl::UnbindInfo error) override {
56     base::LogFidlErrorAndExitProcess(FROM_HERE, protocol_name_)(error.status());
57   }
58 
59  private:
60   std::string protocol_name_;
61 };
62 
63 // An implementation of `fidl::AsyncEventHandler` that invokes the
64 // caller-supplied callback when `on_fidl_error` is called. The lifetime of an
65 // instance of this class needs to match the lifetime of the `fidl::Client` that
66 // it is used with.
67 template <typename Protocol>
68 class FidlErrorEventHandler : public fidl::AsyncEventHandler<Protocol> {
69  public:
70   using OnFidlErrorCallback = base::RepeatingCallback<void(fidl::UnbindInfo)>;
71 
72   FidlErrorEventHandler() = delete;
FidlErrorEventHandler(OnFidlErrorCallback on_fidl_error_callback)73   explicit FidlErrorEventHandler(OnFidlErrorCallback on_fidl_error_callback)
74       : on_fidl_error_callback_(std::move(on_fidl_error_callback)) {}
75 
on_fidl_error(fidl::UnbindInfo error)76   void on_fidl_error(fidl::UnbindInfo error) override {
77     on_fidl_error_callback_.Run(error);
78   }
79 
80  private:
81   OnFidlErrorCallback on_fidl_error_callback_;
82 };
83 
84 }  // namespace base
85 
86 #endif  // BASE_FUCHSIA_FIDL_EVENT_HANDLER_H_
87