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.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/api/atom_api_session.h"
|
2015-06-16 09:23:29 +00:00
|
|
|
|
2019-08-26 16:47:32 +00:00
|
|
|
#include <algorithm>
|
2016-08-01 11:11:17 +00:00
|
|
|
#include <map>
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <memory>
|
2015-06-24 03:59:11 +00:00
|
|
|
#include <string>
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <utility>
|
2015-07-13 22:13:17 +00:00
|
|
|
#include <vector>
|
2015-06-24 03:59:11 +00:00
|
|
|
|
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"
|
2019-01-12 01:00:43 +00:00
|
|
|
#include "base/task/post_task.h"
|
2018-01-02 12:03:46 +00:00
|
|
|
#include "chrome/browser/browser_process.h"
|
2015-07-26 08:30:02 +00:00
|
|
|
#include "chrome/common/pref_names.h"
|
2018-04-10 15:58:49 +00:00
|
|
|
#include "components/download/public/common/download_danger_type.h"
|
2019-08-29 03:27:20 +00:00
|
|
|
#include "components/download/public/common/download_url_parameters.h"
|
2016-08-26 22:30:02 +00:00
|
|
|
#include "components/prefs/pref_service.h"
|
2018-10-04 18:08:56 +00:00
|
|
|
#include "components/prefs/value_map_pref_store.h"
|
|
|
|
#include "components/proxy_config/proxy_config_dictionary.h"
|
|
|
|
#include "components/proxy_config/proxy_config_pref_names.h"
|
2019-01-12 01:00:43 +00:00
|
|
|
#include "content/public/browser/browser_task_traits.h"
|
2015-06-25 08:43:01 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2018-04-10 15:29:26 +00:00
|
|
|
#include "content/public/browser/download_item_utils.h"
|
2016-11-18 10:43:43 +00:00
|
|
|
#include "content/public/browser/download_manager_delegate.h"
|
2019-05-29 23:43:06 +00:00
|
|
|
#include "content/public/browser/network_service_instance.h"
|
2015-06-25 08:43:01 +00:00
|
|
|
#include "content/public/browser/storage_partition.h"
|
2019-09-18 19:58:00 +00:00
|
|
|
#include "mojo/public/cpp/bindings/pending_remote.h"
|
|
|
|
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
|
2019-04-20 17:20:37 +00:00
|
|
|
#include "net/base/completion_repeating_callback.h"
|
2015-06-24 03:59:11 +00:00
|
|
|
#include "net/base/load_flags.h"
|
2016-05-23 05:29:55 +00:00
|
|
|
#include "net/http/http_auth_handler_factory.h"
|
|
|
|
#include "net/http/http_auth_preferences.h"
|
2018-10-04 18:08:56 +00:00
|
|
|
#include "net/http/http_cache.h"
|
2019-06-28 22:22:23 +00:00
|
|
|
#include "services/network/network_service.h"
|
2019-04-23 21:39:21 +00:00
|
|
|
#include "services/network/public/cpp/features.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/api/atom_api_cookies.h"
|
2019-09-03 22:54:14 +00:00
|
|
|
#include "shell/browser/api/atom_api_data_pipe_holder.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/api/atom_api_download_item.h"
|
|
|
|
#include "shell/browser/api/atom_api_net_log.h"
|
2019-10-24 00:51:06 +00:00
|
|
|
#include "shell/browser/api/atom_api_protocol.h"
|
|
|
|
#include "shell/browser/api/atom_api_web_request.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/atom_browser_context.h"
|
|
|
|
#include "shell/browser/atom_browser_main_parts.h"
|
|
|
|
#include "shell/browser/atom_permission_manager.h"
|
|
|
|
#include "shell/browser/browser.h"
|
|
|
|
#include "shell/browser/media/media_device_id_salt.h"
|
2019-08-07 15:04:09 +00:00
|
|
|
#include "shell/browser/net/cert_verifier_client.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/session_preferences.h"
|
2019-10-25 13:03:28 +00:00
|
|
|
#include "shell/common/gin_converters/callback_converter.h"
|
|
|
|
#include "shell/common/gin_converters/content_converter.h"
|
|
|
|
#include "shell/common/gin_converters/file_path_converter.h"
|
|
|
|
#include "shell/common/gin_converters/gurl_converter.h"
|
|
|
|
#include "shell/common/gin_converters/net_converter.h"
|
2019-10-31 07:56:00 +00:00
|
|
|
#include "shell/common/gin_converters/value_converter.h"
|
2019-10-25 13:03:28 +00:00
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
|
|
|
#include "shell/common/gin_helper/object_template_builder.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_includes.h"
|
|
|
|
#include "shell/common/options_switches.h"
|
2016-06-22 06:57:51 +00:00
|
|
|
#include "ui/base/l10n/l10n_util.h"
|
2015-06-16 09:23:29 +00:00
|
|
|
|
2019-07-24 23:01:08 +00:00
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
|
|
#include "shell/browser/extensions/atom_extension_system.h"
|
|
|
|
#endif
|
|
|
|
|
2019-10-31 20:11:51 +00:00
|
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
2019-11-26 21:16:43 +00:00
|
|
|
#include "chrome/browser/spellchecker/spellcheck_factory.h"
|
2019-10-31 20:11:51 +00:00
|
|
|
#include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h"
|
2019-11-26 21:16:43 +00:00
|
|
|
#include "chrome/browser/spellchecker/spellcheck_service.h"
|
2019-10-31 20:11:51 +00:00
|
|
|
#include "components/spellcheck/browser/pref_names.h"
|
|
|
|
#include "components/spellcheck/common/spellcheck_common.h"
|
2019-11-26 21:16:43 +00:00
|
|
|
|
|
|
|
#if BUILDFLAG(USE_BROWSER_SPELLCHECKER)
|
|
|
|
#include "components/spellcheck/browser/spellcheck_platform.h"
|
|
|
|
#include "components/spellcheck/common/spellcheck_features.h"
|
|
|
|
#endif
|
2019-10-31 20:11:51 +00:00
|
|
|
#endif
|
|
|
|
|
2015-07-02 14:12:18 +00:00
|
|
|
using content::BrowserThread;
|
|
|
|
using content::StoragePartition;
|
|
|
|
|
2019-11-05 23:41:20 +00:00
|
|
|
namespace predictors {
|
|
|
|
// NOTE(nornagon): this is copied from
|
|
|
|
// //chrome/browser/predictors/resource_prefetch_predictor.cc we don't need
|
|
|
|
// anything in that file other than this constructor. Without it we get a link
|
|
|
|
// error. Probably upstream the constructor should be moved to
|
|
|
|
// preconnect_manager.cc.
|
|
|
|
PreconnectRequest::PreconnectRequest(
|
|
|
|
const GURL& origin,
|
|
|
|
int num_sockets,
|
|
|
|
const net::NetworkIsolationKey& network_isolation_key)
|
|
|
|
: origin(origin),
|
|
|
|
num_sockets(num_sockets),
|
|
|
|
network_isolation_key(network_isolation_key) {
|
|
|
|
DCHECK_GE(num_sockets, 0);
|
|
|
|
}
|
|
|
|
} // namespace predictors
|
|
|
|
|
2015-07-02 14:12:18 +00:00
|
|
|
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-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;
|
2018-04-13 17:03:16 +00:00
|
|
|
else if (type == "cachestorage")
|
|
|
|
storage_mask |= StoragePartition::REMOVE_DATA_MASK_CACHE_STORAGE;
|
2015-07-02 14:12:18 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-10-25 13:03:28 +00:00
|
|
|
namespace gin {
|
2015-07-13 22:13:17 +00:00
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
template <>
|
2015-07-13 22:13:17 +00:00
|
|
|
struct Converter<ClearStorageDataOptions> {
|
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
|
|
|
ClearStorageDataOptions* out) {
|
2019-10-25 13:03:28 +00:00
|
|
|
gin_helper::Dictionary options;
|
2015-07-13 22:13:17 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-25 13:03:28 +00:00
|
|
|
} // namespace gin
|
2015-07-13 22:13:17 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2015-06-16 09:23:29 +00:00
|
|
|
|
|
|
|
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.
|
2019-10-25 13:03:28 +00:00
|
|
|
std::map<uint32_t, v8::Global<v8::Value>> g_sessions;
|
2016-08-01 11:11:17 +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(),
|
2019-05-13 21:24:39 +00:00
|
|
|
GURL(), base::nullopt, mime_type, mime_type, start_time, base::Time(),
|
|
|
|
etag, last_modified, offset, length, std::string(),
|
2018-04-10 15:29:26 +00:00
|
|
|
download::DownloadItem::INTERRUPTED,
|
2018-04-10 15:58:49 +00:00
|
|
|
download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
|
2018-04-10 16:08:50 +00:00
|
|
|
download::DOWNLOAD_INTERRUPT_REASON_NETWORK_TIMEOUT, false, base::Time(),
|
2018-04-10 15:29:26 +00:00
|
|
|
false, std::vector<download::DownloadItem::ReceivedSlice>());
|
2016-11-18 10:43:43 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 13:24:55 +00:00
|
|
|
void DestroyGlobalHandle(v8::Isolate* isolate,
|
|
|
|
const v8::Global<v8::Value>& global_handle) {
|
|
|
|
v8::Locker locker(isolate);
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
if (!global_handle.IsEmpty()) {
|
|
|
|
v8::Local<v8::Value> local_handle = global_handle.Get(isolate);
|
2019-04-20 17:20:37 +00:00
|
|
|
v8::Local<v8::Object> object;
|
|
|
|
if (local_handle->IsObject() &&
|
|
|
|
local_handle->ToObject(isolate->GetCurrentContext()).ToLocal(&object)) {
|
2018-03-30 13:24:55 +00:00
|
|
|
void* ptr = object->GetAlignedPointerFromInternalField(0);
|
|
|
|
if (!ptr)
|
|
|
|
return;
|
|
|
|
delete static_cast<mate::WrappableBase*>(ptr);
|
|
|
|
object->SetAlignedPointerInInternalField(0, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2018-10-02 18:08:10 +00:00
|
|
|
: network_emulation_token_(base::UnguessableToken::Create()),
|
2016-04-18 04:59:15 +00:00
|
|
|
browser_context_(browser_context) {
|
2016-09-21 23:24:03 +00:00
|
|
|
// Observe DownloadManager to get download notifications.
|
2018-04-18 01:55:30 +00:00
|
|
|
content::BrowserContext::GetDownloadManager(browser_context)
|
|
|
|
->AddObserver(this);
|
2016-04-25 01:17:54 +00:00
|
|
|
|
2017-12-05 06:59:15 +00:00
|
|
|
new SessionPreferences(browser_context);
|
|
|
|
|
2019-11-07 23:35:12 +00:00
|
|
|
protocol_.Reset(isolate, Protocol::Create(isolate, browser_context).ToV8());
|
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
Init(isolate);
|
|
|
|
AttachAsUserData(browser_context);
|
2015-06-16 09:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Session::~Session() {
|
2018-04-18 01:55:30 +00:00
|
|
|
content::BrowserContext::GetDownloadManager(browser_context())
|
|
|
|
->RemoveObserver(this);
|
2019-08-13 05:47:35 +00:00
|
|
|
// TODO(zcbenz): Now since URLRequestContextGetter is gone, is this still
|
|
|
|
// needed?
|
|
|
|
// Refs https://github.com/electron/electron/pull/12305.
|
2018-03-30 13:24:55 +00:00
|
|
|
DestroyGlobalHandle(isolate(), cookies_);
|
|
|
|
DestroyGlobalHandle(isolate(), protocol_);
|
2018-10-04 18:08:56 +00:00
|
|
|
DestroyGlobalHandle(isolate(), net_log_);
|
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,
|
2018-04-10 15:29:26 +00:00
|
|
|
download::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);
|
2018-04-10 15:29:26 +00:00
|
|
|
if (item->GetState() == download::DownloadItem::INTERRUPTED)
|
2016-11-18 10:43:43 +00:00
|
|
|
handle->SetSavePath(item->GetTargetFilePath());
|
2018-04-10 15:29:26 +00:00
|
|
|
content::WebContents* web_contents =
|
|
|
|
content::DownloadItemUtils::GetWebContents(item);
|
|
|
|
bool prevent_default = Emit("will-download", handle, web_contents);
|
2015-08-30 18:52:01 +00:00
|
|
|
if (prevent_default) {
|
|
|
|
item->Cancel(true);
|
|
|
|
item->Remove();
|
|
|
|
}
|
2015-06-16 09:23:29 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 13:03:28 +00:00
|
|
|
v8::Local<v8::Promise> Session::ResolveProxy(gin_helper::Arguments* args) {
|
2019-03-08 20:51:12 +00:00
|
|
|
v8::Isolate* isolate = args->isolate();
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<std::string> promise(isolate);
|
2019-03-08 20:51:12 +00:00
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
|
|
|
|
GURL url;
|
|
|
|
args->GetNext(&url);
|
|
|
|
|
|
|
|
browser_context_->GetResolveProxyHelper()->ResolveProxy(
|
2019-11-01 06:10:32 +00:00
|
|
|
url, base::BindOnce(gin_helper::Promise<std::string>::ResolvePromise,
|
2019-05-01 20:45:08 +00:00
|
|
|
std::move(promise)));
|
2019-03-08 20:51:12 +00:00
|
|
|
|
|
|
|
return handle;
|
2015-06-24 03:59:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 22:09:17 +00:00
|
|
|
v8::Local<v8::Promise> Session::GetCacheSize() {
|
|
|
|
auto* isolate = v8::Isolate::GetCurrent();
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<int64_t> promise(isolate);
|
2019-04-30 22:09:17 +00:00
|
|
|
auto handle = promise.GetHandle();
|
2019-03-08 22:42:03 +00:00
|
|
|
|
2019-04-30 22:09:17 +00:00
|
|
|
content::BrowserContext::GetDefaultStoragePartition(browser_context_.get())
|
|
|
|
->GetNetworkContext()
|
2019-08-23 00:03:28 +00:00
|
|
|
->ComputeHttpCacheSize(
|
|
|
|
base::Time(), base::Time::Max(),
|
|
|
|
base::BindOnce(
|
2019-11-01 06:10:32 +00:00
|
|
|
[](gin_helper::Promise<int64_t> promise, bool is_upper_bound,
|
2019-08-23 00:03:28 +00:00
|
|
|
int64_t size_or_error) {
|
|
|
|
if (size_or_error < 0) {
|
|
|
|
promise.RejectWithErrorMessage(
|
|
|
|
net::ErrorToString(size_or_error));
|
|
|
|
} else {
|
|
|
|
promise.Resolve(size_or_error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
std::move(promise)));
|
2019-04-30 22:09:17 +00:00
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Local<v8::Promise> Session::ClearCache() {
|
|
|
|
auto* isolate = v8::Isolate::GetCurrent();
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<void> promise(isolate);
|
2019-04-30 22:09:17 +00:00
|
|
|
auto handle = promise.GetHandle();
|
|
|
|
|
|
|
|
content::BrowserContext::GetDefaultStoragePartition(browser_context_.get())
|
|
|
|
->GetNetworkContext()
|
|
|
|
->ClearHttpCache(base::Time(), base::Time::Max(), nullptr,
|
2019-11-01 06:10:32 +00:00
|
|
|
base::BindOnce(gin_helper::Promise<void>::ResolvePromise,
|
2019-04-30 22:09:17 +00:00
|
|
|
std::move(promise)));
|
2019-03-08 22:42:03 +00:00
|
|
|
|
|
|
|
return handle;
|
2015-07-02 14:12:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 13:03:28 +00:00
|
|
|
v8::Local<v8::Promise> Session::ClearStorageData(gin_helper::Arguments* args) {
|
2019-03-08 17:02:30 +00:00
|
|
|
v8::Isolate* isolate = args->isolate();
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<void> promise(isolate);
|
2019-03-08 17:02:30 +00:00
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
|
2015-07-13 22:13:17 +00:00
|
|
|
ClearStorageDataOptions options;
|
|
|
|
args->GetNext(&options);
|
|
|
|
|
2018-04-17 22:41:47 +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
|
2018-10-19 18:51:43 +00:00
|
|
|
MediaDeviceIDSalt::Reset(browser_context()->prefs());
|
2016-12-03 11:14:33 +00:00
|
|
|
}
|
2019-03-08 17:02:30 +00:00
|
|
|
|
|
|
|
storage_partition->ClearData(
|
|
|
|
options.storage_types, options.quota_types, options.origin, base::Time(),
|
|
|
|
base::Time::Max(),
|
2019-11-01 06:10:32 +00:00
|
|
|
base::BindOnce(gin_helper::Promise<void>::ResolvePromise,
|
2019-08-23 00:03:28 +00:00
|
|
|
std::move(promise)));
|
2019-03-08 17:02:30 +00:00
|
|
|
return handle;
|
2015-07-02 14:12:18 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 15:28:12 +00:00
|
|
|
void Session::FlushStorageData() {
|
2018-04-17 22:41:47 +00:00
|
|
|
auto* storage_partition =
|
2016-01-12 15:28:12 +00:00
|
|
|
content::BrowserContext::GetStoragePartition(browser_context(), nullptr);
|
|
|
|
storage_partition->Flush();
|
|
|
|
}
|
|
|
|
|
2019-10-25 13:03:28 +00:00
|
|
|
v8::Local<v8::Promise> Session::SetProxy(gin_helper::Arguments* args) {
|
2019-03-08 20:51:12 +00:00
|
|
|
v8::Isolate* isolate = args->isolate();
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<void> promise(isolate);
|
2019-03-08 20:51:12 +00:00
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
|
2019-10-25 13:03:28 +00:00
|
|
|
gin_helper::Dictionary options;
|
2019-03-08 20:51:12 +00:00
|
|
|
args->GetNext(&options);
|
|
|
|
|
2018-10-04 18:08:56 +00:00
|
|
|
if (!browser_context_->in_memory_pref_store()) {
|
2019-03-08 20:51:12 +00:00
|
|
|
promise.Resolve();
|
|
|
|
return handle;
|
2018-10-04 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string proxy_rules, bypass_list, pac_url;
|
|
|
|
|
|
|
|
options.Get("pacScript", &pac_url);
|
|
|
|
options.Get("proxyRules", &proxy_rules);
|
|
|
|
options.Get("proxyBypassRules", &bypass_list);
|
|
|
|
|
|
|
|
// pacScript takes precedence over proxyRules.
|
|
|
|
if (!pac_url.empty()) {
|
|
|
|
browser_context_->in_memory_pref_store()->SetValue(
|
|
|
|
proxy_config::prefs::kProxy,
|
2018-10-25 16:48:25 +00:00
|
|
|
std::make_unique<base::Value>(ProxyConfigDictionary::CreatePacScript(
|
|
|
|
pac_url, true /* pac_mandatory */)),
|
2018-10-04 18:08:56 +00:00
|
|
|
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
|
|
|
|
} else {
|
|
|
|
browser_context_->in_memory_pref_store()->SetValue(
|
|
|
|
proxy_config::prefs::kProxy,
|
2018-10-25 16:48:25 +00:00
|
|
|
std::make_unique<base::Value>(ProxyConfigDictionary::CreateFixedServers(
|
|
|
|
proxy_rules, bypass_list)),
|
2018-10-04 18:08:56 +00:00
|
|
|
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
|
|
|
|
}
|
|
|
|
|
2019-03-08 20:51:12 +00:00
|
|
|
base::ThreadTaskRunnerHandle::Get()->PostTask(
|
2019-11-01 06:10:32 +00:00
|
|
|
FROM_HERE, base::BindOnce(gin_helper::Promise<void>::ResolvePromise,
|
2019-08-23 00:03:28 +00:00
|
|
|
std::move(promise)));
|
2019-03-08 20:51:12 +00:00
|
|
|
|
|
|
|
return handle;
|
2015-07-16 14:36:48 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 02:38:04 +00:00
|
|
|
void Session::SetDownloadPath(const base::FilePath& path) {
|
2018-04-18 01:55:30 +00:00
|
|
|
browser_context_->prefs()->SetFilePath(prefs::kDownloadDefaultDirectory,
|
|
|
|
path);
|
2015-07-26 08:30:02 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 13:03:28 +00:00
|
|
|
void Session::EnableNetworkEmulation(const gin_helper::Dictionary& options) {
|
2018-10-02 18:08:10 +00:00
|
|
|
auto conditions = network::mojom::NetworkConditions::New();
|
|
|
|
|
|
|
|
options.Get("offline", &conditions->offline);
|
|
|
|
options.Get("downloadThroughput", &conditions->download_throughput);
|
|
|
|
options.Get("uploadThroughput", &conditions->upload_throughput);
|
|
|
|
double latency = 0.0;
|
|
|
|
if (options.Get("latency", &latency) && latency) {
|
|
|
|
conditions->latency = base::TimeDelta::FromMillisecondsD(latency);
|
2015-09-27 13:19:52 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 18:08:10 +00:00
|
|
|
auto* network_context = content::BrowserContext::GetDefaultStoragePartition(
|
|
|
|
browser_context_.get())
|
|
|
|
->GetNetworkContext();
|
|
|
|
network_context->SetNetworkConditions(network_emulation_token_,
|
|
|
|
std::move(conditions));
|
2015-09-27 13:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Session::DisableNetworkEmulation() {
|
2018-10-02 18:08:10 +00:00
|
|
|
auto* network_context = content::BrowserContext::GetDefaultStoragePartition(
|
|
|
|
browser_context_.get())
|
|
|
|
->GetNetworkContext();
|
|
|
|
network_context->SetNetworkConditions(
|
|
|
|
network_emulation_token_, network::mojom::NetworkConditions::New());
|
2015-09-27 13:19:52 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 03:45:15 +00:00
|
|
|
void Session::SetCertVerifyProc(v8::Local<v8::Value> val,
|
2019-10-25 13:03:28 +00:00
|
|
|
gin_helper::Arguments* args) {
|
2019-08-07 15:04:09 +00:00
|
|
|
CertVerifierClient::CertVerifyProc proc;
|
2019-10-25 13:03:28 +00:00
|
|
|
if (!(val->IsNull() || gin::ConvertFromV8(args->isolate(), val, &proc))) {
|
2015-11-18 03:17:08 +00:00
|
|
|
args->ThrowError("Must pass null or function");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-18 19:58:00 +00:00
|
|
|
mojo::PendingRemote<network::mojom::CertVerifierClient>
|
|
|
|
cert_verifier_client_remote;
|
2019-08-02 23:56:46 +00:00
|
|
|
if (proc) {
|
2019-09-18 19:58:00 +00:00
|
|
|
mojo::MakeSelfOwnedReceiver(
|
|
|
|
std::make_unique<CertVerifierClient>(proc),
|
|
|
|
cert_verifier_client_remote.InitWithNewPipeAndPassReceiver());
|
2019-06-28 22:22:23 +00:00
|
|
|
}
|
2019-08-02 23:56:46 +00:00
|
|
|
content::BrowserContext::GetDefaultStoragePartition(browser_context_.get())
|
|
|
|
->GetNetworkContext()
|
2019-09-18 19:58:00 +00:00
|
|
|
->SetCertVerifierClient(std::move(cert_verifier_client_remote));
|
2019-08-02 23:56:46 +00:00
|
|
|
|
|
|
|
// This causes the cert verifier cache to be cleared.
|
|
|
|
content::GetNetworkService()->OnCertDBChanged();
|
2015-11-18 03:17:08 +00:00
|
|
|
}
|
|
|
|
|
2016-01-31 21:35:34 +00:00
|
|
|
void Session::SetPermissionRequestHandler(v8::Local<v8::Value> val,
|
2019-10-25 13:03:28 +00:00
|
|
|
gin_helper::Arguments* args) {
|
2019-05-03 21:20:43 +00:00
|
|
|
auto* permission_manager = static_cast<AtomPermissionManager*>(
|
|
|
|
browser_context()->GetPermissionControllerDelegate());
|
|
|
|
if (val->IsNull()) {
|
|
|
|
permission_manager->SetPermissionRequestHandler(
|
|
|
|
AtomPermissionManager::RequestHandler());
|
|
|
|
return;
|
|
|
|
}
|
2019-06-13 18:11:43 +00:00
|
|
|
auto handler = std::make_unique<AtomPermissionManager::RequestHandler>();
|
2019-10-25 13:03:28 +00:00
|
|
|
if (!gin::ConvertFromV8(args->isolate(), val, handler.get())) {
|
2016-01-31 21:35:34 +00:00
|
|
|
args->ThrowError("Must pass null or function");
|
|
|
|
return;
|
|
|
|
}
|
2019-05-03 18:30:48 +00:00
|
|
|
permission_manager->SetPermissionRequestHandler(base::BindRepeating(
|
2019-06-13 18:11:43 +00:00
|
|
|
[](AtomPermissionManager::RequestHandler* handler,
|
|
|
|
content::WebContents* web_contents,
|
2019-05-03 18:30:48 +00:00
|
|
|
content::PermissionType permission_type,
|
|
|
|
AtomPermissionManager::StatusCallback callback,
|
2019-06-13 18:11:43 +00:00
|
|
|
const base::Value& details) {
|
2019-05-03 21:20:43 +00:00
|
|
|
handler->Run(web_contents, permission_type,
|
|
|
|
base::AdaptCallbackForRepeating(std::move(callback)),
|
|
|
|
details);
|
2019-05-03 18:30:48 +00:00
|
|
|
},
|
2019-05-03 21:20:43 +00:00
|
|
|
base::Owned(std::move(handler))));
|
2016-01-31 21:35:34 +00:00
|
|
|
}
|
|
|
|
|
2018-08-28 14:05:08 +00:00
|
|
|
void Session::SetPermissionCheckHandler(v8::Local<v8::Value> val,
|
2019-10-25 13:03:28 +00:00
|
|
|
gin_helper::Arguments* args) {
|
2018-08-28 14:05:08 +00:00
|
|
|
AtomPermissionManager::CheckHandler handler;
|
2019-10-25 13:03:28 +00:00
|
|
|
if (!(val->IsNull() || gin::ConvertFromV8(args->isolate(), val, &handler))) {
|
2018-08-28 14:05:08 +00:00
|
|
|
args->ThrowError("Must pass null or function");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto* permission_manager = static_cast<AtomPermissionManager*>(
|
2018-10-02 21:53:10 +00:00
|
|
|
browser_context()->GetPermissionControllerDelegate());
|
2018-08-28 14:05:08 +00:00
|
|
|
permission_manager->SetPermissionCheckHandler(handler);
|
|
|
|
}
|
|
|
|
|
2019-10-25 13:03:28 +00:00
|
|
|
v8::Local<v8::Promise> Session::ClearHostResolverCache(
|
|
|
|
gin_helper::Arguments* args) {
|
2019-03-08 18:26:17 +00:00
|
|
|
v8::Isolate* isolate = args->isolate();
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<void> promise(isolate);
|
2019-03-08 18:26:17 +00:00
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
2016-02-01 13:33:23 +00:00
|
|
|
|
2019-04-25 17:51:48 +00:00
|
|
|
content::BrowserContext::GetDefaultStoragePartition(browser_context_.get())
|
|
|
|
->GetNetworkContext()
|
2019-05-03 21:30:25 +00:00
|
|
|
->ClearHostCache(nullptr,
|
2019-11-01 06:10:32 +00:00
|
|
|
base::BindOnce(gin_helper::Promise<void>::ResolvePromise,
|
2019-05-03 21:30:25 +00:00
|
|
|
std::move(promise)));
|
2019-04-25 17:51:48 +00:00
|
|
|
|
2019-03-08 18:26:17 +00:00
|
|
|
return handle;
|
2016-02-01 13:33:23 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:30:25 +00:00
|
|
|
v8::Local<v8::Promise> Session::ClearAuthCache() {
|
|
|
|
auto* isolate = v8::Isolate::GetCurrent();
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<void> promise(isolate);
|
2019-03-09 02:41:42 +00:00
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
|
2019-05-03 21:30:25 +00:00
|
|
|
content::BrowserContext::GetDefaultStoragePartition(browser_context_.get())
|
|
|
|
->GetNetworkContext()
|
2019-08-23 00:03:28 +00:00
|
|
|
->ClearHttpAuthCache(
|
|
|
|
base::Time(),
|
2019-11-01 06:10:32 +00:00
|
|
|
base::BindOnce(gin_helper::Promise<void>::ResolvePromise,
|
2019-08-23 00:03:28 +00:00
|
|
|
std::move(promise)));
|
2016-11-30 16:35:15 +00:00
|
|
|
|
2019-03-09 02:41:42 +00:00
|
|
|
return handle;
|
2016-11-30 16:35:15 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 05:29:55 +00:00
|
|
|
void Session::AllowNTLMCredentialsForDomains(const std::string& domains) {
|
2019-08-07 15:04:09 +00:00
|
|
|
network::mojom::HttpAuthDynamicParamsPtr auth_dynamic_params =
|
|
|
|
network::mojom::HttpAuthDynamicParams::New();
|
|
|
|
auth_dynamic_params->server_allowlist = domains;
|
|
|
|
content::GetNetworkService()->ConfigureHttpAuthPrefs(
|
|
|
|
std::move(auth_dynamic_params));
|
2016-05-23 05:29:55 +00:00
|
|
|
}
|
|
|
|
|
2016-06-22 06:57:51 +00:00
|
|
|
void Session::SetUserAgent(const std::string& user_agent,
|
2019-10-25 13:03:28 +00:00
|
|
|
gin_helper::Arguments* args) {
|
2019-08-29 06:50:14 +00:00
|
|
|
browser_context_->SetUserAgent(user_agent);
|
|
|
|
content::BrowserContext::GetDefaultStoragePartition(browser_context_.get())
|
|
|
|
->GetNetworkContext()
|
|
|
|
->SetUserAgent(user_agent);
|
2016-06-22 06:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Session::GetUserAgent() {
|
|
|
|
return browser_context_->GetUserAgent();
|
|
|
|
}
|
|
|
|
|
2019-03-14 15:11:01 +00:00
|
|
|
v8::Local<v8::Promise> Session::GetBlobData(v8::Isolate* isolate,
|
|
|
|
const std::string& uuid) {
|
2019-09-03 22:54:14 +00:00
|
|
|
gin::Handle<DataPipeHolder> holder = DataPipeHolder::From(isolate, uuid);
|
|
|
|
if (holder.IsEmpty()) {
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<v8::Local<v8::Value>> promise(isolate);
|
2019-09-03 22:54:14 +00:00
|
|
|
promise.RejectWithErrorMessage("Could not get blob data handle");
|
|
|
|
return promise.GetHandle();
|
|
|
|
}
|
2016-08-23 05:57:43 +00:00
|
|
|
|
2019-09-03 22:54:14 +00:00
|
|
|
return holder->ReadAll(isolate);
|
2016-08-23 00:39:06 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 03:27:20 +00:00
|
|
|
void Session::DownloadURL(const GURL& url) {
|
|
|
|
auto* download_manager =
|
|
|
|
content::BrowserContext::GetDownloadManager(browser_context());
|
|
|
|
auto download_params = std::make_unique<download::DownloadUrlParameters>(
|
|
|
|
url, MISSING_TRAFFIC_ANNOTATION);
|
|
|
|
download_manager->DownloadUrl(std::move(download_params));
|
|
|
|
}
|
|
|
|
|
2019-10-25 13:03:28 +00:00
|
|
|
void Session::CreateInterruptedDownload(const gin_helper::Dictionary& options) {
|
2016-11-18 10:43:43 +00:00
|
|
|
int64_t offset = 0, length = 0;
|
2019-09-21 14:51:28 +00:00
|
|
|
double start_time = base::Time::Now().ToDoubleT();
|
2016-11-18 10:43:43 +00:00
|
|
|
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) {
|
2019-10-25 13:03:28 +00:00
|
|
|
isolate()->ThrowException(v8::Exception::Error(gin::StringToV8(
|
2016-12-07 07:28:53 +00:00
|
|
|
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) {
|
2019-10-25 13:03:28 +00:00
|
|
|
isolate()->ThrowException(v8::Exception::Error(gin::StringToV8(
|
2016-12-07 07:28:53 +00:00
|
|
|
isolate(), "Must pass an offset value less than length.")));
|
|
|
|
return;
|
|
|
|
}
|
2018-04-17 22:41:47 +00:00
|
|
|
auto* download_manager =
|
2016-11-18 10:43:43 +00:00
|
|
|
content::BrowserContext::GetDownloadManager(browser_context());
|
2019-05-01 20:45:08 +00:00
|
|
|
download_manager->GetDelegate()->GetNextId(base::BindRepeating(
|
2016-11-18 10:43:43 +00:00
|
|
|
&DownloadIdCallback, download_manager, path, url_chain, mime_type, offset,
|
|
|
|
length, last_modified, etag, base::Time::FromDoubleT(start_time)));
|
|
|
|
}
|
|
|
|
|
2017-12-05 06:59:15 +00:00
|
|
|
void Session::SetPreloads(
|
|
|
|
const std::vector<base::FilePath::StringType>& preloads) {
|
|
|
|
auto* prefs = SessionPreferences::FromBrowserContext(browser_context());
|
|
|
|
DCHECK(prefs);
|
|
|
|
prefs->set_preloads(preloads);
|
2017-09-16 15:05:49 +00:00
|
|
|
}
|
2017-12-05 02:50:06 +00:00
|
|
|
|
|
|
|
std::vector<base::FilePath::StringType> Session::GetPreloads() const {
|
2017-12-05 06:59:15 +00:00
|
|
|
auto* prefs = SessionPreferences::FromBrowserContext(browser_context());
|
|
|
|
DCHECK(prefs);
|
|
|
|
return prefs->preloads();
|
2017-09-16 15:05:49 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 23:01:08 +00:00
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
|
|
void Session::LoadChromeExtension(const base::FilePath extension_path) {
|
|
|
|
auto* extension_system = static_cast<extensions::AtomExtensionSystem*>(
|
|
|
|
extensions::ExtensionSystem::Get(browser_context()));
|
|
|
|
extension_system->LoadExtension(extension_path);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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) {
|
|
|
|
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()) {
|
2019-10-24 00:51:06 +00:00
|
|
|
auto handle = WebRequest::Create(isolate, browser_context());
|
2019-08-11 04:24:51 +00:00
|
|
|
web_request_.Reset(isolate, handle.ToV8());
|
2015-12-01 04:52:22 +00:00
|
|
|
}
|
|
|
|
return v8::Local<v8::Value>::New(isolate, web_request_);
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:08:56 +00:00
|
|
|
v8::Local<v8::Value> Session::NetLog(v8::Isolate* isolate) {
|
|
|
|
if (net_log_.IsEmpty()) {
|
2019-06-19 21:23:04 +00:00
|
|
|
auto handle = electron::api::NetLog::Create(isolate, browser_context());
|
2018-10-04 18:08:56 +00:00
|
|
|
net_log_.Reset(isolate, handle.ToV8());
|
|
|
|
}
|
|
|
|
return v8::Local<v8::Value>::New(isolate, net_log_);
|
|
|
|
}
|
|
|
|
|
2019-08-26 16:47:32 +00:00
|
|
|
static void StartPreconnectOnUI(
|
|
|
|
scoped_refptr<AtomBrowserContext> browser_context,
|
|
|
|
const GURL& url,
|
|
|
|
int num_sockets_to_preconnect) {
|
|
|
|
std::vector<predictors::PreconnectRequest> requests = {
|
|
|
|
{url.GetOrigin(), num_sockets_to_preconnect, net::NetworkIsolationKey()}};
|
|
|
|
browser_context->GetPreconnectManager()->Start(url, requests);
|
|
|
|
}
|
|
|
|
|
2019-10-25 13:03:28 +00:00
|
|
|
void Session::Preconnect(const gin_helper::Dictionary& options,
|
|
|
|
gin_helper::Arguments* args) {
|
2019-08-26 16:47:32 +00:00
|
|
|
GURL url;
|
|
|
|
if (!options.Get("url", &url) || !url.is_valid()) {
|
|
|
|
args->ThrowError("Must pass non-empty valid url to session.preconnect.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int num_sockets_to_preconnect = 1;
|
|
|
|
if (options.Get("numSockets", &num_sockets_to_preconnect)) {
|
|
|
|
const int kMinSocketsToPreconnect = 1;
|
|
|
|
const int kMaxSocketsToPreconnect = 6;
|
|
|
|
if (num_sockets_to_preconnect < kMinSocketsToPreconnect ||
|
|
|
|
num_sockets_to_preconnect > kMaxSocketsToPreconnect) {
|
|
|
|
args->ThrowError(
|
|
|
|
base::StringPrintf("numSocketsToPreconnect is outside range [%d,%d]",
|
|
|
|
kMinSocketsToPreconnect, kMaxSocketsToPreconnect));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DCHECK_GT(num_sockets_to_preconnect, 0);
|
2019-09-18 19:58:00 +00:00
|
|
|
base::PostTask(
|
2019-08-26 16:47:32 +00:00
|
|
|
FROM_HERE, {content::BrowserThread::UI},
|
|
|
|
base::BindOnce(&StartPreconnectOnUI, base::RetainedRef(browser_context_),
|
|
|
|
url, num_sockets_to_preconnect));
|
|
|
|
}
|
|
|
|
|
2019-10-31 20:11:51 +00:00
|
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
|
|
base::Value Session::GetSpellCheckerLanguages() {
|
|
|
|
return browser_context_->prefs()
|
|
|
|
->Get(spellcheck::prefs::kSpellCheckDictionaries)
|
|
|
|
->Clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::SetSpellCheckerLanguages(
|
|
|
|
gin_helper::ErrorThrower thrower,
|
|
|
|
const std::vector<std::string>& languages) {
|
|
|
|
base::ListValue language_codes;
|
|
|
|
for (const std::string& lang : languages) {
|
|
|
|
std::string code = spellcheck::GetCorrespondingSpellCheckLanguage(lang);
|
|
|
|
if (code.empty()) {
|
|
|
|
thrower.ThrowError("Invalid language code provided: \"" + lang +
|
|
|
|
"\" is not a valid language code");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
language_codes.AppendString(code);
|
|
|
|
}
|
|
|
|
browser_context_->prefs()->Set(spellcheck::prefs::kSpellCheckDictionaries,
|
|
|
|
language_codes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetSpellCheckerDictionaryDownloadURL(gin_helper::ErrorThrower thrower,
|
|
|
|
const GURL& url) {
|
|
|
|
if (!url.is_valid()) {
|
|
|
|
thrower.ThrowError(
|
|
|
|
"The URL you provided to setSpellCheckerDictionaryDownloadURL is not a "
|
|
|
|
"valid URL");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SpellcheckHunspellDictionary::SetDownloadURLForTesting(url);
|
|
|
|
}
|
2019-11-26 21:16:43 +00:00
|
|
|
|
|
|
|
bool Session::AddWordToSpellCheckerDictionary(const std::string& word) {
|
|
|
|
#if BUILDFLAG(USE_BROWSER_SPELLCHECKER)
|
|
|
|
if (spellcheck::UseBrowserSpellChecker()) {
|
|
|
|
spellcheck_platform::AddWord(base::UTF8ToUTF16(word));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
SpellcheckService* spellcheck =
|
|
|
|
SpellcheckServiceFactory::GetForContext(browser_context_.get());
|
|
|
|
if (!spellcheck)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return spellcheck->GetCustomDictionary()->AddWord(word);
|
|
|
|
}
|
2019-10-31 20:11:51 +00:00
|
|
|
#endif
|
|
|
|
|
2015-06-16 09:23:29 +00:00
|
|
|
// static
|
2019-10-25 13:03:28 +00:00
|
|
|
gin::Handle<Session> Session::CreateFrom(v8::Isolate* isolate,
|
|
|
|
AtomBrowserContext* browser_context) {
|
2018-04-17 22:41:47 +00:00
|
|
|
auto* existing = TrackableObject::FromWrappedClass(isolate, browser_context);
|
2015-06-24 07:37:04 +00:00
|
|
|
if (existing)
|
2019-10-25 13:03:28 +00:00
|
|
|
return gin::CreateHandle(isolate, static_cast<Session*>(existing));
|
2015-06-24 07:37:04 +00:00
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
auto handle =
|
2019-10-25 13:03:28 +00:00
|
|
|
gin::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()] =
|
2019-10-25 13:03:28 +00:00
|
|
|
v8::Global<v8::Value>(isolate, handle.ToV8());
|
2016-08-01 11:11:17 +00:00
|
|
|
|
2015-09-01 10:02:14 +00:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2015-09-05 14:39:48 +00:00
|
|
|
// static
|
2019-10-30 05:30:59 +00:00
|
|
|
gin::Handle<Session> Session::FromPartition(v8::Isolate* isolate,
|
|
|
|
const std::string& partition,
|
|
|
|
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()) {
|
2019-10-30 05:30:59 +00:00
|
|
|
browser_context = AtomBrowserContext::From("", false, std::move(options));
|
2016-07-12 12:01:49 +00:00
|
|
|
} else if (base::StartsWith(partition, kPersistPrefix,
|
|
|
|
base::CompareCase::SENSITIVE)) {
|
|
|
|
std::string name = partition.substr(8);
|
2019-10-30 05:30:59 +00:00
|
|
|
browser_context = AtomBrowserContext::From(name, false, std::move(options));
|
2016-07-12 12:01:49 +00:00
|
|
|
} else {
|
2019-10-30 05:30:59 +00:00
|
|
|
browser_context =
|
|
|
|
AtomBrowserContext::From(partition, true, std::move(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) {
|
2019-10-25 13:03:28 +00:00
|
|
|
prototype->SetClassName(gin::StringToV8(isolate, "Session"));
|
2019-09-04 02:14:16 +00:00
|
|
|
gin_helper::Destroyable::MakeDestroyable(isolate, prototype);
|
2019-10-25 13:03:28 +00:00
|
|
|
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
2015-12-03 08:04:46 +00:00
|
|
|
.SetMethod("resolveProxy", &Session::ResolveProxy)
|
2019-04-30 22:09:17 +00:00
|
|
|
.SetMethod("getCacheSize", &Session::GetCacheSize)
|
|
|
|
.SetMethod("clearCache", &Session::ClearCache)
|
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)
|
2018-05-23 20:51:28 +00:00
|
|
|
.SetMethod("setCertificateVerifyProc", &Session::SetCertVerifyProc)
|
2016-01-31 21:35:34 +00:00
|
|
|
.SetMethod("setPermissionRequestHandler",
|
|
|
|
&Session::SetPermissionRequestHandler)
|
2018-08-28 14:05:08 +00:00
|
|
|
.SetMethod("setPermissionCheckHandler",
|
|
|
|
&Session::SetPermissionCheckHandler)
|
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)
|
2019-08-29 03:27:20 +00:00
|
|
|
.SetMethod("downloadURL", &Session::DownloadURL)
|
2016-11-18 10:43:43 +00:00
|
|
|
.SetMethod("createInterruptedDownload",
|
|
|
|
&Session::CreateInterruptedDownload)
|
2017-12-05 06:59:15 +00:00
|
|
|
.SetMethod("setPreloads", &Session::SetPreloads)
|
2017-09-16 15:05:49 +00:00
|
|
|
.SetMethod("getPreloads", &Session::GetPreloads)
|
2019-07-24 23:01:08 +00:00
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
|
|
.SetMethod("loadChromeExtension", &Session::LoadChromeExtension)
|
2019-10-31 20:11:51 +00:00
|
|
|
#endif
|
|
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
|
|
.SetMethod("getSpellCheckerLanguages", &Session::GetSpellCheckerLanguages)
|
|
|
|
.SetMethod("setSpellCheckerLanguages", &Session::SetSpellCheckerLanguages)
|
|
|
|
.SetProperty("availableSpellCheckerLanguages",
|
|
|
|
&spellcheck::SpellCheckLanguages)
|
|
|
|
.SetMethod("setSpellCheckerDictionaryDownloadURL",
|
|
|
|
&SetSpellCheckerDictionaryDownloadURL)
|
2019-11-26 21:16:43 +00:00
|
|
|
.SetMethod("addWordToSpellCheckerDictionary",
|
|
|
|
&Session::AddWordToSpellCheckerDictionary)
|
2019-07-24 23:01:08 +00:00
|
|
|
#endif
|
2019-08-26 16:47:32 +00:00
|
|
|
.SetMethod("preconnect", &Session::Preconnect)
|
2015-12-01 04:52:22 +00:00
|
|
|
.SetProperty("cookies", &Session::Cookies)
|
2018-10-04 18:08:56 +00:00
|
|
|
.SetProperty("netLog", &Session::NetLog)
|
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
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|
2015-09-01 10:02:14 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
using electron::api::Cookies;
|
|
|
|
using electron::api::NetLog;
|
2019-10-24 00:51:06 +00:00
|
|
|
using electron::api::Protocol;
|
2019-06-19 21:23:04 +00:00
|
|
|
using electron::api::Session;
|
2016-08-02 11:38:35 +00:00
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
v8::Local<v8::Value> FromPartition(const std::string& partition,
|
2019-10-25 13:03:28 +00:00
|
|
|
gin_helper::Arguments* args) {
|
2019-06-19 21:23:04 +00:00
|
|
|
if (!electron::Browser::Get()->is_ready()) {
|
2016-07-12 11:21:59 +00:00
|
|
|
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);
|
2019-10-30 05:30:59 +00:00
|
|
|
return Session::FromPartition(args->isolate(), partition, std::move(options))
|
|
|
|
.ToV8();
|
2016-07-12 11:21:59 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
2015-09-01 10:02:14 +00:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2019-10-25 13:03:28 +00:00
|
|
|
gin_helper::Dictionary dict(isolate, exports);
|
2019-01-09 19:17:05 +00:00
|
|
|
dict.Set(
|
|
|
|
"Session",
|
|
|
|
Session::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
|
|
|
|
dict.Set(
|
|
|
|
"Cookies",
|
|
|
|
Cookies::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
|
2019-02-19 10:48:27 +00:00
|
|
|
dict.Set(
|
|
|
|
"NetLog",
|
|
|
|
NetLog::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
|
2019-10-24 00:51:06 +00:00
|
|
|
dict.Set(
|
|
|
|
"Protocol",
|
|
|
|
Protocol::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
|
2016-07-12 11:21:59 +00:00
|
|
|
dict.SetMethod("fromPartition", &FromPartition);
|
2015-09-01 10:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-03-08 18:29:52 +00:00
|
|
|
NODE_LINKED_MODULE_CONTEXT_AWARE(atom_browser_session, Initialize)
|