initMethodCallHandler method

void initMethodCallHandler()

Implementation

void initMethodCallHandler() {
  methodChannel.setMethodCallHandler((call) async {
    //log("[webview] native->flutter: $call");
    int? webviewId = call.arguments["webviewId"];
    assert(webviewId != null);
    final ref = webviewMap[webviewId];
    if (ref == null) {
      log("webview not found: id = $webviewId");
      return;
    }

    final controller = ref.target;
    if (controller == null) {
      webviewMap.remove(webviewId);
      log("webview is alive but not referenced anymore: id = $webviewId");
      return;
    }

    if (call.method == "OnWebMessageReceived") {
      // NOTE: only linux pass 'JkChannelName'.
      //       Windows put 'JkChannelName' in 'message' json string
      String? channelName = call.arguments["JkChannelName"];
      String message = call.arguments["message"]!;

      var jobj = json.decode(message);
      if (channelName != null) {
        // only for Linux, not for Windows
        jobj = {"JkChannelName": channelName, "msg": jobj};
      }
      controller.notifyMessageReceived_(jobj);
    } else if (call.method == "onNavigationRequest") {
      int requestId = call.arguments["requestId"]!;
      String url = call.arguments["url"]!;
      bool isNewWindow = call.arguments["isNewWindow"]!;
      controller.notifyOnNavigationRequest_(requestId, url, isNewWindow);
    } else if (call.method == "onPageStarted") {
      String url = call.arguments["url"]!;
      controller.notifyOnPageStarted_(url);
    } else if (call.method == "onPageFinished") {
      String url = call.arguments["url"]!;
      controller.notifyOnPageFinished_(url);
    } else if (call.method == "onHttpError") {
      String url = call.arguments["url"]!;
      int errCode = call.arguments["errCode"]!;
      controller.notifyOnHttpError_(url, errCode);
    } else if (call.method == "onSslAuthError") {
      String url = call.arguments["url"]!;
      controller.notifyOnSslAuthError_(url);
    } else if (call.method == "onWebResourceError") {
      String url = call.arguments["url"]!;
      int errCode = call.arguments["errCode"]!;
      String errType = call.arguments["errType"]!;
      var error = WebResourceError(
          url: url,
          errorCode: errCode,
          description: errType,
          errorType: WebResourceErrorType.values.byName(errType));
      controller.notifyOnWebResourceError_(error);
    } else if (call.method == "onUrlChange") {
      String url = call.arguments["url"]!;
      controller.notifyOnUrlChange_(url);
    } else if (call.method == "onPageTitleChanged") {
      String title = call.arguments["title"]!;
      controller.notifyOnPageTitleChanged_(title);
    } else if (call.method == "onMoveFocusRequest") {
      bool isNext = call.arguments["isNext"]!;
      controller.notifyOnFocusRequest_(isNext);
    } else if (call.method == "OnFullScreenChanged") {
      bool? isFullScreen = call.arguments["isFullScreen"];
      assert(isFullScreen != null);
      controller.notifyFullScreenChanged_(isFullScreen!);
    } else if (call.method == "onHistoryChanged") {
      controller.notifyHistoryChanged_();
    } else if (call.method == "onAskPermission") {
      String url = call.arguments["url"]!;
      int kind = call.arguments["kind"]!;
      int deferralId = call.arguments["deferralId"]!;
      controller.notifyAskPermission_(
        url,
        WinWebViewPermissionResourceType.values[kind],
        deferralId,
      );
    } else {
      assert(false, "unknown call from native: ${call.method}");
    }
  });
}