|
| 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 | + |
| 5 | +import 'dart:math'; |
| 6 | +import 'dart:ui'; |
| 7 | + |
| 8 | +import 'package:flutter/foundation.dart'; |
| 9 | +import 'package:plugin_platform_interface/plugin_platform_interface.dart'; |
| 10 | + |
| 11 | +import 'platform_navigation_delegate.dart'; |
| 12 | +import 'webview_platform.dart'; |
| 13 | + |
| 14 | +/// Interface for a platform implementation of a web view controller. |
| 15 | +/// |
| 16 | +/// Platform implementations should extend this class rather than implement it |
| 17 | +/// as `webview_flutter` does not consider newly added methods to be breaking |
| 18 | +/// changes. Extending this class (using `extends`) ensures that the subclass |
| 19 | +/// will get the default implementation, while platform implementations that |
| 20 | +/// `implements` this interface will be broken by newly added |
| 21 | +/// [PlatformWebViewCookieManager] methods. |
| 22 | +abstract class PlatformWebViewController extends PlatformInterface { |
| 23 | + /// Creates a new [PlatformWebViewController] |
| 24 | + factory PlatformWebViewController( |
| 25 | + PlatformWebViewControllerCreationParams params) { |
| 26 | + final PlatformWebViewController webViewControllerDelegate = |
| 27 | + WebViewPlatform.instance!.createPlatformWebViewController(params); |
| 28 | + PlatformInterface.verify(webViewControllerDelegate, _token); |
| 29 | + return webViewControllerDelegate; |
| 30 | + } |
| 31 | + |
| 32 | + /// Used by the platform implementation to create a new [PlatformWebViewController]. |
| 33 | + /// |
| 34 | + /// Should only be used by platform implementations because they can't extend |
| 35 | + /// a class that only contains a factory constructor. |
| 36 | + @protected |
| 37 | + PlatformWebViewController.implementation(this.params) : super(token: _token); |
| 38 | + |
| 39 | + static final Object _token = Object(); |
| 40 | + |
| 41 | + /// The parameters used to initialize the [PlatformWebViewController]. |
| 42 | + final PlatformWebViewControllerCreationParams params; |
| 43 | + |
| 44 | + /// Loads the file located on the specified [absoluteFilePath]. |
| 45 | + /// |
| 46 | + /// The [absoluteFilePath] parameter should contain the absolute path to the |
| 47 | + /// file as it is stored on the device. For example: |
| 48 | + /// `/Users/username/Documents/www/index.html`. |
| 49 | + /// |
| 50 | + /// Throws an ArgumentError if the [absoluteFilePath] does not exist. |
| 51 | + Future<void> loadFile( |
| 52 | + String absoluteFilePath, |
| 53 | + ) { |
| 54 | + throw UnimplementedError( |
| 55 | + 'loadFile is not implemented on the current platform'); |
| 56 | + } |
| 57 | + |
| 58 | + /// Loads the Flutter asset specified in the pubspec.yaml file. |
| 59 | + /// |
| 60 | + /// Throws an ArgumentError if [key] is not part of the specified assets |
| 61 | + /// in the pubspec.yaml file. |
| 62 | + Future<void> loadFlutterAsset( |
| 63 | + String key, |
| 64 | + ) { |
| 65 | + throw UnimplementedError( |
| 66 | + 'loadFlutterAsset is not implemented on the current platform'); |
| 67 | + } |
| 68 | + |
| 69 | + /// Loads the supplied HTML string. |
| 70 | + /// |
| 71 | + /// The [baseUrl] parameter is used when resolving relative URLs within the |
| 72 | + /// HTML string. |
| 73 | + Future<void> loadHtmlString( |
| 74 | + String html, { |
| 75 | + String? baseUrl, |
| 76 | + }) { |
| 77 | + throw UnimplementedError( |
| 78 | + 'loadHtmlString is not implemented on the current platform'); |
| 79 | + } |
| 80 | + |
| 81 | + /// Makes a specific HTTP request ands loads the response in the webview. |
| 82 | + /// |
| 83 | + /// [WebViewRequest.method] must be one of the supported HTTP methods |
| 84 | + /// in [WebViewRequestMethod]. |
| 85 | + /// |
| 86 | + /// If [WebViewRequest.headers] is not empty, its key-value pairs will be |
| 87 | + /// added as the headers for the request. |
| 88 | + /// |
| 89 | + /// If [WebViewRequest.body] is not null, it will be added as the body |
| 90 | + /// for the request. |
| 91 | + /// |
| 92 | + /// Throws an ArgumentError if [WebViewRequest.uri] has empty scheme. |
| 93 | + Future<void> loadRequest( |
| 94 | + LoadRequestParams params, |
| 95 | + ) { |
| 96 | + throw UnimplementedError( |
| 97 | + 'loadRequest is not implemented on the current platform'); |
| 98 | + } |
| 99 | + |
| 100 | + /// Accessor to the current URL that the WebView is displaying. |
| 101 | + /// |
| 102 | + /// If no URL was ever loaded, returns `null`. |
| 103 | + Future<String?> currentUrl() { |
| 104 | + throw UnimplementedError( |
| 105 | + 'currentUrl is not implemented on the current platform'); |
| 106 | + } |
| 107 | + |
| 108 | + /// Checks whether there's a back history item. |
| 109 | + Future<bool> canGoBack() { |
| 110 | + throw UnimplementedError( |
| 111 | + 'canGoBack is not implemented on the current platform'); |
| 112 | + } |
| 113 | + |
| 114 | + /// Checks whether there's a forward history item. |
| 115 | + Future<bool> canGoForward() { |
| 116 | + throw UnimplementedError( |
| 117 | + 'canGoForward is not implemented on the current platform'); |
| 118 | + } |
| 119 | + |
| 120 | + /// Goes back in the history of this WebView. |
| 121 | + /// |
| 122 | + /// If there is no back history item this is a no-op. |
| 123 | + Future<void> goBack() { |
| 124 | + throw UnimplementedError( |
| 125 | + 'goBack is not implemented on the current platform'); |
| 126 | + } |
| 127 | + |
| 128 | + /// Goes forward in the history of this WebView. |
| 129 | + /// |
| 130 | + /// If there is no forward history item this is a no-op. |
| 131 | + Future<void> goForward() { |
| 132 | + throw UnimplementedError( |
| 133 | + 'goForward is not implemented on the current platform'); |
| 134 | + } |
| 135 | + |
| 136 | + /// Reloads the current URL. |
| 137 | + Future<void> reload() { |
| 138 | + throw UnimplementedError( |
| 139 | + 'reload is not implemented on the current platform'); |
| 140 | + } |
| 141 | + |
| 142 | + /// Clears all caches used by the [WebView]. |
| 143 | + /// |
| 144 | + /// The following caches are cleared: |
| 145 | + /// 1. Browser HTTP Cache. |
| 146 | + /// 2. [Cache API](https://developers.google.com/web/fundamentals/instant-and-offline/web-storage/cache-api) caches. |
| 147 | + /// These are not yet supported in iOS WkWebView. Service workers tend to use this cache. |
| 148 | + /// 3. Application cache. |
| 149 | + Future<void> clearCache() { |
| 150 | + throw UnimplementedError( |
| 151 | + 'clearCache is not implemented on the current platform'); |
| 152 | + } |
| 153 | + |
| 154 | + /// Clears the local storage used by the [WebView]. |
| 155 | + Future<void> clearLocalStorage() { |
| 156 | + throw UnimplementedError( |
| 157 | + 'clearLocalStorage is not implemented on the current platform'); |
| 158 | + } |
| 159 | + |
| 160 | + /// Sets the [PlatformNavigationDelegate] containing the callback methods that |
| 161 | + /// are called during navigation events. |
| 162 | + Future<void> setPlatformNavigationDelegate( |
| 163 | + PlatformNavigationDelegate handler) { |
| 164 | + throw UnimplementedError( |
| 165 | + 'setPlatformNavigationDelegate is not implemented on the current platform'); |
| 166 | + } |
| 167 | + |
| 168 | + /// Runs the given JavaScript in the context of the current page. |
| 169 | + /// |
| 170 | + /// The Future completes with an error if a JavaScript error occurred. |
| 171 | + Future<void> runJavaScript(String javaScript) { |
| 172 | + throw UnimplementedError( |
| 173 | + 'runJavaScript is not implemented on the current platform'); |
| 174 | + } |
| 175 | + |
| 176 | + /// Runs the given JavaScript in the context of the current page, and returns the result. |
| 177 | + /// |
| 178 | + /// The Future completes with an error if a JavaScript error occurred, or if the |
| 179 | + /// type the given expression evaluates to is unsupported. Unsupported values include |
| 180 | + /// certain non-primitive types on iOS, as well as `undefined` or `null` on iOS 14+. |
| 181 | + Future<String> runJavaScriptReturningResult(String javaScript) { |
| 182 | + throw UnimplementedError( |
| 183 | + 'runJavaScriptReturningResult is not implemented on the current platform'); |
| 184 | + } |
| 185 | + |
| 186 | + /// Adds a new JavaScript channel to the set of enabled channels. |
| 187 | + Future<void> addJavaScriptChannel( |
| 188 | + JavaScriptChannelParams javaScriptChannelParams, |
| 189 | + ) { |
| 190 | + throw UnimplementedError( |
| 191 | + 'addJavaScriptChannel is not implemented on the current platform'); |
| 192 | + } |
| 193 | + |
| 194 | + /// Removes the JavaScript channel with the matching name from the set of |
| 195 | + /// enabled channels. |
| 196 | + /// |
| 197 | + /// This disables the channel with the matching name if it was previously |
| 198 | + /// enabled through the [addJavaScriptChannel]. |
| 199 | + Future<void> removeJavaScriptChannel(String javaScriptChannelName) { |
| 200 | + throw UnimplementedError( |
| 201 | + 'removeJavaScriptChannel is not implemented on the current platform'); |
| 202 | + } |
| 203 | + |
| 204 | + /// Returns the title of the currently loaded page. |
| 205 | + Future<String?> getTitle() { |
| 206 | + throw UnimplementedError( |
| 207 | + 'getTitle is not implemented on the current platform'); |
| 208 | + } |
| 209 | + |
| 210 | + /// Set the scrolled position of this view. |
| 211 | + /// |
| 212 | + /// The parameters `x` and `y` specify the position to scroll to in WebView pixels. |
| 213 | + Future<void> scrollTo(int x, int y) { |
| 214 | + throw UnimplementedError( |
| 215 | + 'scrollTo is not implemented on the current platform'); |
| 216 | + } |
| 217 | + |
| 218 | + /// Move the scrolled position of this view. |
| 219 | + /// |
| 220 | + /// The parameters `x` and `y` specify the amount of WebView pixels to scroll by. |
| 221 | + Future<void> scrollBy(int x, int y) { |
| 222 | + throw UnimplementedError( |
| 223 | + 'scrollBy is not implemented on the current platform'); |
| 224 | + } |
| 225 | + |
| 226 | + /// Return the current scroll position of this view. |
| 227 | + /// |
| 228 | + /// Scroll position is measured from the top left. |
| 229 | + Future<Point<int>> getScrollPosition() { |
| 230 | + throw UnimplementedError( |
| 231 | + 'getScrollPosition is not implemented on the current platform'); |
| 232 | + } |
| 233 | + |
| 234 | + /// Whether to enable the platform's webview content debugging tools. |
| 235 | + Future<void> enableDebugging(bool enabled) { |
| 236 | + throw UnimplementedError( |
| 237 | + 'enableDebugging is not implemented on the current platform'); |
| 238 | + } |
| 239 | + |
| 240 | + /// Whether to allow swipe based navigation on supported platforms. |
| 241 | + Future<void> enableGestureNavigation(bool enabled) { |
| 242 | + throw UnimplementedError( |
| 243 | + 'enableGestureNavigation is not implemented on the current platform'); |
| 244 | + } |
| 245 | + |
| 246 | + /// Whhether to support zooming using its on-screen zoom controls and gestures. |
| 247 | + Future<void> enableZoom(bool enabled) { |
| 248 | + throw UnimplementedError( |
| 249 | + 'enableZoom is not implemented on the current platform'); |
| 250 | + } |
| 251 | + |
| 252 | + /// Set the current background color of this view. |
| 253 | + Future<void> setBackgroundColor(Color color) { |
| 254 | + throw UnimplementedError( |
| 255 | + 'setBackgroundColor is not implemented on the current platform'); |
| 256 | + } |
| 257 | + |
| 258 | + /// Sets the JavaScript execution mode to be used by the webview. |
| 259 | + Future<void> setJavaScriptMode(JavaScriptMode javaScriptMode) { |
| 260 | + throw UnimplementedError( |
| 261 | + 'setJavaScriptMode is not implemented on the current platform'); |
| 262 | + } |
| 263 | + |
| 264 | + /// Sets the value used for the HTTP `User-Agent:` request header. |
| 265 | + Future<void> setUserAgent(String? userAgent) { |
| 266 | + throw UnimplementedError( |
| 267 | + 'setUserAgent is not implemented on the current platform'); |
| 268 | + } |
| 269 | +} |
| 270 | + |
| 271 | +/// Describes the parameters necessary for registering a JavaScript channel. |
| 272 | +class JavaScriptChannelParams { |
| 273 | + /// Creates a new [JavaScriptChannelParams] object. |
| 274 | + JavaScriptChannelParams({ |
| 275 | + required this.name, |
| 276 | + required this.onMessageReceived, |
| 277 | + }); |
| 278 | + |
| 279 | + /// The name that identifies the JavaScript channel. |
| 280 | + final String name; |
| 281 | + |
| 282 | + /// The callback method that is invoked when a [JavaScriptMessage] is |
| 283 | + /// received. |
| 284 | + final void Function(JavaScriptMessage) onMessageReceived; |
| 285 | +} |
0 commit comments