提交 7a49b37a 编写于 作者: J Jason Simmons

Implement a protocol for discovering active Flutter instances on Android (#2720)

Tools can send a broadcast intent that will cause Flutter processes to
write their names and observatory ports to the log in JSON format.
上级 b6ad8850
......@@ -7,6 +7,7 @@
#include "base/logging.h"
#include "dart/runtime/include/dart_api.h"
#include "sky/engine/core/script/embedder_resources.h"
#include "sky/engine/tonic/dart_converter.h"
#include "sky/engine/tonic/dart_error.h"
#include "sky/engine/tonic/dart_library_natives.h"
......@@ -38,6 +39,7 @@ namespace {
static Dart_LibraryTagHandler g_embedder_tag_handler;
static DartLibraryNatives* g_natives;
static EmbedderResources* g_resources;
static int observatory_port_;
Dart_NativeFunction GetNativeFunction(Dart_Handle name,
int argument_count,
......@@ -61,7 +63,15 @@ void DartServiceIsolate::TriggerResourceLoad(Dart_NativeArguments args) {
}
void DartServiceIsolate::NotifyServerState(Dart_NativeArguments args) {
// NO-OP.
Dart_Handle exception = nullptr;
int port = DartConverter<int>::FromArguments(args, 1, exception);
if (!exception) {
observatory_port_ = port;
}
}
int DartServiceIsolate::GetObservatoryPort() {
return observatory_port_;
}
void DartServiceIsolate::Shutdown(Dart_NativeArguments args) {
......
......@@ -21,6 +21,8 @@ class DartServiceIsolate {
bool running_precompiled,
char** error);
static int GetObservatoryPort();
private:
// Native entries.
static void TriggerResourceLoad(Dart_NativeArguments args);
......
......@@ -5,7 +5,11 @@
package io.flutter.view;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.res.Configuration;
import android.opengl.Matrix;
import android.graphics.Rect;
......@@ -24,6 +28,8 @@ import android.view.accessibility.AccessibilityNodeInfo;
import android.view.accessibility.AccessibilityNodeProvider;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import org.json.JSONException;
import org.json.JSONObject;
import org.chromium.base.JNINamespace;
import org.chromium.mojo.bindings.Interface.Binding;
......@@ -68,6 +74,8 @@ public class FlutterView extends SurfaceView
AccessibilityManager.TouchExplorationStateChangeListener {
private static final String TAG = "FlutterView";
private static final String ACTION_DISCOVER = "io.flutter.view.DISCOVER";
private long mNativePlatformView;
private SkyEngine.Proxy mSkyEngine;
private ServiceProviderImpl mPlatformServiceProvider;
......@@ -83,6 +91,7 @@ public class FlutterView extends SurfaceView
private final KeyboardViewState mKeyboardState;
private final RawKeyboardServiceState mRawKeyboardState;
private final AccessibilityManager mAccessibilityManager;
private BroadcastReceiver discoveryReceiver;
public FlutterView(Context context) {
this(context, null);
......@@ -139,6 +148,11 @@ public class FlutterView extends SurfaceView
mAsyncOnMessageListeners = new HashMap<String, OnMessageListenerAsync>();
setLocale(getResources().getConfiguration().locale);
if ((context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
discoveryReceiver = new DiscoveryReceiver();
context.registerReceiver(discoveryReceiver, new IntentFilter(ACTION_DISCOVER));
}
}
@Override
......@@ -193,6 +207,10 @@ public class FlutterView extends SurfaceView
}
public void destroy() {
if (discoveryReceiver != null) {
getContext().unregisterReceiver(discoveryReceiver);
}
if (mPlatformServiceProviderBinding != null) {
mPlatformServiceProviderBinding.unbind().close();
mPlatformServiceProvider.unbindServices();
......@@ -439,6 +457,7 @@ public class FlutterView extends SurfaceView
}
private static native long nativeAttach(int inputObserverHandle);
private static native int nativeGetObservatoryPort();
private static native void nativeDetach(long nativePlatformViewAndroid);
private static native void nativeSurfaceCreated(long nativePlatformViewAndroid,
Surface surface);
......@@ -647,4 +666,17 @@ public class FlutterView extends SurfaceView
callback.call(reply);
}
}
/** Broadcast receiver used to discover active Flutter instances. */
private class DiscoveryReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
JSONObject discover = new JSONObject();
try {
discover.put("id", getContext().getPackageName());
discover.put("observatoryPort", nativeGetObservatoryPort());
Log.i(TAG, "DISCOVER: " + discover);
} catch (JSONException e) {}
}
}
}
......@@ -11,6 +11,7 @@
#include "base/bind.h"
#include "base/location.h"
#include "jni/FlutterView_jni.h"
#include "sky/engine/core/script/dart_service_isolate.h"
#include "sky/shell/gpu/direct/surface_notifications_direct.h"
#include "sky/shell/shell.h"
#include "sky/shell/shell_view.h"
......@@ -28,6 +29,10 @@ static jlong Attach(JNIEnv* env, jclass clazz, jint skyEngineHandle) {
return reinterpret_cast<jlong>(shell_view->view());
}
jint GetObservatoryPort(JNIEnv* env, jclass clazz) {
return blink::DartServiceIsolate::GetObservatoryPort();
}
// static
bool PlatformViewAndroid::Register(JNIEnv* env) {
return RegisterNativesImpl(env);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册