Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion components/cronet/android/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public class org.chromium.net.CronetEngine$Builder {
public org.chromium.net.CronetEngine$Builder setDnsOptions(org.chromium.net.DnsOptions$Builder);
public org.chromium.net.CronetEngine$Builder setDnsOptions(org.chromium.net.DnsOptions);
public org.chromium.net.CronetEngine$Builder setLibraryLoader(org.chromium.net.CronetEngine$Builder$LibraryLoader);
public org.chromium.net.CronetEngine$Builder setProxyUrl(java.lang.String);
public org.chromium.net.CronetEngine$Builder setQuicOptions(org.chromium.net.QuicOptions$Builder);
public org.chromium.net.CronetEngine$Builder setQuicOptions(org.chromium.net.QuicOptions);
public org.chromium.net.CronetEngine$Builder setStoragePath(java.lang.String);
Expand Down Expand Up @@ -323,6 +324,7 @@ public abstract class org.chromium.net.ICronetEngineBuilder {
public abstract org.chromium.net.ICronetEngineBuilder enableSdch(boolean);
public abstract org.chromium.net.ICronetEngineBuilder setExperimentalOptions(java.lang.String);
public abstract org.chromium.net.ICronetEngineBuilder setLibraryLoader(org.chromium.net.CronetEngine$Builder$LibraryLoader);
public abstract org.chromium.net.ICronetEngineBuilder setProxyUrl(java.lang.String);
public abstract org.chromium.net.ICronetEngineBuilder setStoragePath(java.lang.String);
public abstract org.chromium.net.ICronetEngineBuilder setUserAgent(java.lang.String);
public org.chromium.net.ICronetEngineBuilder enableBrotli(boolean);
Expand Down Expand Up @@ -640,4 +642,4 @@ public class org.chromium.net.apihelpers.UrlRequestCallbacks {
public static org.chromium.net.apihelpers.UrlRequestCallbacks$CallbackAndResponseFuturePair<java.lang.String, org.chromium.net.apihelpers.StringCronetCallback> forStringBody(org.chromium.net.apihelpers.RedirectHandler);
public static org.chromium.net.apihelpers.UrlRequestCallbacks$CallbackAndResponseFuturePair<org.json.JSONObject, org.chromium.net.apihelpers.JsonCronetCallback> forJsonBody(org.chromium.net.apihelpers.RedirectHandler);
}
Stamp: ded30b9fbe22dff6cc175cd483aa1e4b
Stamp: 77c7e1b163bf63684a4a0fedc6505679
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ public abstract static class LibraryLoader {
/** Reference to the actual builder implementation. {@hide exclude from JavaDoc}. */
protected final ICronetEngineBuilder mBuilderDelegate;

/**
* Sets a proxy URL for the Engine. The proxy will be used for all requests
*
* @param proxyUrl URL of the proxy to use
*/
public Builder setProxyUrl(String proxyUrl) {
mBuilderDelegate.setProxyUrl(proxyUrl);
return this;
}

/**
* Constructs a {@link Builder} object that facilitates creating a {@link CronetEngine}. The
* default configuration enables HTTP/2 and QUIC, but disables the HTTP cache.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public abstract ICronetEngineBuilder setLibraryLoader(

public abstract ICronetEngineBuilder setUserAgent(String userAgent);

public abstract ICronetEngineBuilder setProxyUrl(String proxy_url);

public abstract String getDefaultUserAgent();

public abstract ExperimentalCronetEngine build();
Expand Down
2 changes: 1 addition & 1 deletion components/cronet/android/api_version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
36
37
11 changes: 11 additions & 0 deletions components/cronet/android/cronet_context_adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ void CronetContextAdapter::InitRequestContextOnInitThread(
context_->InitRequestContextOnInitThread();
}

void CronetContextAdapter::InitRequestContextOnInitThreadWithUri(
JNIEnv* env,
const JavaParamRef<jobject>& jcaller,
const JavaParamRef<jstring>& juri) {
jcronet_url_request_context_.Reset(env, jcaller);
std::string uri(
base::android::ConvertJavaStringToUTF8(env, juri));
context_->InitRequestContextOnInitThreadWithUri(uri);
}

void CronetContextAdapter::ConfigureNetworkQualityEstimatorForTesting(
JNIEnv* env,
const JavaParamRef<jobject>& jcaller,
Expand Down Expand Up @@ -243,6 +253,7 @@ static jlong JNI_CronetUrlRequestContext_CreateRequestContextConfig(
configOptions.http_cache_max_size(), configOptions.disable_cache(),
configOptions.storage_path(),
/* accept_languages */ std::string(), configOptions.user_agent(),
configOptions.proxy_url(),
configOptions.experimental_options(),
base::WrapUnique(reinterpret_cast<net::CertVerifier*>(
configOptions.mock_cert_verifier())),
Expand Down
8 changes: 8 additions & 0 deletions components/cronet/android/cronet_context_adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class CronetContextAdapter : public CronetContext::Callback {
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller);

void InitRequestContextOnInitThreadWithUri(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller,
const base::android::JavaParamRef<jstring>& juri);

// Releases all resources for the request context and deletes the object.
// Blocks until network thread is destroyed after running all pending tasks.
void Destroy(JNIEnv* env,
Expand Down Expand Up @@ -98,6 +103,9 @@ class CronetContextAdapter : public CronetContext::Callback {
// Called on init Java thread to initialize URLRequestContext.
void InitRequestContextOnInitThread();

// Called on init Java thread to initialize URLRequestContext with a proxy.
void InitRequestContextOnInitThreadWithUri(const base::android::JavaParamRef<jstring>& juri);

// Configures the network quality estimator to observe requests to localhost,
// to use smaller responses when estimating throughput, and to disable the
// device offline checks when computing the effective connection type or when
Expand Down
9 changes: 9 additions & 0 deletions components/cronet/android/cronet_library_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,15 @@ std::unique_ptr<net::ProxyConfigService> CreateProxyConfigService(
return service;
}

// Creates a proxy config service with a fixed proxy URL.
std::unique_ptr<net::ProxyConfigService> CreateFixedProxyConfigService(
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, std::string_view uri) {
std::unique_ptr<net::ProxyConfigService> service =
net::ProxyConfigService::CreateFixedSystemProxyConfigService(
io_task_runner, uri);
return service;
}

// Creates a proxy resolution service appropriate for this platform.
std::unique_ptr<net::ProxyResolutionService> CreateProxyResolutionService(
std::unique_ptr<net::ProxyConfigService> proxy_config_service,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public ICronetEngineBuilder setUserAgent(String userAgent) {
return this;
}

@Override
public ICronetEngineBuilder setProxyUrl(String proxyUrl) {
// HttpEngine.Builder doesn't have this method
// mBackend.setProxyUrl(proxyUrl);
return this;
}

@Override
public ICronetEngineBuilder setStoragePath(String value) {
mBackend.setStoragePath(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ static HttpCacheMode fromPublicBuilderCacheMode(@HttpCacheSetting int cacheMode)
private final CronetSource mSource;
private boolean mPublicKeyPinningBypassForLocalTrustAnchorsEnabled;
private String mUserAgent;
private String mProxyUrl;
private String mStoragePath;
private boolean mQuicEnabled;
private boolean mHttp2Enabled;
Expand Down Expand Up @@ -226,6 +227,16 @@ String getUserAgent() {
return mUserAgent;
}

@Override
public CronetEngineBuilderImpl setProxyUrl(String proxyUrl) {
mProxyUrl = proxyUrl;
return this;
}

public String getProxyUrl() {
return mProxyUrl;
}

@Override
public CronetEngineBuilderImpl setStoragePath(String value) {
if (!new File(value).isDirectory()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,18 @@ public void run() {
// mUrlRequestContextAdapter is guaranteed to exist until
// initialization on init and network threads completes and
// initNetworkThread is called back on network thread.
CronetUrlRequestContextJni.get()
.initRequestContextOnInitThread(
mUrlRequestContextAdapter,
CronetUrlRequestContext.this);
if (builder.getProxyUrl() != null && builder.getProxyUrl().startsWith("socks5://")) {
CronetUrlRequestContextJni.get()
.initRequestContextOnInitThreadWithUri(
mUrlRequestContextAdapter,
CronetUrlRequestContext.this,
builder.getProxyUrl());
} else {
CronetUrlRequestContextJni.get()
.initRequestContextOnInitThread(
mUrlRequestContextAdapter,
CronetUrlRequestContext.this);
}
}
}

Expand Down Expand Up @@ -447,6 +455,10 @@ private static RequestContextConfigOptions createRequestContextConfigOptions(
resultBuilder.setExperimentalOptions(engineBuilder.experimentalOptions());
}

if (engineBuilder.getProxyUrl() != null) {
resultBuilder.setProxyUrl(engineBuilder.getProxyUrl());
}

return resultBuilder.build();
}

Expand Down Expand Up @@ -1098,6 +1110,9 @@ void addPkp(
@NativeClassQualifiedName("CronetContextAdapter")
void destroy(long nativePtr, CronetUrlRequestContext caller);

@NativeClassQualifiedName("CronetContextAdapter")
void initRequestContextOnInitThreadWithUri(long nativePtr, CronetUrlRequestContext caller, String uri);

@NativeClassQualifiedName("CronetContextAdapter")
boolean startNetLogToFile(
long nativePtr, CronetUrlRequestContext caller, String fileName, boolean logAll);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ message RequestContextConfigOptions {
optional bool enable_network_quality_estimator = 12;
optional bool bypass_public_key_pinning_for_local_trust_anchors = 13;
optional int32 network_thread_priority = 14;
optional string proxy_url = 15;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public void onCreate() {
myBuilder
.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_IN_MEMORY, 100 * 1024)
.enableHttp2(true)
.setProxyUrl("socks5://127.0.0.1:1080")
.enableQuic(true);
mCronetEngine = myBuilder.build();
}
Expand Down Expand Up @@ -62,6 +63,7 @@ public void restartCronetEngine() {
.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_IN_MEMORY, 100 * 1024)
.enableHttp2(true)
.enableQuic(true)
.setProxyUrl("socks5://127.0.0.1:1080")
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ public String getDefaultUserAgent() {
throw new UnsupportedOperationException();
}

@Override
public ICronetEngineBuilder setProxyUrl(String value) {
throw new UnsupportedOperationException();
}

@Override
public ICronetEngineBuilder setConnectionMigrationOptions(
ConnectionMigrationOptions options) {
Expand Down
13 changes: 13 additions & 0 deletions components/cronet/cronet_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,19 @@ CronetContext::NetworkTasks::~NetworkTasks() {
net::NetworkChangeNotifier::RemoveNetworkObserver(this);
}

void CronetContext::InitRequestContextOnInitThreadWithUri(std::string_view uri) {
DCHECK(OnInitThread());
auto proxy_config_service =
cronet::CreateFixedProxyConfigService(GetNetworkTaskRunner(), uri);
g_net_log.Get().EnsureInitializedOnInitThread();
GetNetworkTaskRunner()->PostTask(
FROM_HERE,
base::BindOnce(&CronetContext::NetworkTasks::Initialize,
base::Unretained(network_tasks_), GetNetworkTaskRunner(),
GetFileThread()->task_runner(),
std::move(proxy_config_service)));
}

void CronetContext::InitRequestContextOnInitThread() {
DCHECK(OnInitThread());
// Cannot create this inside Initialize because Android requires this to be
Expand Down
2 changes: 2 additions & 0 deletions components/cronet/cronet_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ class CronetContext {
// Blocks until network thread is destroyed after running all pending tasks.
virtual ~CronetContext();

void InitRequestContextOnInitThreadWithUri(std::string_view uri);

// Called on init thread to initialize URLRequestContext.
void InitRequestContextOnInitThread();

Expand Down
3 changes: 3 additions & 0 deletions components/cronet/cronet_global_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ void PostTaskToInitThread(const base::Location& posted_from,
// idempotent, and must complete initialization before returning.
void EnsureInitialized();

std::unique_ptr<net::ProxyConfigService> CreateFixedProxyConfigService(
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, std::string_view uri);

// Creates a proxy config service appropriate for this platform that fetches the
// system proxy settings. Cronet will call this API only after a prior call
// to EnsureInitialized() has returned.
Expand Down
6 changes: 6 additions & 0 deletions components/cronet/cronet_global_state_stubs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ void PostTaskToInitThread(const base::Location& posted_from,
InitTaskRunner()->PostTask(posted_from, std::move(task));
}

std::unique_ptr<net::ProxyConfigService> CreateFixedProxyConfigService(
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, std::string_view uri) {
return net::ProxyConfigService::CreateFixedSystemProxyConfigService(
io_task_runner, uri);
}

std::unique_ptr<net::ProxyConfigService> CreateProxyConfigService(
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) {
return net::ProxyConfigService::CreateSystemProxyConfigService(
Expand Down
6 changes: 6 additions & 0 deletions components/cronet/native/cronet.idl
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,12 @@ struct EngineParams {
*/
string user_agent;

/**
* Use an HTTP(S) or SOCKS proxy. Setting this will use the proxy for all
* connections
*/
string proxy_url;

/**
* Sets a default value for the Accept-Language header value for UrlRequests
* created by this engine. Explicitly setting the Accept-Language header
Expand Down
16 changes: 12 additions & 4 deletions components/cronet/native/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ Cronet_RESULT Cronet_EngineImpl::StartWithParams(
context_config_builder.storage_path = params->storage_path;
context_config_builder.accept_language = params->accept_language;
context_config_builder.user_agent = params->user_agent;
context_config_builder.proxy_url = params->proxy_url;
context_config_builder.experimental_options = params->experimental_options;
context_config_builder.bypass_public_key_pinning_for_local_trust_anchors =
params->enable_public_key_pinning_bypass_for_local_trust_anchors;
Expand Down Expand Up @@ -199,10 +200,17 @@ Cronet_RESULT Cronet_EngineImpl::StartWithParams(
// private and mark CronetLibraryLoader.postToInitThread() as
// @VisibleForTesting (as the only external use will be in a test).

// Initialize context on the init thread.
cronet::PostTaskToInitThread(
FROM_HERE, base::BindOnce(&CronetContext::InitRequestContextOnInitThread,
base::Unretained(context_.get())));

// Initialize context on the init thread, potentially with a proxy.
if (params->proxy_url.empty()) {
cronet::PostTaskToInitThread(
FROM_HERE, base::BindOnce(&CronetContext::InitRequestContextOnInitThread,
base::Unretained(context_.get())));
} else {
cronet::PostTaskToInitThread(
FROM_HERE, base::BindOnce(&CronetContext::InitRequestContextOnInitThreadWithUri,
base::Unretained(context_.get()), params->proxy_url));
}
return CheckResult(Cronet_RESULT_SUCCESS);
}

Expand Down
6 changes: 6 additions & 0 deletions components/cronet/native/generated/cronet.idl_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,9 @@ CRONET_EXPORT
void Cronet_EngineParams_user_agent_set(Cronet_EngineParamsPtr self,
const Cronet_String user_agent);
CRONET_EXPORT
void Cronet_EngineParams_proxy_url_set(Cronet_EngineParamsPtr self,
const Cronet_String proxy_url);
CRONET_EXPORT
void Cronet_EngineParams_accept_language_set(
Cronet_EngineParamsPtr self,
const Cronet_String accept_language);
Expand Down Expand Up @@ -845,6 +848,9 @@ CRONET_EXPORT
Cronet_String Cronet_EngineParams_user_agent_get(
const Cronet_EngineParamsPtr self);
CRONET_EXPORT
Cronet_String Cronet_EngineParams_proxy_url_get(
const Cronet_EngineParamsPtr self);
CRONET_EXPORT
Cronet_String Cronet_EngineParams_accept_language_get(
const Cronet_EngineParamsPtr self);
CRONET_EXPORT
Expand Down
12 changes: 12 additions & 0 deletions components/cronet/native/generated/cronet.idl_impl_struct.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ void Cronet_EngineParams_user_agent_set(Cronet_EngineParamsPtr self,
self->user_agent = user_agent;
}

void Cronet_EngineParams_proxy_url_set(Cronet_EngineParamsPtr self,
const Cronet_String proxy_url) {
DCHECK(self);
self->proxy_url = proxy_url;
}

void Cronet_EngineParams_accept_language_set(
Cronet_EngineParamsPtr self,
const Cronet_String accept_language) {
Expand Down Expand Up @@ -342,6 +348,12 @@ Cronet_String Cronet_EngineParams_user_agent_get(
return self->user_agent.c_str();
}

Cronet_String Cronet_EngineParams_proxy_url_get(
const Cronet_EngineParamsPtr self) {
DCHECK(self);
return self->proxy_url.c_str();
}

Cronet_String Cronet_EngineParams_accept_language_get(
const Cronet_EngineParamsPtr self) {
DCHECK(self);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ struct Cronet_EngineParams {

bool enable_check_result = true;
std::string user_agent;
std::string proxy_url;
std::string accept_language;
std::string storage_path;
bool enable_quic = true;
Expand Down
1 change: 1 addition & 0 deletions components/cronet/native/sample/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Cronet_EnginePtr CreateCronetEngine() {
Cronet_EnginePtr cronet_engine = Cronet_Engine_Create();
Cronet_EngineParamsPtr engine_params = Cronet_EngineParams_Create();
Cronet_EngineParams_user_agent_set(engine_params, "CronetSample/1");
Cronet_EngineParams_proxy_url_set(engine_params, "socks5://127.0.0.1:1080");
Cronet_EngineParams_enable_quic_set(engine_params, true);

Cronet_Engine_StartWithParams(cronet_engine, engine_params);
Expand Down
Loading