mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
* Update project.pbxproj files to say Flutter rather than Chromium Also, the templates now have an empty organization so that we don't cause people to give their apps a Flutter copyright. * Update the copyright notice checker to require a standard notice on all files * Update copyrights on Dart files. (This was a mechanical commit.) * Fix weird license headers on Dart files that deviate from our conventions; relicense Shrine. Some were already marked "The Flutter Authors", not clear why. Their dates have been normalized. Some were missing the blank line after the license. Some were randomly different in trivial ways for no apparent reason (e.g. missing the trailing period). * Clean up the copyrights in non-Dart files. (Manual edits.) Also, make sure templates don't have copyrights. * Fix some more ORGANIZATIONNAMEs
142 lines
4.7 KiB
Dart
142 lines
4.7 KiB
Dart
// Copyright 2014 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.
|
|
|
|
import 'dart:async';
|
|
import 'dart:ui' as ui;
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
typedef _MessageHandler = Future<ByteData> Function(ByteData);
|
|
|
|
/// This class registers web platform plugins.
|
|
class PluginRegistry {
|
|
/// Creates a plugin registry.
|
|
PluginRegistry(this._binaryMessenger);
|
|
|
|
final BinaryMessenger _binaryMessenger;
|
|
|
|
/// Creates a registrar for the given plugin implementation class.
|
|
Registrar registrarFor(Type key) => Registrar(_binaryMessenger);
|
|
|
|
/// Registers this plugin handler with the engine, so that unrecognized
|
|
/// platform messages are forwarded to the registry, where they can be
|
|
/// correctly dispatched to one of the registered plugins.
|
|
void registerMessageHandler() {
|
|
// The function below is only defined in the Web dart:ui.
|
|
// ignore: undefined_function
|
|
ui.webOnlySetPluginHandler(_binaryMessenger.handlePlatformMessage);
|
|
}
|
|
}
|
|
|
|
/// A registrar for a particular plugin.
|
|
///
|
|
/// Gives access to a [BinaryMessenger] which has been configured to receive
|
|
/// platform messages from the framework side.
|
|
class Registrar {
|
|
/// Creates a registrar with the given [BinaryMessenger].
|
|
Registrar(this.messenger);
|
|
|
|
/// A [BinaryMessenger] configured to receive platform messages from the
|
|
/// framework side.
|
|
///
|
|
/// Use this [BinaryMessenger] when creating platform channels in order for
|
|
/// them to receive messages from the platform side. For example:
|
|
///
|
|
///
|
|
/// class MyPlugin {
|
|
/// static void registerWith(Registrar registrar) {
|
|
/// final MethodChannel channel = MethodChannel(
|
|
/// 'com.my_plugin/my_plugin',
|
|
/// const StandardMethodCodec(),
|
|
/// registrar.messenger);
|
|
/// final MyPlugin instance = MyPlugin();
|
|
/// channel.setMethodCallHandler(instance.handleMethodCall);
|
|
/// }
|
|
/// ...
|
|
/// }
|
|
final BinaryMessenger messenger;
|
|
}
|
|
|
|
/// The default plugin registry for the web.
|
|
final PluginRegistry webPluginRegistry = PluginRegistry(pluginBinaryMessenger);
|
|
|
|
/// A [BinaryMessenger] which does the inverse of the default framework
|
|
/// messenger.
|
|
///
|
|
/// Instead of sending messages from the framework to the engine, this
|
|
/// receives messages from the framework and dispatches them to registered
|
|
/// plugins.
|
|
class _PlatformBinaryMessenger extends BinaryMessenger {
|
|
final Map<String, _MessageHandler> _handlers = <String, _MessageHandler>{};
|
|
|
|
/// Receives a platform message from the framework.
|
|
@override
|
|
Future<void> handlePlatformMessage(String channel, ByteData data,
|
|
ui.PlatformMessageResponseCallback callback) async {
|
|
ByteData response;
|
|
try {
|
|
final MessageHandler handler = _handlers[channel];
|
|
if (handler != null) {
|
|
response = await handler(data);
|
|
} else {
|
|
ui.channelBuffers.push(channel, data, callback);
|
|
callback = null;
|
|
}
|
|
} catch (exception, stack) {
|
|
FlutterError.reportError(FlutterErrorDetails(
|
|
exception: exception,
|
|
stack: stack,
|
|
library: 'flutter web shell',
|
|
context: ErrorDescription('during a plugin platform message call'),
|
|
));
|
|
} finally {
|
|
if (callback != null) {
|
|
callback(response);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Sends a platform message from the platform side back to the framework.
|
|
@override
|
|
Future<ByteData> send(String channel, ByteData message) {
|
|
final Completer<ByteData> completer = Completer<ByteData>();
|
|
ui.window.onPlatformMessage(channel, message, (ByteData reply) {
|
|
try {
|
|
completer.complete(reply);
|
|
} catch (exception, stack) {
|
|
FlutterError.reportError(FlutterErrorDetails(
|
|
exception: exception,
|
|
stack: stack,
|
|
library: 'flutter web shell',
|
|
context: ErrorDescription('during a plugin-to-framework message'),
|
|
));
|
|
}
|
|
});
|
|
return completer.future;
|
|
}
|
|
|
|
@override
|
|
void setMessageHandler(
|
|
String channel, Future<ByteData> Function(ByteData message) handler) {
|
|
if (handler == null)
|
|
_handlers.remove(channel);
|
|
else
|
|
_handlers[channel] = handler;
|
|
ui.channelBuffers.drain(channel, (ByteData data, ui.PlatformMessageResponseCallback callback) async {
|
|
await handlePlatformMessage(channel, data, callback);
|
|
});
|
|
}
|
|
|
|
@override
|
|
void setMockMessageHandler(
|
|
String channel, Future<ByteData> Function(ByteData message) handler) {
|
|
throw FlutterError(
|
|
'Setting mock handlers is not supported on the platform side.');
|
|
}
|
|
}
|
|
|
|
/// The default [BinaryMessenger] for Flutter Web plugins.
|
|
final BinaryMessenger pluginBinaryMessenger = _PlatformBinaryMessenger();
|