flutter_inappbrowser.dart 64.1 KB
Newer Older
pichillilorenzo's avatar
pichillilorenzo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
*/

pichillilorenzo's avatar
pichillilorenzo committed
22
import 'dart:io';
pichillilorenzo's avatar
pichillilorenzo committed
23
import 'dart:async';
24
import 'dart:collection';
25
import 'dart:typed_data';
26
import 'dart:convert';
pichillilorenzo's avatar
pichillilorenzo committed
27

pichillilorenzo's avatar
pichillilorenzo committed
28 29
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
pichillilorenzo's avatar
pichillilorenzo committed
30
import 'package:flutter/services.dart';
pichillilorenzo's avatar
pichillilorenzo committed
31 32
import 'package:flutter/widgets.dart';
import 'package:flutter/gestures.dart';
33
import 'package:uuid/uuid.dart';
pichillilorenzo's avatar
pichillilorenzo committed
34
import 'package:mime/mime.dart';
35 36

typedef Future<dynamic> ListenerCallback(MethodCall call);
37
typedef Future<void> JavaScriptHandlerCallback(List<dynamic> arguments);
38

39 40 41 42 43 44 45
var _uuidGenerator = new Uuid();

///
enum ConsoleMessageLevel {
  DEBUG, ERROR, LOG, TIP, WARNING
}

46 47
///Public class representing a resource request of the [InAppBrowser] WebView.
///It is used by the method [InAppBrowser.onLoadResource()].
48 49 50 51 52 53 54 55 56 57
class WebResourceRequest {

  String url;
  Map<String, String> headers;
  String method;

  WebResourceRequest(this.url, this.headers, this.method);

}

58 59
///Public class representing a resource response of the [InAppBrowser] WebView.
///It is used by the method [InAppBrowser.onLoadResource()].
60 61 62 63 64
class WebResourceResponse {

  String url;
  Map<String, String> headers;
  int statusCode;
65 66
  int startTime;
  int duration;
67 68
  Uint8List data;

69
  WebResourceResponse(this.url, this.headers, this.statusCode, this.startTime, this.duration, this.data);
70 71 72

}

73 74 75 76 77 78 79 80 81 82 83 84 85
///Public class representing a JavaScript console message from WebCore.
///This could be a issued by a call to one of the console logging functions (e.g. console.log('...')) or a JavaScript error on the page.
///
///To receive notifications of these messages, override the [InAppBrowser.onConsoleMessage()] function.
class ConsoleMessage {

  String sourceURL = "";
  int lineNumber = 1;
  String message = "";
  ConsoleMessageLevel messageLevel = ConsoleMessageLevel.LOG;

  ConsoleMessage(this.sourceURL, this.lineNumber, this.message, this.messageLevel);
}
pichillilorenzo's avatar
pichillilorenzo committed
86

87 88
class _ChannelManager {
  static const MethodChannel channel = const MethodChannel('com.pichillilorenzo/flutter_inappbrowser');
89
  static bool initialized = false;
90
  static final listeners = HashMap<String, ListenerCallback>();
91 92

  static Future<dynamic> _handleMethod(MethodCall call) async {
93
    String uuid = call.arguments["uuid"];
pichillilorenzo's avatar
pichillilorenzo committed
94
    return await listeners[uuid](call);
95 96
  }

pichillilorenzo's avatar
pichillilorenzo committed
97
  static void addListener(String key, ListenerCallback callback) {
98 99
    if (!initialized)
      init();
100
    listeners.putIfAbsent(key, () => callback);
101 102 103 104
  }

  static void init () {
    channel.setMethodCallHandler(_handleMethod);
105
    initialized = true;
106 107 108
  }
}

pichillilorenzo's avatar
pichillilorenzo committed
109
///InAppBrowser class. [webViewController] can be used to access the [InAppWebView] API.
110
///
111
///This class uses the native WebView of the platform.
pichillilorenzo's avatar
pichillilorenzo committed
112 113
class InAppBrowser {

114
  String uuid;
115
  Map<String, List<JavaScriptHandlerCallback>> javaScriptHandlersMap = HashMap<String, List<JavaScriptHandlerCallback>>();
pichillilorenzo's avatar
pichillilorenzo committed
116
  bool _isOpened = false;
pichillilorenzo's avatar
pichillilorenzo committed
117 118
  /// WebView Controller that can be used to access the [InAppWebView] API.
  InAppWebViewController webViewController;
119

pichillilorenzo's avatar
pichillilorenzo committed
120 121
  ///
  InAppBrowser () {
122
    uuid = _uuidGenerator.v4();
123
    _ChannelManager.addListener(uuid, _handleMethod);
pichillilorenzo's avatar
pichillilorenzo committed
124
    _isOpened = false;
pichillilorenzo's avatar
pichillilorenzo committed
125
    webViewController = new InAppWebViewController.fromInAppBrowser(uuid, _ChannelManager.channel, this);
pichillilorenzo's avatar
pichillilorenzo committed
126 127 128 129
  }

  Future<dynamic> _handleMethod(MethodCall call) async {
    switch(call.method) {
pichillilorenzo's avatar
pichillilorenzo committed
130 131 132 133
      case "onBrowserCreated":
        this._isOpened = true;
        onBrowserCreated();
        break;
134
      case "onExit":
pichillilorenzo's avatar
pichillilorenzo committed
135
        this._isOpened = false;
pichillilorenzo's avatar
pichillilorenzo committed
136 137
        onExit();
        break;
pichillilorenzo's avatar
pichillilorenzo committed
138
      default:
pichillilorenzo's avatar
pichillilorenzo committed
139
        return webViewController._handleMethod(call);
pichillilorenzo's avatar
pichillilorenzo committed
140 141 142
    }
  }

pichillilorenzo's avatar
pichillilorenzo committed
143
  ///Opens an [url] in a new [InAppBrowser] instance.
pichillilorenzo's avatar
pichillilorenzo committed
144
  ///
145
  ///- [url]: The [url] to load. Call [encodeUriComponent()] on this if the [url] contains Unicode characters. The default value is `about:blank`.
pichillilorenzo's avatar
pichillilorenzo committed
146
  ///
147
  ///- [headers]: The additional headers to be used in the HTTP request for this URL, specified as a map from name to value.
pichillilorenzo's avatar
pichillilorenzo committed
148
  ///
149
  ///- [options]: Options for the [InAppBrowser].
pichillilorenzo's avatar
pichillilorenzo committed
150
  ///
pichillilorenzo's avatar
pichillilorenzo committed
151 152 153 154
  ///  - All platforms support:
  ///    - __useShouldOverrideUrlLoading__: Set to `true` to be able to listen at the [shouldOverrideUrlLoading()] event. The default value is `false`.
  ///    - __useOnLoadResource__: Set to `true` to be able to listen at the [onLoadResource()] event. The default value is `false`.
  ///    - __clearCache__: Set to `true` to have all the browser's cache cleared before the new window is opened. The default value is `false`.
pichillilorenzo's avatar
pichillilorenzo committed
155
  ///    - __userAgent__: Set the custom WebView's user-agent.
pichillilorenzo's avatar
pichillilorenzo committed
156 157 158 159
  ///    - __javaScriptEnabled__: Set to `true` to enable JavaScript. The default value is `true`.
  ///    - __javaScriptCanOpenWindowsAutomatically__: Set to `true` to allow JavaScript open windows without user interaction. The default value is `false`.
  ///    - __hidden__: Set to `true` to create the browser and load the page, but not show it. The `onLoadStop` event fires when loading is complete. Omit or set to `false` (default) to have the browser open and load normally.
  ///    - __toolbarTop__: Set to `false` to hide the toolbar at the top of the WebView. The default value is `true`.
pichillilorenzo's avatar
pichillilorenzo committed
160
  ///    - __toolbarTopBackgroundColor__: Set the custom background color of the toolbar at the top.
pichillilorenzo's avatar
pichillilorenzo committed
161 162
  ///    - __hideUrlBar__: Set to `true` to hide the url bar on the toolbar at the top. The default value is `false`.
  ///    - __mediaPlaybackRequiresUserGesture__: Set to `true` to prevent HTML5 audio or video from autoplaying. The default value is `true`.
pichillilorenzo's avatar
pichillilorenzo committed
163
  ///
pichillilorenzo's avatar
pichillilorenzo committed
164
  ///  - **Android** supports these additional options:
pichillilorenzo's avatar
pichillilorenzo committed
165
  ///
pichillilorenzo's avatar
pichillilorenzo committed
166 167 168 169 170 171 172 173 174 175
  ///    - __hideTitleBar__: Set to `true` if you want the title should be displayed. The default value is `false`.
  ///    - __closeOnCannotGoBack__: Set to `false` to not close the InAppBrowser when the user click on the back button and the WebView cannot go back to the history. The default value is `true`.
  ///    - __clearSessionCache__: Set to `true` to have the session cookie cache cleared before the new window is opened.
  ///    - __builtInZoomControls__: Set to `true` if the WebView should use its built-in zoom mechanisms. The default value is `false`.
  ///    - __supportZoom__: Set to `false` if the WebView should not support zooming using its on-screen zoom controls and gestures. The default value is `true`.
  ///    - __databaseEnabled__: Set to `true` if you want the database storage API is enabled. The default value is `false`.
  ///    - __domStorageEnabled__: Set to `true` if you want the DOM storage API is enabled. The default value is `false`.
  ///    - __useWideViewPort__: Set to `true` if the WebView should enable support for the "viewport" HTML meta tag or should use a wide viewport. When the value of the setting is false, the layout width is always set to the width of the WebView control in device-independent (CSS) pixels. When the value is true and the page contains the viewport meta tag, the value of the width specified in the tag is used. If the page does not contain the tag or does not provide a width, then a wide viewport will be used. The default value is `true`.
  ///    - __safeBrowsingEnabled__: Set to `true` if you want the Safe Browsing is enabled. Safe Browsing allows WebView to protect against malware and phishing attacks by verifying the links. The default value is `true`.
  ///    - __progressBar__: Set to `false` to hide the progress bar at the bottom of the toolbar at the top. The default value is `true`.
pichillilorenzo's avatar
pichillilorenzo committed
176
  ///
pichillilorenzo's avatar
pichillilorenzo committed
177
  ///  - **iOS** supports these additional options:
pichillilorenzo's avatar
pichillilorenzo committed
178
  ///
pichillilorenzo's avatar
pichillilorenzo committed
179 180
  ///    - __disallowOverScroll__: Set to `true` to disable the bouncing of the WebView when the scrolling has reached an edge of the content. The default value is `false`.
  ///    - __toolbarBottom__: Set to `false` to hide the toolbar at the bottom of the WebView. The default value is `true`.
pichillilorenzo's avatar
pichillilorenzo committed
181
  ///    - __toolbarBottomBackgroundColor__: Set the custom background color of the toolbar at the bottom.
pichillilorenzo's avatar
pichillilorenzo committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  ///    - __toolbarBottomTranslucent__: Set to `true` to set the toolbar at the bottom translucent. The default value is `true`.
  ///    - __closeButtonCaption__: Set the custom text for the close button.
  ///    - __closeButtonColor__: Set the custom color for the close button.
  ///    - __presentationStyle__: Set the custom modal presentation style when presenting the WebView. The default value is `0 //fullscreen`. See [UIModalPresentationStyle](https://developer.apple.com/documentation/uikit/uimodalpresentationstyle) for all the available styles.
  ///    - __transitionStyle__: Set to the custom transition style when presenting the WebView. The default value is `0 //crossDissolve`. See [UIModalTransitionStyle](https://developer.apple.com/documentation/uikit/uimodaltransitionStyle) for all the available styles.
  ///    - __enableViewportScale__: Set to `true` to allow a viewport meta tag to either disable or restrict the range of user scaling. The default value is `false`.
  ///    - __suppressesIncrementalRendering__: Set to `true` if you want the WebView suppresses content rendering until it is fully loaded into memory.. The default value is `false`.
  ///    - __allowsAirPlayForMediaPlayback__: Set to `true` to allow AirPlay. The default value is `true`.
  ///    - __allowsBackForwardNavigationGestures__: Set to `true` to allow the horizontal swipe gestures trigger back-forward list navigations. The default value is `true`.
  ///    - __allowsLinkPreview__: Set to `true` to allow that pressing on a link displays a preview of the destination for the link. The default value is `true`.
  ///    - __ignoresViewportScaleLimits__: Set to `true` if you want that the WebView should always allow scaling of the webpage, regardless of the author's intent. The ignoresViewportScaleLimits property overrides the `user-scalable` HTML property in a webpage. The default value is `false`.
  ///    - __allowsInlineMediaPlayback__: Set to `true` to allow HTML5 media playback to appear inline within the screen layout, using browser-supplied controls rather than native controls. For this to work, add the `webkit-playsinline` attribute to any `<video>` elements. The default value is `false`.
  ///    - __allowsPictureInPictureMediaPlayback__: Set to `true` to allow HTML5 videos play picture-in-picture. The default value is `true`.
  ///    - __spinner__: Set to `false` to hide the spinner when the WebView is loading a page. The default value is `true`.
  Future<void> open({String url = "about:blank", Map<String, String> headers = const {}, Map<String, dynamic> options = const {}}) async {
pichillilorenzo's avatar
pichillilorenzo committed
197
    assert(url != null && url.isNotEmpty);
pichillilorenzo's avatar
pichillilorenzo committed
198
    this._throwIsAlreadyOpened(message: 'Cannot open $url!');
pichillilorenzo's avatar
pichillilorenzo committed
199
    Map<String, dynamic> args = <String, dynamic>{};
200
    args.putIfAbsent('uuid', () => uuid);
pichillilorenzo's avatar
pichillilorenzo committed
201
    args.putIfAbsent('url', () => url);
202
    args.putIfAbsent('headers', () => headers);
pichillilorenzo's avatar
pichillilorenzo committed
203
    args.putIfAbsent('options', () => options);
pichillilorenzo's avatar
pichillilorenzo committed
204 205
    args.putIfAbsent('openWithSystemBrowser', () => false);
    args.putIfAbsent('isLocalFile', () => false);
pichillilorenzo's avatar
pichillilorenzo committed
206
    args.putIfAbsent('isData', () => false);
207
    args.putIfAbsent('useChromeSafariBrowser', () => false);
pichillilorenzo's avatar
pichillilorenzo committed
208 209 210
    await _ChannelManager.channel.invokeMethod('open', args);
  }

pichillilorenzo's avatar
pichillilorenzo committed
211
  ///Opens the given [assetFilePath] file in a new [InAppBrowser] instance. The other arguments are the same of [InAppBrowser.open()].
pichillilorenzo's avatar
pichillilorenzo committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
  ///
  ///To be able to load your local files (assets, js, css, etc.), you need to add them in the `assets` section of the `pubspec.yaml` file, otherwise they cannot be found!
  ///
  ///Example of a `pubspec.yaml` file:
  ///```yaml
  ///...
  ///
  ///# The following section is specific to Flutter.
  ///flutter:
  ///
  ///  # The following line ensures that the Material Icons font is
  ///  # included with your application, so that you can use the icons in
  ///  # the material Icons class.
  ///  uses-material-design: true
  ///
  ///  assets:
  ///    - assets/index.html
  ///    - assets/css/
  ///    - assets/images/
  ///
  ///...
  ///```
  ///Example of a `main.dart` file:
  ///```dart
  ///...
pichillilorenzo's avatar
pichillilorenzo committed
237
  ///inAppBrowser.openFile("assets/index.html");
pichillilorenzo's avatar
pichillilorenzo committed
238 239
  ///...
  ///```
pichillilorenzo's avatar
pichillilorenzo committed
240
  Future<void> openFile(String assetFilePath, {Map<String, String> headers = const {}, Map<String, dynamic> options = const {}}) async {
pichillilorenzo's avatar
pichillilorenzo committed
241
    assert(assetFilePath != null && assetFilePath.isNotEmpty);
pichillilorenzo's avatar
pichillilorenzo committed
242
    this._throwIsAlreadyOpened(message: 'Cannot open $assetFilePath!');
pichillilorenzo's avatar
pichillilorenzo committed
243 244 245 246
    Map<String, dynamic> args = <String, dynamic>{};
    args.putIfAbsent('uuid', () => uuid);
    args.putIfAbsent('url', () => assetFilePath);
    args.putIfAbsent('headers', () => headers);
pichillilorenzo's avatar
pichillilorenzo committed
247 248 249
    args.putIfAbsent('options', () => options);
    args.putIfAbsent('openWithSystemBrowser', () => false);
    args.putIfAbsent('isLocalFile', () => true);
pichillilorenzo's avatar
pichillilorenzo committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    args.putIfAbsent('isData', () => false);
    args.putIfAbsent('useChromeSafariBrowser', () => false);
    await _ChannelManager.channel.invokeMethod('open', args);
  }

  ///Opens a new [InAppBrowser] instance with [data] as a content, using [baseUrl] as the base URL for it.
  ///The [mimeType] parameter specifies the format of the data.
  ///The [encoding] parameter specifies the encoding of the data.
  Future<void> openData(String data, {String mimeType = "text/html", String encoding = "utf8", String baseUrl = "about:blank", Map<String, dynamic> options = const {}}) async {
    assert(data != null);
    Map<String, dynamic> args = <String, dynamic>{};
    args.putIfAbsent('uuid', () => uuid);
    args.putIfAbsent('options', () => options);
    args.putIfAbsent('data', () => data);
    args.putIfAbsent('mimeType', () => mimeType);
    args.putIfAbsent('encoding', () => encoding);
    args.putIfAbsent('baseUrl', () => baseUrl);
    args.putIfAbsent('openWithSystemBrowser', () => false);
    args.putIfAbsent('isLocalFile', () => false);
    args.putIfAbsent('isData', () => true);
pichillilorenzo's avatar
pichillilorenzo committed
270 271 272 273 274 275
    args.putIfAbsent('useChromeSafariBrowser', () => false);
    await _ChannelManager.channel.invokeMethod('open', args);
  }

  ///This is a static method that opens an [url] in the system browser. You wont be able to use the [InAppBrowser] methods here!
  static Future<void> openWithSystemBrowser(String url) async {
pichillilorenzo's avatar
pichillilorenzo committed
276
    assert(url != null && url.isNotEmpty);
pichillilorenzo's avatar
pichillilorenzo committed
277 278 279 280 281
    Map<String, dynamic> args = <String, dynamic>{};
    args.putIfAbsent('uuid', () => "");
    args.putIfAbsent('url', () => url);
    args.putIfAbsent('headers', () => {});
    args.putIfAbsent('isLocalFile', () => false);
pichillilorenzo's avatar
pichillilorenzo committed
282
    args.putIfAbsent('isData', () => false);
pichillilorenzo's avatar
pichillilorenzo committed
283 284 285
    args.putIfAbsent('openWithSystemBrowser', () => true);
    args.putIfAbsent('useChromeSafariBrowser', () => false);
    return await _ChannelManager.channel.invokeMethod('open', args);
286 287
  }

pichillilorenzo's avatar
pichillilorenzo committed
288 289
  ///Displays an [InAppBrowser] window that was opened hidden. Calling this has no effect if the [InAppBrowser] was already visible.
  Future<void> show() async {
pichillilorenzo's avatar
pichillilorenzo committed
290
    this._throwIsNotOpened();
291 292
    Map<String, dynamic> args = <String, dynamic>{};
    args.putIfAbsent('uuid', () => uuid);
pichillilorenzo's avatar
pichillilorenzo committed
293
    await _ChannelManager.channel.invokeMethod('show', args);
pichillilorenzo's avatar
pichillilorenzo committed
294 295 296 297
  }

  ///Hides the [InAppBrowser] window. Calling this has no effect if the [InAppBrowser] was already hidden.
  Future<void> hide() async {
pichillilorenzo's avatar
pichillilorenzo committed
298
    this._throwIsNotOpened();
299 300
    Map<String, dynamic> args = <String, dynamic>{};
    args.putIfAbsent('uuid', () => uuid);
pichillilorenzo's avatar
pichillilorenzo committed
301
    await _ChannelManager.channel.invokeMethod('hide', args);
pichillilorenzo's avatar
pichillilorenzo committed
302 303 304 305
  }

  ///Closes the [InAppBrowser] window.
  Future<void> close() async {
pichillilorenzo's avatar
pichillilorenzo committed
306
    this._throwIsNotOpened();
307 308
    Map<String, dynamic> args = <String, dynamic>{};
    args.putIfAbsent('uuid', () => uuid);
pichillilorenzo's avatar
pichillilorenzo committed
309
    await _ChannelManager.channel.invokeMethod('close', args);
pichillilorenzo's avatar
pichillilorenzo committed
310 311
  }

312 313
  ///Check if the Web View of the [InAppBrowser] instance is hidden.
  Future<bool> isHidden() async {
pichillilorenzo's avatar
pichillilorenzo committed
314
    this._throwIsNotOpened();
315 316 317
    Map<String, dynamic> args = <String, dynamic>{};
    args.putIfAbsent('uuid', () => uuid);
    return await _ChannelManager.channel.invokeMethod('isHidden', args);
318 319
  }

pichillilorenzo's avatar
pichillilorenzo committed
320 321
  ///Sets the [InAppBrowser] options with the new [options] and evaluates them.
  Future<void> setOptions(Map<String, dynamic> options) async {
pichillilorenzo's avatar
pichillilorenzo committed
322
    this._throwIsNotOpened();
pichillilorenzo's avatar
pichillilorenzo committed
323
    Map<String, dynamic> args = <String, dynamic>{};
324
    args.putIfAbsent('uuid', () => uuid);
pichillilorenzo's avatar
pichillilorenzo committed
325 326 327
    args.putIfAbsent('options', () => options);
    args.putIfAbsent('optionsType', () => "InAppBrowserOptions");
    await _ChannelManager.channel.invokeMethod('setOptions', args);
pichillilorenzo's avatar
pichillilorenzo committed
328 329
  }

pichillilorenzo's avatar
pichillilorenzo committed
330 331
  ///Gets the current [InAppBrowser] options. Returns `null` if the options are not setted yet.
  Future<Map<String, dynamic>> getOptions() async {
pichillilorenzo's avatar
pichillilorenzo committed
332
    this._throwIsNotOpened();
pichillilorenzo's avatar
pichillilorenzo committed
333
    Map<String, dynamic> args = <String, dynamic>{};
334
    args.putIfAbsent('uuid', () => uuid);
pichillilorenzo's avatar
pichillilorenzo committed
335 336 337 338
    args.putIfAbsent('optionsType', () => "InAppBrowserOptions");
    Map<dynamic, dynamic> options = await _ChannelManager.channel.invokeMethod('getOptions', args);
    options = options.cast<String, dynamic>();
    return options;
339 340
  }

pichillilorenzo's avatar
pichillilorenzo committed
341 342 343
  ///Returns `true` if the [InAppBrowser] instance is opened, otherwise `false`.
  bool isOpened() {
    return this._isOpened;
344 345
  }

pichillilorenzo's avatar
pichillilorenzo committed
346 347 348 349 350
  ///Event fires when the [InAppBrowser] is created.
  void onBrowserCreated() {

  }

pichillilorenzo's avatar
pichillilorenzo committed
351 352 353 354 355 356 357 358 359 360 361
  ///Event fires when the [InAppBrowser] starts to load an [url].
  void onLoadStart(String url) {

  }

  ///Event fires when the [InAppBrowser] finishes loading an [url].
  void onLoadStop(String url) {

  }

  ///Event fires when the [InAppBrowser] encounters an error loading an [url].
362
  void onLoadError(String url, int code, String message) {
pichillilorenzo's avatar
pichillilorenzo committed
363 364 365

  }

pichillilorenzo's avatar
pichillilorenzo committed
366 367 368 369 370
  ///Event fires when the current [progress] (range 0-100) of loading a page is changed.
  void onProgressChanged(int progress) {

  }

pichillilorenzo's avatar
pichillilorenzo committed
371 372 373 374 375
  ///Event fires when the [InAppBrowser] window is closed.
  void onExit() {

  }

376 377 378 379 380
  ///Event fires when the [InAppBrowser] webview receives a [ConsoleMessage].
  void onConsoleMessage(ConsoleMessage consoleMessage) {

  }

pichillilorenzo's avatar
pichillilorenzo committed
381
  ///Give the host application a chance to take control when a URL is about to be loaded in the current WebView.
pichillilorenzo's avatar
pichillilorenzo committed
382
  ///
pichillilorenzo's avatar
pichillilorenzo committed
383 384
  ///**NOTE**: In order to be able to listen this event, you need to set `useShouldOverrideUrlLoading` option to `true`.
  void shouldOverrideUrlLoading(String url) {
pichillilorenzo's avatar
pichillilorenzo committed
385 386 387

  }

pichillilorenzo's avatar
pichillilorenzo committed
388 389 390 391 392 393
  ///Event fires when the [InAppBrowser] webview loads a resource.
  ///
  ///**NOTE**: In order to be able to listen this event, you need to set `useOnLoadResource` option to `true`.
  ///
  ///**NOTE only for iOS**: In some cases, the [response.data] of a [response] with `text/assets` encoding could be empty.
  void onLoadResource(WebResourceResponse response, WebResourceRequest request) {
pichillilorenzo's avatar
pichillilorenzo committed
394

pichillilorenzo's avatar
pichillilorenzo committed
395 396
  }

pichillilorenzo's avatar
pichillilorenzo committed
397 398 399 400 401 402 403
  ///Event fires when the [InAppBrowser] webview scrolls.
  ///[x] represents the current horizontal scroll origin in pixels.
  ///[y] represents the current vertical scroll origin in pixels.
  void onScrollChanged(int x, int y) {

  }

pichillilorenzo's avatar
pichillilorenzo committed
404 405 406 407 408 409 410 411 412 413 414
  void _throwIsAlreadyOpened({String message = ''}) {
    if (this.isOpened()) {
      throw Exception(['Error: ${ (message.isEmpty) ? '' : message + ' '}The browser is already opened.']);
    }
  }

  void _throwIsNotOpened({String message = ''}) {
    if (!this.isOpened()) {
      throw Exception(['Error: ${ (message.isEmpty) ? '' : message + ' '}The browser is not opened.']);
    }
  }
pichillilorenzo's avatar
pichillilorenzo committed
415
}
416 417 418 419 420 421 422 423

///ChromeSafariBrowser class.
///
///This class uses native [Chrome Custom Tabs](https://developer.android.com/reference/android/support/customtabs/package-summary) on Android
///and [SFSafariViewController](https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller) on iOS.
///
///[browserFallback] represents the [InAppBrowser] instance fallback in case [Chrome Custom Tabs]/[SFSafariViewController] is not available.
class ChromeSafariBrowser {
424
  String uuid;
425
  InAppBrowser browserFallback;
pichillilorenzo's avatar
pichillilorenzo committed
426
  bool _isOpened = false;
427

pichillilorenzo's avatar
pichillilorenzo committed
428
  ///Initialize the [ChromeSafariBrowser] instance with an [InAppBrowser] fallback instance or `null`.
429
  ChromeSafariBrowser (bf) {
430
    uuid = _uuidGenerator.v4();
431 432
    browserFallback = bf;
    _ChannelManager.addListener(uuid, _handleMethod);
pichillilorenzo's avatar
pichillilorenzo committed
433
    _isOpened = false;
434 435 436 437 438 439 440 441 442 443 444 445
  }

  Future<dynamic> _handleMethod(MethodCall call) async {
    switch(call.method) {
      case "onChromeSafariBrowserOpened":
        onOpened();
        break;
      case "onChromeSafariBrowserLoaded":
        onLoaded();
        break;
      case "onChromeSafariBrowserClosed":
        onClosed();
pichillilorenzo's avatar
pichillilorenzo committed
446
        this._isOpened = false;
447
        break;
pichillilorenzo's avatar
pichillilorenzo committed
448 449
      default:
        throw UnimplementedError("Unimplemented ${call.method} method");
450 451 452
    }
  }

pichillilorenzo's avatar
pichillilorenzo committed
453
  ///Opens an [url] in a new [ChromeSafariBrowser] instance.
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
  ///
  ///- [url]: The [url] to load. Call [encodeUriComponent()] on this if the [url] contains Unicode characters.
  ///
  ///- [options]: Options for the [ChromeSafariBrowser].
  ///
  ///- [headersFallback]: The additional header of the [InAppBrowser] instance fallback to be used in the HTTP request for this URL, specified as a map from name to value.
  ///
  ///- [optionsFallback]: Options used by the [InAppBrowser] instance fallback.
  ///
  ///**Android** supports these options:
  ///
  ///- __addShareButton__: Set to `false` if you don't want the default share button. The default value is `true`.
  ///- __showTitle__: Set to `false` if the title shouldn't be shown in the custom tab. The default value is `true`.
  ///- __toolbarBackgroundColor__: Set the custom background color of the toolbar.
  ///- __enableUrlBarHiding__: Set to `true` to enable the url bar to hide as the user scrolls down on the page. The default value is `false`.
  ///- __instantAppsEnabled__: Set to `true` to enable Instant Apps. The default value is `false`.
  ///
  ///**iOS** supports these options:
472
  ///
473 474 475 476 477 478 479
  ///- __entersReaderIfAvailable__: Set to `true` if Reader mode should be entered automatically when it is available for the webpage. The default value is `false`.
  ///- __barCollapsingEnabled__: Set to `true` to enable bar collapsing. The default value is `false`.
  ///- __dismissButtonStyle__: Set the custom style for the dismiss button. The default value is `0 //done`. See [SFSafariViewController.DismissButtonStyle](https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller/dismissbuttonstyle) for all the available styles.
  ///- __preferredBarTintColor__: Set the custom background color of the navigation bar and the toolbar.
  ///- __preferredControlTintColor__: Set the custom color of the control buttons on the navigation bar and the toolbar.
  ///- __presentationStyle__: Set the custom modal presentation style when presenting the WebView. The default value is `0 //fullscreen`. See [UIModalPresentationStyle](https://developer.apple.com/documentation/uikit/uimodalpresentationstyle) for all the available styles.
  ///- __transitionStyle__: Set to the custom transition style when presenting the WebView. The default value is `0 //crossDissolve`. See [UIModalTransitionStyle](https://developer.apple.com/documentation/uikit/uimodaltransitionStyle) for all the available styles.
480
  Future<void> open(String url, {Map<String, dynamic> options = const {}, Map<String, String> headersFallback = const {}, Map<String, dynamic> optionsFallback = const {}}) async {
pichillilorenzo's avatar
pichillilorenzo committed
481
    assert(url != null && url.isNotEmpty);
pichillilorenzo's avatar
pichillilorenzo committed
482
    this._throwIsAlreadyOpened(message: 'Cannot open $url!');
483
    Map<String, dynamic> args = <String, dynamic>{};
484 485
    args.putIfAbsent('uuid', () => uuid);
    args.putIfAbsent('uuidFallback', () => (browserFallback != null) ? browserFallback.uuid : '');
486 487 488 489
    args.putIfAbsent('url', () => url);
    args.putIfAbsent('headers', () => headersFallback);
    args.putIfAbsent('options', () => options);
    args.putIfAbsent('optionsFallback', () => optionsFallback);
490
    args.putIfAbsent('isData', () => false);
491
    args.putIfAbsent('useChromeSafariBrowser', () => true);
pichillilorenzo's avatar
pichillilorenzo committed
492 493
    await _ChannelManager.channel.invokeMethod('open', args);
    this._isOpened = true;
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
  }

  ///Event fires when the [ChromeSafariBrowser] is opened.
  void onOpened() {

  }

  ///Event fires when the [ChromeSafariBrowser] is loaded.
  void onLoaded() {

  }

  ///Event fires when the [ChromeSafariBrowser] is closed.
  void onClosed() {

  }
pichillilorenzo's avatar
pichillilorenzo committed
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525

  bool isOpened() {
    return this._isOpened;
  }

  void _throwIsAlreadyOpened({String message = ''}) {
    if (this.isOpened()) {
      throw Exception(['Error: ${ (message.isEmpty) ? '' : message + ' '}The browser is already opened.']);
    }
  }

  void _throwIsNotOpened({String message = ''}) {
    if (!this.isOpened()) {
      throw Exception(['Error: ${ (message.isEmpty) ? '' : message + ' '}The browser is not opened.']);
    }
  }
pichillilorenzo's avatar
pichillilorenzo committed
526 527
}

pichillilorenzo's avatar
pichillilorenzo committed
528 529 530 531 532 533 534 535 536
typedef onWebViewCreatedCallback = void Function(InAppWebViewController controller);
typedef onWebViewLoadStartCallback = void Function(InAppWebViewController controller, String url);
typedef onWebViewLoadStopCallback = void Function(InAppWebViewController controller, String url);
typedef onWebViewLoadErrorCallback = void Function(InAppWebViewController controller, String url, int code, String message);
typedef onWebViewProgressChangedCallback = void Function(InAppWebViewController controller, int progress);
typedef onWebViewConsoleMessageCallback = void Function(InAppWebViewController controller, ConsoleMessage consoleMessage);
typedef shouldOverrideUrlLoadingCallback = void Function(InAppWebViewController controller, String url);
typedef onWebViewLoadResourceCallback = void Function(InAppWebViewController controller, WebResourceResponse response, WebResourceRequest request);
typedef onWebViewScrollChangedCallback = void Function(InAppWebViewController controller, int x, int y);
pichillilorenzo's avatar
pichillilorenzo committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557

///Initial [data] as a content for an [InAppWebView] instance, using [baseUrl] as the base URL for it.
///The [mimeType] property specifies the format of the data.
///The [encoding] property specifies the encoding of the data.
class InAppWebViewInitialData {
  String data;
  String mimeType;
  String encoding;
  String baseUrl;

  InAppWebViewInitialData(this.data, {this.mimeType = "text/html", this.encoding = "utf8", this.baseUrl = "about:blank"});

  Map<String, String> toMap() {
    return {
      "data": data,
      "mimeType": mimeType,
      "encoding": encoding,
      "baseUrl": baseUrl
    };
  }
}
pichillilorenzo's avatar
pichillilorenzo committed
558 559 560 561 562 563 564 565 566

///InAppWebView Widget class.
///
///Flutter Widget for adding an **inline native WebView** integrated in the flutter widget tree.
///
///All platforms support these options:
///  - __useShouldOverrideUrlLoading__: Set to `true` to be able to listen at the [InAppWebView.shouldOverrideUrlLoading()] event. The default value is `false`.
///  - __useOnLoadResource__: Set to `true` to be able to listen at the [InAppWebView.onLoadResource()] event. The default value is `false`.
///  - __clearCache__: Set to `true` to have all the browser's cache cleared before the new window is opened. The default value is `false`.
pichillilorenzo's avatar
pichillilorenzo committed
567
///  - __userAgent___: Set the custom WebView's user-agent.
pichillilorenzo's avatar
pichillilorenzo committed
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
///  - __javaScriptEnabled__: Set to `true` to enable JavaScript. The default value is `true`.
///  - __javaScriptCanOpenWindowsAutomatically__: Set to `true` to allow JavaScript open windows without user interaction. The default value is `false`.
///  - __mediaPlaybackRequiresUserGesture__: Set to `true` to prevent HTML5 audio or video from autoplaying. The default value is `true`.
///
///  **Android** supports these additional options:
///
///  - __clearSessionCache__: Set to `true` to have the session cookie cache cleared before the new window is opened.
///  - __builtInZoomControls__: Set to `true` if the WebView should use its built-in zoom mechanisms. The default value is `false`.
///  - __supportZoom__: Set to `false` if the WebView should not support zooming using its on-screen zoom controls and gestures. The default value is `true`.
///  - __databaseEnabled__: Set to `true` if you want the database storage API is enabled. The default value is `false`.
///  - __domStorageEnabled__: Set to `true` if you want the DOM storage API is enabled. The default value is `false`.
///  - __useWideViewPort__: Set to `true` if the WebView should enable support for the "viewport" HTML meta tag or should use a wide viewport. When the value of the setting is false, the layout width is always set to the width of the WebView control in device-independent (CSS) pixels. When the value is true and the page contains the viewport meta tag, the value of the width specified in the tag is used. If the page does not contain the tag or does not provide a width, then a wide viewport will be used. The default value is `true`.
///  - __safeBrowsingEnabled__: Set to `true` if you want the Safe Browsing is enabled. Safe Browsing allows WebView to protect against malware and phishing attacks by verifying the links. The default value is `true`.
///
///  **iOS** supports these additional options:
///
///  - __disallowOverScroll__: Set to `true` to disable the bouncing of the WebView when the scrolling has reached an edge of the content. The default value is `false`.
///  - __enableViewportScale__: Set to `true` to allow a viewport meta tag to either disable or restrict the range of user scaling. The default value is `false`.
///  - __suppressesIncrementalRendering__: Set to `true` if you want the WebView suppresses content rendering until it is fully loaded into memory.. The default value is `false`.
///  - __allowsAirPlayForMediaPlayback__: Set to `true` to allow AirPlay. The default value is `true`.
///  - __allowsBackForwardNavigationGestures__: Set to `true` to allow the horizontal swipe gestures trigger back-forward list navigations. The default value is `true`.
///  - __allowsLinkPreview__: Set to `true` to allow that pressing on a link displays a preview of the destination for the link. The default value is `true`.
///  - __ignoresViewportScaleLimits__: Set to `true` if you want that the WebView should always allow scaling of the webpage, regardless of the author's intent. The ignoresViewportScaleLimits property overrides the `user-scalable` HTML property in a webpage. The default value is `false`.
///  - __allowsInlineMediaPlayback__: Set to `true` to allow HTML5 media playback to appear inline within the screen layout, using browser-supplied controls rather than native controls. For this to work, add the `webkit-playsinline` attribute to any `<video>` elements. The default value is `false`.
///  - __allowsPictureInPictureMediaPlayback__: Set to `true` to allow HTML5 videos play picture-in-picture. The default value is `true`.
class InAppWebView extends StatefulWidget {

  ///Event fires when the [InAppWebView] is created.
  final onWebViewCreatedCallback onWebViewCreated;

  ///Event fires when the [InAppWebView] starts to load an [url].
  final onWebViewLoadStartCallback onLoadStart;

  ///Event fires when the [InAppWebView] finishes loading an [url].
  final onWebViewLoadStopCallback onLoadStop;

  ///Event fires when the [InAppWebView] encounters an error loading an [url].
  final onWebViewLoadErrorCallback onLoadError;

  ///Event fires when the current [progress] of loading a page is changed.
  final onWebViewProgressChangedCallback onProgressChanged;

  ///Event fires when the [InAppWebView] receives a [ConsoleMessage].
  final onWebViewConsoleMessageCallback onConsoleMessage;

  ///Give the host application a chance to take control when a URL is about to be loaded in the current WebView.
  ///
  ///**NOTE**: In order to be able to listen this event, you need to set `useShouldOverrideUrlLoading` option to `true`.
  final shouldOverrideUrlLoadingCallback shouldOverrideUrlLoading;

  ///Event fires when the [InAppWebView] loads a resource.
  ///
  ///**NOTE**: In order to be able to listen this event, you need to set `useOnLoadResource` option to `true`.
  ///
  ///**NOTE only for iOS**: In some cases, the [response.data] of a [response] with `text/assets` encoding could be empty.
  final onWebViewLoadResourceCallback onLoadResource;

pichillilorenzo's avatar
pichillilorenzo committed
625 626 627 628 629
  ///Event fires when the [InAppWebView] scrolls.
  ///[x] represents the current horizontal scroll origin in pixels.
  ///[y] represents the current vertical scroll origin in pixels.
  final onWebViewScrollChangedCallback onScrollChanged;

pichillilorenzo's avatar
pichillilorenzo committed
630 631 632 633
  ///Initial url that will be loaded.
  final String initialUrl;
  ///Initial asset file that will be loaded. See [InAppWebView.loadFile()] for explanation.
  final String initialFile;
pichillilorenzo's avatar
pichillilorenzo committed
634 635
  ///Initial [InAppWebViewInitialData] that will be loaded.
  final InAppWebViewInitialData initialData;
pichillilorenzo's avatar
pichillilorenzo committed
636 637 638 639 640 641 642 643 644 645
  ///Initial headers that will be used.
  final Map<String, String> initialHeaders;
  ///Initial options that will be used.
  final Map<String, dynamic> initialOptions;
  final Set<Factory<OneSequenceGestureRecognizer>> gestureRecognizers;

  const InAppWebView({
    Key key,
    this.initialUrl = "about:blank",
    this.initialFile,
pichillilorenzo's avatar
pichillilorenzo committed
646
    this.initialData,
pichillilorenzo's avatar
pichillilorenzo committed
647 648 649 650 651 652 653 654 655 656
    this.initialHeaders = const {},
    this.initialOptions = const {},
    this.onWebViewCreated,
    this.onLoadStart,
    this.onLoadStop,
    this.onLoadError,
    this.onConsoleMessage,
    this.onProgressChanged,
    this.shouldOverrideUrlLoading,
    this.onLoadResource,
pichillilorenzo's avatar
pichillilorenzo committed
657
    this.onScrollChanged,
pichillilorenzo's avatar
pichillilorenzo committed
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
    this.gestureRecognizers,
  }) : super(key: key);

  @override
  _InAppWebViewState createState() => _InAppWebViewState();
}

class _InAppWebViewState extends State<InAppWebView> {

  InAppWebViewController _controller;

  @override
  void dispose() {
    super.dispose();
    if (_controller != null) {
      _controller._dispose();
      _controller = null;
    }
  }

  @override
  Widget build(BuildContext context) {
    if (defaultTargetPlatform == TargetPlatform.android) {
      return GestureDetector(
        onLongPress: () {},
        child: AndroidView(
          viewType: 'com.pichillilorenzo/flutter_inappwebview',
          onPlatformViewCreated: _onPlatformViewCreated,
          gestureRecognizers: widget.gestureRecognizers,
          layoutDirection: TextDirection.rtl,
          creationParams: <String, dynamic>{
              'initialUrl': widget.initialUrl,
              'initialFile': widget.initialFile,
pichillilorenzo's avatar
pichillilorenzo committed
691
              'initialData': widget.initialData?.toMap(),
pichillilorenzo's avatar
pichillilorenzo committed
692 693 694 695 696 697
              'initialHeaders': widget.initialHeaders,
              'initialOptions': widget.initialOptions
            },
          creationParamsCodec: const StandardMessageCodec(),
        ),
      );
698 699 700 701 702 703 704 705 706 707 708 709 710 711
    } else if (defaultTargetPlatform == TargetPlatform.iOS) {
      return UiKitView(
        viewType: 'com.pichillilorenzo/flutter_inappwebview',
        onPlatformViewCreated: _onPlatformViewCreated,
        gestureRecognizers: widget.gestureRecognizers,
        creationParams: <String, dynamic>{
          'initialUrl': widget.initialUrl,
          'initialFile': widget.initialFile,
          'initialData': widget.initialData?.toMap(),
          'initialHeaders': widget.initialHeaders,
          'initialOptions': widget.initialOptions
        },
        creationParamsCodec: const StandardMessageCodec(),
      );
pichillilorenzo's avatar
pichillilorenzo committed
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
    }
    return Text(
        '$defaultTargetPlatform is not yet supported by the flutter_inappbrowser plugin');
  }

  @override
  void didUpdateWidget(InAppWebView oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  void _onPlatformViewCreated(int id) {
    _controller = InAppWebViewController(id, widget);
    if (widget.onWebViewCreated != null) {
      widget.onWebViewCreated(_controller);
    }
  }
}

pichillilorenzo's avatar
pichillilorenzo committed
730
/// Controls an [InAppWebView] widget instance.
pichillilorenzo's avatar
pichillilorenzo committed
731
///
pichillilorenzo's avatar
pichillilorenzo committed
732
/// An [InAppWebViewController] instance can be obtained by setting the [InAppWebView.onWebViewCreated]
pichillilorenzo's avatar
pichillilorenzo committed
733 734 735 736 737 738 739 740 741 742 743
/// callback for an [InAppWebView] widget.
class InAppWebViewController {

  InAppWebView _widget;
  MethodChannel _channel;
  Map<String, List<JavaScriptHandlerCallback>> javaScriptHandlersMap = HashMap<String, List<JavaScriptHandlerCallback>>();
  bool _isOpened = false;
  int _id;
  String _inAppBrowserUuid;
  InAppBrowser _inAppBrowser;

744

pichillilorenzo's avatar
pichillilorenzo committed
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
  InAppWebViewController(int id, InAppWebView widget) {
    _id = id;
    _channel = MethodChannel('com.pichillilorenzo/flutter_inappwebview_$id');
    _channel.setMethodCallHandler(_handleMethod);
    _widget = widget;
  }

  InAppWebViewController.fromInAppBrowser(String uuid, MethodChannel channel, InAppBrowser inAppBrowser) {
    _inAppBrowserUuid = uuid;
    _channel = channel;
    _inAppBrowser = inAppBrowser;
  }

  Future<dynamic> _handleMethod(MethodCall call) async {
    switch(call.method) {
      case "onLoadStart":
        String url = call.arguments["url"];
        if (_widget != null)
          _widget.onLoadStart(this, url);
        else
          _inAppBrowser.onLoadStart(url);
        break;
      case "onLoadStop":
        String url = call.arguments["url"];
        if (_widget != null)
          _widget.onLoadStop(this, url);
        else
          _inAppBrowser.onLoadStop(url);
        break;
      case "onLoadError":
        String url = call.arguments["url"];
        int code = call.arguments["code"];
        String message = call.arguments["message"];
        if (_widget != null)
          _widget.onLoadError(this, url, code, message);
        else
          _inAppBrowser.onLoadError(url, code, message);
        break;
      case "onProgressChanged":
        int progress = call.arguments["progress"];
        if (_widget != null)
          _widget.onProgressChanged(this, progress);
        else
          _inAppBrowser.onProgressChanged(progress);
        break;
      case "shouldOverrideUrlLoading":
        String url = call.arguments["url"];
        if (_widget != null)
          _widget.shouldOverrideUrlLoading(this, url);
        else
          _inAppBrowser.shouldOverrideUrlLoading(url);
        break;
      case "onLoadResource":
        Map<dynamic, dynamic> rawResponse = call.arguments["response"];
        rawResponse = rawResponse.cast<String, dynamic>();
        Map<dynamic, dynamic> rawRequest = call.arguments["request"];
        rawRequest = rawRequest.cast<String, dynamic>();

        String urlResponse = rawResponse["url"];
        Map<dynamic, dynamic> headersResponse = rawResponse["headers"];
        headersResponse = headersResponse.cast<String, String>();
        int statusCode = rawResponse["statusCode"];
        int startTime = rawResponse["startTime"];
        int duration = rawResponse["duration"];
        Uint8List data = rawResponse["data"];

        String urlRequest = rawRequest["url"];
        Map<dynamic, dynamic> headersRequest = rawRequest["headers"];
        headersRequest = headersResponse.cast<String, String>();
        String method = rawRequest["method"];

        var response = new WebResourceResponse(urlResponse, headersResponse, statusCode, startTime, duration, data);
        var request = new WebResourceRequest(urlRequest, headersRequest, method);

        if (_widget != null)
          _widget.onLoadResource(this, response, request);
        else
          _inAppBrowser.onLoadResource(response, request);
        break;
      case "onConsoleMessage":
        String sourceURL = call.arguments["sourceURL"];
        int lineNumber = call.arguments["lineNumber"];
        String message = call.arguments["message"];
        ConsoleMessageLevel messageLevel;
        ConsoleMessageLevel.values.forEach((element) {
          if ("ConsoleMessageLevel." + call.arguments["messageLevel"] == element.toString()) {
            messageLevel = element;
            return;
          }
        });
        if (_widget != null)
          _widget.onConsoleMessage(this, ConsoleMessage(sourceURL, lineNumber, message, messageLevel));
        else
          _inAppBrowser.onConsoleMessage(ConsoleMessage(sourceURL, lineNumber, message, messageLevel));
        break;
pichillilorenzo's avatar
pichillilorenzo committed
840 841 842 843 844 845 846 847
      case "onScrollChanged":
        int x = call.arguments["x"];
        int y = call.arguments["y"];
        if (_widget != null)
          _widget.onScrollChanged(this, x, y);
        else
          _inAppBrowser.onScrollChanged(x, y);
        break;
pichillilorenzo's avatar
pichillilorenzo committed
848 849 850 851 852 853 854 855 856 857 858 859 860 861
      case "onCallJsHandler":
        String handlerName = call.arguments["handlerName"];
        List<dynamic> args = jsonDecode(call.arguments["args"]);
        if (javaScriptHandlersMap.containsKey(handlerName)) {
          for (var handler in javaScriptHandlersMap[handlerName]) {
            handler(args);
          }
        }
        break;
      default:
        throw UnimplementedError("Unimplemented ${call.method} method");
    }
  }

pichillilorenzo's avatar
pichillilorenzo committed
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
  ///Gets the URL for the current page.
  ///This is not always the same as the URL passed to [InAppWebView.onLoadStarted] because although the load for that URL has begun, the current page may not have changed.
  Future<String> getUrl() async {
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    return await _channel.invokeMethod('getUrl', args);
  }

  ///Gets the title for the current page.
  Future<String> getTitle() async {
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    return await _channel.invokeMethod('getTitle', args);
  }

  ///Gets the progress for the current page. The progress value is between 0 and 100.
  Future<int> getProgress() async {
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    return await _channel.invokeMethod('getProgress', args);
  }

  ///Gets the favicon for the current page.
  Future<List<int>> getFavicon() async {
    var completer = new Completer<List<int>>();
    var faviconData = new List<int>();
    HttpClient client = new HttpClient();
    var url = Uri.parse(await getUrl());
    // solution found here: https://stackoverflow.com/a/15750809/4637638
    var faviconUrl = Uri.parse("https://plus.google.com/_/favicon?domain_url=" + url.scheme + "://" + url.host);

    client.getUrl(faviconUrl).then((HttpClientRequest request) {
      return request.close();
    }).then((HttpClientResponse response) {
      response.listen((List<int> data) {
        faviconData = data;
      }, onDone: () => completer.complete(faviconData));
    });

    return completer.future;
  }

pichillilorenzo's avatar
pichillilorenzo committed
913 914
  ///Loads the given [url] with optional [headers] specified as a map from name to value.
  Future<void> loadUrl(String url, {Map<String, String> headers = const {}}) async {
pichillilorenzo's avatar
pichillilorenzo committed
915
    assert(url != null && url.isNotEmpty);
pichillilorenzo's avatar
pichillilorenzo committed
916 917 918 919 920 921 922 923 924 925
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened(message: 'Cannot laod $url!');
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    args.putIfAbsent('url', () => url);
    args.putIfAbsent('headers', () => headers);
    await _channel.invokeMethod('loadUrl', args);
  }

pichillilorenzo's avatar
pichillilorenzo committed
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
  ///Loads the given [url] with [postData] using `POST` method into this WebView.
  Future<void> postUrl(String url, Uint8List postData) async {
    assert(url != null && url.isNotEmpty);
    assert(postData != null);
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened(message: 'Cannot laod $url!');
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    args.putIfAbsent('url', () => url);
    args.putIfAbsent('postData', () => postData);
    await _channel.invokeMethod('postUrl', args);
  }

  ///Loads the given [data] into this WebView, using [baseUrl] as the base URL for the content.
  ///The [mimeType] parameter specifies the format of the data.
  ///The [encoding] parameter specifies the encoding of the data.
  Future<void> loadData(String data, {String mimeType = "text/html", String encoding = "utf8", String baseUrl = "about:blank"}) async {
    assert(data != null);
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    args.putIfAbsent('data', () => data);
    args.putIfAbsent('mimeType', () => mimeType);
    args.putIfAbsent('encoding', () => encoding);
    args.putIfAbsent('baseUrl', () => baseUrl);
    await _channel.invokeMethod('loadData', args);
  }

pichillilorenzo's avatar
pichillilorenzo committed
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
  ///Loads the given [assetFilePath] with optional [headers] specified as a map from name to value.
  ///
  ///To be able to load your local files (assets, js, css, etc.), you need to add them in the `assets` section of the `pubspec.yaml` file, otherwise they cannot be found!
  ///
  ///Example of a `pubspec.yaml` file:
  ///```yaml
  ///...
  ///
  ///# The following section is specific to Flutter.
  ///flutter:
  ///
  ///  # The following line ensures that the Material Icons font is
  ///  # included with your application, so that you can use the icons in
  ///  # the material Icons class.
  ///  uses-material-design: true
  ///
  ///  assets:
  ///    - assets/index.html
  ///    - assets/css/
  ///    - assets/images/
  ///
  ///...
  ///```
  ///Example of a `main.dart` file:
  ///```dart
  ///...
  ///inAppBrowser.loadFile("assets/index.html");
  ///...
  ///```
  Future<void> loadFile(String assetFilePath, {Map<String, String> headers = const {}}) async {
pichillilorenzo's avatar
pichillilorenzo committed
987
    assert(assetFilePath != null && assetFilePath.isNotEmpty);
pichillilorenzo's avatar
pichillilorenzo committed
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened(message: 'Cannot laod $assetFilePath!');
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    args.putIfAbsent('url', () => assetFilePath);
    args.putIfAbsent('headers', () => headers);
    await _channel.invokeMethod('loadFile', args);
  }

  ///Reloads the [InAppWebView] window.
  Future<void> reload() async {
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    await _channel.invokeMethod('reload', args);
  }

  ///Goes back in the history of the [InAppWebView] window.
  Future<void> goBack() async {
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    await _channel.invokeMethod('goBack', args);
  }

1018
  ///Returns a boolean value indicating whether the [InAppWebView] can move backward.
pichillilorenzo's avatar
pichillilorenzo committed
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
  Future<bool> canGoBack() async {
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    return await _channel.invokeMethod('canGoBack', args);
  }

  ///Goes forward in the history of the [InAppWebView] window.
  Future<void> goForward() async {
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    await _channel.invokeMethod('goForward', args);
  }

1038
  ///Returns a boolean value indicating whether the [InAppWebView] can move forward.
pichillilorenzo's avatar
pichillilorenzo committed
1039 1040 1041 1042 1043 1044 1045 1046 1047
  Future<bool> canGoForward() async {
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    return await _channel.invokeMethod('canGoForward', args);
  }

1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
  ///Goes to the history item that is the number of steps away from the current item. Steps is negative if backward and positive if forward.
  Future<void> goBackOrForward(int steps) async {
    assert(steps != null);

    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    args.putIfAbsent('steps', () => steps);
    await _channel.invokeMethod('goBackOrForward', args);
  }

1061
  ///Returns a boolean value indicating whether the [InAppWebView] can go back or forward the given number of steps. Steps is negative if backward and positive if forward.
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
  Future<bool> canGoBackOrForward(int steps) async {
    assert(steps != null);

    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    args.putIfAbsent('steps', () => steps);
    return await _channel.invokeMethod('canGoBackOrForward', args);
  }

1074
  ///Navigates to a [WebHistoryItem] from the back-forward [WebHistory.list] and sets it as the current item.
1075 1076 1077 1078
  Future<void> goTo(WebHistoryItem historyItem) async {
    await goBackOrForward(historyItem.offset);
  }

pichillilorenzo's avatar
pichillilorenzo committed
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
  ///Check if the Web View of the [InAppWebView] instance is in a loading state.
  Future<bool> isLoading() async {
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    return await _channel.invokeMethod('isLoading', args);
  }

  ///Stops the Web View of the [InAppWebView] instance from loading.
  Future<void> stopLoading() async {
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    await _channel.invokeMethod('stopLoading', args);
  }

  ///Injects JavaScript code into the [InAppWebView] window and returns the result of the evaluation.
  Future<String> injectScriptCode(String source) async {
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    args.putIfAbsent('source', () => source);
    return await _channel.invokeMethod('injectScriptCode', args);
  }

  ///Injects a JavaScript file into the [InAppWebView] window.
  Future<void> injectScriptFile(String urlFile) async {
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    args.putIfAbsent('urlFile', () => urlFile);
    await _channel.invokeMethod('injectScriptFile', args);
  }

  ///Injects CSS into the [InAppWebView] window.
  Future<void> injectStyleCode(String source) async {
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    args.putIfAbsent('source', () => source);
    await _channel.invokeMethod('injectStyleCode', args);
  }

  ///Injects a CSS file into the [InAppWebView] window.
  Future<void> injectStyleFile(String urlFile) async {
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    args.putIfAbsent('urlFile', () => urlFile);
    await _channel.invokeMethod('injectStyleFile', args);
  }

  ///Adds/Appends a JavaScript message handler [callback] ([JavaScriptHandlerCallback]) that listen to post messages sent from JavaScript by the handler with name [handlerName].
  ///Returns the position `index` of the handler that can be used to remove it with the [removeJavaScriptHandler()] method.
  ///
  ///The Android implementation uses [addJavascriptInterface](https://developer.android.com/reference/android/webkit/WebView#addJavascriptInterface(java.lang.Object,%20java.lang.String)).
  ///The iOS implementation uses [addScriptMessageHandler](https://developer.apple.com/documentation/webkit/wkusercontentcontroller/1537172-addscriptmessagehandler?language=objc)
  ///
  ///The JavaScript function that can be used to call the handler is `window.flutter_inappbrowser.callHandler(handlerName <String>, ...args);`, where `args` are [rest parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters).
  ///The `args` will be stringified automatically using `JSON.stringify(args)` method and then they will be decoded on the Dart side.
  int addJavaScriptHandler(String handlerName, JavaScriptHandlerCallback callback) {
    this.javaScriptHandlersMap.putIfAbsent(handlerName, () => List<JavaScriptHandlerCallback>());
    this.javaScriptHandlersMap[handlerName].add(callback);
    return this.javaScriptHandlersMap[handlerName].indexOf(callback);
  }

  ///Removes a JavaScript message handler previously added with the [addJavaScriptHandler()] method in the [handlerName] list by its position [index].
  ///Returns `true` if the callback is removed, otherwise `false`.
  bool removeJavaScriptHandler(String handlerName, int index) {
    try {
      this.javaScriptHandlersMap[handlerName].removeAt(index);
      return true;
    }
    on RangeError catch(e) {
      print(e);
    }
    return false;
  }

  ///Takes a screenshot (in PNG format) of the WebView's visible viewport and returns a `Uint8List`. Returns `null` if it wasn't be able to take it.
  ///
  ///**NOTE for iOS**: available from iOS 11.0+.
  Future<Uint8List> takeScreenshot() async {
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    return await _channel.invokeMethod('takeScreenshot', args);
  }

  ///Sets the [InAppWebView] options with the new [options] and evaluates them.
  Future<void> setOptions(Map<String, dynamic> options) async {
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    args.putIfAbsent('options', () => options);
    args.putIfAbsent('optionsType', () => "InAppBrowserOptions");
    await _channel.invokeMethod('setOptions', args);
  }

  ///Gets the current [InAppWebView] options. Returns `null` if the options are not setted yet.
  Future<Map<String, dynamic>> getOptions() async {
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    args.putIfAbsent('optionsType', () => "InAppBrowserOptions");
    Map<dynamic, dynamic> options = await _ChannelManager.channel.invokeMethod('getOptions', args);
    options = options.cast<String, dynamic>();
    return options;
  }

1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
  ///Gets the WebHistory for this WebView. This contains the back/forward list for use in querying each item in the history stack.
  ///This contains only a snapshot of the current state.
  ///Multiple calls to this method may return different objects.
  ///The object returned from this method will not be updated to reflect any new state.
  Future<WebHistory> getCopyBackForwardList() async {
    Map<String, dynamic> args = <String, dynamic>{};
    if (_inAppBrowserUuid != null) {
      _inAppBrowser._throwIsNotOpened();
      args.putIfAbsent('uuid', () => _inAppBrowserUuid);
    }
    Map<dynamic, dynamic> result = await _channel.invokeMethod('getCopyBackForwardList', args);
    result = result.cast<String, dynamic>();

    List<dynamic> historyListMap = result["history"];
    historyListMap = historyListMap.cast<LinkedHashMap<dynamic, dynamic>>();

    int currentIndex = result["currentIndex"];

    List<WebHistoryItem> historyList = List();
1226 1227 1228
    for(var i = 0; i < historyListMap.length; i++) {
      LinkedHashMap<dynamic, dynamic> historyItem = historyListMap[i];
      historyList.add(WebHistoryItem(historyItem["originalUrl"], historyItem["title"], historyItem["url"], i, i - currentIndex));
1229 1230 1231 1232
    }
    return WebHistory(historyList, currentIndex);
  }

pichillilorenzo's avatar
pichillilorenzo committed
1233 1234 1235 1236 1237 1238
  Future<void> _dispose() async {
    await _channel.invokeMethod('dispose');
  }

}

1239 1240 1241 1242 1243
///WebHistory class.
///
///This class contains a snapshot of the current back/forward list for a WebView.
class WebHistory {
  List<WebHistoryItem> _list;
1244
  ///List of all [WebHistoryItem]s.
1245
  List<WebHistoryItem> get list => _list;
1246
  ///Index of the current [WebHistoryItem].
1247 1248 1249 1250 1251 1252 1253 1254 1255
  int currentIndex;

  WebHistory(this._list, this.currentIndex);
}

///WebHistoryItem class.
///
///A convenience class for accessing fields in an entry in the back/forward list of a WebView. Each WebHistoryItem is a snapshot of the requested history item.
class WebHistoryItem {
1256
  ///Original url of this history item.
1257
  String originalUrl;
1258
  ///Document title of this history item.
1259
  String title;
1260
  ///Url of this history item.
1261
  String url;
1262 1263 1264 1265
  ///0-based position index in the back-forward [WebHistory.list].
  int index;
  ///Position offset respect to the currentIndex of the back-forward [WebHistory.list].
  int offset;
1266

1267
  WebHistoryItem(this.originalUrl, this.title, this.url, this.index, this.offset);
1268 1269
}

pichillilorenzo's avatar
pichillilorenzo committed
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
///InAppLocalhostServer class.
///
///This class allows you to create a simple server on `http://localhost:[port]/` in order to be able to load your assets file on a server. The default [port] value is `8080`.
class InAppLocalhostServer {

  HttpServer _server;
  int _port = 8080;

  InAppLocalhostServer({int port = 8080}) {
    this._port = port;
  }

  ///Starts a server on http://localhost:[port]/.
  ///
  ///**NOTE for iOS**: For the iOS Platform, you need to add the `NSAllowsLocalNetworking` key with `true` in the `Info.plist` file (See [ATS Configuration Basics](https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW35)):
  ///```xml
  ///<key>NSAppTransportSecurity</key>
  ///<dict>
  ///    <key>NSAllowsLocalNetworking</key>
  ///    <true/>
  ///</dict>
  ///```
  ///The `NSAllowsLocalNetworking` key is available since **iOS 10**.
  Future<void> start() async {

    if (this._server != null) {
      throw Exception('Server already started on http://localhost:$_port');
    }

    var completer = new Completer();

    runZoned(() {
      HttpServer.bind('127.0.0.1', _port).then((server) {
        print('Server running on http://localhost:' + _port.toString());

        this._server = server;

        server.listen((HttpRequest request) async {
          var body = List<int>();
          var path = request.requestedUri.path;
          path = (path.startsWith('/')) ? path.substring(1) : path;
          path += (path.endsWith('/')) ? 'index.html' : '';

          try {
            body = (await rootBundle.load(path))
                .buffer.asUint8List();
          } catch (e) {
            print(e.toString());
            request.response.close();
            return;
          }

          var contentType = ['text', 'html'];
          if (!request.requestedUri.path.endsWith('/') && request.requestedUri.pathSegments.isNotEmpty) {
            var mimeType = lookupMimeType(request.requestedUri.path, headerBytes: body);
            if (mimeType != null) {
              contentType = mimeType.split('/');
            }
          }

          request.response.headers.contentType = new ContentType(contentType[0], contentType[1], charset: 'utf-8');
          request.response.add(body);
          request.response.close();
        });

        completer.complete();
      });
    }, onError: (e, stackTrace) => print('Error: $e $stackTrace'));

    return completer.future;
  }

  ///Closes the server.
  Future<void> close() async {
    if (this._server != null) {
      await this._server.close(force: true);
      print('Server running on http://localhost:$_port closed');
      this._server = null;
    }
  }

1351 1352
}

1353
///Manages the cookies used by WebView instances.
pichillilorenzo's avatar
pichillilorenzo committed
1354 1355
///
///**NOTE for iOS**: available from iOS 11.0+.
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
class CookieManager {
  static bool _initialized = false;
  static const MethodChannel _channel = const MethodChannel('com.pichillilorenzo/flutter_inappbrowser_cookiemanager');

  static void _init () {
    _channel.setMethodCallHandler(_handleMethod);
    _initialized = true;
  }

  static Future<dynamic> _handleMethod(MethodCall call) async {
  }

1368
  ///Sets a cookie for the given [url]. Any existing cookie with the same [host], [path] and [name] will be replaced with the new cookie. The cookie being set will be ignored if it is expired.
1369 1370 1371 1372 1373 1374
  ///
  ///The default value of [path] is `"/"`.
  ///If [domain] is `null`, its default value will be the domain name of [url].
  static Future<void> setCookie(String url, String name, String value,
      { String domain,
        String path = "/",
1375
        int expiresDate,
1376
        int maxAge,
1377 1378 1379 1380
        bool isSecure }) async {
    if (!_initialized)
      _init();

1381 1382 1383
    if (domain == null)
      domain = _getDomainName(url);

1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
    assert(url != null && url.isNotEmpty);
    assert(name != null && name.isNotEmpty);
    assert(value != null && value.isNotEmpty);
    assert(domain != null && domain.isNotEmpty);
    assert(path != null && path.isNotEmpty);

    Map<String, dynamic> args = <String, dynamic>{};
    args.putIfAbsent('url', () => url);
    args.putIfAbsent('name', () => name);
    args.putIfAbsent('value', () => value);
    args.putIfAbsent('domain', () => domain);
    args.putIfAbsent('path', () => path);
1396 1397
    args.putIfAbsent('expiresDate', () => expiresDate.toString());
    args.putIfAbsent('maxAge', () => maxAge);
1398 1399 1400 1401
    args.putIfAbsent('isSecure', () => isSecure);

    await _channel.invokeMethod('setCookie', args);
  }
1402 1403 1404

  ///Gets all the cookies for the given [url].
  static Future<List<Map<String, dynamic>>> getCookies(String url) async {
1405 1406 1407
    if (!_initialized)
      _init();

1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
    assert(url != null && url.isNotEmpty);

    Map<String, dynamic> args = <String, dynamic>{};
    args.putIfAbsent('url', () => url);
    List<dynamic> cookies = await _channel.invokeMethod('getCookies', args);
    cookies = cookies.cast<Map<dynamic, dynamic>>();
    for(var i = 0; i < cookies.length; i++) {
      cookies[i] = cookies[i].cast<String, dynamic>();
    }
    cookies = cookies.cast<Map<String, dynamic>>();
    return cookies;
  }

1421 1422
  ///Gets a cookie by its [name] for the given [url].
  static Future<Map<String, dynamic>> getCookie(String url, String name) async {
1423 1424 1425
    if (!_initialized)
      _init();

1426
    assert(url != null && url.isNotEmpty);
1427
    assert(name != null && name.isNotEmpty);
1428 1429 1430 1431 1432 1433 1434

    Map<String, dynamic> args = <String, dynamic>{};
    args.putIfAbsent('url', () => url);
    List<dynamic> cookies = await _channel.invokeMethod('getCookies', args);
    cookies = cookies.cast<Map<dynamic, dynamic>>();
    for(var i = 0; i < cookies.length; i++) {
      cookies[i] = cookies[i].cast<String, dynamic>();
1435
      if (cookies[i]["name"] == name)
1436 1437 1438 1439
        return cookies[i];
    }
    return null;
  }
1440 1441

  ///Removes a cookie by its [name] for the given [url], [domain] and [path].
pichillilorenzo's avatar
pichillilorenzo committed
1442
  ///
1443 1444 1445
  ///The default value of [path] is `"/"`.
  ///If [domain] is `null` or empty, its default value will be the domain name of [url].
  static Future<void> deleteCookie(String url, String name, {String domain = "", String path = "/"}) async {
1446 1447
    if (!_initialized)
      _init();
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465

    if (domain == null || domain.isEmpty)
      domain = _getDomainName(url);

    assert(url != null && url.isNotEmpty);
    assert(name != null && name.isNotEmpty);
    assert(domain != null && url.isNotEmpty);
    assert(path != null && url.isNotEmpty);

    Map<String, dynamic> args = <String, dynamic>{};
    args.putIfAbsent('url', () => url);
    args.putIfAbsent('name', () => name);
    args.putIfAbsent('domain', () => domain);
    args.putIfAbsent('path', () => path);
    await _channel.invokeMethod('deleteCookie', args);
  }

  ///Removes all cookies for the given [url], [domain] and [path].
pichillilorenzo's avatar
pichillilorenzo committed
1466
  ///
1467 1468 1469
  ///The default value of [path] is `"/"`.
  ///If [domain] is `null` or empty, its default value will be the domain name of [url].
  static Future<void> deleteCookies(String url, {String domain = "", String path = "/"}) async {
1470 1471
    if (!_initialized)
      _init();
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488

    if (domain == null || domain.isEmpty)
      domain = _getDomainName(url);

    assert(url != null && url.isNotEmpty);
    assert(domain != null && url.isNotEmpty);
    assert(path != null && url.isNotEmpty);

    Map<String, dynamic> args = <String, dynamic>{};
    args.putIfAbsent('url', () => url);
    args.putIfAbsent('domain', () => domain);
    args.putIfAbsent('path', () => path);
    await _channel.invokeMethod('deleteCookies', args);
  }

  ///Removes all cookies.
  static Future<void> deleteAllCookies() async {
1489 1490 1491
    if (!_initialized)
      _init();

1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
    Map<String, dynamic> args = <String, dynamic>{};
    await _channel.invokeMethod('deleteAllCookies', args);
  }

  static String _getDomainName(String url) {
    Uri uri = Uri.parse(url);
    String domain = uri.host;
    if (domain == null)
      return "";
    return domain.startsWith("www.") ? domain.substring(4) : domain;
  }
1503
}