Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ cronet_binaries/
# AOT compiled files.
*.exe

*.jar
*.cxx

*.flutter-plugins
*.flutter-plugins-dependencies

*.idea

# Directory for quick experiments.
experiments/

Expand Down
10 changes: 10 additions & 0 deletions .metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: adc687823a831bbebe28bdccfac1a628ca621513
channel: stable

project_type: plugin
59 changes: 59 additions & 0 deletions .pubignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# See https://dart.dev/guides/libraries/private-files

# Files and directories created by pub.
.dart_tool/
.packages
pubspec.lock

# IDE and debugger files.
.clangd
.gdb_history
.history
.vscode
compile_commands.json

# Directory created by dartdoc.
# If you don't generate documentation locally you can remove this line.
doc/api/

# Avoid committing generated Javascript files:
*.dart.js
*.info.json # Produced by the --dump-info flag.
*.js # When generated by dart2js. Don't specify *.js if your
# project includes source files written in JavaScript.
*.js_
*.js.deps
*.js.map

# Cronet pre-built binaries
cronet_binaries/

# Generated shared libraries.
*.so
*.so.*
*.dylib
*.dll

# AOT compiled files.
*.exe

*.jar
*.cxx

# Jar dependencies should be published to pub
!android/libs/*

# Cronet binaries for android should be published to pub
!android/src/main/jniLibs/**

*.flutter-plugins
*.flutter-plugins-dependencies

*.idea

# Directory for quick experiments.
experiments/

# Files generated by tests for debugging purposes.
test/debug_generated/*
!test/debug_generated/readme.md
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.0.1

* TODO: Describe initial release.
70 changes: 25 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Ported from: https://chromium.googlesource.com/chromium/src/+/master/components/

[![Build Status](https://github.com/unsuitable001/dart_cronet_sample/workflows/Dart%20CI/badge.svg)](https://github.com/unsuitable001/dart_cronet_sample/actions?query=workflow%3A"Dart+CI")

Checkout the Flutter version with Android support: https://github.com/unsuitable001/flutter_cronet_sample
~~Checkout the Flutter version with Android support: https://github.com/unsuitable001/flutter_cronet_sample~~

Flutter (Android/Linux) support is now merged in this repository.

## Usage

Expand All @@ -15,15 +17,29 @@ Checkout the Flutter version with Android support: https://github.com/unsuitable
```pubspec
dependencies:
cronet_sample:
git: https://github.com/unsuitable001/dart_cronet_sample.git
git:
url: https://github.com/unsuitable001/dart_cronet_sample.git

```

2. Run this from the `root` of your project

Desktop Platforms

```bash
pub get
pub run cronet_sample
pub run cronet_sample <platform>
```
Supported platforms: `linux64`


Mobile Platforms (Flutter)

```bash
flutter pub get
```
Binaries aren't checked into git. They will be published on pub though. Meanwhile,
they can be downloaded from `Releases` section.

3. Import

Expand Down Expand Up @@ -85,49 +101,13 @@ Copy the cronet's binary to the `cronet_binaries/<platform><arch>` folder from p
From the root of the repo, run

```bash
dart run
dart example/main.dart
```

### Output

You'll get the HTML page of `example.com` along with some other texts (I used them for lazy & easy debugging).


![example.com output](/output.png?raw=true "Screenshot")

## Comparison

I compared this with `dart:io` library's http client. I ran tests 5 times, repeatitively (same machine, same network, same site - http://info.cern.ch/). The results are given below -

cd example
flutter run
```
Round 1:
cronet implemenation took: 385 ms
dart:io implemenation took: 351 ms

Round 2:
cronet implemenation took: 382 ms
dart:io implemenation took: 345 ms

Round 3:
cronet implemenation took: 378 ms
dart:io implemenation took: 349 ms

Round 4:
cronet implemenation took: 385 ms
dart:io implemenation took: 363 ms

Round 5:
cronet implemenation took: 385 ms
dart:io implemenation took: 359 ms
```

Well, now `cronet` based solution is marginally slower than `dart:io`. Making the API similar to `dart:io` surely added some overhead, like - receiving message from C side and then forwarding that data to another stream.

### Compare Yourself

*Run from root of the project*
or

```bash
./benchmark/benchmark.sh
```
cd example_dart
dart run
```
3 changes: 3 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ analyzer:
strong-mode:
implicit-casts: false
implicit-dynamic: false
exclude:
- example/**
- lib/cronet_sample.dart # due to method channel

linter:
rules:
Expand Down
8 changes: 8 additions & 0 deletions android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
18 changes: 18 additions & 0 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.10) # for example
project(flutter_cronet)
add_library(wrapper

# Sets the library as a shared library.
SHARED

# Provides a relative path to your source file(s).
"../lib/src/native/wrapper/wrapper.cc"
"../lib/src/native/wrapper/wrapper.h"
"../lib/src/native/wrapper/sample_executor.cc"
"../lib/src/native/wrapper/sample_executor.h"
"../lib/src/native/include/dart_api_dl.c"
)

include_directories(../lib/src/native/include/)

add_compile_options(-fPIC -ldl -rdynamic -DDART_SHARED_LIB -fpermissive)
52 changes: 52 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
group 'com.example.cronet_sample'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

rootProject.allprojects {
repositories {
google()
jcenter()
}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 30

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
externalNativeBuild {
// Encapsulates your CMake build configurations.
cmake {
// Provides a relative path to your CMake build script.
path "CMakeLists.txt"
}
}

defaultConfig {
minSdkVersion 16
ndk {
abiFilters 'arm64-v8a'
}
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation files(fileTree(dir: 'libs', includes: ['*.jar']))
}
3 changes: 3 additions & 0 deletions android/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
5 changes: 5 additions & 0 deletions android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
1 change: 1 addition & 0 deletions android/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'cronet_sample'
5 changes: 5 additions & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cronet_sample">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.cronet_sample

import androidx.annotation.NonNull

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar

import org.chromium.base.ContextUtils

/** CronetSamplePlugin */
class CronetSamplePlugin: FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private lateinit var channel : MethodChannel

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
System.loadLibrary("cronet.91.0.4456.0")
System.loadLibrary("wrapper")
ContextUtils.initApplicationContext(flutterPluginBinding.applicationContext)
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "cronet_sample")
channel.setMethodCallHandler(this)
}

override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (call.method == "getPlatformVersion") {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
} else {
result.notImplemented()
}
}

override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
}
16 changes: 0 additions & 16 deletions benchmark/benchmark.sh

This file was deleted.

21 changes: 0 additions & 21 deletions benchmark/http_based.dart

This file was deleted.

10 changes: 7 additions & 3 deletions bin/cronet_sample.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import 'package:cronet_sample/src/prepare_cronet.dart';

void main(List<String> arguments) async {
buildWrapper();
downloadCronetBinaries(['linux64']);
void main(List<String> platforms) {
platforms.forEach((platform) async {
if (platform.startsWith('linux')) {
buildWrapper();
}
await downloadCronetBinaries(platform);
});
}
Loading