2010-10-03 23:04:50 +02:00
|
|
|
// Copyright (c) 2008 The Chromium Embedded Framework Authors.
|
|
|
|
// Portions copyright (c) 2006-2008 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.
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "libcef/browser_request_context.h"
|
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include <winhttp.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "libcef/browser_file_system.h"
|
|
|
|
#include "libcef/browser_resource_loader_bridge.h"
|
|
|
|
#include "libcef/cef_context.h"
|
|
|
|
#include "libcef/cef_thread.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-16 18:56:12 +02:00
|
|
|
#include "base/compiler_specific.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
#include "base/file_path.h"
|
2011-03-02 03:25:01 +01:00
|
|
|
#include "base/file_util.h"
|
2012-06-21 22:35:13 +02:00
|
|
|
#include "base/thread_task_runner_handle.h"
|
2012-05-23 21:01:04 +02:00
|
|
|
#include "base/threading/worker_pool.h"
|
2011-07-03 02:03:30 +02:00
|
|
|
#include "build/build_config.h"
|
2012-05-23 21:01:04 +02:00
|
|
|
#include "chrome/browser/net/sqlite_persistent_cookie_store.h"
|
2011-01-07 22:34:20 +01:00
|
|
|
#include "net/base/cert_verifier.h"
|
2012-04-05 00:32:24 +02:00
|
|
|
#include "net/cookies/cookie_monster.h"
|
|
|
|
#include "net/base/default_server_bound_cert_store.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
#include "net/base/host_resolver.h"
|
2012-04-05 00:32:24 +02:00
|
|
|
#include "net/base/server_bound_cert_service.h"
|
2011-05-16 18:56:12 +02:00
|
|
|
#include "net/base/ssl_config_service_defaults.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
#include "net/ftp/ftp_network_layer.h"
|
|
|
|
#include "net/http/http_auth_handler_factory.h"
|
2011-10-12 21:09:15 +02:00
|
|
|
#include "net/http/http_server_properties_impl.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
#include "net/proxy/proxy_config_service.h"
|
|
|
|
#include "net/proxy/proxy_config_service_fixed.h"
|
2011-11-23 23:47:09 +01:00
|
|
|
#include "net/proxy/proxy_resolver.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
#include "net/proxy/proxy_service.h"
|
2012-11-16 19:28:07 +01:00
|
|
|
#include "net/url_request/http_user_agent_settings.h"
|
2012-08-29 22:11:26 +02:00
|
|
|
#include "net/url_request/url_request_job_factory_impl.h"
|
2011-04-05 18:17:33 +02:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
|
2011-12-16 15:51:10 +01:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebKitPlatformSupport.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
#include "webkit/blob/blob_storage_controller.h"
|
2011-07-03 02:03:30 +02:00
|
|
|
#include "webkit/blob/blob_url_request_job_factory.h"
|
2011-04-05 18:17:33 +02:00
|
|
|
#include "webkit/fileapi/file_system_context.h"
|
2011-07-03 02:03:30 +02:00
|
|
|
#include "webkit/fileapi/file_system_url_request_job_factory.h"
|
2012-10-03 23:50:01 +02:00
|
|
|
#include "webkit/user_agent/user_agent.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2010-10-15 20:37:25 +02:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
#pragma comment(lib, "winhttp.lib")
|
2012-01-10 00:46:23 +01:00
|
|
|
#endif
|
2010-10-15 20:37:25 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2011-11-24 00:22:01 +01:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
|
2010-10-15 20:37:25 +02:00
|
|
|
// ProxyConfigService implementation that does nothing.
|
|
|
|
class ProxyConfigServiceNull : public net::ProxyConfigService {
|
2012-01-10 00:46:23 +01:00
|
|
|
public:
|
2010-10-15 20:37:25 +02:00
|
|
|
ProxyConfigServiceNull() {}
|
2011-11-23 23:47:09 +01:00
|
|
|
virtual void AddObserver(Observer* observer) OVERRIDE {}
|
|
|
|
virtual void RemoveObserver(Observer* observer) OVERRIDE {}
|
2011-05-16 18:56:12 +02:00
|
|
|
virtual ProxyConfigService::ConfigAvailability
|
2011-11-23 23:47:09 +01:00
|
|
|
GetLatestProxyConfig(net::ProxyConfig* config) OVERRIDE
|
2011-05-16 18:56:12 +02:00
|
|
|
{ return ProxyConfigService::CONFIG_VALID; }
|
2011-11-23 23:47:09 +01:00
|
|
|
virtual void OnLazyPoll() OVERRIDE {}
|
2010-10-15 20:37:25 +02:00
|
|
|
};
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#endif // defined(OS_WIN)
|
2011-11-24 00:22:01 +01:00
|
|
|
|
2011-11-23 23:47:09 +01:00
|
|
|
// ProxyResolver implementation that forewards resolution to a CefProxyHandler.
|
|
|
|
class CefProxyResolver : public net::ProxyResolver {
|
2012-01-10 00:46:23 +01:00
|
|
|
public:
|
|
|
|
explicit CefProxyResolver(CefRefPtr<CefProxyHandler> handler)
|
2011-11-23 23:47:09 +01:00
|
|
|
: ProxyResolver(false),
|
|
|
|
handler_(handler) {}
|
|
|
|
virtual ~CefProxyResolver() {}
|
|
|
|
|
|
|
|
virtual int GetProxyForURL(const GURL& url,
|
|
|
|
net::ProxyInfo* results,
|
2011-12-21 19:35:55 +01:00
|
|
|
const net::CompletionCallback& callback,
|
|
|
|
RequestHandle* request,
|
2012-01-10 00:46:23 +01:00
|
|
|
const net::BoundNetLog& net_log) OVERRIDE {
|
2011-11-23 23:47:09 +01:00
|
|
|
CefProxyInfo proxy_info;
|
|
|
|
handler_->GetProxyForUrl(url.spec(), proxy_info);
|
|
|
|
if (proxy_info.IsDirect())
|
|
|
|
results->UseDirect();
|
|
|
|
else if (proxy_info.IsNamedProxy())
|
|
|
|
results->UseNamedProxy(proxy_info.ProxyList());
|
|
|
|
else if (proxy_info.IsPacString())
|
|
|
|
results->UsePacString(proxy_info.ProxyList());
|
|
|
|
|
|
|
|
return net::OK;
|
2012-01-10 00:46:23 +01:00
|
|
|
}
|
2011-11-23 23:47:09 +01:00
|
|
|
|
|
|
|
virtual int SetPacScript(
|
2011-12-21 19:35:55 +01:00
|
|
|
const scoped_refptr<net::ProxyResolverScriptData>& pac_script,
|
2012-01-10 00:46:23 +01:00
|
|
|
const net::CompletionCallback& callback) OVERRIDE {
|
2011-11-23 23:47:09 +01:00
|
|
|
return net::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void CancelRequest(RequestHandle request) OVERRIDE {}
|
2012-01-10 00:46:23 +01:00
|
|
|
virtual net::LoadState GetLoadState(RequestHandle request) const OVERRIDE {
|
|
|
|
return net::LOAD_STATE_IDLE;
|
|
|
|
}
|
2011-11-23 23:47:09 +01:00
|
|
|
virtual net::LoadState GetLoadStateThreadSafe(RequestHandle request) const
|
2012-01-10 00:46:23 +01:00
|
|
|
OVERRIDE {
|
|
|
|
return net::LOAD_STATE_IDLE;
|
|
|
|
}
|
2011-11-23 23:47:09 +01:00
|
|
|
virtual void CancelSetPacScript() OVERRIDE {}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
protected:
|
2011-11-23 23:47:09 +01:00
|
|
|
CefRefPtr<CefProxyHandler> handler_;
|
|
|
|
};
|
|
|
|
|
|
|
|
net::ProxyConfigService* CreateProxyConfigService() {
|
|
|
|
#if defined(OS_POSIX) && !defined(OS_MACOSX)
|
|
|
|
// Use no proxy to avoid ProxyConfigServiceLinux.
|
|
|
|
// Enabling use of the ProxyConfigServiceLinux requires:
|
|
|
|
// -Calling from a thread with a TYPE_UI MessageLoop,
|
|
|
|
// -If at all possible, passing in a pointer to the IO thread's MessageLoop,
|
|
|
|
// -Keep in mind that proxy auto configuration is also non-functional on linux
|
|
|
|
// in this context because of v8 threading issues.
|
|
|
|
// TODO(port): rename "linux" to some nonspecific unix.
|
|
|
|
return new net::ProxyConfigServiceFixed(net::ProxyConfig());
|
|
|
|
#else
|
|
|
|
// Use the system proxy settings.
|
|
|
|
return net::ProxyService::CreateSystemProxyConfigService(
|
2012-06-21 22:35:13 +02:00
|
|
|
base::ThreadTaskRunnerHandle::Get(), NULL);
|
2011-11-23 23:47:09 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-11-16 19:28:07 +01:00
|
|
|
class CefHttpUserAgentSettings : public net::HttpUserAgentSettings {
|
|
|
|
public:
|
|
|
|
CefHttpUserAgentSettings() {}
|
|
|
|
virtual ~CefHttpUserAgentSettings() {}
|
|
|
|
|
|
|
|
// hard-code A-L and A-C for test shells
|
|
|
|
virtual std::string GetAcceptLanguage() const OVERRIDE {
|
|
|
|
return "en-us,en";
|
|
|
|
}
|
|
|
|
virtual std::string GetAcceptCharset() const OVERRIDE {
|
|
|
|
return "iso-8859-1,*,utf-8";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual std::string GetUserAgent(const GURL& url) const OVERRIDE {
|
|
|
|
return webkit_glue::GetUserAgent(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefHttpUserAgentSettings);
|
|
|
|
};
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
} // namespace
|
2010-10-15 20:37:25 +02:00
|
|
|
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
BrowserRequestContext::BrowserRequestContext()
|
2012-04-05 00:32:24 +02:00
|
|
|
: ALLOW_THIS_IN_INITIALIZER_LIST(storage_(this)) {
|
2010-10-03 23:04:50 +02:00
|
|
|
Init(FilePath(), net::HttpCache::NORMAL, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
BrowserRequestContext::BrowserRequestContext(
|
|
|
|
const FilePath& cache_path,
|
|
|
|
net::HttpCache::Mode cache_mode,
|
2011-05-16 18:56:12 +02:00
|
|
|
bool no_proxy)
|
2012-04-05 00:32:24 +02:00
|
|
|
: ALLOW_THIS_IN_INITIALIZER_LIST(storage_(this)) {
|
2010-10-03 23:04:50 +02:00
|
|
|
Init(cache_path, cache_mode, no_proxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowserRequestContext::Init(
|
|
|
|
const FilePath& cache_path,
|
|
|
|
net::HttpCache::Mode cache_mode,
|
|
|
|
bool no_proxy) {
|
2011-11-02 20:50:00 +01:00
|
|
|
SetCookieStoragePath(cache_path);
|
2011-03-02 03:25:01 +01:00
|
|
|
|
2012-04-05 00:32:24 +02:00
|
|
|
storage_.set_server_bound_cert_service(new net::ServerBoundCertService(
|
2012-05-23 21:01:04 +02:00
|
|
|
new net::DefaultServerBoundCertStore(NULL),
|
|
|
|
base::WorkerPool::GetTaskRunner(true)));
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2012-11-16 19:28:07 +01:00
|
|
|
storage_.set_http_user_agent_settings(new CefHttpUserAgentSettings);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-11-23 23:47:09 +01:00
|
|
|
CefRefPtr<CefApp> app = _Context->application();
|
|
|
|
if (app.get()) {
|
|
|
|
CefRefPtr<CefProxyHandler> handler = app->GetProxyHandler();
|
|
|
|
if (handler) {
|
|
|
|
// The client will provide proxy resolution.
|
|
|
|
storage_.set_proxy_service(
|
|
|
|
new net::ProxyService(CreateProxyConfigService(),
|
|
|
|
new CefProxyResolver(handler), NULL));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-15 20:37:25 +02:00
|
|
|
#if defined(OS_WIN)
|
2011-11-23 23:47:09 +01:00
|
|
|
if (!proxy_service()) {
|
|
|
|
const CefSettings& settings = _Context->settings();
|
|
|
|
if (!settings.auto_detect_proxy_settings_enabled) {
|
|
|
|
// Using the system proxy resolver on Windows when "Automatically detect
|
|
|
|
// settings" (auto-detection) is checked under LAN Settings can hurt
|
|
|
|
// resource loading performance because the call to WinHttpGetProxyForUrl
|
|
|
|
// in proxy_resolver_winhttp.cc will block the IO thread. This is
|
|
|
|
// especially true for Windows 7 where auto-detection is checked by
|
|
|
|
// default. To avoid slow resource loading on Windows we only use the
|
|
|
|
// system proxy resolver if auto-detection is unchecked.
|
|
|
|
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_config = {0};
|
|
|
|
if (WinHttpGetIEProxyConfigForCurrentUser(&ie_config)) {
|
|
|
|
if (ie_config.fAutoDetect == TRUE) {
|
|
|
|
storage_.set_proxy_service(
|
|
|
|
net::ProxyService::CreateWithoutProxyResolver(
|
|
|
|
new ProxyConfigServiceNull(), NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ie_config.lpszAutoConfigUrl)
|
|
|
|
GlobalFree(ie_config.lpszAutoConfigUrl);
|
|
|
|
if (ie_config.lpszProxy)
|
|
|
|
GlobalFree(ie_config.lpszProxy);
|
|
|
|
if (ie_config.lpszProxyBypass)
|
|
|
|
GlobalFree(ie_config.lpszProxyBypass);
|
2011-10-21 23:14:50 +02:00
|
|
|
}
|
2011-01-06 20:08:15 +01:00
|
|
|
}
|
2010-10-15 20:37:25 +02:00
|
|
|
}
|
2012-01-10 00:46:23 +01:00
|
|
|
#endif // defined(OS_WIN)
|
|
|
|
|
2011-04-05 18:17:33 +02:00
|
|
|
if (!proxy_service()) {
|
2011-11-23 23:47:09 +01:00
|
|
|
storage_.set_proxy_service(
|
|
|
|
net::ProxyService::CreateUsingSystemProxyResolver(
|
|
|
|
CreateProxyConfigService(), 0, NULL));
|
2010-10-15 20:37:25 +02:00
|
|
|
}
|
|
|
|
|
2012-11-06 18:13:58 +01:00
|
|
|
storage_.set_host_resolver(net::HostResolver::CreateDefaultResolver(NULL));
|
2012-04-05 00:32:24 +02:00
|
|
|
storage_.set_cert_verifier(net::CertVerifier::CreateDefault());
|
2011-05-16 18:56:12 +02:00
|
|
|
storage_.set_ssl_config_service(new net::SSLConfigServiceDefaults);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-01-24 22:24:21 +01:00
|
|
|
// Add support for single sign-on.
|
|
|
|
url_security_manager_.reset(net::URLSecurityManager::Create(NULL, NULL));
|
|
|
|
|
|
|
|
std::vector<std::string> supported_schemes;
|
|
|
|
supported_schemes.push_back("basic");
|
|
|
|
supported_schemes.push_back("digest");
|
|
|
|
supported_schemes.push_back("ntlm");
|
|
|
|
supported_schemes.push_back("negotiate");
|
|
|
|
|
2011-05-16 18:56:12 +02:00
|
|
|
storage_.set_http_auth_handler_factory(
|
2011-01-24 22:24:21 +01:00
|
|
|
net::HttpAuthHandlerRegistryFactory::Create(supported_schemes,
|
|
|
|
url_security_manager_.get(),
|
2011-04-05 18:17:33 +02:00
|
|
|
host_resolver(),
|
2011-01-24 22:24:21 +01:00
|
|
|
std::string(),
|
|
|
|
false,
|
2011-04-05 18:17:33 +02:00
|
|
|
false));
|
2011-10-12 21:09:15 +02:00
|
|
|
storage_.set_http_server_properties(new net::HttpServerPropertiesImpl);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
net::HttpCache::DefaultBackend* backend = new net::HttpCache::DefaultBackend(
|
2012-06-20 21:44:00 +02:00
|
|
|
cache_path.empty() ? net::MEMORY_CACHE : net::DISK_CACHE,
|
2010-10-03 23:04:50 +02:00
|
|
|
cache_path, 0, BrowserResourceLoaderBridge::GetCacheThread());
|
|
|
|
|
2012-10-03 23:50:01 +02:00
|
|
|
net::HttpNetworkSession::Params network_session_params;
|
|
|
|
network_session_params.host_resolver = host_resolver();
|
|
|
|
network_session_params.cert_verifier = cert_verifier();
|
|
|
|
network_session_params.server_bound_cert_service =
|
|
|
|
server_bound_cert_service();
|
|
|
|
network_session_params.proxy_service = proxy_service();
|
|
|
|
network_session_params.ssl_config_service = ssl_config_service();
|
|
|
|
network_session_params.http_auth_handler_factory =
|
|
|
|
http_auth_handler_factory();
|
|
|
|
network_session_params.http_server_properties = http_server_properties();
|
|
|
|
network_session_params.host_resolver = host_resolver();
|
|
|
|
|
|
|
|
net::HttpCache* cache = new net::HttpCache(network_session_params, backend);
|
2010-10-03 23:04:50 +02:00
|
|
|
cache->set_mode(cache_mode);
|
2011-05-16 18:56:12 +02:00
|
|
|
storage_.set_http_transaction_factory(cache);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-05-16 18:56:12 +02:00
|
|
|
storage_.set_ftp_transaction_factory(
|
|
|
|
new net::FtpNetworkLayer(host_resolver()));
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2012-08-29 22:11:26 +02:00
|
|
|
net::URLRequestJobFactory* job_factory = new net::URLRequestJobFactoryImpl();
|
2011-12-06 11:16:51 +01:00
|
|
|
|
|
|
|
BrowserFileSystem* file_system = _Context->file_system();
|
|
|
|
// Create the context if it doesn't already exist.
|
|
|
|
file_system->CreateContext();
|
|
|
|
if (file_system->file_system_context()) {
|
|
|
|
job_factory->SetProtocolHandler(
|
|
|
|
"filesystem",
|
|
|
|
fileapi::CreateFileSystemProtocolHandler(
|
2012-05-23 21:01:04 +02:00
|
|
|
file_system->file_system_context()));
|
2012-11-06 18:13:58 +01:00
|
|
|
|
|
|
|
blob_storage_controller_.reset(new webkit_blob::BlobStorageController());
|
|
|
|
job_factory->SetProtocolHandler(
|
|
|
|
"blob",
|
|
|
|
new webkit_blob::BlobProtocolHandler(
|
|
|
|
blob_storage_controller_.get(),
|
|
|
|
file_system->file_system_context(),
|
|
|
|
CefThread::GetMessageLoopProxyForThread(CefThread::FILE)));
|
2011-12-06 11:16:51 +01:00
|
|
|
}
|
|
|
|
|
2011-07-03 02:03:30 +02:00
|
|
|
storage_.set_job_factory(job_factory);
|
2011-12-15 15:47:23 +01:00
|
|
|
|
|
|
|
url_request_interceptor_.reset(
|
|
|
|
BrowserResourceLoaderBridge::CreateRequestInterceptor());
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BrowserRequestContext::~BrowserRequestContext() {
|
|
|
|
}
|
|
|
|
|
2011-11-02 20:50:00 +01:00
|
|
|
void BrowserRequestContext::SetCookieStoragePath(const FilePath& path) {
|
|
|
|
REQUIRE_IOT();
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-11-02 20:50:00 +01:00
|
|
|
if (cookie_store() && ((cookie_store_path_.empty() && path.empty()) ||
|
|
|
|
cookie_store_path_ == path)) {
|
|
|
|
// The path has not changed so don't do anything.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-20 21:44:00 +02:00
|
|
|
FilePath new_path = path;
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
scoped_refptr<SQLitePersistentCookieStore> persistent_store;
|
2012-06-20 21:44:00 +02:00
|
|
|
if (!new_path.empty()) {
|
|
|
|
if (!file_util::PathExists(new_path) &&
|
|
|
|
!file_util::CreateDirectory(new_path)) {
|
|
|
|
NOTREACHED() << "Failed to create cookie storage directory";
|
|
|
|
new_path.clear();
|
2011-11-02 20:50:00 +01:00
|
|
|
} else {
|
2012-06-20 21:44:00 +02:00
|
|
|
FilePath cookie_path = new_path.Append(FILE_PATH_LITERAL("Cookies"));
|
2012-06-21 22:35:13 +02:00
|
|
|
persistent_store =
|
|
|
|
new SQLitePersistentCookieStore(cookie_path, false, NULL);
|
2011-11-02 20:50:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the new cookie store that will be used for all new requests. The old
|
|
|
|
// cookie store, if any, will be automatically flushed and closed when no
|
|
|
|
// longer referenced.
|
|
|
|
storage_.set_cookie_store(
|
|
|
|
new net::CookieMonster(persistent_store.get(), NULL));
|
2012-06-20 21:44:00 +02:00
|
|
|
cookie_store_path_ = new_path;
|
2011-11-02 20:50:00 +01:00
|
|
|
}
|