|
| 1 | +/**************************************************************************/ |
| 2 | +/* EditorMessageDispatcher.kt */ |
| 3 | +/**************************************************************************/ |
| 4 | +/* This file is part of: */ |
| 5 | +/* GODOT ENGINE */ |
| 6 | +/* https://godotengine.org */ |
| 7 | +/**************************************************************************/ |
| 8 | +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | +/* */ |
| 11 | +/* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | +/* a copy of this software and associated documentation files (the */ |
| 13 | +/* "Software"), to deal in the Software without restriction, including */ |
| 14 | +/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | +/* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | +/* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | +/* the following conditions: */ |
| 18 | +/* */ |
| 19 | +/* The above copyright notice and this permission notice shall be */ |
| 20 | +/* included in all copies or substantial portions of the Software. */ |
| 21 | +/* */ |
| 22 | +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | +/**************************************************************************/ |
| 30 | + |
| 31 | +package org.godotengine.editor |
| 32 | + |
| 33 | +import android.annotation.SuppressLint |
| 34 | +import android.content.Intent |
| 35 | +import android.content.pm.PackageManager |
| 36 | +import android.os.Bundle |
| 37 | +import android.os.Handler |
| 38 | +import android.os.Message |
| 39 | +import android.os.Messenger |
| 40 | +import android.os.RemoteException |
| 41 | +import android.util.Log |
| 42 | +import java.util.concurrent.ConcurrentHashMap |
| 43 | + |
| 44 | +/** |
| 45 | + * Used by the [GodotEditor] classes to dispatch messages across processes. |
| 46 | + */ |
| 47 | +internal class EditorMessageDispatcher(private val editor: GodotEditor) { |
| 48 | + |
| 49 | + companion object { |
| 50 | + private val TAG = EditorMessageDispatcher::class.java.simpleName |
| 51 | + |
| 52 | + /** |
| 53 | + * Extra used to pass the message dispatcher payload through an [Intent] |
| 54 | + */ |
| 55 | + const val EXTRA_MSG_DISPATCHER_PAYLOAD = "message_dispatcher_payload" |
| 56 | + |
| 57 | + /** |
| 58 | + * Key used to pass the editor id through a [Bundle] |
| 59 | + */ |
| 60 | + private const val KEY_EDITOR_ID = "editor_id" |
| 61 | + |
| 62 | + /** |
| 63 | + * Key used to pass the editor messenger through a [Bundle] |
| 64 | + */ |
| 65 | + private const val KEY_EDITOR_MESSENGER = "editor_messenger" |
| 66 | + |
| 67 | + /** |
| 68 | + * Requests the recipient to quit right away. |
| 69 | + */ |
| 70 | + private const val MSG_FORCE_QUIT = 0 |
| 71 | + |
| 72 | + /** |
| 73 | + * Requests the recipient to store the passed [android.os.Messenger] instance. |
| 74 | + */ |
| 75 | + private const val MSG_REGISTER_MESSENGER = 1 |
| 76 | + } |
| 77 | + |
| 78 | + private val recipientsMessengers = ConcurrentHashMap<Int, Messenger>() |
| 79 | + |
| 80 | + @SuppressLint("HandlerLeak") |
| 81 | + private val dispatcherHandler = object : Handler() { |
| 82 | + override fun handleMessage(msg: Message) { |
| 83 | + when (msg.what) { |
| 84 | + MSG_FORCE_QUIT -> editor.finish() |
| 85 | + |
| 86 | + MSG_REGISTER_MESSENGER -> { |
| 87 | + val editorId = msg.arg1 |
| 88 | + val messenger = msg.replyTo |
| 89 | + registerMessenger(editorId, messenger) |
| 90 | + } |
| 91 | + |
| 92 | + else -> super.handleMessage(msg) |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Request the window with the given [editorId] to force quit. |
| 99 | + */ |
| 100 | + fun requestForceQuit(editorId: Int): Boolean { |
| 101 | + val messenger = recipientsMessengers[editorId] ?: return false |
| 102 | + return try { |
| 103 | + Log.v(TAG, "Requesting 'forceQuit' for $editorId") |
| 104 | + val msg = Message.obtain(null, MSG_FORCE_QUIT) |
| 105 | + messenger.send(msg) |
| 106 | + true |
| 107 | + } catch (e: RemoteException) { |
| 108 | + Log.e(TAG, "Error requesting 'forceQuit' to $editorId", e) |
| 109 | + recipientsMessengers.remove(editorId) |
| 110 | + false |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Utility method to register a receiver messenger. |
| 116 | + */ |
| 117 | + private fun registerMessenger(editorId: Int, messenger: Messenger?, messengerDeathCallback: Runnable? = null) { |
| 118 | + try { |
| 119 | + if (messenger == null) { |
| 120 | + Log.w(TAG, "Invalid 'replyTo' payload") |
| 121 | + } else if (messenger.binder.isBinderAlive) { |
| 122 | + messenger.binder.linkToDeath({ |
| 123 | + Log.v(TAG, "Removing messenger for $editorId") |
| 124 | + recipientsMessengers.remove(editorId) |
| 125 | + messengerDeathCallback?.run() |
| 126 | + }, 0) |
| 127 | + recipientsMessengers[editorId] = messenger |
| 128 | + } |
| 129 | + } catch (e: RemoteException) { |
| 130 | + Log.e(TAG, "Unable to register messenger from $editorId", e) |
| 131 | + recipientsMessengers.remove(editorId) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Utility method to register a [Messenger] attached to this handler with a host. |
| 137 | + * |
| 138 | + * This is done so that the host can send request to the editor instance attached to this handle. |
| 139 | + * |
| 140 | + * Note that this is only done when the editor instance is internal (not exported) to prevent |
| 141 | + * arbitrary apps from having the ability to send requests. |
| 142 | + */ |
| 143 | + private fun registerSelfTo(pm: PackageManager, host: Messenger?, selfId: Int) { |
| 144 | + try { |
| 145 | + if (host == null || !host.binder.isBinderAlive) { |
| 146 | + Log.v(TAG, "Host is unavailable") |
| 147 | + return |
| 148 | + } |
| 149 | + |
| 150 | + val activityInfo = pm.getActivityInfo(editor.componentName, 0) |
| 151 | + if (activityInfo.exported) { |
| 152 | + Log.v(TAG, "Not registering self to host as we're exported") |
| 153 | + return |
| 154 | + } |
| 155 | + |
| 156 | + Log.v(TAG, "Registering self $selfId to host") |
| 157 | + val msg = Message.obtain(null, MSG_REGISTER_MESSENGER) |
| 158 | + msg.arg1 = selfId |
| 159 | + msg.replyTo = Messenger(dispatcherHandler) |
| 160 | + host.send(msg) |
| 161 | + } catch (e: RemoteException) { |
| 162 | + Log.e(TAG, "Unable to register self with host", e) |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Parses the starting intent and retrieve an editor messenger if available |
| 168 | + */ |
| 169 | + fun parseStartIntent(pm: PackageManager, intent: Intent) { |
| 170 | + val messengerBundle = intent.getBundleExtra(EXTRA_MSG_DISPATCHER_PAYLOAD) ?: return |
| 171 | + |
| 172 | + // Retrieve the sender messenger payload and store it. This can be used to communicate back |
| 173 | + // to the sender. |
| 174 | + val senderId = messengerBundle.getInt(KEY_EDITOR_ID) |
| 175 | + val senderMessenger: Messenger? = messengerBundle.getParcelable(KEY_EDITOR_MESSENGER) |
| 176 | + registerMessenger(senderId, senderMessenger) |
| 177 | + |
| 178 | + // Register ourselves to the sender so that it can communicate with us. |
| 179 | + registerSelfTo(pm, senderMessenger, editor.getEditorId()) |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Returns the payload used by the [EditorMessageDispatcher] class to establish an IPC bridge |
| 184 | + * across editor instances. |
| 185 | + */ |
| 186 | + fun getMessageDispatcherPayload(): Bundle { |
| 187 | + return Bundle().apply { |
| 188 | + putInt(KEY_EDITOR_ID, editor.getEditorId()) |
| 189 | + putParcelable(KEY_EDITOR_MESSENGER, Messenger(dispatcherHandler)) |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments