Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[webview_flutter] Implementation of the webview_flutter_platform_interface package #4302

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Below is a list of people and organizations that have contributed
# to the Flutter project. Names should be added to the list like so:
#
# Name/Organization <email address>

Google Inc.
The Chromium Authors
German Saprykin <[email protected]>
Benjamin Sauer <[email protected]>
[email protected]
Ali Bitek <[email protected]>
Pol Batlló <[email protected]>
Anatoly Pulyaevskiy
Hayden Flinner <[email protected]>
Stefano Rodriguez <[email protected]>
Salvatore Giordano <[email protected]>
Brian Armstrong <[email protected]>
Paul DeMarco <[email protected]>
Fabricio Nogueira <[email protected]>
Simon Lightfoot <[email protected]>
Ashton Thomas <[email protected]>
Thomas Danner <[email protected]>
Diego Velásquez <[email protected]>
Hajime Nakamura <[email protected]>
Tuyển Vũ Xuân <[email protected]>
Miguel Ruivo <[email protected]>
Sarthak Verma <[email protected]>
Mike Diarmid <[email protected]>
Invertase <[email protected]>
Elliot Hesp <[email protected]>
Vince Varga <[email protected]>
Aawaz Gyawali <[email protected]>
EUI Limited <[email protected]>
Katarina Sheremet <[email protected]>
Thomas Stockx <[email protected]>
Sarbagya Dhaubanjar <[email protected]>
Ozkan Eksi <[email protected]>
Rishab Nayak <[email protected]>
ko2ic <[email protected]>
Jonathan Younger <[email protected]>
Jose Sanchez <[email protected]>
Debkanchan Samadder <[email protected]>
Audrius Karosevicius <[email protected]>
Lukasz Piliszczuk <[email protected]>
SoundReply Solutions GmbH <[email protected]>
Rafal Wachol <[email protected]>
Pau Picas <[email protected]>
Christian Weder <[email protected]>
Alexandru Tuca <[email protected]>
Christian Weder <[email protected]>
Rhodes Davis Jr. <[email protected]>
Luigi Agosti <[email protected]>
Quentin Le Guennec <[email protected]>
Koushik Ravikumar <[email protected]>
Nissim Dsilva <[email protected]>
Giancarlo Rocha <[email protected]>
Ryo Miyake <[email protected]>
Théo Champion <[email protected]>
Kazuki Yamaguchi <[email protected]>
Eitan Schwartz <[email protected]>
Chris Rutkowski <[email protected]>
Juan Alvarez <[email protected]>
Aleksandr Yurkovskiy <[email protected]>
Anton Borries <[email protected]>
Alex Li <[email protected]>
Rahul Raj <[email protected]>
Maurits van Beusekom <[email protected]>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

* Extracted platform interface from `webview_flutter`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright 2013 The Flutter Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# webview_flutter_platform_interface

A common platform interface for the [`webview_flutter`](https://pub.dev/packages/webview_flutter) plugin.

This interface allows platform-specific implementations of the `webview_flutter`
plugin, as well as the plugin itself, to ensure they are supporting the
same interface.

# Usage

To implement a new platform-specific implementation of `webview_flutter`, extend
[`WebviewPlatform`](lib/src/platform_interface/webview_platform.dart) with an implementation that performs the
platform-specific behavior, and when you register your plugin, set the default
`WebviewPlatform` by calling
`WebviewPlatform.setInstance(MyPlatformWebview())`.

# Note on breaking changes

Strongly prefer non-breaking changes (such as adding a method to the interface)
over breaking changes for this package.

See https://flutter.dev/go/platform-interface-breaking-changes for a discussion
on why a less-clean interface is preferable to a breaking change.
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
// 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.

import 'dart:async';

import 'package:flutter/services.dart';

import '../platform_interface/javascript_channel_registry.dart';
import '../platform_interface/platform_interface.dart';
import '../types/types.dart';

/// A [WebViewPlatformController] that uses a method channel to control the webview.
class MethodChannelWebViewPlatform implements WebViewPlatformController {
/// Constructs an instance that will listen for webviews broadcasting to the
/// given [id], using the given [WebViewPlatformCallbacksHandler].
MethodChannelWebViewPlatform(
int id,
this._platformCallbacksHandler,
this._javascriptChannelRegistry,
) : assert(_platformCallbacksHandler != null),
_channel = MethodChannel('plugins.flutter.io/webview_$id') {
_channel.setMethodCallHandler(_onMethodCall);
}

final JavascriptChannelRegistry _javascriptChannelRegistry;

final WebViewPlatformCallbacksHandler _platformCallbacksHandler;

final MethodChannel _channel;

static const MethodChannel _cookieManagerChannel =
MethodChannel('plugins.flutter.io/cookie_manager');

Future<bool?> _onMethodCall(MethodCall call) async {
switch (call.method) {
case 'javascriptChannelMessage':
final String channel = call.arguments['channel']!;
final String message = call.arguments['message']!;
_javascriptChannelRegistry.onJavascriptChannelMessage(channel, message);
return true;
case 'navigationRequest':
return await _platformCallbacksHandler.onNavigationRequest(
url: call.arguments['url']!,
isForMainFrame: call.arguments['isForMainFrame']!,
);
case 'onPageFinished':
_platformCallbacksHandler.onPageFinished(call.arguments['url']!);
return null;
case 'onProgress':
_platformCallbacksHandler.onProgress(call.arguments['progress']);
return null;
case 'onPageStarted':
_platformCallbacksHandler.onPageStarted(call.arguments['url']!);
return null;
case 'onWebResourceError':
_platformCallbacksHandler.onWebResourceError(
WebResourceError(
errorCode: call.arguments['errorCode']!,
description: call.arguments['description']!,
// iOS doesn't support `failingUrl`.
failingUrl: call.arguments['failingUrl'],
domain: call.arguments['domain'],
errorType: call.arguments['errorType'] == null
? null
: WebResourceErrorType.values.firstWhere(
(WebResourceErrorType type) {
return type.toString() ==
'$WebResourceErrorType.${call.arguments['errorType']}';
},
),
),
);
return null;
}

throw MissingPluginException(
'${call.method} was invoked but has no handler',
);
}

@override
Future<void> loadUrl(
String url,
Map<String, String>? headers,
) async {
assert(url != null);
return _channel.invokeMethod<void>('loadUrl', <String, dynamic>{
'url': url,
'headers': headers,
});
}

@override
Future<String?> currentUrl() => _channel.invokeMethod<String>('currentUrl');

@override
Future<bool> canGoBack() =>
_channel.invokeMethod<bool>("canGoBack").then((result) => result!);

@override
Future<bool> canGoForward() =>
_channel.invokeMethod<bool>("canGoForward").then((result) => result!);

@override
Future<void> goBack() => _channel.invokeMethod<void>("goBack");

@override
Future<void> goForward() => _channel.invokeMethod<void>("goForward");

@override
Future<void> reload() => _channel.invokeMethod<void>("reload");

@override
Future<void> clearCache() => _channel.invokeMethod<void>("clearCache");

@override
Future<void> updateSettings(WebSettings settings) async {
final Map<String, dynamic> updatesMap = _webSettingsToMap(settings);
if (updatesMap.isNotEmpty) {
await _channel.invokeMethod<void>('updateSettings', updatesMap);
}
}

@override
Future<String> evaluateJavascript(String javascriptString) {
return _channel
.invokeMethod<String>('evaluateJavascript', javascriptString)
.then((result) => result!);
}

@override
Future<void> addJavascriptChannels(Set<String> javascriptChannelNames) {
return _channel.invokeMethod<void>(
'addJavascriptChannels', javascriptChannelNames.toList());
}

@override
Future<void> removeJavascriptChannels(Set<String> javascriptChannelNames) {
return _channel.invokeMethod<void>(
'removeJavascriptChannels', javascriptChannelNames.toList());
}

@override
Future<String?> getTitle() => _channel.invokeMethod<String>("getTitle");

@override
Future<void> scrollTo(int x, int y) {
return _channel.invokeMethod<void>('scrollTo', <String, int>{
'x': x,
'y': y,
});
}

@override
Future<void> scrollBy(int x, int y) {
return _channel.invokeMethod<void>('scrollBy', <String, int>{
'x': x,
'y': y,
});
}

@override
Future<int> getScrollX() =>
_channel.invokeMethod<int>("getScrollX").then((result) => result!);

@override
Future<int> getScrollY() =>
_channel.invokeMethod<int>("getScrollY").then((result) => result!);

/// Method channel implementation for [WebViewPlatform.clearCookies].
static Future<bool> clearCookies() {
return _cookieManagerChannel
.invokeMethod<bool>('clearCookies')
.then<bool>((dynamic result) => result!);
}

static Map<String, dynamic> _webSettingsToMap(WebSettings? settings) {
final Map<String, dynamic> map = <String, dynamic>{};
void _addIfNonNull(String key, dynamic value) {
if (value == null) {
return;
}
map[key] = value;
}

void _addSettingIfPresent<T>(String key, WebSetting<T> setting) {
if (!setting.isPresent) {
return;
}
map[key] = setting.value;
}

_addIfNonNull('jsMode', settings!.javascriptMode?.index);
_addIfNonNull('hasNavigationDelegate', settings.hasNavigationDelegate);
_addIfNonNull('hasProgressTracking', settings.hasProgressTracking);
_addIfNonNull('debuggingEnabled', settings.debuggingEnabled);
_addIfNonNull(
'gestureNavigationEnabled', settings.gestureNavigationEnabled);
_addIfNonNull(
'allowsInlineMediaPlayback', settings.allowsInlineMediaPlayback);
_addSettingIfPresent('userAgent', settings.userAgent);
return map;
}

/// Converts a [CreationParams] object to a map as expected by `platform_views` channel.
///
/// This is used for the `creationParams` argument of the platform views created by
/// [AndroidWebViewBuilder] and [CupertinoWebViewBuilder].
static Map<String, dynamic> creationParamsToMap(
CreationParams creationParams, {
bool usesHybridComposition = false,
}) {
return <String, dynamic>{
'initialUrl': creationParams.initialUrl,
'settings': _webSettingsToMap(creationParams.webSettings),
'javascriptChannelNames': creationParams.javascriptChannelNames.toList(),
'userAgent': creationParams.userAgent,
'autoMediaPlaybackPolicy': creationParams.autoMediaPlaybackPolicy.index,
'usesHybridComposition': usesHybridComposition,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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.

import '../types/javascript_channel.dart';
import '../types/javascript_message.dart';

/// Utility class for managing named JavaScript channels and forwarding incoming
/// messages on the correct channel.
class JavascriptChannelRegistry {
/// Constructs a [JavascriptChannelRegistry] initializing it with the given
/// set of [JavascriptChannel]s.
JavascriptChannelRegistry(Set<JavascriptChannel>? channels) {
updateJavascriptChannelsFromSet(channels);
}

/// Maps a channel name to a channel.
final Map<String, JavascriptChannel> channels = <String, JavascriptChannel>{};

/// Invoked when a JavaScript channel message is received.
void onJavascriptChannelMessage(String channel, String message) {
final JavascriptChannel? javascriptChannel = channels[channel];

if (javascriptChannel == null) {
throw ArgumentError('No channel registered with name $channel.');
}

javascriptChannel.onMessageReceived(JavascriptMessage(message));
}

/// Updates the set of [JavascriptChannel]s with the new set.
void updateJavascriptChannelsFromSet(Set<JavascriptChannel>? channels) {
this.channels.clear();
if (channels == null) {
return;
}

for (final JavascriptChannel channel in channels) {
this.channels[channel.name] = channel;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// 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.

export 'javascript_channel_registry.dart';
export 'webview_platform.dart';
export 'webview_platform_callbacks_handler.dart';
export 'webview_platform_controller.dart';
Loading