1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.net.impl;
6 
7 import android.content.Context;
8 import android.net.http.HttpEngine;
9 import android.os.Build;
10 import android.os.ext.SdkExtensions;
11 
12 import androidx.annotation.RequiresExtension;
13 
14 import org.chromium.net.CronetEngine;
15 import org.chromium.net.CronetProvider;
16 import org.chromium.net.ExperimentalCronetEngine;
17 
18 /**
19  * A Cronet provider implementation which loads the HttpEngine implementation from the Android
20  * platform.
21  *
22  * <p>Note that the httpengine native provider doesn't provide functionality which was deemed to be
23  *  too implementation specific, namely access to the netlog and internal metrics. Additionally,
24  * support for experimental features is not guaranteed (as with any other Cronet provider).
25  */
26 public class HttpEngineNativeProvider extends CronetProvider {
27     /**
28      * String returned by {@link CronetProvider#getName} for {@link CronetProvider} that provides
29      * Cronet implementation based on the HttpEngine implementation present in the Platform. This
30      * implementation doesn't provide functionality which was deemed to be implementation specific,
31      * namely access to the netlog and internal metrics. Additionally, support for experimental
32      * features is not guaranteed (as with any other Cronet provider).
33      */
34     // TODO(crbug.com/40287946): Move this to CronetProvider
35     public static final String PROVIDER_NAME_HTTPENGINE_NATIVE = "HttpEngine-Native-Provider";
36 
37     static final int EXT_API_LEVEL = Build.VERSION_CODES.S;
38     static final int EXT_VERSION = 7;
39 
HttpEngineNativeProvider(Context context)40     public HttpEngineNativeProvider(Context context) {
41         super(context);
42     }
43 
44     @Override
45     @RequiresExtension(extension = EXT_API_LEVEL, version = EXT_VERSION)
createBuilder()46     public CronetEngine.Builder createBuilder() {
47         return new ExperimentalCronetEngine.Builder(
48                 new AndroidHttpEngineBuilderWrapper(new HttpEngine.Builder(mContext)));
49     }
50 
51     @Override
getName()52     public String getName() {
53         return PROVIDER_NAME_HTTPENGINE_NATIVE;
54     }
55 
56     @Override
57     @RequiresExtension(extension = EXT_API_LEVEL, version = EXT_VERSION)
getVersion()58     public String getVersion() {
59         return HttpEngine.getVersionString();
60     }
61 
62     @Override
isEnabled()63     public boolean isEnabled() {
64         return Build.VERSION.SDK_INT >= Build.VERSION_CODES.R
65                 && SdkExtensions.getExtensionVersion(EXT_API_LEVEL) >= EXT_VERSION;
66     }
67 }
68