Commit 59cb6968 authored by bengr's avatar bengr Committed by Commit bot

Add Data Saver support to Cronet

Adds support for the Data Saver compression proxy to the Cronet
library.

BUG=461910

Review URL: https://codereview.chromium.org/937513003

Cr-Commit-Position: refs/heads/master@{#329674}
parent 4b59cf7c
......@@ -108,6 +108,7 @@
'DISABLE_FTP_SUPPORT=1',
],
'dependencies': [
'../components/components.gyp:data_reduction_proxy_core_browser_small',
'../net/net.gyp:net_small',
],
'includes': [ 'cronet/cronet_static.gypi' ],
......@@ -117,6 +118,7 @@
'target_name': 'cronet_static',
'dependencies': [
'../base/base.gyp:base_i18n',
'../components/components.gyp:data_reduction_proxy_core_browser',
'../net/net.gyp:net',
],
'includes': [ 'cronet/cronet_static.gypi' ],
......
include_rules = [
"+components/data_reduction_proxy",
"+components/metrics",
"+jni",
]
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/cronet/android/cronet_data_reduction_proxy.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/prefs/pref_registry_simple.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/pref_service_factory.h"
#include "base/single_thread_task_runner.h"
#include "components/cronet/android/cronet_in_memory_pref_store.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_switches.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_interceptor.h"
namespace cronet {
namespace {
scoped_ptr<PrefService> CreatePrefService() {
scoped_refptr<PrefRegistrySimple> pref_registry(new PrefRegistrySimple());
data_reduction_proxy::RegisterSimpleProfilePrefs(pref_registry.get());
base::PrefServiceFactory pref_service_factory;
pref_service_factory.set_user_prefs(
make_scoped_refptr(new CronetInMemoryPrefStore()));
scoped_ptr<PrefService> pref_service =
pref_service_factory.Create(pref_registry.get()).Pass();
pref_registry = nullptr;
return pref_service.Pass();
}
// TODO(bengr): Apply test configurations directly, instead of via the
// command line.
void AddOptionsToCommandLine(const std::string& primary_proxy,
const std::string& fallback_proxy,
const std::string& secure_proxy_check_url,
base::CommandLine* command_line) {
DCHECK((primary_proxy.empty() && fallback_proxy.empty() &&
secure_proxy_check_url.empty()) ||
(!primary_proxy.empty() && !fallback_proxy.empty() &&
!secure_proxy_check_url.empty()));
if (primary_proxy.empty())
return;
command_line->AppendSwitchASCII(
data_reduction_proxy::switches::kDataReductionProxy, primary_proxy);
command_line->AppendSwitchASCII(
data_reduction_proxy::switches::kDataReductionProxyFallback,
fallback_proxy);
command_line->AppendSwitchASCII(
data_reduction_proxy::switches::kDataReductionProxySecureProxyCheckURL,
secure_proxy_check_url);
}
} // namespace
CronetDataReductionProxy::CronetDataReductionProxy(
const std::string& key,
const std::string& primary_proxy,
const std::string& fallback_proxy,
const std::string& secure_proxy_check_url,
const std::string& user_agent,
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
net::NetLog* net_log)
: task_runner_(task_runner) {
DCHECK(task_runner_->BelongsToCurrentThread());
AddOptionsToCommandLine(primary_proxy, fallback_proxy, secure_proxy_check_url,
base::CommandLine::ForCurrentProcess());
prefs_ = CreatePrefService();
// In Cronet, the Data Reduction Proxy's UI classes are Created on Cronet's
// network thread.
settings_.reset(
new data_reduction_proxy::DataReductionProxySettings());
io_data_.reset(
new data_reduction_proxy::DataReductionProxyIOData(
data_reduction_proxy::Client::CRONET_ANDROID,
data_reduction_proxy::DataReductionProxyParams::kAllowed |
data_reduction_proxy::DataReductionProxyParams::kFallbackAllowed,
net_log, task_runner, task_runner, false, false, user_agent));
io_data_->request_options()->SetKeyOnIO(key);
}
CronetDataReductionProxy::~CronetDataReductionProxy() {
io_data_->ShutdownOnUIThread();
}
scoped_ptr<net::NetworkDelegate>
CronetDataReductionProxy::CreateNetworkDelegate(
scoped_ptr<net::NetworkDelegate> wrapped_network_delegate) {
return io_data_->CreateNetworkDelegate(wrapped_network_delegate.Pass(),
false /* No bypass UMA */ );
}
scoped_ptr<net::URLRequestInterceptor>
CronetDataReductionProxy::CreateInterceptor() {
return io_data_->CreateInterceptor();
}
void CronetDataReductionProxy::Init(bool enable,
net::URLRequestContext* context) {
url_request_context_getter_ =
new net::TrivialURLRequestContextGetter(
context, task_runner_);
scoped_ptr<data_reduction_proxy::DataReductionProxyCompressionStats>
compression_stats(
new data_reduction_proxy::DataReductionProxyCompressionStats(
prefs_.get(), task_runner_, base::TimeDelta()));
scoped_ptr<data_reduction_proxy::DataReductionProxyService>
data_reduction_proxy_service(
new data_reduction_proxy::DataReductionProxyService(
compression_stats.Pass(), settings_.get(),
url_request_context_getter_.get(), task_runner_));
io_data_->SetDataReductionProxyService(
data_reduction_proxy_service->GetWeakPtr());
settings_->InitDataReductionProxySettings(
prefs_.get(), io_data_.get(), data_reduction_proxy_service.Pass());
settings_->SetDataReductionProxyEnabled(enable);
settings_->MaybeActivateDataReductionProxy(true);
}
} // namespace cronet
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_CRONET_ANDROID_CRONET_DATA_REDUCTION_PROXY_H_
#define COMPONENTS_CRONET_ANDROID_CRONET_DATA_REDUCTION_PROXY_H_
#include <string>
#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
class PrefService;
namespace base {
class SingleThreadTaskRunner;
}
namespace data_reduction_proxy {
class DataReductionProxyIOData;
class DataReductionProxySettings;
}
namespace net {
class NetLog;
class NetworkDelegate;
class URLRequestContext;
class URLRequestContextGetter;
class URLRequestInterceptor;
}
namespace cronet {
// Wrapper and configurator of Data Reduction Proxy objects for Cronet. It
// configures the Data Reduction Proxy to run both its UI and IO classes on
// Cronet's network thread.
class CronetDataReductionProxy {
public:
// Construct Data Reduction Proxy Settings and IOData objects and set
// the authentication key. The |task_runner| should be suitable for running
// tasks on the network thread. The primary proxy, fallback proxy, and secure
// proxy check url can override defaults. All or none must be specified.
CronetDataReductionProxy(
const std::string& key,
const std::string& primary_proxy,
const std::string& fallback_proxy,
const std::string& secure_proxy_check_url,
const std::string& user_agent,
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
net::NetLog* net_log);
~CronetDataReductionProxy();
// Constructs a network delegate suitable for adding Data Reduction Proxy
// request headers.
scoped_ptr<net::NetworkDelegate> CreateNetworkDelegate(
scoped_ptr<net::NetworkDelegate> wrapped_network_delegate);
// Constructs a URLRequestInterceptor suitable for carrying out the Data
// Reduction Proxy's bypass protocol.
scoped_ptr<net::URLRequestInterceptor> CreateInterceptor();
// Constructs a bridge between the Settings and IOData objects, sets up a
// context for secure proxy check requests, and enables the proxy, if
// |enable| is true.
void Init(bool enable, net::URLRequestContext* context);
private:
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
scoped_ptr<PrefService> prefs_;
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
scoped_ptr<data_reduction_proxy::DataReductionProxySettings> settings_;
scoped_ptr<data_reduction_proxy::DataReductionProxyIOData> io_data_;
DISALLOW_COPY_AND_ASSIGN(CronetDataReductionProxy);
};
} // namespace cronet
#endif // COMPONENTS_CRONET_ANDROID_CRONET_DATA_REDUCTION_PROXY_H_
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/cronet/android/cronet_in_memory_pref_store.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
CronetInMemoryPrefStore::CronetInMemoryPrefStore() {}
CronetInMemoryPrefStore::~CronetInMemoryPrefStore() {}
bool CronetInMemoryPrefStore::GetValue(const std::string& key,
const base::Value** value) const {
return prefs_.GetValue(key, value);
}
bool CronetInMemoryPrefStore::GetMutableValue(const std::string& key,
base::Value** value) {
return prefs_.GetValue(key, value);
}
void CronetInMemoryPrefStore::AddObserver(PrefStore::Observer* observer) {
observers_.AddObserver(observer);
}
void CronetInMemoryPrefStore::RemoveObserver(PrefStore::Observer* observer) {
observers_.RemoveObserver(observer);
}
bool CronetInMemoryPrefStore::HasObservers() const {
return observers_.might_have_observers();
}
bool CronetInMemoryPrefStore::IsInitializationComplete() const {
return true;
}
void CronetInMemoryPrefStore::SetValue(
const std::string& key, base::Value* value, uint32 flags) {
DCHECK(value);
if (prefs_.SetValue(key, value))
ReportValueChanged(key, flags);
}
void CronetInMemoryPrefStore::SetValueSilently(const std::string& key,
base::Value* value,
uint32 flags) {
prefs_.SetValue(key, value);
}
void CronetInMemoryPrefStore::RemoveValue(const std::string& key,
uint32 flags) {
if (prefs_.RemoveValue(key))
ReportValueChanged(key, flags);
}
bool CronetInMemoryPrefStore::ReadOnly() const {
return false;
}
PersistentPrefStore::PrefReadError
CronetInMemoryPrefStore::GetReadError() const {
return PersistentPrefStore::PREF_READ_ERROR_NONE;
}
PersistentPrefStore::PrefReadError CronetInMemoryPrefStore::ReadPrefs() {
return PersistentPrefStore::PREF_READ_ERROR_NONE;
}
void CronetInMemoryPrefStore::ReadPrefsAsync(
ReadErrorDelegate* error_delegate_raw) {
}
void CronetInMemoryPrefStore::ReportValueChanged(const std::string& key,
uint32 flags) {
FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(key));
}
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_CRONET_ANDROID_CRONET_IN_MEMORY_PREF_STORE_H_
#define COMPONENTS_CRONET_ANDROID_CRONET_IN_MEMORY_PREF_STORE_H_
#include <string>
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/observer_list.h"
#include "base/prefs/persistent_pref_store.h"
#include "base/prefs/pref_value_map.h"
namespace base {
class Value;
}
// A light-weight prefstore implementation that keeps preferences
// in a memory backed store. This is not a persistent prefstore.
// TODO(bengr): Move to base/prefs or some other shared location.
class CronetInMemoryPrefStore : public PersistentPrefStore {
public:
CronetInMemoryPrefStore();
// PrefStore overrides:
bool GetValue(const std::string& key,
const base::Value** result) const override;
void AddObserver(PrefStore::Observer* observer) override;
void RemoveObserver(PrefStore::Observer* observer) override;
bool HasObservers() const override;
bool IsInitializationComplete() const override;
// PersistentPrefStore overrides:
bool GetMutableValue(const std::string& key, base::Value** result) override;
void ReportValueChanged(const std::string& key, uint32 flags) override;
void SetValue(const std::string& key,
base::Value* value,
uint32 flags) override;
void SetValueSilently(const std::string& key,
base::Value* value,
uint32 flags) override;
void RemoveValue(const std::string& key, uint32 flags) override;
bool ReadOnly() const override;
PrefReadError GetReadError() const override;
PersistentPrefStore::PrefReadError ReadPrefs() override;
void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override;
void CommitPendingWrite() override {}
private:
~CronetInMemoryPrefStore() override;
// Stores the preference values.
PrefValueMap prefs_;
ObserverList<PrefStore::Observer, true> observers_;
DISALLOW_COPY_AND_ASSIGN(CronetInMemoryPrefStore);
};
#endif // COMPONENTS_CRONET_ANDROID_CRONET_IN_MEMORY_PREF_STORE_H_
......@@ -12,6 +12,7 @@
#include "base/android/jni_registrar.h"
#include "base/android/jni_utils.h"
#include "base/android/library_loader/library_loader_hooks.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "components/cronet/android/chromium_url_request.h"
......@@ -98,6 +99,9 @@ void CronetInitOnMainThread(JNIEnv* env, jclass jcaller) {
base::i18n::InitializeICU();
#endif
// TODO(bengr): Remove once Data Reduction Proxy no longer needs this for
// configuration information.
base::CommandLine::Init(0, nullptr);
DCHECK(!base::MessageLoop::current());
DCHECK(!g_main_message_loop);
g_main_message_loop = new base::MessageLoopForUI();
......
......@@ -226,6 +226,14 @@ CronetURLRequestAdapter::GetNegotiatedProtocol(JNIEnv* env,
env, url_request_->response_info().npn_negotiated_protocol);
}
base::android::ScopedJavaLocalRef<jstring>
CronetURLRequestAdapter::GetProxyServer(JNIEnv* env,
jobject jcaller) const {
DCHECK(context_->IsOnNetworkThread());
return ConvertUTF8ToJavaString(
env, url_request_->response_info().proxy_server.ToString());
}
jboolean CronetURLRequestAdapter::GetWasCached(JNIEnv* env,
jobject jcaller) const {
DCHECK(context_->IsOnNetworkThread());
......
......@@ -104,6 +104,11 @@ class CronetURLRequestAdapter : public net::URLRequest::Delegate {
JNIEnv* env,
jobject jcaller) const;
// Returns the host and port of the proxy server, if one was used.
base::android::ScopedJavaLocalRef<jstring> GetProxyServer(
JNIEnv* env,
jobject jcaller) const;
// Returns true if response is coming from the cache.
jboolean GetWasCached(JNIEnv* env, jobject jcaller) const;
......
......@@ -10,8 +10,10 @@
#include "base/files/file_util.h"
#include "base/files/scoped_file.h"
#include "base/logging.h"
#include "base/memory/scoped_vector.h"
#include "base/single_thread_task_runner.h"
#include "base/values.h"
#include "components/cronet/android/cronet_data_reduction_proxy.h"
#include "components/cronet/url_request_context_config.h"
#include "jni/CronetUrlRequestContext_jni.h"
#include "net/base/load_flags.h"
......@@ -23,6 +25,7 @@
#include "net/sdch/sdch_owner.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_builder.h"
#include "net/url_request/url_request_interceptor.h"
namespace {
......@@ -151,10 +154,32 @@ void CronetURLRequestContextAdapter::InitializeOnNetworkThread(
DCHECK(proxy_config_service_);
// TODO(mmenke): Add method to have the builder enable SPDY.
net::URLRequestContextBuilder context_builder;
context_builder.set_network_delegate(new BasicNetworkDelegate());
scoped_ptr<net::NetLog> net_log(new net::NetLog);
scoped_ptr<net::NetworkDelegate> network_delegate(new BasicNetworkDelegate());
DCHECK(!data_reduction_proxy_);
// For now, the choice to enable the data reduction proxy happens once,
// at initialization. It cannot be disabled thereafter.
if (!config->data_reduction_proxy_key.empty()) {
data_reduction_proxy_.reset(
new CronetDataReductionProxy(
config->data_reduction_proxy_key,
config->data_reduction_primary_proxy,
config->data_reduction_fallback_proxy,
config->data_reduction_secure_proxy_check_url,
config->user_agent,
GetNetworkTaskRunner(),
net_log.get()));
network_delegate =
data_reduction_proxy_->CreateNetworkDelegate(network_delegate.Pass());
ScopedVector<net::URLRequestInterceptor> interceptors;
interceptors.push_back(data_reduction_proxy_->CreateInterceptor());
context_builder.SetInterceptors(interceptors.Pass());
}
context_builder.set_network_delegate(network_delegate.release());
context_builder.set_net_log(net_log.release());
context_builder.set_proxy_config_service(proxy_config_service_.release());
config->ConfigureURLRequestContextBuilder(&context_builder);
context_.reset(context_builder.Build());
default_load_flags_ = net::LOAD_DO_NOT_SAVE_COOKIES |
......@@ -216,6 +241,8 @@ void CronetURLRequestContextAdapter::InitializeOnNetworkThread(
Java_CronetUrlRequestContext_initNetworkThread(
env, jcronet_url_request_context.obj());
if (data_reduction_proxy_)
data_reduction_proxy_->Init(true, GetURLRequestContext());
is_context_initialized_ = true;
while (!tasks_waiting_for_context_.empty()) {
tasks_waiting_for_context_.front().Run();
......
......@@ -30,6 +30,7 @@ class SdchOwner;
namespace cronet {
class CronetDataReductionProxy;
struct URLRequestContextConfig;
bool CronetUrlRequestContextAdapterRegisterJni(JNIEnv* env);
......@@ -103,6 +104,7 @@ class CronetURLRequestContextAdapter {
std::queue<base::Closure> tasks_waiting_for_context_;
bool is_context_initialized_;
int default_load_flags_;
scoped_ptr<CronetDataReductionProxy> data_reduction_proxy_;
DISALLOW_COPY_AND_ASSIGN(CronetURLRequestContextAdapter);
};
......
......@@ -107,17 +107,19 @@ final class CronetUrlRequest implements UrlRequest {
private final HeadersList mAllHeaders = new HeadersList();
private final boolean mWasCached;
private final String mNegotiatedProtocol;
private final String mProxyServer;
private Map<String, List<String>> mResponseHeaders;
private List<Pair<String, String>> mUnmodifiableAllHeaders;
NativeResponseInfo(String[] urlChain, int httpStatusCode,
String httpStatusText, boolean wasCached,
String negotiatedProtocol) {
String negotiatedProtocol, String proxyServer) {
mResponseInfoUrlChain = urlChain;
mHttpStatusCode = httpStatusCode;
mHttpStatusText = httpStatusText;
mWasCached = wasCached;
mNegotiatedProtocol = negotiatedProtocol;
mProxyServer = proxyServer;
}
@Override
......@@ -177,6 +179,11 @@ final class CronetUrlRequest implements UrlRequest {
public String getNegotiatedProtocol() {
return mNegotiatedProtocol;
}
@Override
public String getProxyServer() {
return mProxyServer;
}
};
static final class NativeExtendedResponseInfo implements
......@@ -421,7 +428,8 @@ final class CronetUrlRequest implements UrlRequest {
httpStatusCode,
nativeGetHttpStatusText(urlRequestAdapter),
nativeGetWasCached(urlRequestAdapter),
nativeGetNegotiatedProtocol(urlRequestAdapter));
nativeGetNegotiatedProtocol(urlRequestAdapter),
nativeGetProxyServer(urlRequestAdapter));
nativePopulateResponseHeaders(urlRequestAdapter,
responseInfo.mAllHeaders);
return responseInfo;
......@@ -701,6 +709,9 @@ final class CronetUrlRequest implements UrlRequest {
@NativeClassQualifiedName("CronetURLRequestAdapter")
private native String nativeGetNegotiatedProtocol(long nativePtr);
@NativeClassQualifiedName("CronetURLRequestAdapter")
private native String nativeGetProxyServer(long nativePtr);
@NativeClassQualifiedName("CronetURLRequestAdapter")
private native boolean nativeGetWasCached(long nativePtr);
}
......@@ -68,4 +68,9 @@ public interface ResponseInfo {
* and without a revalidation request.
*/
String getNegotiatedProtocol();
/**
* @return the proxy server that was used for the request.
*/
String getProxyServer();
};
......@@ -108,6 +108,41 @@ public class UrlRequestContextConfig {
return putBoolean(UrlRequestContextConfigList.ENABLE_SDCH, value);
}
/**
* String, key to use when authenticating with the proxy.
*/
public UrlRequestContextConfig enableDataReductionProxy(String key) {
return (putString(
UrlRequestContextConfigList.DATA_REDUCTION_PROXY_KEY, key));
}
/**
* Overrides Data Reduction Proxy configuration parameters with a primary
* proxy name, fallback proxy name, and a secure proxy check url. Proxies
* are specified as [scheme://]host[:port]. Used for testing.
* @param primaryProxy The primary data reduction proxy to use.
* @param fallbackProxy A fallback data reduction proxy to use.
* @param secureProxyCheckUrl A url to fetch to determine if using a secure
* proxy is allowed.
*/
public UrlRequestContextConfig setDataReductionProxyOptions(
String primaryProxy,
String fallbackProxy,
String secureProxyCheckUrl) {
if (primaryProxy.isEmpty() || fallbackProxy.isEmpty()
|| secureProxyCheckUrl.isEmpty()) {
throw new IllegalArgumentException(
"Primary and fallback proxies and check url must be set");
}
putString(UrlRequestContextConfigList.DATA_REDUCTION_PRIMARY_PROXY,
primaryProxy);
putString(UrlRequestContextConfigList.DATA_REDUCTION_FALLBACK_PROXY,
fallbackProxy);
putString(UrlRequestContextConfigList
.DATA_REDUCTION_SECURE_PROXY_CHECK_URL, secureProxyCheckUrl);
return this;
}
/**
* Enumeration, disable or enable cache in memory or on disk.
*/
......
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 19
header-name: header-value
Via: 1.1 Chrome-Compression-Proxy
......@@ -103,6 +103,53 @@ public class CronetUrlRequestContextTest extends CronetTestBase {
assertEquals(userAgentValue, listener.mResponseAsString);
}
@SmallTest
@Feature({"Cronet"})
public void testDataReductionProxyEnabled() throws Exception {
mActivity = launchCronetTestAppAndSkipFactoryInit();
// Ensure native code is loaded before trying to start test server.
UrlRequestContext.createContext(
getInstrumentation().getTargetContext(),
new UrlRequestContextConfig().setLibraryName("cronet_tests"))
.shutdown();
assertTrue(NativeTestServer.startNativeTestServer(
getInstrumentation().getTargetContext()));
String serverHostPort = NativeTestServer.getHostPort();
// Enable the Data Reduction Proxy and configure it to use the test
// server as its primary proxy, and to check successfully that this
// proxy is OK to use.
UrlRequestContextConfig config = new UrlRequestContextConfig();
config.enableDataReductionProxy("test-key");
config.setDataReductionProxyOptions(
serverHostPort, "unused.net:9999",
NativeTestServer.getFileURL("/secureproxychecksuccess.txt"));
config.setLibraryName("cronet_tests");
mActivity.mUrlRequestContext =
mActivity.mUrlRequestContext.createContext(
getInstrumentation().getTargetContext(), config);
TestUrlRequestListener listener = new TestUrlRequestListener();
// Construct and start a request that can only be returned by the test
// server. This request will fail if the configuration logic for the
// Data Reduction Proxy is not used.
UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest(
"http://google.com/datareductionproxysuccess.txt",
listener, listener.getExecutor());
urlRequest.start();
listener.blockForDone();
// Verify that the request is successful and that the Data Reduction
// Proxy logic configured to use the test server as its proxy.
assertEquals(200, listener.mResponseInfo.getHttpStatusCode());
assertEquals(serverHostPort, listener.mResponseInfo.getProxyServer());
assertEquals("http://www.google.com/datareductionproxysuccess.txt",
listener.mResponseInfo.getUrl());
}
@SmallTest
@Feature({"Cronet"})
public void testShutdown() throws Exception {
......
......@@ -20,6 +20,7 @@
#include "components/cronet/android/cronet_url_request_context_adapter.h"
#include "components/cronet/android/url_request_context_adapter.h"
#include "jni/NativeTestServer_jni.h"
#include "net/base/host_port_pair.h"
#include "net/base/url_util.h"
#include "net/dns/host_resolver_impl.h"
#include "net/dns/mock_host_resolver.h"
......@@ -298,6 +299,13 @@ jstring GetSdchURL(JNIEnv* env, jclass jcaller) {
return base::android::ConvertUTF8ToJavaString(env, url).Release();
}
jstring GetHostPort(JNIEnv* env, jclass jcaller) {
DCHECK(g_test_server);
std::string host_port =
net::HostPortPair::FromURL(g_test_server->base_url()).ToString();
return base::android::ConvertUTF8ToJavaString(env, host_port).Release();
}
bool RegisterNativeTestServer(JNIEnv* env) {
return RegisterNativesImpl(env);
}
......
......@@ -91,6 +91,10 @@ public final class NativeTestServer {
return nativeGetFileURL("/notfound.html");
}
public static String getHostPort() {
return nativeGetHostPort();
}
@CalledByNative
private static void onHostResolverProcRegistered() {
sHostResolverBlock.open();
......@@ -107,4 +111,5 @@ public final class NativeTestServer {
private static native String nativeGetRedirectToEchoBody();
private static native String nativeGetFileURL(String filePath);
private static native String nativeGetSdchURL();
private static native String nativeGetHostPort();
}
......@@ -21,10 +21,14 @@
'android/chromium_url_request_context.h',
'android/chromium_url_request_error_list.h',
'android/chromium_url_request_priority_list.h',
'android/cronet_data_reduction_proxy.cc',
'android/cronet_data_reduction_proxy.h',
'android/cronet_histogram_manager.cc',
'android/cronet_histogram_manager.h',
'android/cronet_library_loader.cc',
'android/cronet_library_loader.h',
'android/cronet_in_memory_pref_store.cc',
'android/cronet_in_memory_pref_store.h',
'android/cronet_upload_data_stream_adapter.cc',
'android/cronet_upload_data_stream_adapter.h',
'android/cronet_upload_data_stream_delegate.cc',
......
......@@ -106,6 +106,18 @@ void URLRequestContextConfig::RegisterJSONConverter(
converter->RegisterStringField(
REQUEST_CONTEXT_CONFIG_QUIC_OPTIONS,
&URLRequestContextConfig::quic_connection_options);
converter->RegisterStringField(
REQUEST_CONTEXT_CONFIG_DATA_REDUCTION_PRIMARY_PROXY,
&URLRequestContextConfig::data_reduction_primary_proxy);
converter->RegisterStringField(
REQUEST_CONTEXT_CONFIG_DATA_REDUCTION_FALLBACK_PROXY,
&URLRequestContextConfig::data_reduction_fallback_proxy);
converter->RegisterStringField(
REQUEST_CONTEXT_CONFIG_DATA_REDUCTION_SECURE_PROXY_CHECK_URL,
&URLRequestContextConfig::data_reduction_secure_proxy_check_url);
converter->RegisterStringField(
REQUEST_CONTEXT_CONFIG_DATA_REDUCTION_PROXY_KEY,
&URLRequestContextConfig::data_reduction_proxy_key);
}
} // namespace cronet
......@@ -76,6 +76,11 @@ struct URLRequestContextConfig {
ScopedVector<QuicHint> quic_hints;
// Comma-separted list of QUIC connection options.
std::string quic_connection_options;
// Enable Data Reduction Proxy with authentication key.
std::string data_reduction_proxy_key;
std::string data_reduction_primary_proxy;
std::string data_reduction_fallback_proxy;
std::string data_reduction_secure_proxy_check_url;
private:
DISALLOW_COPY_AND_ASSIGN(URLRequestContextConfig);
......
......@@ -15,6 +15,10 @@ DEFINE_CONTEXT_CONFIG(NATIVE_LIBRARY_NAME)
DEFINE_CONTEXT_CONFIG(ENABLE_QUIC)
DEFINE_CONTEXT_CONFIG(ENABLE_SPDY)
DEFINE_CONTEXT_CONFIG(ENABLE_SDCH)
DEFINE_CONTEXT_CONFIG(DATA_REDUCTION_PROXY_KEY)
DEFINE_CONTEXT_CONFIG(DATA_REDUCTION_PRIMARY_PROXY)
DEFINE_CONTEXT_CONFIG(DATA_REDUCTION_FALLBACK_PROXY)
DEFINE_CONTEXT_CONFIG(DATA_REDUCTION_SECURE_PROXY_CHECK_URL)
DEFINE_CONTEXT_CONFIG(HTTP_CACHE)
DEFINE_CONTEXT_CONFIG(HTTP_CACHE_MAX_SIZE)
......
This diff is collapsed.
......@@ -40,19 +40,20 @@ extern const char kExperimentsOption[];
extern const char kAndroidWebViewProtocolVersion[];
#endif
#define CLIENT_ENUMS_LIST \
CLIENT_ENUM(UNKNOWN, "") \
CLIENT_ENUM(WEBVIEW_ANDROID, "webview") \
CLIENT_ENUM(CHROME_ANDROID, "android") \
CLIENT_ENUM(CHROME_IOS, "ios") \
CLIENT_ENUM(CHROME_MAC, "mac") \
CLIENT_ENUM(CHROME_CHROMEOS, "chromeos") \
CLIENT_ENUM(CHROME_LINUX, "linux") \
CLIENT_ENUM(CHROME_WINDOWS, "win") \
CLIENT_ENUM(CHROME_FREEBSD, "freebsd") \
CLIENT_ENUM(CHROME_OPENBSD, "openbsd") \
CLIENT_ENUM(CHROME_SOLARIS, "solaris") \
CLIENT_ENUM(CHROME_QNX, "qnx")
#define CLIENT_ENUMS_LIST \
CLIENT_ENUM(UNKNOWN, "") \
CLIENT_ENUM(CRONET_ANDROID, "cronet") \
CLIENT_ENUM(WEBVIEW_ANDROID, "webview") \
CLIENT_ENUM(CHROME_ANDROID, "android") \
CLIENT_ENUM(CHROME_IOS, "ios") \
CLIENT_ENUM(CHROME_MAC, "mac") \
CLIENT_ENUM(CHROME_CHROMEOS, "chromeos") \
CLIENT_ENUM(CHROME_LINUX, "linux") \
CLIENT_ENUM(CHROME_WINDOWS, "win") \
CLIENT_ENUM(CHROME_FREEBSD, "freebsd") \
CLIENT_ENUM(CHROME_OPENBSD, "openbsd") \
CLIENT_ENUM(CHROME_SOLARIS, "solaris") \
CLIENT_ENUM(CHROME_QNX, "qnx")
#define CLIENT_ENUM(name, str_value) name,
typedef enum {
......
......@@ -35,6 +35,8 @@
#include "net/url_request/static_http_user_agent_settings.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_storage.h"
#include "net/url_request/url_request_intercepting_job_factory.h"
#include "net/url_request/url_request_interceptor.h"
#include "net/url_request/url_request_job_factory_impl.h"
#include "net/url_request/url_request_throttler_manager.h"
......@@ -229,6 +231,11 @@ void URLRequestContextBuilder::SetSpdyAndQuicEnabled(bool spdy_enabled,
http_network_session_params_.enable_quic = quic_enabled;
}
void URLRequestContextBuilder::SetInterceptors(
ScopedVector<URLRequestInterceptor> url_request_interceptors) {
url_request_interceptors_ = url_request_interceptors.Pass();
}
void URLRequestContextBuilder::SetCookieAndChannelIdStores(
const scoped_refptr<CookieStore>& cookie_store,
scoped_ptr<ChannelIDService> channel_id_service) {
......@@ -408,8 +415,19 @@ URLRequestContext* URLRequestContextBuilder::Build() {
}
#endif // !defined(DISABLE_FTP_SUPPORT)
storage->set_job_factory(job_factory);
scoped_ptr<net::URLRequestJobFactory> top_job_factory(job_factory);
if (!url_request_interceptors_.empty()) {
// Set up interceptors in the reverse order.
for (ScopedVector<net::URLRequestInterceptor>::reverse_iterator i =
url_request_interceptors_.rbegin();
i != url_request_interceptors_.rend(); ++i) {
top_job_factory.reset(new net::URLRequestInterceptingJobFactory(
top_job_factory.Pass(), make_scoped_ptr(*i)));
}
url_request_interceptors_.weak_clear();
}
storage->set_job_factory(top_job_factory.release());
// TODO(willchan): Support sdch.
return context;
......
......@@ -21,6 +21,7 @@
#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "build/build_config.h"
#include "net/base/net_export.h"
#include "net/base/network_delegate.h"
......@@ -43,6 +44,7 @@ class HostMappingRules;
class HttpAuthHandlerFactory;
class ProxyConfigService;
class URLRequestContext;
class URLRequestInterceptor;
class NET_EXPORT URLRequestContextBuilder {
public:
......@@ -179,6 +181,9 @@ class NET_EXPORT URLRequestContextBuilder {
throttling_enabled_ = throttling_enabled;
}
void SetInterceptors(
ScopedVector<URLRequestInterceptor> url_request_interceptors);
// Override the default in-memory cookie store and channel id service.
// |cookie_store| must not be NULL. |channel_id_service| may be NULL to
// disable channel id for this context.
......@@ -242,6 +247,7 @@ class NET_EXPORT URLRequestContextBuilder {
scoped_refptr<CookieStore> cookie_store_;
scoped_ptr<FtpTransactionFactory> ftp_transaction_factory_;
std::vector<SchemeFactory> extra_http_auth_handlers_;
ScopedVector<URLRequestInterceptor> url_request_interceptors_;
DISALLOW_COPY_AND_ASSIGN(URLRequestContextBuilder);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment