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
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233

ash / components / arc / session / arc_bridge_host_impl.h [blame]

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_COMPONENTS_ARC_SESSION_ARC_BRIDGE_HOST_IMPL_H_
#define ASH_COMPONENTS_ARC_SESSION_ARC_BRIDGE_HOST_IMPL_H_

#include <memory>
#include <vector>

#include "ash/components/arc/mojom/arc_bridge.mojom.h"
#include "ash/components/arc/session/arc_bridge_service.h"
#include "ash/components/arc/session/connection_holder.h"
#include "base/memory/raw_ptr.h"
#include "base/threading/thread_checker.h"
#include "chromeos/ash/components/mojo_service_manager/connection.h"
#include "chromeos/ash/components/mojo_service_manager/mojom/mojo_service_manager.mojom.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote.h"

namespace arc {

class ArcBridgeService;
class MojoChannelBase;

// Implementation of the ArcBridgeHost.
// The ArcBridgeHost also registers in Mojo Service Manager by default.
// The lifetime of ArcBridgeHost mojo channel is tied to this instance.
// Also, any ARC related Mojo channel will be closed if ArcBridgeHost Mojo
// channel is closed on error.
// When ARC Instance (not Host) Mojo channel gets ready (= passed via
// OnFooInstanceReady(), and the QueryVersion() gets completed), then this sets
// the raw pointer to the ArcBridgeService so that other services can access
// to the pointer, and resets it on channel closing.
// Note that ArcBridgeService must be alive while ArcBridgeHostImpl is alive.
class ArcBridgeHostImpl
    : public mojom::ArcBridgeHost,
      public chromeos::mojo_service_manager::mojom::ServiceProvider {
 public:
  explicit ArcBridgeHostImpl(ArcBridgeService* arc_bridge_service);

  ArcBridgeHostImpl(const ArcBridgeHostImpl&) = delete;
  ArcBridgeHostImpl& operator=(const ArcBridgeHostImpl&) = delete;

  ~ArcBridgeHostImpl() override;

  void AddReceiver(
      mojo::PendingReceiver<mojom::ArcBridgeHost> pending_receiver);

  // ArcBridgeHost overrides.
  void OnAccessibilityHelperInstanceReady(
      mojo::PendingRemote<ax::android::mojom::AccessibilityHelperInstance>
          accessibility_helper_remote) override;
  void OnAdbdMonitorInstanceReady(
      mojo::PendingRemote<mojom::AdbdMonitorInstance> adbd_monitor_remote)
      override;
  void OnAppInstanceReady(
      mojo::PendingRemote<mojom::AppInstance> app_ptr) override;
  void OnAppPermissionsInstanceReady(
      mojo::PendingRemote<mojom::AppPermissionsInstance> app_permissions_remote)
      override;
  void OnAppfuseInstanceReady(
      mojo::PendingRemote<mojom::AppfuseInstance> appfuse_remote) override;
  void OnArcWifiInstanceReady(
      mojo::PendingRemote<mojom::ArcWifiInstance> arc_wifi_remote) override;
  void OnAudioInstanceReady(
      mojo::PendingRemote<mojom::AudioInstance> audio_remote) override;
  void OnAuthInstanceReady(
      mojo::PendingRemote<mojom::AuthInstance> auth_remote) override;
  void OnBackupSettingsInstanceReady(
      mojo::PendingRemote<mojom::BackupSettingsInstance> backup_settings_remote)
      override;
  void OnBluetoothInstanceReady(
      mojo::PendingRemote<mojom::BluetoothInstance> bluetooth_remote) override;
  void OnBootPhaseMonitorInstanceReady(
      mojo::PendingRemote<mojom::BootPhaseMonitorInstance>
          boot_phase_monitor_remote) override;
  void OnCameraInstanceReady(
      mojo::PendingRemote<mojom::CameraInstance> camera_remote) override;
  void OnChromeFeatureFlagsInstanceReady(
      mojo::PendingRemote<mojom::ChromeFeatureFlagsInstance>
          chrome_feature_flags_remote) override;
  void OnCompatibilityModeInstanceReady(
      mojo::PendingRemote<mojom::CompatibilityModeInstance>
          compatibility_mode_remote) override;
  void OnCrashCollectorInstanceReady(
      mojo::PendingRemote<mojom::CrashCollectorInstance> crash_collector_remote)
      override;
  void OnArcShellExecutionInstanceReady(
      mojo::PendingRemote<mojom::ArcShellExecutionInstance>
          arc_shell_execution_remote) override;
  void OnDigitalGoodsInstanceReady(
      mojo::PendingRemote<mojom::DigitalGoodsInstance> digital_goods_remote)
      override;
  void OnDiskSpaceInstanceReady(
      mojo::PendingRemote<mojom::DiskSpaceInstance> disk_space_remote) override;
  void OnEnterpriseReportingInstanceReady(
      mojo::PendingRemote<mojom::EnterpriseReportingInstance>
          enterprise_reporting_remote) override;
  void OnErrorNotificationInstanceReady(
      mojo::PendingRemote<mojom::ErrorNotificationInstance> error_dialog_remote)
      override;
  void OnFileSystemInstanceReady(mojo::PendingRemote<mojom::FileSystemInstance>
                                     file_system_remote) override;
  void OnIioSensorInstanceReady(
      mojo::PendingRemote<mojom::IioSensorInstance> iio_sensor_remote) override;
  void OnImeInstanceReady(
      mojo::PendingRemote<mojom::ImeInstance> ime_remote) override;
  void OnInputMethodManagerInstanceReady(
      mojo::PendingRemote<mojom::InputMethodManagerInstance>
          input_method_manager_remote) override;
  void OnIntentHelperInstanceReady(
      mojo::PendingRemote<mojom::IntentHelperInstance> intent_helper_remote)
      override;
  void OnKeymasterInstanceReady(
      mojo::PendingRemote<mojom::KeymasterInstance> keymaster_remote) override;
  void OnKeyMintInstanceReady(
      mojo::PendingRemote<mojom::keymint::KeyMintInstance> keymint_remote)
      override;
  void OnMediaSessionInstanceReady(
      mojo::PendingRemote<mojom::MediaSessionInstance> media_session_remote)
      override;
  void OnMemoryInstanceReady(
      mojo::PendingRemote<mojom::MemoryInstance> memory_remote) override;
  void OnMetricsInstanceReady(
      mojo::PendingRemote<mojom::MetricsInstance> metrics_remote) override;
  void OnMidisInstanceReady(
      mojo::PendingRemote<mojom::MidisInstance> midis_remote) override;
  void OnNearbyShareInstanceReady(
      mojo::PendingRemote<mojom::NearbyShareInstance> nearby_share_remote)
      override;
  void OnNetInstanceReady(
      mojo::PendingRemote<mojom::NetInstance> net_remote) override;
  void OnNotificationsInstanceReady(
      mojo::PendingRemote<mojom::NotificationsInstance> notifications_remote)
      override;
  void OnObbMounterInstanceReady(mojo::PendingRemote<mojom::ObbMounterInstance>
                                     obb_mounter_remote) override;
  void OnOemCryptoInstanceReady(
      mojo::PendingRemote<mojom::OemCryptoInstance> oemcrypto_remote) override;
  void OnOnDeviceSafetyInstanceReady(
      mojo::PendingRemote<mojom::OnDeviceSafetyInstance>
          on_device_safety_remote) override;
  void OnPaymentAppInstanceReady(
      mojo::PendingRemote<chromeos::payments::mojom::PaymentAppInstance>
          payment_app_remote) override;
  void OnPipInstanceReady(
      mojo::PendingRemote<mojom::PipInstance> policy_remote) override;
  void OnPolicyInstanceReady(
      mojo::PendingRemote<mojom::PolicyInstance> policy_remote) override;
  void OnPowerInstanceReady(
      mojo::PendingRemote<mojom::PowerInstance> power_remote) override;
  void OnPrintSpoolerInstanceReady(
      mojo::PendingRemote<mojom::PrintSpoolerInstance> print_spooler_remote)
      override;
  void OnPrivacyItemsInstanceReady(
      mojo::PendingRemote<mojom::PrivacyItemsInstance> privacy_items_remote)
      override;
  void OnProcessInstanceReady(
      mojo::PendingRemote<mojom::ProcessInstance> process_remote) override;
  void OnScreenCaptureInstanceReady(
      mojo::PendingRemote<mojom::ScreenCaptureInstance> screen_capture_remote)
      override;
  void OnSharesheetInstanceReady(mojo::PendingRemote<mojom::SharesheetInstance>
                                     sharesheet_remote) override;
  void OnSystemStateInstanceReady(
      mojo::PendingRemote<mojom::SystemStateInstance> system_state_remote)
      override;
  void OnSystemUiInstanceReady(
      mojo::PendingRemote<mojom::SystemUiInstance> system_ui_remote) override;
  void OnTimerInstanceReady(
      mojo::PendingRemote<mojom::TimerInstance> timer_remote) override;
  void OnTracingInstanceReady(
      mojo::PendingRemote<mojom::TracingInstance> trace_remote) override;
  void OnTtsInstanceReady(
      mojo::PendingRemote<mojom::TtsInstance> tts_remote) override;
  void OnUsbHostInstanceReady(
      mojo::PendingRemote<mojom::UsbHostInstance> usb_host_remote) override;
  void OnVideoInstanceReady(
      mojo::PendingRemote<mojom::VideoInstance> video_remote) override;
  void OnVolumeMounterInstanceReady(
      mojo::PendingRemote<mojom::VolumeMounterInstance> volume_mounter_remote)
      override;
  void OnWakeLockInstanceReady(
      mojo::PendingRemote<mojom::WakeLockInstance> wake_lock_remote) override;
  void OnWallpaperInstanceReady(
      mojo::PendingRemote<mojom::WallpaperInstance> wallpaper_remote) override;
  void OnWebApkInstanceReady(
      mojo::PendingRemote<mojom::WebApkInstance> webapk_remote) override;

  size_t GetNumMojoChannelsForTesting() const;

 private:
  // chromeos::mojo_service_manager::mojom::ServiceProvider overrides.
  void Request(
      chromeos::mojo_service_manager::mojom::ProcessIdentityPtr identity,
      mojo::ScopedMessagePipeHandle receiver) override;

  // Called when the bridge channel is closed. This typically only happens when
  // the ARC instance crashes.
  void OnClosed();

  // The common implementation to handle ArcBridgeHost overrides.
  // |T| is a ARC Mojo Instance type.
  template <typename InstanceType, typename HostType>
  void OnInstanceReady(ConnectionHolder<InstanceType, HostType>* holder,
                       mojo::PendingRemote<InstanceType> remote);

  // Called if one of the established channels is closed.
  void OnChannelClosed(MojoChannelBase* channel);

  THREAD_CHECKER(thread_checker_);

  // Owned by ArcServiceManager.
  const raw_ptr<ArcBridgeService> arc_bridge_service_;

  // The mojo receiver of service provider.
  mojo::Receiver<chromeos::mojo_service_manager::mojom::ServiceProvider>
      provider_receiver_{this};
  // The mojo receiver set of ArcBridgeHost.
  mojo::ReceiverSet<mojom::ArcBridgeHost> receivers_;

  // Put as a last member to ensure that any callback tied to the elements
  // is not invoked.
  std::vector<std::unique_ptr<MojoChannelBase>> mojo_channels_
      GUARDED_BY_CONTEXT(thread_checker_);
};

}  // namespace arc

#endif  // ASH_COMPONENTS_ARC_SESSION_ARC_BRIDGE_HOST_IMPL_H_