2018-11-07 12:24:35 -08:00
|
|
|
// Copyright 2013 The Flutter Authors. All rights reserved.
|
2018-07-13 10:55:24 -07:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
part of dart.ui;
|
|
|
|
|
|
2019-01-14 12:46:32 -08:00
|
|
|
/// A wrapper for a raw callback handle.
|
|
|
|
|
///
|
|
|
|
|
/// This is the return type for [PluginUtilities.getCallbackHandle].
|
2018-07-13 10:55:24 -07:00
|
|
|
class CallbackHandle {
|
|
|
|
|
/// Create an instance using a raw callback handle.
|
|
|
|
|
///
|
|
|
|
|
/// Only values produced by a call to [CallbackHandle.toRawHandle] should be
|
|
|
|
|
/// used, otherwise this object will be an invalid handle.
|
|
|
|
|
CallbackHandle.fromRawHandle(this._handle)
|
|
|
|
|
: assert(_handle != null, "'_handle' must not be null.");
|
|
|
|
|
|
2019-01-14 12:46:32 -08:00
|
|
|
final int _handle;
|
|
|
|
|
|
|
|
|
|
/// Get the raw callback handle to pass over a [MethodChannel] or [SendPort]
|
|
|
|
|
/// (to pass to another [Isolate]).
|
2018-07-13 10:55:24 -07:00
|
|
|
int toRawHandle() => _handle;
|
|
|
|
|
|
|
|
|
|
@override
|
2019-01-14 12:46:32 -08:00
|
|
|
bool operator ==(dynamic other) {
|
|
|
|
|
if (runtimeType != other.runtimeType)
|
|
|
|
|
return false;
|
|
|
|
|
final CallbackHandle typedOther = other;
|
|
|
|
|
return _handle == typedOther._handle;
|
|
|
|
|
}
|
2018-07-13 10:55:24 -07:00
|
|
|
|
|
|
|
|
@override
|
2019-01-14 12:46:32 -08:00
|
|
|
int get hashCode => _handle.hashCode;
|
2018-07-13 10:55:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Functionality for Flutter plugin authors.
|
2019-01-14 12:46:32 -08:00
|
|
|
///
|
|
|
|
|
/// See also:
|
|
|
|
|
///
|
|
|
|
|
/// * [IsolateNameServer], which provides utilities for dealing with
|
|
|
|
|
/// [Isolate]s.
|
|
|
|
|
class PluginUtilities {
|
2018-07-13 10:55:24 -07:00
|
|
|
// This class is only a namespace, and should not be instantiated or
|
|
|
|
|
// extended directly.
|
|
|
|
|
factory PluginUtilities._() => null;
|
2018-09-18 17:07:03 -07:00
|
|
|
|
2018-07-13 10:55:24 -07:00
|
|
|
static Map<Function, CallbackHandle> _forwardCache =
|
|
|
|
|
<Function, CallbackHandle>{};
|
|
|
|
|
static Map<CallbackHandle, Function> _backwardCache =
|
|
|
|
|
<CallbackHandle, Function>{};
|
|
|
|
|
|
|
|
|
|
/// Get a handle to a named top-level or static callback function which can
|
|
|
|
|
/// be easily passed between isolates.
|
|
|
|
|
///
|
2019-01-14 12:46:32 -08:00
|
|
|
/// The `callback` argument must not be null.
|
2018-07-13 10:55:24 -07:00
|
|
|
///
|
|
|
|
|
/// Returns a [CallbackHandle] that can be provided to
|
|
|
|
|
/// [PluginUtilities.getCallbackFromHandle] to retrieve a tear-off of the
|
|
|
|
|
/// original callback. If `callback` is not a top-level or static function,
|
|
|
|
|
/// null is returned.
|
|
|
|
|
static CallbackHandle getCallbackHandle(Function callback) {
|
|
|
|
|
assert(callback != null, "'callback' must not be null.");
|
2018-09-18 17:07:03 -07:00
|
|
|
return _forwardCache.putIfAbsent(callback, () {
|
|
|
|
|
final int handle = _getCallbackHandle(callback);
|
2019-05-07 16:10:21 -07:00
|
|
|
return handle != null ? CallbackHandle.fromRawHandle(handle) : null;
|
2018-09-18 17:07:03 -07:00
|
|
|
});
|
2018-07-13 10:55:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get a tear-off of a named top-level or static callback represented by a
|
|
|
|
|
/// handle.
|
|
|
|
|
///
|
2019-01-14 12:46:32 -08:00
|
|
|
/// The `handle` argument must not be null.
|
2018-07-13 10:55:24 -07:00
|
|
|
///
|
|
|
|
|
/// If `handle` is not a valid handle returned by
|
|
|
|
|
/// [PluginUtilities.getCallbackHandle], null is returned. Otherwise, a
|
|
|
|
|
/// tear-off of the callback associated with `handle` is returned.
|
|
|
|
|
static Function getCallbackFromHandle(CallbackHandle handle) {
|
|
|
|
|
assert(handle != null, "'handle' must not be null.");
|
|
|
|
|
return _backwardCache.putIfAbsent(
|
|
|
|
|
handle, () => _getCallbackFromHandle(handle.toRawHandle()));
|
|
|
|
|
}
|
|
|
|
|
}
|