2015-06-16 09:23:29 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/api/atom_api_session.h"
|
|
|
|
|
2016-08-01 11:11:17 +00:00
|
|
|
#include <map>
|
2015-06-24 03:59:11 +00:00
|
|
|
#include <string>
|
2015-07-13 22:13:17 +00:00
|
|
|
#include <vector>
|
2015-06-24 03:59:11 +00:00
|
|
|
|
2015-06-16 09:23:29 +00:00
|
|
|
#include "atom/browser/api/atom_api_cookies.h"
|
2015-09-20 10:56:10 +00:00
|
|
|
#include "atom/browser/api/atom_api_download_item.h"
|
2016-06-08 14:31:27 +00:00
|
|
|
#include "atom/browser/api/atom_api_protocol.h"
|
2015-12-01 04:52:22 +00:00
|
|
|
#include "atom/browser/api/atom_api_web_request.h"
|
2015-11-04 08:54:36 +00:00
|
|
|
#include "atom/browser/atom_browser_context.h"
|
|
|
|
#include "atom/browser/atom_browser_main_parts.h"
|
2016-01-31 21:35:34 +00:00
|
|
|
#include "atom/browser/atom_permission_manager.h"
|
2016-08-26 22:30:02 +00:00
|
|
|
#include "atom/browser/browser.h"
|
2015-11-18 03:17:08 +00:00
|
|
|
#include "atom/browser/net/atom_cert_verifier.h"
|
2015-08-07 10:10:19 +00:00
|
|
|
#include "atom/common/native_mate_converters/callback.h"
|
2016-01-31 21:35:34 +00:00
|
|
|
#include "atom/common/native_mate_converters/content_converter.h"
|
2015-07-30 02:38:04 +00:00
|
|
|
#include "atom/common/native_mate_converters/file_path_converter.h"
|
2016-08-26 22:30:02 +00:00
|
|
|
#include "atom/common/native_mate_converters/gurl_converter.h"
|
2015-11-05 14:06:36 +00:00
|
|
|
#include "atom/common/native_mate_converters/net_converter.h"
|
2016-07-12 12:53:19 +00:00
|
|
|
#include "atom/common/native_mate_converters/value_converter.h"
|
2015-09-07 08:29:54 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
2015-07-26 08:30:02 +00:00
|
|
|
#include "base/files/file_path.h"
|
2016-04-18 04:59:15 +00:00
|
|
|
#include "base/guid.h"
|
2016-01-14 09:31:54 +00:00
|
|
|
#include "base/strings/string_number_conversions.h"
|
2015-07-02 14:12:18 +00:00
|
|
|
#include "base/strings/string_util.h"
|
2016-07-04 06:08:55 +00:00
|
|
|
#include "base/threading/thread_task_runner_handle.h"
|
2016-12-03 11:14:33 +00:00
|
|
|
#include "brightray/browser/media/media_device_id_salt.h"
|
2015-09-27 13:19:52 +00:00
|
|
|
#include "brightray/browser/net/devtools_network_conditions.h"
|
2016-04-18 04:59:15 +00:00
|
|
|
#include "brightray/browser/net/devtools_network_controller_handle.h"
|
2015-07-26 08:30:02 +00:00
|
|
|
#include "chrome/common/pref_names.h"
|
2016-08-26 22:30:02 +00:00
|
|
|
#include "components/prefs/pref_service.h"
|
2015-06-25 08:43:01 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2016-11-18 10:43:43 +00:00
|
|
|
#include "content/public/browser/download_manager_delegate.h"
|
2015-06-25 08:43:01 +00:00
|
|
|
#include "content/public/browser/storage_partition.h"
|
2015-08-10 07:02:16 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
2015-06-16 09:23:29 +00:00
|
|
|
#include "native_mate/object_template_builder.h"
|
2015-06-24 03:59:11 +00:00
|
|
|
#include "net/base/load_flags.h"
|
2015-07-02 14:12:18 +00:00
|
|
|
#include "net/disk_cache/disk_cache.h"
|
2016-02-01 13:33:23 +00:00
|
|
|
#include "net/dns/host_cache.h"
|
2016-05-23 05:29:55 +00:00
|
|
|
#include "net/http/http_auth_handler_factory.h"
|
|
|
|
#include "net/http/http_auth_preferences.h"
|
2015-07-16 14:36:48 +00:00
|
|
|
#include "net/proxy/proxy_config_service_fixed.h"
|
2016-08-26 22:30:02 +00:00
|
|
|
#include "net/proxy/proxy_service.h"
|
2016-06-22 06:57:51 +00:00
|
|
|
#include "net/url_request/static_http_user_agent_settings.h"
|
2015-06-24 03:59:11 +00:00
|
|
|
#include "net/url_request/url_request_context.h"
|
|
|
|
#include "net/url_request/url_request_context_getter.h"
|
2016-06-22 06:57:51 +00:00
|
|
|
#include "ui/base/l10n/l10n_util.h"
|
2015-06-16 09:23:29 +00:00
|
|
|
|
2016-09-21 23:24:03 +00:00
|
|
|
using atom::api::Cookies;
|
2015-07-02 14:12:18 +00:00
|
|
|
using content::BrowserThread;
|
|
|
|
using content::StoragePartition;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-07-13 22:13:17 +00:00
|
|
|
struct ClearStorageDataOptions {
|
|
|
|
GURL origin;
|
2016-03-08 04:40:10 +00:00
|
|
|
uint32_t storage_types = StoragePartition::REMOVE_DATA_MASK_ALL;
|
|
|
|
uint32_t quota_types = StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL;
|
2015-07-13 22:13:17 +00:00
|
|
|
};
|
2015-07-02 14:12:18 +00:00
|
|
|
|
2016-11-30 16:35:15 +00:00
|
|
|
struct ClearAuthCacheOptions {
|
|
|
|
std::string type;
|
|
|
|
GURL origin;
|
|
|
|
std::string realm;
|
|
|
|
base::string16 username;
|
|
|
|
base::string16 password;
|
|
|
|
net::HttpAuth::Scheme auth_scheme;
|
|
|
|
};
|
|
|
|
|
2016-03-08 04:40:10 +00:00
|
|
|
uint32_t GetStorageMask(const std::vector<std::string>& storage_types) {
|
|
|
|
uint32_t storage_mask = 0;
|
2015-07-13 22:13:17 +00:00
|
|
|
for (const auto& it : storage_types) {
|
2015-12-07 11:56:23 +00:00
|
|
|
auto type = base::ToLowerASCII(it);
|
2015-07-13 22:13:17 +00:00
|
|
|
if (type == "appcache")
|
2015-07-02 14:12:18 +00:00
|
|
|
storage_mask |= StoragePartition::REMOVE_DATA_MASK_APPCACHE;
|
2015-07-13 22:13:17 +00:00
|
|
|
else if (type == "cookies")
|
2015-07-02 14:12:18 +00:00
|
|
|
storage_mask |= StoragePartition::REMOVE_DATA_MASK_COOKIES;
|
2015-07-13 22:13:17 +00:00
|
|
|
else if (type == "filesystem")
|
2015-07-02 14:12:18 +00:00
|
|
|
storage_mask |= StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS;
|
2015-07-13 22:13:17 +00:00
|
|
|
else if (type == "indexdb")
|
2015-07-02 14:12:18 +00:00
|
|
|
storage_mask |= StoragePartition::REMOVE_DATA_MASK_INDEXEDDB;
|
2015-07-13 22:13:17 +00:00
|
|
|
else if (type == "localstorage")
|
2015-07-02 14:12:18 +00:00
|
|
|
storage_mask |= StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE;
|
2015-07-13 22:13:17 +00:00
|
|
|
else if (type == "shadercache")
|
2015-07-02 14:12:18 +00:00
|
|
|
storage_mask |= StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE;
|
2015-07-13 22:13:17 +00:00
|
|
|
else if (type == "websql")
|
2015-07-02 14:12:18 +00:00
|
|
|
storage_mask |= StoragePartition::REMOVE_DATA_MASK_WEBSQL;
|
2015-07-13 22:13:17 +00:00
|
|
|
else if (type == "serviceworkers")
|
2015-07-02 14:12:18 +00:00
|
|
|
storage_mask |= StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS;
|
|
|
|
}
|
|
|
|
return storage_mask;
|
|
|
|
}
|
|
|
|
|
2016-03-08 04:40:10 +00:00
|
|
|
uint32_t GetQuotaMask(const std::vector<std::string>& quota_types) {
|
|
|
|
uint32_t quota_mask = 0;
|
2015-07-13 22:13:17 +00:00
|
|
|
for (const auto& it : quota_types) {
|
2015-12-07 11:56:23 +00:00
|
|
|
auto type = base::ToLowerASCII(it);
|
2015-07-13 22:13:17 +00:00
|
|
|
if (type == "temporary")
|
2015-07-02 14:12:18 +00:00
|
|
|
quota_mask |= StoragePartition::QUOTA_MANAGED_STORAGE_MASK_TEMPORARY;
|
2015-07-13 22:13:17 +00:00
|
|
|
else if (type == "persistent")
|
2015-07-02 14:12:18 +00:00
|
|
|
quota_mask |= StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
|
2015-07-13 22:13:17 +00:00
|
|
|
else if (type == "syncable")
|
|
|
|
quota_mask |= StoragePartition::QUOTA_MANAGED_STORAGE_MASK_SYNCABLE;
|
2015-07-02 14:12:18 +00:00
|
|
|
}
|
|
|
|
return quota_mask;
|
|
|
|
}
|
|
|
|
|
2016-11-30 16:35:15 +00:00
|
|
|
net::HttpAuth::Scheme GetAuthSchemeFromString(const std::string& scheme) {
|
|
|
|
if (scheme == "basic")
|
|
|
|
return net::HttpAuth::AUTH_SCHEME_BASIC;
|
|
|
|
if (scheme == "digest")
|
|
|
|
return net::HttpAuth::AUTH_SCHEME_DIGEST;
|
|
|
|
if (scheme == "ntlm")
|
|
|
|
return net::HttpAuth::AUTH_SCHEME_NTLM;
|
|
|
|
if (scheme == "negotiate")
|
|
|
|
return net::HttpAuth::AUTH_SCHEME_NEGOTIATE;
|
|
|
|
return net::HttpAuth::AUTH_SCHEME_MAX;
|
|
|
|
}
|
|
|
|
|
2016-06-22 06:57:51 +00:00
|
|
|
void SetUserAgentInIO(scoped_refptr<net::URLRequestContextGetter> getter,
|
|
|
|
const std::string& accept_lang,
|
|
|
|
const std::string& user_agent) {
|
|
|
|
getter->GetURLRequestContext()->set_http_user_agent_settings(
|
|
|
|
new net::StaticHttpUserAgentSettings(
|
|
|
|
net::HttpUtil::GenerateAcceptLanguageHeader(accept_lang),
|
|
|
|
user_agent));
|
|
|
|
}
|
|
|
|
|
2015-07-02 14:12:18 +00:00
|
|
|
} // namespace
|
|
|
|
|
2015-07-13 22:13:17 +00:00
|
|
|
namespace mate {
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Converter<ClearStorageDataOptions> {
|
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
|
|
|
ClearStorageDataOptions* out) {
|
|
|
|
mate::Dictionary options;
|
|
|
|
if (!ConvertFromV8(isolate, val, &options))
|
|
|
|
return false;
|
|
|
|
options.Get("origin", &out->origin);
|
|
|
|
std::vector<std::string> types;
|
|
|
|
if (options.Get("storages", &types))
|
|
|
|
out->storage_types = GetStorageMask(types);
|
|
|
|
if (options.Get("quotas", &types))
|
|
|
|
out->quota_types = GetQuotaMask(types);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-30 16:35:15 +00:00
|
|
|
template <>
|
|
|
|
struct Converter<ClearAuthCacheOptions> {
|
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
|
|
|
ClearAuthCacheOptions* out) {
|
|
|
|
mate::Dictionary options;
|
|
|
|
if (!ConvertFromV8(isolate, val, &options))
|
|
|
|
return false;
|
|
|
|
options.Get("type", &out->type);
|
|
|
|
options.Get("origin", &out->origin);
|
|
|
|
options.Get("realm", &out->realm);
|
|
|
|
options.Get("username", &out->username);
|
|
|
|
options.Get("password", &out->password);
|
|
|
|
std::string scheme;
|
|
|
|
if (options.Get("scheme", &scheme))
|
|
|
|
out->auth_scheme = GetAuthSchemeFromString(scheme);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2015-10-22 17:26:36 +00:00
|
|
|
struct Converter<net::ProxyConfig> {
|
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
|
|
|
net::ProxyConfig* out) {
|
2016-07-21 04:31:08 +00:00
|
|
|
std::string proxy_rules, proxy_bypass_rules;
|
2016-01-11 14:45:34 +00:00
|
|
|
GURL pac_url;
|
2016-01-10 22:25:56 +00:00
|
|
|
mate::Dictionary options;
|
2016-01-11 14:45:34 +00:00
|
|
|
// Fallback to previous API when passed String.
|
|
|
|
// https://git.io/vuhjj
|
|
|
|
if (ConvertFromV8(isolate, val, &proxy_rules)) {
|
|
|
|
pac_url = GURL(proxy_rules); // Assume it is PAC script if it is URL.
|
|
|
|
} else if (ConvertFromV8(isolate, val, &options)) {
|
|
|
|
options.Get("pacScript", &pac_url);
|
|
|
|
options.Get("proxyRules", &proxy_rules);
|
2016-07-21 04:31:08 +00:00
|
|
|
options.Get("proxyBypassRules", &proxy_bypass_rules);
|
2016-01-11 14:45:34 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
2016-01-11 06:54:01 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 14:45:34 +00:00
|
|
|
// pacScript takes precedence over proxyRules.
|
|
|
|
if (!pac_url.is_empty() && pac_url.is_valid()) {
|
2015-10-22 17:26:36 +00:00
|
|
|
out->set_pac_url(pac_url);
|
2016-01-11 14:45:34 +00:00
|
|
|
} else {
|
|
|
|
out->proxy_rules().ParseFromString(proxy_rules);
|
2016-07-21 04:31:08 +00:00
|
|
|
out->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_rules);
|
2016-01-11 06:54:01 +00:00
|
|
|
}
|
2015-10-22 17:26:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-08 00:35:37 +00:00
|
|
|
template<>
|
2017-02-08 18:34:07 +00:00
|
|
|
struct Converter<atom::VerifyRequestParams> {
|
2017-02-08 00:35:37 +00:00
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
2017-02-08 18:34:07 +00:00
|
|
|
atom::VerifyRequestParams val) {
|
2017-02-08 00:35:37 +00:00
|
|
|
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
|
|
|
dict.Set("hostname", val.hostname);
|
|
|
|
dict.Set("certificate", val.certificate);
|
|
|
|
dict.Set("verificationResult", val.default_result);
|
|
|
|
return dict.GetHandle();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-13 22:13:17 +00:00
|
|
|
} // namespace mate
|
|
|
|
|
2015-06-16 09:23:29 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2015-06-24 03:59:11 +00:00
|
|
|
namespace {
|
|
|
|
|
2016-07-12 12:01:49 +00:00
|
|
|
const char kPersistPrefix[] = "persist:";
|
|
|
|
|
2016-08-01 11:11:17 +00:00
|
|
|
// Referenced session objects.
|
|
|
|
std::map<uint32_t, v8::Global<v8::Object>> g_sessions;
|
|
|
|
|
2015-06-24 03:59:11 +00:00
|
|
|
class ResolveProxyHelper {
|
|
|
|
public:
|
|
|
|
ResolveProxyHelper(AtomBrowserContext* browser_context,
|
|
|
|
const GURL& url,
|
|
|
|
Session::ResolveProxyCallback callback)
|
2015-06-25 08:43:01 +00:00
|
|
|
: callback_(callback),
|
2015-06-26 03:04:12 +00:00
|
|
|
original_thread_(base::ThreadTaskRunnerHandle::Get()) {
|
|
|
|
scoped_refptr<net::URLRequestContextGetter> context_getter =
|
2016-07-04 06:08:55 +00:00
|
|
|
browser_context->url_request_context_getter();
|
2015-06-26 03:04:12 +00:00
|
|
|
context_getter->GetNetworkTaskRunner()->PostTask(
|
2015-06-25 08:43:01 +00:00
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&ResolveProxyHelper::ResolveProxy,
|
2015-06-26 03:04:12 +00:00
|
|
|
base::Unretained(this), context_getter, url));
|
2015-06-24 03:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnResolveProxyCompleted(int result) {
|
|
|
|
std::string proxy;
|
|
|
|
if (result == net::OK)
|
|
|
|
proxy = proxy_info_.ToPacString();
|
2015-06-25 08:43:01 +00:00
|
|
|
original_thread_->PostTask(FROM_HERE,
|
|
|
|
base::Bind(callback_, proxy));
|
2015-06-24 03:59:11 +00:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-06-26 03:04:12 +00:00
|
|
|
void ResolveProxy(scoped_refptr<net::URLRequestContextGetter> context_getter,
|
|
|
|
const GURL& url) {
|
2015-06-25 08:43:01 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
|
|
|
|
|
|
|
|
net::ProxyService* proxy_service =
|
2015-06-26 03:04:12 +00:00
|
|
|
context_getter->GetURLRequestContext()->proxy_service();
|
2015-06-25 08:43:01 +00:00
|
|
|
net::CompletionCallback completion_callback =
|
|
|
|
base::Bind(&ResolveProxyHelper::OnResolveProxyCompleted,
|
2015-06-26 03:04:12 +00:00
|
|
|
base::Unretained(this));
|
2015-06-25 08:43:01 +00:00
|
|
|
|
|
|
|
// Start the request.
|
|
|
|
int result = proxy_service->ResolveProxy(
|
2016-11-30 07:30:03 +00:00
|
|
|
url, "GET", &proxy_info_, completion_callback, &pac_req_, nullptr,
|
2017-01-24 05:33:21 +00:00
|
|
|
net::NetLogWithSource());
|
2015-06-25 08:43:01 +00:00
|
|
|
|
|
|
|
// Completed synchronously.
|
|
|
|
if (result != net::ERR_IO_PENDING)
|
|
|
|
completion_callback.Run(result);
|
|
|
|
}
|
|
|
|
|
2015-06-24 03:59:11 +00:00
|
|
|
Session::ResolveProxyCallback callback_;
|
|
|
|
net::ProxyInfo proxy_info_;
|
|
|
|
net::ProxyService::PacRequest* pac_req_;
|
2015-06-25 08:43:01 +00:00
|
|
|
scoped_refptr<base::SingleThreadTaskRunner> original_thread_;
|
2015-06-24 03:59:11 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ResolveProxyHelper);
|
|
|
|
};
|
|
|
|
|
2015-07-13 21:27:07 +00:00
|
|
|
// Runs the callback in UI thread.
|
2017-01-24 08:06:24 +00:00
|
|
|
void RunCallbackInUI(const base::Callback<void()>& callback) {
|
|
|
|
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback);
|
|
|
|
}
|
2016-01-14 09:31:54 +00:00
|
|
|
template<typename ...T>
|
2015-07-16 14:36:48 +00:00
|
|
|
void RunCallbackInUI(const base::Callback<void(T...)>& callback, T... result) {
|
2015-07-13 21:27:07 +00:00
|
|
|
BrowserThread::PostTask(
|
2015-07-16 14:36:48 +00:00
|
|
|
BrowserThread::UI, FROM_HERE, base::Bind(callback, result...));
|
2015-07-02 14:12:18 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 21:27:07 +00:00
|
|
|
// Callback of HttpCache::GetBackend.
|
|
|
|
void OnGetBackend(disk_cache::Backend** backend_ptr,
|
2016-01-14 09:31:54 +00:00
|
|
|
Session::CacheAction action,
|
2015-07-13 21:27:07 +00:00
|
|
|
const net::CompletionCallback& callback,
|
2015-07-02 14:12:18 +00:00
|
|
|
int result) {
|
2015-07-13 21:27:07 +00:00
|
|
|
if (result != net::OK) {
|
|
|
|
RunCallbackInUI(callback, result);
|
|
|
|
} else if (backend_ptr && *backend_ptr) {
|
2016-01-14 09:31:54 +00:00
|
|
|
if (action == Session::CacheAction::CLEAR) {
|
|
|
|
(*backend_ptr)->DoomAllEntries(base::Bind(&RunCallbackInUI<int>,
|
|
|
|
callback));
|
|
|
|
} else if (action == Session::CacheAction::STATS) {
|
|
|
|
base::StringPairs stats;
|
|
|
|
(*backend_ptr)->GetStats(&stats);
|
2016-07-10 13:56:42 +00:00
|
|
|
for (const auto& stat : stats) {
|
2016-07-10 11:32:40 +00:00
|
|
|
if (stat.first == "Current size") {
|
2016-01-14 09:31:54 +00:00
|
|
|
int current_size;
|
2016-07-10 11:32:40 +00:00
|
|
|
base::StringToInt(stat.second, ¤t_size);
|
2016-01-14 09:31:54 +00:00
|
|
|
RunCallbackInUI(callback, current_size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-13 21:27:07 +00:00
|
|
|
} else {
|
2015-07-16 14:36:48 +00:00
|
|
|
RunCallbackInUI<int>(callback, net::ERR_FAILED);
|
2015-07-13 21:27:07 +00:00
|
|
|
}
|
2015-07-02 14:12:18 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 12:48:14 +00:00
|
|
|
void DoCacheActionInIO(
|
2015-08-10 07:02:16 +00:00
|
|
|
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
|
2016-01-14 09:31:54 +00:00
|
|
|
Session::CacheAction action,
|
2015-08-10 07:02:16 +00:00
|
|
|
const net::CompletionCallback& callback) {
|
|
|
|
auto request_context = context_getter->GetURLRequestContext();
|
2015-07-02 14:12:18 +00:00
|
|
|
auto http_cache = request_context->http_transaction_factory()->GetCache();
|
2015-07-13 22:13:17 +00:00
|
|
|
if (!http_cache)
|
2015-07-16 14:36:48 +00:00
|
|
|
RunCallbackInUI<int>(callback, net::ERR_FAILED);
|
2015-07-02 14:12:18 +00:00
|
|
|
|
2015-07-13 22:13:17 +00:00
|
|
|
// Call GetBackend and make the backend's ptr accessable in OnGetBackend.
|
|
|
|
using BackendPtr = disk_cache::Backend*;
|
2016-07-10 11:09:55 +00:00
|
|
|
auto* backend_ptr = new BackendPtr(nullptr);
|
2015-07-13 21:27:07 +00:00
|
|
|
net::CompletionCallback on_get_backend =
|
2016-01-14 09:31:54 +00:00
|
|
|
base::Bind(&OnGetBackend, base::Owned(backend_ptr), action, callback);
|
2015-07-13 21:27:07 +00:00
|
|
|
int rv = http_cache->GetBackend(backend_ptr, on_get_backend);
|
2015-07-02 14:12:18 +00:00
|
|
|
if (rv != net::ERR_IO_PENDING)
|
2015-07-13 21:27:07 +00:00
|
|
|
on_get_backend.Run(net::OK);
|
2015-07-02 14:12:18 +00:00
|
|
|
}
|
|
|
|
|
2015-07-16 14:36:48 +00:00
|
|
|
void SetProxyInIO(net::URLRequestContextGetter* getter,
|
2015-10-22 17:26:36 +00:00
|
|
|
const net::ProxyConfig& config,
|
2015-07-16 14:36:48 +00:00
|
|
|
const base::Closure& callback) {
|
|
|
|
auto proxy_service = getter->GetURLRequestContext()->proxy_service();
|
2016-07-04 06:08:55 +00:00
|
|
|
proxy_service->ResetConfigService(base::WrapUnique(
|
2015-12-07 11:56:23 +00:00
|
|
|
new net::ProxyConfigServiceFixed(config)));
|
2015-10-22 17:26:36 +00:00
|
|
|
// Refetches and applies the new pac script if provided.
|
|
|
|
proxy_service->ForceReloadProxyConfig();
|
2015-07-16 14:36:48 +00:00
|
|
|
RunCallbackInUI(callback);
|
|
|
|
}
|
|
|
|
|
2016-07-13 03:05:57 +00:00
|
|
|
void SetCertVerifyProcInIO(
|
|
|
|
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
|
|
|
|
const AtomCertVerifier::VerifyProc& proc) {
|
|
|
|
auto request_context = context_getter->GetURLRequestContext();
|
|
|
|
static_cast<AtomCertVerifier*>(request_context->cert_verifier())->
|
|
|
|
SetVerifyProc(proc);
|
|
|
|
}
|
|
|
|
|
2016-02-01 13:33:23 +00:00
|
|
|
void ClearHostResolverCacheInIO(
|
|
|
|
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
|
|
|
|
const base::Closure& callback) {
|
|
|
|
auto request_context = context_getter->GetURLRequestContext();
|
|
|
|
auto cache = request_context->host_resolver()->GetHostCache();
|
|
|
|
if (cache) {
|
|
|
|
cache->clear();
|
|
|
|
DCHECK_EQ(0u, cache->size());
|
|
|
|
if (!callback.is_null())
|
|
|
|
RunCallbackInUI(callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 16:35:15 +00:00
|
|
|
void ClearAuthCacheInIO(
|
|
|
|
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
|
|
|
|
const ClearAuthCacheOptions& options,
|
|
|
|
const base::Closure& callback) {
|
|
|
|
auto request_context = context_getter->GetURLRequestContext();
|
|
|
|
auto network_session =
|
|
|
|
request_context->http_transaction_factory()->GetSession();
|
|
|
|
if (network_session) {
|
|
|
|
if (options.type == "password") {
|
|
|
|
auto auth_cache = network_session->http_auth_cache();
|
|
|
|
if (!options.origin.is_empty()) {
|
|
|
|
auth_cache->Remove(
|
|
|
|
options.origin, options.realm, options.auth_scheme,
|
|
|
|
net::AuthCredentials(options.username, options.password));
|
|
|
|
} else {
|
2017-01-24 08:06:09 +00:00
|
|
|
auth_cache->ClearEntriesAddedWithin(base::TimeDelta::Max());
|
2016-11-30 16:35:15 +00:00
|
|
|
}
|
|
|
|
} else if (options.type == "clientCertificate") {
|
|
|
|
auto client_auth_cache = network_session->ssl_client_auth_cache();
|
|
|
|
client_auth_cache->Remove(net::HostPortPair::FromURL(options.origin));
|
|
|
|
}
|
|
|
|
network_session->CloseAllConnections();
|
|
|
|
}
|
|
|
|
if (!callback.is_null())
|
|
|
|
RunCallbackInUI(callback);
|
|
|
|
}
|
|
|
|
|
2016-05-23 05:29:55 +00:00
|
|
|
void AllowNTLMCredentialsForDomainsInIO(
|
|
|
|
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
|
|
|
|
const std::string& domains) {
|
|
|
|
auto request_context = context_getter->GetURLRequestContext();
|
|
|
|
auto auth_handler = request_context->http_auth_handler_factory();
|
|
|
|
if (auth_handler) {
|
|
|
|
auto auth_preferences = const_cast<net::HttpAuthPreferences*>(
|
|
|
|
auth_handler->http_auth_preferences());
|
|
|
|
if (auth_preferences)
|
|
|
|
auth_preferences->set_server_whitelist(domains);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-15 06:13:56 +00:00
|
|
|
void OnClearStorageDataDone(const base::Closure& callback) {
|
2016-07-15 13:47:40 +00:00
|
|
|
if (!callback.is_null())
|
|
|
|
callback.Run();
|
2016-07-15 06:13:56 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 10:43:43 +00:00
|
|
|
void DownloadIdCallback(content::DownloadManager* download_manager,
|
|
|
|
const base::FilePath& path,
|
|
|
|
const std::vector<GURL>& url_chain,
|
|
|
|
const std::string& mime_type,
|
|
|
|
int64_t offset,
|
|
|
|
int64_t length,
|
|
|
|
const std::string& last_modified,
|
|
|
|
const std::string& etag,
|
|
|
|
const base::Time& start_time,
|
|
|
|
uint32_t id) {
|
2016-11-24 15:16:39 +00:00
|
|
|
download_manager->CreateDownloadItem(
|
2016-11-18 10:43:43 +00:00
|
|
|
base::GenerateGUID(), id, path, path, url_chain, GURL(), GURL(), GURL(),
|
|
|
|
GURL(), mime_type, mime_type, start_time, base::Time(), etag,
|
|
|
|
last_modified, offset, length, std::string(),
|
|
|
|
content::DownloadItem::INTERRUPTED,
|
|
|
|
content::DownloadDangerType::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
|
|
|
|
content::DOWNLOAD_INTERRUPT_REASON_NETWORK_TIMEOUT, false);
|
|
|
|
}
|
|
|
|
|
2015-06-24 03:59:11 +00:00
|
|
|
} // namespace
|
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
Session::Session(v8::Isolate* isolate, AtomBrowserContext* browser_context)
|
2016-04-18 04:59:15 +00:00
|
|
|
: devtools_network_emulation_client_id_(base::GenerateGUID()),
|
|
|
|
browser_context_(browser_context) {
|
2016-09-21 23:24:03 +00:00
|
|
|
// Observe DownloadManager to get download notifications.
|
2015-09-06 02:30:59 +00:00
|
|
|
content::BrowserContext::GetDownloadManager(browser_context)->
|
|
|
|
AddObserver(this);
|
2016-04-25 01:17:54 +00:00
|
|
|
|
|
|
|
Init(isolate);
|
|
|
|
AttachAsUserData(browser_context);
|
2015-06-16 09:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Session::~Session() {
|
2015-09-06 02:30:59 +00:00
|
|
|
content::BrowserContext::GetDownloadManager(browser_context())->
|
|
|
|
RemoveObserver(this);
|
2016-08-01 11:11:17 +00:00
|
|
|
g_sessions.erase(weak_map_id());
|
2015-08-30 18:52:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Session::OnDownloadCreated(content::DownloadManager* manager,
|
2015-09-20 10:45:34 +00:00
|
|
|
content::DownloadItem* item) {
|
2016-04-27 14:59:39 +00:00
|
|
|
if (item->IsSavePackageDownload())
|
2015-10-14 04:41:31 +00:00
|
|
|
return;
|
2016-04-25 01:17:54 +00:00
|
|
|
|
|
|
|
v8::Locker locker(isolate());
|
|
|
|
v8::HandleScope handle_scope(isolate());
|
2016-11-18 10:43:43 +00:00
|
|
|
auto handle = DownloadItem::Create(isolate(), item);
|
|
|
|
if (item->GetState() == content::DownloadItem::INTERRUPTED)
|
|
|
|
handle->SetSavePath(item->GetTargetFilePath());
|
|
|
|
bool prevent_default = Emit("will-download", handle, item->GetWebContents());
|
2015-08-30 18:52:01 +00:00
|
|
|
if (prevent_default) {
|
|
|
|
item->Cancel(true);
|
|
|
|
item->Remove();
|
|
|
|
}
|
2015-06-16 09:23:29 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 03:59:11 +00:00
|
|
|
void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) {
|
2015-09-05 14:39:48 +00:00
|
|
|
new ResolveProxyHelper(browser_context(), url, callback);
|
2015-06-24 03:59:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 09:31:54 +00:00
|
|
|
template<Session::CacheAction action>
|
|
|
|
void Session::DoCacheAction(const net::CompletionCallback& callback) {
|
2015-07-13 21:27:07 +00:00
|
|
|
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
2016-01-14 12:48:14 +00:00
|
|
|
base::Bind(&DoCacheActionInIO,
|
2016-07-04 10:19:20 +00:00
|
|
|
make_scoped_refptr(browser_context_->GetRequestContext()),
|
2016-01-14 09:31:54 +00:00
|
|
|
action,
|
2015-07-13 21:27:07 +00:00
|
|
|
callback));
|
2015-07-02 14:12:18 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 22:13:17 +00:00
|
|
|
void Session::ClearStorageData(mate::Arguments* args) {
|
2016-07-15 06:13:56 +00:00
|
|
|
// clearStorageData([options, callback])
|
2015-07-13 22:13:17 +00:00
|
|
|
ClearStorageDataOptions options;
|
|
|
|
base::Closure callback;
|
|
|
|
args->GetNext(&options);
|
2016-07-15 13:47:40 +00:00
|
|
|
args->GetNext(&callback);
|
2015-07-13 22:13:17 +00:00
|
|
|
|
2015-07-02 14:12:18 +00:00
|
|
|
auto storage_partition =
|
2015-09-05 14:39:48 +00:00
|
|
|
content::BrowserContext::GetStoragePartition(browser_context(), nullptr);
|
2016-12-03 11:14:33 +00:00
|
|
|
if (options.storage_types & StoragePartition::REMOVE_DATA_MASK_COOKIES) {
|
|
|
|
// Reset media device id salt when cookies are cleared.
|
|
|
|
// https://w3c.github.io/mediacapture-main/#dom-mediadeviceinfo-deviceid
|
|
|
|
brightray::MediaDeviceIDSalt::Reset(browser_context()->prefs());
|
|
|
|
}
|
2015-07-02 14:12:18 +00:00
|
|
|
storage_partition->ClearData(
|
2015-07-13 22:13:17 +00:00
|
|
|
options.storage_types, options.quota_types, options.origin,
|
2015-07-02 14:12:18 +00:00
|
|
|
content::StoragePartition::OriginMatcherFunction(),
|
2016-07-15 13:47:40 +00:00
|
|
|
base::Time(), base::Time::Max(),
|
|
|
|
base::Bind(&OnClearStorageDataDone, callback));
|
2015-07-02 14:12:18 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 15:28:12 +00:00
|
|
|
void Session::FlushStorageData() {
|
|
|
|
auto storage_partition =
|
|
|
|
content::BrowserContext::GetStoragePartition(browser_context(), nullptr);
|
|
|
|
storage_partition->Flush();
|
|
|
|
}
|
|
|
|
|
2015-10-22 17:26:36 +00:00
|
|
|
void Session::SetProxy(const net::ProxyConfig& config,
|
2015-07-16 14:36:48 +00:00
|
|
|
const base::Closure& callback) {
|
2016-07-04 10:19:20 +00:00
|
|
|
auto getter = browser_context_->GetRequestContext();
|
2015-07-16 14:36:48 +00:00
|
|
|
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
2015-10-22 17:26:36 +00:00
|
|
|
base::Bind(&SetProxyInIO, base::Unretained(getter), config, callback));
|
2015-07-16 14:36:48 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 02:38:04 +00:00
|
|
|
void Session::SetDownloadPath(const base::FilePath& path) {
|
|
|
|
browser_context_->prefs()->SetFilePath(
|
|
|
|
prefs::kDownloadDefaultDirectory, path);
|
2015-07-26 08:30:02 +00:00
|
|
|
}
|
|
|
|
|
2015-09-27 13:19:52 +00:00
|
|
|
void Session::EnableNetworkEmulation(const mate::Dictionary& options) {
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<brightray::DevToolsNetworkConditions> conditions;
|
2015-09-27 13:19:52 +00:00
|
|
|
bool offline = false;
|
2016-08-22 23:26:31 +00:00
|
|
|
double latency = 0.0, download_throughput = 0.0, upload_throughput = 0.0;
|
2015-09-27 13:19:52 +00:00
|
|
|
if (options.Get("offline", &offline) && offline) {
|
|
|
|
conditions.reset(new brightray::DevToolsNetworkConditions(offline));
|
|
|
|
} else {
|
|
|
|
options.Get("latency", &latency);
|
|
|
|
options.Get("downloadThroughput", &download_throughput);
|
|
|
|
options.Get("uploadThroughput", &upload_throughput);
|
|
|
|
conditions.reset(
|
|
|
|
new brightray::DevToolsNetworkConditions(false,
|
|
|
|
latency,
|
|
|
|
download_throughput,
|
|
|
|
upload_throughput));
|
|
|
|
}
|
|
|
|
|
2016-04-18 04:59:15 +00:00
|
|
|
browser_context_->network_controller_handle()->SetNetworkState(
|
|
|
|
devtools_network_emulation_client_id_, std::move(conditions));
|
2016-04-18 06:05:24 +00:00
|
|
|
browser_context_->network_delegate()->SetDevToolsNetworkEmulationClientId(
|
|
|
|
devtools_network_emulation_client_id_);
|
2015-09-27 13:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Session::DisableNetworkEmulation() {
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<brightray::DevToolsNetworkConditions> conditions;
|
2016-04-18 04:59:15 +00:00
|
|
|
browser_context_->network_controller_handle()->SetNetworkState(
|
|
|
|
devtools_network_emulation_client_id_, std::move(conditions));
|
2016-04-18 06:05:24 +00:00
|
|
|
browser_context_->network_delegate()->SetDevToolsNetworkEmulationClientId(
|
|
|
|
std::string());
|
2015-09-27 13:19:52 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 03:45:15 +00:00
|
|
|
void Session::SetCertVerifyProc(v8::Local<v8::Value> val,
|
|
|
|
mate::Arguments* args) {
|
2015-11-18 03:17:08 +00:00
|
|
|
AtomCertVerifier::VerifyProc proc;
|
2015-11-18 03:45:15 +00:00
|
|
|
if (!(val->IsNull() || mate::ConvertFromV8(args->isolate(), val, &proc))) {
|
2015-11-18 03:17:08 +00:00
|
|
|
args->ThrowError("Must pass null or function");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-13 03:05:57 +00:00
|
|
|
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&SetCertVerifyProcInIO,
|
|
|
|
make_scoped_refptr(browser_context_->GetRequestContext()),
|
|
|
|
proc));
|
2015-11-18 03:17:08 +00:00
|
|
|
}
|
|
|
|
|
2016-01-31 21:35:34 +00:00
|
|
|
void Session::SetPermissionRequestHandler(v8::Local<v8::Value> val,
|
2016-02-01 12:15:53 +00:00
|
|
|
mate::Arguments* args) {
|
2016-01-31 21:35:34 +00:00
|
|
|
AtomPermissionManager::RequestHandler handler;
|
|
|
|
if (!(val->IsNull() || mate::ConvertFromV8(args->isolate(), val, &handler))) {
|
|
|
|
args->ThrowError("Must pass null or function");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto permission_manager = static_cast<AtomPermissionManager*>(
|
|
|
|
browser_context()->GetPermissionManager());
|
|
|
|
permission_manager->SetPermissionRequestHandler(handler);
|
|
|
|
}
|
|
|
|
|
2016-02-01 13:33:23 +00:00
|
|
|
void Session::ClearHostResolverCache(mate::Arguments* args) {
|
|
|
|
base::Closure callback;
|
|
|
|
args->GetNext(&callback);
|
|
|
|
|
|
|
|
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&ClearHostResolverCacheInIO,
|
2016-07-04 10:19:20 +00:00
|
|
|
make_scoped_refptr(browser_context_->GetRequestContext()),
|
2016-02-01 13:33:23 +00:00
|
|
|
callback));
|
|
|
|
}
|
|
|
|
|
2016-11-30 16:35:15 +00:00
|
|
|
void Session::ClearAuthCache(mate::Arguments* args) {
|
|
|
|
ClearAuthCacheOptions options;
|
2016-12-09 18:07:04 +00:00
|
|
|
if (!args->GetNext(&options)) {
|
|
|
|
args->ThrowError("Must specify options object");
|
|
|
|
return;
|
|
|
|
}
|
2016-11-30 16:35:15 +00:00
|
|
|
base::Closure callback;
|
|
|
|
args->GetNext(&callback);
|
|
|
|
|
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&ClearAuthCacheInIO,
|
|
|
|
make_scoped_refptr(browser_context_->GetRequestContext()),
|
|
|
|
options, callback));
|
|
|
|
}
|
|
|
|
|
2016-05-23 05:29:55 +00:00
|
|
|
void Session::AllowNTLMCredentialsForDomains(const std::string& domains) {
|
|
|
|
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&AllowNTLMCredentialsForDomainsInIO,
|
2016-07-04 10:19:20 +00:00
|
|
|
make_scoped_refptr(browser_context_->GetRequestContext()),
|
2016-05-23 05:29:55 +00:00
|
|
|
domains));
|
|
|
|
}
|
|
|
|
|
2016-06-22 06:57:51 +00:00
|
|
|
void Session::SetUserAgent(const std::string& user_agent,
|
|
|
|
mate::Arguments* args) {
|
|
|
|
browser_context_->SetUserAgent(user_agent);
|
|
|
|
|
|
|
|
std::string accept_lang = l10n_util::GetApplicationLocale("");
|
|
|
|
args->GetNext(&accept_lang);
|
|
|
|
|
2016-07-04 10:19:20 +00:00
|
|
|
auto getter = browser_context_->GetRequestContext();
|
2016-06-22 06:57:51 +00:00
|
|
|
getter->GetNetworkTaskRunner()->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&SetUserAgentInIO, getter, accept_lang, user_agent));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Session::GetUserAgent() {
|
|
|
|
return browser_context_->GetUserAgent();
|
|
|
|
}
|
|
|
|
|
2016-08-31 00:08:32 +00:00
|
|
|
void Session::GetBlobData(
|
|
|
|
const std::string& uuid,
|
|
|
|
const AtomBlobReader::CompletionCallback& callback) {
|
|
|
|
if (callback.is_null())
|
2016-08-23 05:57:43 +00:00
|
|
|
return;
|
|
|
|
|
2016-08-31 00:08:32 +00:00
|
|
|
AtomBlobReader* blob_reader =
|
|
|
|
browser_context()->GetBlobReader();
|
2016-08-23 00:39:06 +00:00
|
|
|
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
2016-08-31 00:08:32 +00:00
|
|
|
base::Bind(&AtomBlobReader::StartReading,
|
|
|
|
base::Unretained(blob_reader),
|
|
|
|
uuid,
|
2016-08-23 00:39:06 +00:00
|
|
|
callback));
|
|
|
|
}
|
|
|
|
|
2016-11-18 10:43:43 +00:00
|
|
|
void Session::CreateInterruptedDownload(const mate::Dictionary& options) {
|
|
|
|
int64_t offset = 0, length = 0;
|
|
|
|
double start_time = 0.0;
|
|
|
|
std::string mime_type, last_modified, etag;
|
|
|
|
base::FilePath path;
|
|
|
|
std::vector<GURL> url_chain;
|
|
|
|
options.Get("path", &path);
|
|
|
|
options.Get("urlChain", &url_chain);
|
|
|
|
options.Get("mimeType", &mime_type);
|
|
|
|
options.Get("offset", &offset);
|
|
|
|
options.Get("length", &length);
|
|
|
|
options.Get("lastModified", &last_modified);
|
|
|
|
options.Get("eTag", &etag);
|
|
|
|
options.Get("startTime", &start_time);
|
2016-12-07 07:28:53 +00:00
|
|
|
if (path.empty() || url_chain.empty() || length == 0) {
|
|
|
|
isolate()->ThrowException(v8::Exception::Error(mate::StringToV8(
|
|
|
|
isolate(), "Must pass non-empty path, urlChain and length.")));
|
2016-11-18 10:43:43 +00:00
|
|
|
return;
|
2016-12-07 07:28:53 +00:00
|
|
|
}
|
|
|
|
if (offset >= length) {
|
|
|
|
isolate()->ThrowException(v8::Exception::Error(mate::StringToV8(
|
|
|
|
isolate(), "Must pass an offset value less than length.")));
|
|
|
|
return;
|
|
|
|
}
|
2016-11-18 10:43:43 +00:00
|
|
|
auto download_manager =
|
|
|
|
content::BrowserContext::GetDownloadManager(browser_context());
|
|
|
|
download_manager->GetDelegate()->GetNextId(base::Bind(
|
|
|
|
&DownloadIdCallback, download_manager, path, url_chain, mime_type, offset,
|
|
|
|
length, last_modified, etag, base::Time::FromDoubleT(start_time)));
|
|
|
|
}
|
|
|
|
|
2015-06-16 09:23:29 +00:00
|
|
|
v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) {
|
|
|
|
if (cookies_.IsEmpty()) {
|
2016-09-21 23:24:03 +00:00
|
|
|
auto handle = Cookies::Create(isolate, browser_context());
|
2015-06-16 09:23:29 +00:00
|
|
|
cookies_.Reset(isolate, handle.ToV8());
|
|
|
|
}
|
|
|
|
return v8::Local<v8::Value>::New(isolate, cookies_);
|
|
|
|
}
|
|
|
|
|
2016-06-08 14:31:27 +00:00
|
|
|
v8::Local<v8::Value> Session::Protocol(v8::Isolate* isolate) {
|
|
|
|
if (protocol_.IsEmpty()) {
|
|
|
|
auto handle = atom::api::Protocol::Create(isolate, browser_context());
|
|
|
|
protocol_.Reset(isolate, handle.ToV8());
|
|
|
|
}
|
|
|
|
return v8::Local<v8::Value>::New(isolate, protocol_);
|
|
|
|
}
|
|
|
|
|
2015-12-01 04:52:22 +00:00
|
|
|
v8::Local<v8::Value> Session::WebRequest(v8::Isolate* isolate) {
|
|
|
|
if (web_request_.IsEmpty()) {
|
|
|
|
auto handle = atom::api::WebRequest::Create(isolate, browser_context());
|
|
|
|
web_request_.Reset(isolate, handle.ToV8());
|
|
|
|
}
|
|
|
|
return v8::Local<v8::Value>::New(isolate, web_request_);
|
|
|
|
}
|
|
|
|
|
2015-06-16 09:23:29 +00:00
|
|
|
// static
|
2015-06-24 07:37:04 +00:00
|
|
|
mate::Handle<Session> Session::CreateFrom(
|
2015-09-05 14:39:48 +00:00
|
|
|
v8::Isolate* isolate, AtomBrowserContext* browser_context) {
|
2015-06-24 07:37:04 +00:00
|
|
|
auto existing = TrackableObject::FromWrappedClass(isolate, browser_context);
|
|
|
|
if (existing)
|
|
|
|
return mate::CreateHandle(isolate, static_cast<Session*>(existing));
|
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
auto handle = mate::CreateHandle(
|
|
|
|
isolate, new Session(isolate, browser_context));
|
2016-08-01 11:11:17 +00:00
|
|
|
|
|
|
|
// The Sessions should never be garbage collected, since the common pattern is
|
|
|
|
// to use partition strings, instead of using the Session object directly.
|
|
|
|
g_sessions[handle->weak_map_id()] =
|
|
|
|
v8::Global<v8::Object>(isolate, handle.ToV8());
|
|
|
|
|
2015-09-01 10:02:14 +00:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2015-09-05 14:39:48 +00:00
|
|
|
// static
|
|
|
|
mate::Handle<Session> Session::FromPartition(
|
2016-07-12 12:53:19 +00:00
|
|
|
v8::Isolate* isolate, const std::string& partition,
|
|
|
|
const base::DictionaryValue& options) {
|
2016-07-12 12:39:54 +00:00
|
|
|
scoped_refptr<AtomBrowserContext> browser_context;
|
2016-07-12 12:01:49 +00:00
|
|
|
if (partition.empty()) {
|
2016-07-12 12:53:19 +00:00
|
|
|
browser_context = AtomBrowserContext::From("", false, options);
|
2016-07-12 12:01:49 +00:00
|
|
|
} else if (base::StartsWith(partition, kPersistPrefix,
|
|
|
|
base::CompareCase::SENSITIVE)) {
|
|
|
|
std::string name = partition.substr(8);
|
2016-07-12 12:53:19 +00:00
|
|
|
browser_context = AtomBrowserContext::From(name, false, options);
|
2016-07-12 12:01:49 +00:00
|
|
|
} else {
|
2016-07-12 12:53:19 +00:00
|
|
|
browser_context = AtomBrowserContext::From(partition, true, options);
|
2016-07-12 12:01:49 +00:00
|
|
|
}
|
2016-07-12 12:39:54 +00:00
|
|
|
return CreateFrom(isolate, browser_context.get());
|
2015-09-05 14:39:48 +00:00
|
|
|
}
|
|
|
|
|
2015-12-03 08:04:46 +00:00
|
|
|
// static
|
|
|
|
void Session::BuildPrototype(v8::Isolate* isolate,
|
2016-08-02 09:08:12 +00:00
|
|
|
v8::Local<v8::FunctionTemplate> prototype) {
|
2016-08-02 10:28:12 +00:00
|
|
|
prototype->SetClassName(mate::StringToV8(isolate, "Session"));
|
2016-08-02 09:08:12 +00:00
|
|
|
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
2015-12-03 08:04:46 +00:00
|
|
|
.MakeDestroyable()
|
|
|
|
.SetMethod("resolveProxy", &Session::ResolveProxy)
|
2016-01-14 09:31:54 +00:00
|
|
|
.SetMethod("getCacheSize", &Session::DoCacheAction<CacheAction::STATS>)
|
|
|
|
.SetMethod("clearCache", &Session::DoCacheAction<CacheAction::CLEAR>)
|
2015-12-03 08:04:46 +00:00
|
|
|
.SetMethod("clearStorageData", &Session::ClearStorageData)
|
2016-01-12 15:28:12 +00:00
|
|
|
.SetMethod("flushStorageData", &Session::FlushStorageData)
|
2015-12-03 08:04:46 +00:00
|
|
|
.SetMethod("setProxy", &Session::SetProxy)
|
|
|
|
.SetMethod("setDownloadPath", &Session::SetDownloadPath)
|
|
|
|
.SetMethod("enableNetworkEmulation", &Session::EnableNetworkEmulation)
|
|
|
|
.SetMethod("disableNetworkEmulation", &Session::DisableNetworkEmulation)
|
2016-11-09 13:05:46 +00:00
|
|
|
.SetMethod("_setCertificateVerifyProc", &Session::SetCertVerifyProc)
|
2016-01-31 21:35:34 +00:00
|
|
|
.SetMethod("setPermissionRequestHandler",
|
|
|
|
&Session::SetPermissionRequestHandler)
|
2016-02-01 13:33:23 +00:00
|
|
|
.SetMethod("clearHostResolverCache", &Session::ClearHostResolverCache)
|
2016-11-30 16:35:15 +00:00
|
|
|
.SetMethod("clearAuthCache", &Session::ClearAuthCache)
|
2016-05-23 05:29:55 +00:00
|
|
|
.SetMethod("allowNTLMCredentialsForDomains",
|
|
|
|
&Session::AllowNTLMCredentialsForDomains)
|
2016-06-22 06:57:51 +00:00
|
|
|
.SetMethod("setUserAgent", &Session::SetUserAgent)
|
|
|
|
.SetMethod("getUserAgent", &Session::GetUserAgent)
|
2016-08-23 05:57:43 +00:00
|
|
|
.SetMethod("getBlobData", &Session::GetBlobData)
|
2016-11-18 10:43:43 +00:00
|
|
|
.SetMethod("createInterruptedDownload",
|
|
|
|
&Session::CreateInterruptedDownload)
|
2015-12-01 04:52:22 +00:00
|
|
|
.SetProperty("cookies", &Session::Cookies)
|
2016-06-08 14:31:27 +00:00
|
|
|
.SetProperty("protocol", &Session::Protocol)
|
2015-12-01 04:52:22 +00:00
|
|
|
.SetProperty("webRequest", &Session::WebRequest);
|
2015-12-03 08:04:46 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 09:23:29 +00:00
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|
2015-09-01 10:02:14 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-08-02 11:38:35 +00:00
|
|
|
using atom::api::Session;
|
|
|
|
|
2016-07-12 11:21:59 +00:00
|
|
|
v8::Local<v8::Value> FromPartition(
|
2016-07-12 12:01:49 +00:00
|
|
|
const std::string& partition, mate::Arguments* args) {
|
2016-07-12 11:21:59 +00:00
|
|
|
if (!atom::Browser::Get()->is_ready()) {
|
|
|
|
args->ThrowError("Session can only be received when app is ready");
|
|
|
|
return v8::Null(args->isolate());
|
|
|
|
}
|
2016-07-12 12:53:19 +00:00
|
|
|
base::DictionaryValue options;
|
|
|
|
args->GetNext(&options);
|
2016-08-02 11:38:35 +00:00
|
|
|
return Session::FromPartition(args->isolate(), partition, options).ToV8();
|
2016-07-12 11:21:59 +00:00
|
|
|
}
|
|
|
|
|
2015-09-01 10:02:14 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context, void* priv) {
|
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
|
|
|
mate::Dictionary dict(isolate, exports);
|
2016-08-02 11:38:35 +00:00
|
|
|
dict.Set("Session", Session::GetConstructor(isolate)->GetFunction());
|
2016-09-21 23:24:03 +00:00
|
|
|
dict.Set("Cookies", Cookies::GetConstructor(isolate)->GetFunction());
|
2016-07-12 11:21:59 +00:00
|
|
|
dict.SetMethod("fromPartition", &FromPartition);
|
2015-09-01 10:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_session, Initialize)
|