build static method

CronetEngine build({
  1. CacheMode? cacheMode,
  2. int? cacheMaxSize,
  3. bool? enableBrotli,
  4. bool? enableHttp2,
  5. bool? enablePublicKeyPinningBypassForLocalTrustAnchors,
  6. bool? enableQuic,
  7. String? storagePath,
  8. String? userAgent,
})

Construct a new CronetEngine with the given configuration.

cacheMode controls the type of caching that should be used by the engine. If cacheMode is not CacheMode.disabled then cacheMaxSize must be set. If cacheMode is CacheMode.disk or CacheMode.diskNoHttp then storagePath must be set.

cacheMaxSize is the maximum amount of data that should be cached, in bytes.

enableBrotli controls whether Brotli compression can be used.

enableHttp2 controls whether the HTTP/2 protocol can be used.

enablePublicKeyPinningBypassForLocalTrustAnchors enables or disables public key pinning bypass for local trust anchors. Disabling the bypass for local trust anchors is highly discouraged since it may prohibit the app from communicating with the pinned hosts. E.g., a user may want to send all traffic through an SSL enabled proxy by changing the device proxy settings and adding the proxy certificate to the list of local trust anchor.

enableQuic controls whether the QUIC protocol can be used.

storagePath sets the path of an existing directory where HTTP data can be cached and where cookies can be stored. NOTE: a unique storagePath should be used per CronetEngine.

userAgent controls the User-Agent header.

Implementation

static CronetEngine build(
    {CacheMode? cacheMode,
    int? cacheMaxSize,
    bool? enableBrotli,
    bool? enableHttp2,
    bool? enablePublicKeyPinningBypassForLocalTrustAnchors,
    bool? enableQuic,
    String? storagePath,
    String? userAgent}) {
  final builder = jb.CronetEngine_Builder(
      JObject.fromReference(Jni.getCachedApplicationContext()));

  try {
    if (storagePath != null) {
      builder.setStoragePath(storagePath.toJString());
    }

    if (cacheMode == CacheMode.disabled) {
      builder.enableHttpCache(0, 0); // HTTP_CACHE_DISABLED, 0 bytes
    } else if (cacheMode != null && cacheMaxSize != null) {
      builder.enableHttpCache(cacheMode.index, cacheMaxSize);
    }

    if (enableBrotli != null) {
      builder.enableBrotli(enableBrotli);
    }

    if (enableHttp2 != null) {
      builder.enableHttp2(enableHttp2);
    }

    if (enablePublicKeyPinningBypassForLocalTrustAnchors != null) {
      builder.enablePublicKeyPinningBypassForLocalTrustAnchors(
          enablePublicKeyPinningBypassForLocalTrustAnchors);
    }

    if (enableQuic != null) {
      builder.enableQuic(enableQuic);
    }

    if (userAgent != null) {
      builder.setUserAgent(userAgent.toJString());
    }

    return CronetEngine._(builder.build());
  } on JniException catch (e) {
    // TODO: Decode this exception in a better way when
    // https://github.com/dart-lang/jnigen/issues/239 is fixed.
    if (e.message.contains('java.lang.IllegalArgumentException:')) {
      throw ArgumentError(
          e.message.split('java.lang.IllegalArgumentException:').last);
    }
    rethrow;
  }
}