Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
eaae468
add changed files
bparrishMines Feb 22, 2023
9d90d9f
some more changes
bparrishMines Feb 23, 2023
1094620
update tests
bparrishMines Feb 23, 2023
ce1634c
fix tests
bparrishMines Feb 23, 2023
3fae384
Merge branch 'main' of github.com:flutter/packages into instance_mana…
bparrishMines Feb 23, 2023
44e6f81
update instancemanager
bparrishMines Feb 23, 2023
ce6550b
ensure callback methods are passed to copy
bparrishMines Feb 23, 2023
def8956
version bump
bparrishMines Feb 23, 2023
ca9993e
update tests since objects can't be added multiple times
bparrishMines Feb 23, 2023
1408d00
Merge branch 'main' of github.com:flutter/packages into instance_mana…
bparrishMines Feb 27, 2023
1c48779
fix test
bparrishMines Feb 28, 2023
a552de9
change back to using copyable
bparrishMines Feb 28, 2023
9d58bd8
probably fix tests
bparrishMines Feb 28, 2023
2a05f7c
some reverts
bparrishMines Feb 28, 2023
73ea794
update changelog
bparrishMines Feb 28, 2023
625146d
Merge branch 'main' of github.com:flutter/packages into instance_mana…
bparrishMines Feb 28, 2023
32ff598
add a copy
bparrishMines Feb 28, 2023
b848c1f
raise minimum meta version
bparrishMines Feb 28, 2023
8efddc9
remove meta
bparrishMines Feb 28, 2023
e2b53b1
remove copy override
bparrishMines Feb 28, 2023
a9a9aa6
remove print
bparrishMines Feb 28, 2023
0ea91f7
remove unneeded changes
bparrishMines Feb 28, 2023
8141e9a
include raising of pigeon version
bparrishMines Feb 28, 2023
4a5aab1
Merge branch 'main' of github.com:flutter/packages into instance_mana…
bparrishMines Mar 6, 2023
f196771
update globalInstanceManager
bparrishMines Mar 6, 2023
7e32787
move location of asserts
bparrishMines Mar 6, 2023
9b572e2
Merge branch 'main' of github.com:flutter/packages into instance_mana…
bparrishMines Mar 9, 2023
1062ddb
lint
bparrishMines Mar 9, 2023
9605570
use a constant
bparrishMines Mar 16, 2023
d786495
Merge branch 'main' of github.com:flutter/packages into instance_mana…
bparrishMines Mar 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add changed files
  • Loading branch information
bparrishMines committed Feb 22, 2023
commit eaae468632b6dd56a9ffe50fe443eb82f73dac20

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import 'dart:ui';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart' show BinaryMessenger;
import 'package:flutter/widgets.dart' show AndroidViewSurface;
import 'package:flutter/widgets.dart'
show AndroidViewSurface, WidgetsFlutterBinding;

import 'android_webview.g.dart';
import 'android_webview_api_impls.dart';
Expand All @@ -25,25 +26,36 @@ export 'android_webview_api_impls.dart' show FileChooserMode;
/// Root of the Java class hierarchy.
///
/// See https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html.
class JavaObject with Copyable {
@immutable
class JavaObject {
/// Constructs a [JavaObject] without creating the associated Java object.
///
/// This should only be used by subclasses created by this library or to
/// create copies.
JavaObject.detached({
BinaryMessenger? binaryMessenger,
InstanceManager? instanceManager,
required BinaryMessenger? binaryMessenger,
required InstanceManager? instanceManager,
}) : _api = JavaObjectHostApiImpl(
binaryMessenger: binaryMessenger,
instanceManager: instanceManager,
);

static InstanceManager? _globalInstanceManager;
Copy link
Collaborator

@stuartmorgan-g stuartmorgan-g Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The less error-prone way of doing lazy initialization is:

static InstanceManager globalInstanceManager = _initInstanceManager();

static final InstanceManager _initInstanceManager() {
  WidgetsFlutterBinding.ensureInitialized();
  // Clears the native `InstanceManager` on initial use of the Dart one.
  InstanceManagerHostApi().clear();
  return InstanceManager(
    onWeakReferenceRemoved: (int identifier) {
      JavaObjectHostApiImpl().dispose(identifier);
    },
  );
}

(For non-statics, you can do the same thing but with late final to make it lazy.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @camsim99 FYI when updating the InstanceManager.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated example

/// Global instance of [InstanceManager].
static final InstanceManager globalInstanceManager = _initInstanceManager();

static InstanceManager _initInstanceManager() {
  WidgetsFlutterBinding.ensureInitialized();
  // Clears the native `InstanceManager` on initial use of the Dart one.
  InstanceManagerHostApi().clear();
  return InstanceManager(
    onWeakReferenceRemoved: (int identifier) {
      JavaObjectHostApiImpl().dispose(identifier);
     },
  );
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I'll fix my copy to avoid confusion. Sorry about that.


/// Global instance of [InstanceManager].
static final InstanceManager globalInstanceManager = InstanceManager(
onWeakReferenceRemoved: (int identifier) {
JavaObjectHostApiImpl().dispose(identifier);
},
);
static InstanceManager get globalInstanceManager {
if (_globalInstanceManager == null) {
WidgetsFlutterBinding.ensureInitialized();
// Clears the native `InstanceManager` on initial use of the Dart one.
InstanceManagerHostApi().clear();
_globalInstanceManager = InstanceManager(
onWeakReferenceRemoved: (int identifier) {
JavaObjectHostApiImpl().dispose(identifier);
},
);
}
return _globalInstanceManager!;
}

/// Pigeon Host Api implementation for [JavaObject].
final JavaObjectHostApiImpl _api;
Expand All @@ -52,11 +64,6 @@ class JavaObject with Copyable {
static void dispose(JavaObject instance) {
instance._api.instanceManager.removeWeakReference(instance);
}

@override
JavaObject copy() {
return JavaObject.detached();
}
}

/// An Android View that displays web pages.
Expand All @@ -78,21 +85,31 @@ class JavaObject with Copyable {
/// [Web-based content](https://developer.android.com/guide/webapps).
///
/// When a [WebView] is no longer needed [release] must be called.
@immutable
class WebView extends JavaObject {
/// Constructs a new WebView.
///
/// Due to changes in Flutter 3.0 the [useHybridComposition] doesn't have
/// any effect and should not be exposed publicly. More info here:
/// https://github.com/flutter/flutter/issues/108106
WebView({this.useHybridComposition = false}) : super.detached() {
WebView({
this.useHybridComposition = false,
@visibleForTesting super.binaryMessenger,
@visibleForTesting super.instanceManager,
}) : super.detached() {
api.createFromInstance(this);
}

/// Constructs a [WebView] without creating the associated Java object.
///
/// This should only be used by subclasses created by this library or to
/// create copies.
WebView.detached({this.useHybridComposition = false}) : super.detached();
@protected
WebView.detached({
this.useHybridComposition = false,
required super.binaryMessenger,
required super.instanceManager,
}) : super.detached();

/// Pigeon Host Api implementation for [WebView].
@visibleForTesting
Expand Down Expand Up @@ -396,11 +413,6 @@ class WebView extends JavaObject {
Future<void> setBackgroundColor(Color color) {
return api.setBackgroundColorFromInstance(this, color.value);
}

@override
WebView copy() {
return WebView.detached(useHybridComposition: useHybridComposition);
}
}

/// Manages cookies globally for all webviews.
Expand Down Expand Up @@ -453,21 +465,24 @@ class CookieManager {
/// obtained from [WebView.settings] is tied to the life of the WebView. If a
/// WebView has been destroyed, any method call on [WebSettings] will throw an
/// Exception.
@immutable
class WebSettings extends JavaObject {
/// Constructs a [WebSettings].
///
/// This constructor is only used for testing. An instance should be obtained
/// with [WebView.settings].
@visibleForTesting
WebSettings(WebView webView) : super.detached() {
WebSettings(WebView webView, {super.binaryMessenger, super.instanceManager})
: super.detached() {
api.createFromInstance(this, webView);
}

/// Constructs a [WebSettings] without creating the associated Java object.
///
/// This should only be used by subclasses created by this library or to
/// create copies.
WebSettings.detached() : super.detached();
WebSettings.detached({required super.binaryMessenger, required super.instanceManager})
: super.detached();

/// Pigeon Host Api implementation for [WebSettings].
@visibleForTesting
Expand Down Expand Up @@ -590,21 +605,18 @@ class WebSettings extends JavaObject {
Future<void> setAllowFileAccess(bool enabled) {
return api.setAllowFileAccessFromInstance(this, enabled);
}

@override
WebSettings copy() {
return WebSettings.detached();
}
}

/// Exposes a channel to receive calls from javaScript.
///
/// See [WebView.addJavaScriptChannel].
@immutable
class JavaScriptChannel extends JavaObject {
/// Constructs a [JavaScriptChannel].
JavaScriptChannel(
this.channelName, {
required this.postMessage,
super.binaryMessenger, super.instanceManager
}) : super.detached() {
AndroidWebViewFlutterApis.instance.ensureSetUp();
api.createFromInstance(this);
Expand All @@ -629,14 +641,10 @@ class JavaScriptChannel extends JavaObject {

/// Callback method when javaScript calls `postMessage` on the object instance passed.
final void Function(String message) postMessage;

@override
JavaScriptChannel copy() {
return JavaScriptChannel.detached(channelName, postMessage: postMessage);
}
}

/// Receive various notifications and requests for [WebView].
@immutable
class WebViewClient extends JavaObject {
/// Constructs a [WebViewClient].
WebViewClient({
Expand Down Expand Up @@ -821,22 +829,11 @@ class WebViewClient extends JavaObject {
) {
return api.setShouldOverrideUrlLoadingReturnValueFromInstance(this, value);
}

@override
WebViewClient copy() {
return WebViewClient.detached(
onPageStarted: onPageStarted,
onPageFinished: onPageFinished,
onReceivedRequestError: onReceivedRequestError,
onReceivedError: onReceivedError,
requestLoading: requestLoading,
urlLoading: urlLoading,
);
}
}

/// The interface to be used when content can not be handled by the rendering
/// engine for [WebView], and should be downloaded instead.
@immutable
class DownloadListener extends JavaObject {
/// Constructs a [DownloadListener].
DownloadListener({required this.onDownloadStart}) : super.detached() {
Expand All @@ -863,14 +860,10 @@ class DownloadListener extends JavaObject {
String mimetype,
int contentLength,
) onDownloadStart;

@override
DownloadListener copy() {
return DownloadListener.detached(onDownloadStart: onDownloadStart);
}
}

/// Handles JavaScript dialogs, favicons, titles, and the progress for [WebView].
@immutable
class WebChromeClient extends JavaObject {
/// Constructs a [WebChromeClient].
WebChromeClient({this.onProgressChanged, this.onShowFileChooser})
Expand Down Expand Up @@ -937,19 +930,12 @@ class WebChromeClient extends JavaObject {
value,
);
}

@override
WebChromeClient copy() {
return WebChromeClient.detached(
onProgressChanged: onProgressChanged,
onShowFileChooser: onShowFileChooser,
);
}
}

/// Parameters received when a [WebChromeClient] should show a file chooser.
///
/// See https://developer.android.com/reference/android/webkit/WebChromeClient.FileChooserParams.
@immutable
class FileChooserParams extends JavaObject {
/// Constructs a [FileChooserParams] without creating the associated Java
/// object.
Expand All @@ -976,16 +962,6 @@ class FileChooserParams extends JavaObject {

/// Mode of how to select files for a file chooser.
final FileChooserMode mode;

@override
FileChooserParams copy() {
return FileChooserParams.detached(
isCaptureEnabled: isCaptureEnabled,
acceptTypes: acceptTypes,
filenameHint: filenameHint,
mode: mode,
);
}
}

/// Encompasses parameters to the [WebViewClient.requestLoading] method.
Expand Down Expand Up @@ -1061,6 +1037,7 @@ class FlutterAssetManager {
/// Manages the JavaScript storage APIs provided by the [WebView].
///
/// Wraps [WebStorage](https://developer.android.com/reference/android/webkit/WebStorage).
@immutable
class WebStorage extends JavaObject {
/// Constructs a [WebStorage].
///
Expand Down Expand Up @@ -1089,9 +1066,4 @@ class WebStorage extends JavaObject {
Future<void> deleteAllData() {
return api.deleteAllDataFromInstance(this);
}

@override
WebStorage copy() {
return WebStorage.detached();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Autogenerated from Pigeon (v4.2.14), do not edit directly.
// Autogenerated from Pigeon (v9.0.0), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import

import 'dart:async';
import 'dart:typed_data' show Float64List, Int32List, Int64List, Uint8List;

Expand Down Expand Up @@ -151,6 +152,42 @@ class WebViewPoint {
}
}

/// Host API for managing the native `InstanceManager`.
class InstanceManagerHostApi {
/// Constructor for [InstanceManagerHostApi]. The [binaryMessenger] named argument is
/// available for dependency injection. If it is left null, the default
/// BinaryMessenger will be used which routes to the host platform.
InstanceManagerHostApi({BinaryMessenger? binaryMessenger})
: _binaryMessenger = binaryMessenger;
final BinaryMessenger? _binaryMessenger;

static const MessageCodec<Object?> codec = StandardMessageCodec();

/// Clear the native `InstanceManager`.
///
/// This is typically only used after a hot restart.
Future<void> clear() async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.InstanceManagerHostApi.clear', codec,
binaryMessenger: _binaryMessenger);
final List<Object?>? replyList = await channel.send(null) as List<Object?>?;
if (replyList == null) {
throw PlatformException(
code: 'channel-error',
message: 'Unable to establish connection on channel.',
);
} else if (replyList.length > 1) {
throw PlatformException(
code: replyList[0]! as String,
message: replyList[1] as String?,
details: replyList[2],
);
} else {
return;
}
}
}

/// Handles methods calls to the native Java Object class.
///
/// Also handles calls to remove the reference to an instance with `dispose`.
Expand Down Expand Up @@ -297,7 +334,6 @@ class _WebViewHostApiCodec extends StandardMessageCodec {
switch (type) {
case 128:
return WebViewPoint.decode(readValue(buffer)!);

default:
return super.readValueOfType(type, buffer);
}
Expand Down Expand Up @@ -1394,10 +1430,8 @@ class _WebViewClientFlutterApiCodec extends StandardMessageCodec {
switch (type) {
case 128:
return WebResourceErrorData.decode(readValue(buffer)!);

case 129:
return WebResourceRequestData.decode(readValue(buffer)!);

default:
return super.readValueOfType(type, buffer);
}
Expand Down Expand Up @@ -1935,7 +1969,6 @@ class _FileChooserParamsFlutterApiCodec extends StandardMessageCodec {
switch (type) {
case 128:
return FileChooserModeEnumData.decode(readValue(buffer)!);

default:
return super.readValueOfType(type, buffer);
}
Expand Down
Loading