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"
|
|
|
|
|
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-06-23 15:40:41 +00:00
|
|
|
#include "atom/browser/atom_browser_context.h"
|
2015-08-07 10:10:19 +00:00
|
|
|
#include "atom/common/native_mate_converters/callback.h"
|
2015-06-24 03:59:11 +00:00
|
|
|
#include "atom/common/native_mate_converters/gurl_converter.h"
|
2015-07-30 02:38:04 +00:00
|
|
|
#include "atom/common/native_mate_converters/file_path_converter.h"
|
2015-07-26 08:30:02 +00:00
|
|
|
#include "base/files/file_path.h"
|
|
|
|
#include "base/prefs/pref_service.h"
|
2015-07-02 14:12:18 +00:00
|
|
|
#include "base/strings/string_util.h"
|
2015-07-26 08:30:02 +00:00
|
|
|
#include "base/thread_task_runner_handle.h"
|
|
|
|
#include "chrome/common/pref_names.h"
|
2015-06-25 08:43:01 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
#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"
|
2015-06-24 03:59:11 +00:00
|
|
|
#include "net/proxy/proxy_service.h"
|
2015-07-16 14:36:48 +00:00
|
|
|
#include "net/proxy/proxy_config_service_fixed.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"
|
2015-06-16 09:23:29 +00:00
|
|
|
|
|
|
|
#include "atom/common/node_includes.h"
|
|
|
|
|
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;
|
|
|
|
uint32 storage_types = StoragePartition::REMOVE_DATA_MASK_ALL;
|
|
|
|
uint32 quota_types = StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL;
|
|
|
|
};
|
2015-07-02 14:12:18 +00:00
|
|
|
|
2015-07-13 22:13:17 +00:00
|
|
|
uint32 GetStorageMask(const std::vector<std::string>& storage_types) {
|
|
|
|
uint32 storage_mask = 0;
|
|
|
|
for (const auto& it : storage_types) {
|
2015-07-02 14:12:18 +00:00
|
|
|
auto type = base::StringToLowerASCII(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;
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:13:17 +00:00
|
|
|
uint32 GetQuotaMask(const std::vector<std::string>& quota_types) {
|
|
|
|
uint32 quota_mask = 0;
|
|
|
|
for (const auto& it : quota_types) {
|
|
|
|
auto type = base::StringToLowerASCII(it);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // 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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mate
|
|
|
|
|
2015-06-16 09:23:29 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2015-06-24 03:59:11 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
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 =
|
|
|
|
browser_context->GetRequestContext();
|
|
|
|
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(
|
|
|
|
url, net::LOAD_NORMAL, &proxy_info_, completion_callback,
|
|
|
|
&pac_req_, nullptr, net::BoundNetLog());
|
|
|
|
|
|
|
|
// 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.
|
2015-07-16 14:36:48 +00:00
|
|
|
template <typename ...T>
|
|
|
|
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,
|
|
|
|
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) {
|
2015-07-16 14:36:48 +00:00
|
|
|
(*backend_ptr)->DoomAllEntries(base::Bind(&RunCallbackInUI<int>, callback));
|
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
|
|
|
}
|
|
|
|
|
2015-08-10 07:02:16 +00:00
|
|
|
void ClearHttpCacheInIO(
|
|
|
|
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
|
|
|
|
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*;
|
|
|
|
BackendPtr* backend_ptr = new BackendPtr(nullptr);
|
2015-07-13 21:27:07 +00:00
|
|
|
net::CompletionCallback on_get_backend =
|
|
|
|
base::Bind(&OnGetBackend, base::Owned(backend_ptr), callback);
|
|
|
|
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,
|
|
|
|
const std::string& proxy,
|
|
|
|
const base::Closure& callback) {
|
|
|
|
net::ProxyConfig config;
|
|
|
|
config.proxy_rules().ParseFromString(proxy);
|
|
|
|
auto proxy_service = getter->GetURLRequestContext()->proxy_service();
|
|
|
|
proxy_service->ResetConfigService(new net::ProxyConfigServiceFixed(config));
|
|
|
|
RunCallbackInUI(callback);
|
|
|
|
}
|
|
|
|
|
2015-06-24 03:59:11 +00:00
|
|
|
} // namespace
|
|
|
|
|
2015-06-23 15:40:41 +00:00
|
|
|
Session::Session(AtomBrowserContext* browser_context)
|
|
|
|
: browser_context_(browser_context) {
|
2015-06-24 07:37:04 +00:00
|
|
|
AttachAsUserData(browser_context);
|
2015-06-16 09:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Session::~Session() {
|
|
|
|
}
|
|
|
|
|
2015-06-24 03:59:11 +00:00
|
|
|
void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) {
|
|
|
|
new ResolveProxyHelper(browser_context_, url, callback);
|
|
|
|
}
|
|
|
|
|
2015-07-13 21:27:07 +00:00
|
|
|
void Session::ClearCache(const net::CompletionCallback& callback) {
|
|
|
|
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&ClearHttpCacheInIO,
|
2015-08-10 07:02:16 +00:00
|
|
|
make_scoped_refptr(browser_context_->GetRequestContext()),
|
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) {
|
|
|
|
// clearStorageData([options, ]callback)
|
|
|
|
ClearStorageDataOptions options;
|
|
|
|
args->GetNext(&options);
|
|
|
|
base::Closure callback;
|
|
|
|
if (!args->GetNext(&callback)) {
|
|
|
|
args->ThrowError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-02 14:12:18 +00:00
|
|
|
auto storage_partition =
|
|
|
|
content::BrowserContext::GetStoragePartition(browser_context_, nullptr);
|
|
|
|
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(),
|
|
|
|
base::Time(), base::Time::Max(), callback);
|
|
|
|
}
|
|
|
|
|
2015-07-16 14:36:48 +00:00
|
|
|
void Session::SetProxy(const std::string& proxy,
|
|
|
|
const base::Closure& callback) {
|
|
|
|
auto getter = browser_context_->GetRequestContext();
|
|
|
|
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&SetProxyInIO, base::Unretained(getter), proxy, callback));
|
|
|
|
}
|
|
|
|
|
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-06-16 09:23:29 +00:00
|
|
|
v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) {
|
|
|
|
if (cookies_.IsEmpty()) {
|
2015-06-20 02:41:40 +00:00
|
|
|
auto handle = atom::api::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_);
|
|
|
|
}
|
|
|
|
|
|
|
|
mate::ObjectTemplateBuilder Session::GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) {
|
|
|
|
return mate::ObjectTemplateBuilder(isolate)
|
2015-06-24 03:59:11 +00:00
|
|
|
.SetMethod("resolveProxy", &Session::ResolveProxy)
|
2015-07-02 14:12:18 +00:00
|
|
|
.SetMethod("clearCache", &Session::ClearCache)
|
|
|
|
.SetMethod("clearStorageData", &Session::ClearStorageData)
|
2015-07-16 14:36:48 +00:00
|
|
|
.SetMethod("setProxy", &Session::SetProxy)
|
2015-07-26 08:30:02 +00:00
|
|
|
.SetMethod("setDownloadPath", &Session::SetDownloadPath)
|
2015-06-16 09:23:29 +00:00
|
|
|
.SetProperty("cookies", &Session::Cookies);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2015-06-24 07:37:04 +00:00
|
|
|
mate::Handle<Session> Session::CreateFrom(
|
2015-06-20 02:41:40 +00:00
|
|
|
v8::Isolate* isolate,
|
2015-06-23 15:40:41 +00:00
|
|
|
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));
|
|
|
|
|
2015-06-23 02:18:43 +00:00
|
|
|
return mate::CreateHandle(isolate, new Session(browser_context));
|
2015-06-16 09:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|