ime_decoder.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_SERVICES_IME_IME_DECODER_H_
  5. #define ASH_SERVICES_IME_IME_DECODER_H_
  6. #include "ash/services/ime/public/cpp/shared_lib/interfaces.h"
  7. #include "base/no_destructor.h"
  8. #include "base/scoped_native_library.h"
  9. #include "third_party/abseil-cpp/absl/types/optional.h"
  10. namespace ash {
  11. namespace ime {
  12. // START: Signatures of "C" API entry points of CrOS 1P IME shared library.
  13. // Must match API specs in ash/services/ime/public/cpp/shared_lib/interfaces.h
  14. inline constexpr char kSetImeEngineLoggerFnName[] = "SetImeEngineLogger";
  15. typedef void (*SetImeEngineLoggerFn)(ChromeLoggerFunc logger_func);
  16. inline constexpr char kInitProtoModeFnName[] = "InitProtoMode";
  17. typedef void (*InitProtoModeFn)(ImeCrosPlatform* platform);
  18. inline constexpr char kCloseProtoModeFnName[] = "CloseProtoMode";
  19. typedef void (*CloseProtoModeFn)();
  20. inline constexpr char kImeDecoderSupportsFnName[] = "ImeDecoderSupports";
  21. typedef bool (*ImeDecoderSupportsFn)(const char* ime_spec);
  22. inline constexpr char kImeDecoderActivateImeFnName[] = "ImeDecoderActivateIme";
  23. typedef bool (*ImeDecoderActivateImeFn)(const char* ime_spec,
  24. ImeClientDelegate* delegate);
  25. inline constexpr char kImeDecoderProcessFnName[] = "ImeDecoderProcess";
  26. typedef void (*ImeDecoderProcessFn)(const uint8_t* data, size_t size);
  27. inline constexpr char kInitMojoModeFnName[] = "InitMojoMode";
  28. typedef void (*InitMojoModeFn)(ImeCrosPlatform* platform);
  29. inline constexpr char kCloseMojoModeFnName[] = "CloseMojoMode";
  30. typedef void (*CloseMojoModeFn)();
  31. inline constexpr char kConnectToInputMethodFnName[] = "ConnectToInputMethod";
  32. typedef bool (*ConnectToInputMethodFn)(
  33. const char* ime_spec,
  34. uint32_t receiver_input_method_handle,
  35. uint32_t remote_input_method_host_handle,
  36. uint32_t remote_input_method_host_version);
  37. inline constexpr char kInitializeConnectionFactoryFnName[] =
  38. "InitializeConnectionFactory";
  39. typedef bool (*InitializeConnectionFactoryFn)(
  40. uint32_t receiver_connection_factory_handle);
  41. inline constexpr char kIsInputMethodConnectedFnName[] =
  42. "IsInputMethodConnected";
  43. typedef bool (*IsInputMethodConnectedFn)();
  44. // END: Signatures of "C" API entry points of CrOS 1P IME shared lib.
  45. // TODO(b/214153032): Rename to ImeSharedLib to better reflect what this
  46. // represents. This class manages the dynamic loading of CrOS 1P IME shared lib
  47. // .so, and facilitates access to its "C" API entry points.
  48. class ImeDecoder {
  49. public:
  50. virtual ~ImeDecoder() = default;
  51. // Function pointers to "C" API entry points of the loaded IME shared library.
  52. // See ash/services/ime/public/cpp/shared_lib/interfaces.h for API specs.
  53. struct EntryPoints {
  54. InitProtoModeFn init_proto_mode;
  55. CloseProtoModeFn close_proto_mode;
  56. // TODO(b/214153032): Prefix the following with "proto_mode_" to better
  57. // indicate they only pertain to the IME shared lib's ProtoMode. While it's
  58. // "hard" to rename corresponding "C" API functions due to cross-repo
  59. // backward compat requirements, these are local and rename is feasible.
  60. ImeDecoderSupportsFn supports;
  61. ImeDecoderActivateImeFn activate_ime;
  62. ImeDecoderProcessFn process;
  63. InitMojoModeFn init_mojo_mode;
  64. CloseMojoModeFn close_mojo_mode;
  65. // TODO(b/214153032): Prefix the following with "mojo_mode_" to better
  66. // indicate they only pertain to the IME shared lib's MojoMode. While it's
  67. // "hard" to rename corresponding "C" API functions due to cross-repo
  68. // backward compat requirements, these are local and rename is feasible.
  69. ConnectToInputMethodFn connect_to_input_method;
  70. InitializeConnectionFactoryFn initialize_connection_factory;
  71. IsInputMethodConnectedFn is_input_method_connected;
  72. };
  73. // Loads the IME shared library (if not already loaded) then returns its entry
  74. // points. Entry points are only available if the IME shared library has been
  75. // successfully loaded.
  76. virtual absl::optional<EntryPoints> MaybeLoadThenReturnEntryPoints() = 0;
  77. };
  78. // A proxy class for the IME decoder.
  79. // ImeDecoder is implemented as a singleton and is initialized before 'ime'
  80. // sandbox is engaged.
  81. // TODO(b/214153032): Rename to ImeSharedLibImpl, as soon as ImeDecoder is
  82. // renamed to ImeSharedLib, to better reflect what this represents.
  83. class ImeDecoderImpl : public ImeDecoder {
  84. public:
  85. // Gets the singleton ImeDecoderImpl.
  86. static ImeDecoderImpl* GetInstance();
  87. ImeDecoderImpl(const ImeDecoderImpl&) = delete;
  88. ImeDecoderImpl& operator=(const ImeDecoderImpl&) = delete;
  89. absl::optional<EntryPoints> MaybeLoadThenReturnEntryPoints() override;
  90. private:
  91. friend class base::NoDestructor<ImeDecoderImpl>;
  92. explicit ImeDecoderImpl();
  93. ~ImeDecoderImpl() override;
  94. // Result of IME decoder DSO initialization.
  95. absl::optional<base::ScopedNativeLibrary> library_;
  96. absl::optional<EntryPoints> entry_points_;
  97. };
  98. } // namespace ime
  99. } // namespace ash
  100. #endif // ASH_SERVICES_IME_IME_DECODER_H_