• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5part of engine;
6
7/// A registry for factories that create platform views.
8class PlatformViewRegistry {
9  final Map<String, PlatformViewFactory> _registeredFactories =
10      <String, PlatformViewFactory>{};
11
12  final Map<int, html.Element> _createdViews = <int, html.Element>{};
13
14  /// Private constructor so this class can be a singleton.
15  PlatformViewRegistry._();
16
17  /// Register [viewTypeId] as being creating by the given [factory].
18  bool registerViewFactory(String viewTypeId, PlatformViewFactory factory) {
19    if (_registeredFactories.containsKey(viewTypeId)) {
20      return false;
21    }
22    _registeredFactories[viewTypeId] = factory;
23    return true;
24  }
25
26  /// Returns the view that has been created with the given [id], or `null` if
27  /// no such view exists.
28  html.Element getCreatedView(int id) {
29    return _createdViews[id];
30  }
31}
32
33/// A function which takes a unique [id] and creates an HTML element.
34typedef PlatformViewFactory = html.Element Function(int viewId);
35
36/// The platform view registry for this app.
37final PlatformViewRegistry platformViewRegistry = PlatformViewRegistry._();
38
39/// Handles a platform call to `flutter/platform_views`.
40///
41/// Used to create platform views.
42void handlePlatformViewCall(
43  ByteData data,
44  ui.PlatformMessageResponseCallback callback,
45) {
46  const MethodCodec codec = StandardMethodCodec();
47  final MethodCall decoded = codec.decodeMethodCall(data);
48
49  switch (decoded.method) {
50    case 'create':
51      _createPlatformView(decoded, callback);
52      return;
53  }
54  callback(null);
55}
56
57void _createPlatformView(
58    MethodCall methodCall, ui.PlatformMessageResponseCallback callback) {
59  final Map args = methodCall.arguments;
60  final int id = args['id'];
61  final String viewType = args['viewType'];
62  // TODO(het): Use 'direction', 'width', and 'height'.
63  if (!platformViewRegistry._registeredFactories.containsKey(viewType)) {
64    // TODO(het): Do we have a way of nicely reporting errors during platform
65    // channel calls?
66    callback(null);
67  }
68  // TODO(het): Use creation parameters.
69  final html.Element element =
70      platformViewRegistry._registeredFactories[viewType](id);
71
72  platformViewRegistry._createdViews[id] = element;
73}
74