From: k2.nagaraju Date: Mon, 15 Oct 2018 06:01:45 +0000 (+0530) Subject: Add cookie manager support. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9375e6e37e7df4d232720e114602557171e23d07;p=platform%2Fframework%2Fweb%2Fcrosswalk-tizen.git Add cookie manager support. Added |cookie_manager.*| files from chromium to electron and added required changes in |CookieManager| and atom |URLRequestContextGetter| to support file cookies. This patch requires below chromium patch https://review.tizen.org/gerrit/#/c/191289/ Change-Id: Ib4b8c2fd182a521328f5d6d67a51595f6c7b7165 Signed-off-by: k2.nagaraju --- diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 18f375d65..e6d6b9317 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -953,8 +953,8 @@ void WebContents::HandleWrtPluginMessage(Ewk_Wrt_Message_Data* msg) { LOG(INFO) << msg_type; #define TYPE_BEGIN(x) (!strncmp(msg_type, x, strlen(x))) #define TYPE_IS(x) (!strcmp(msg_type, x)) + auto extension_server = extensions::XWalkExtensionServer::GetInstance(); if (TYPE_BEGIN("xwalk://")) { - auto extension_server = extensions::XWalkExtensionServer::GetInstance(); extension_server->HandleIPCMessage(msg); } else { Eina_Stringshare* msg_id = msg->GetId(); @@ -962,8 +962,10 @@ void WebContents::HandleWrtPluginMessage(Ewk_Wrt_Message_Data* msg) { Eina_Stringshare* msg_value = msg->GetValue(); if (TYPE_IS("tizen://exit")) { atom::Browser::Get()->Quit(); + } else if (TYPE_IS("tizen://deleteAllCookies")) { + atom::Browser::Get()->ClearCookie(); + extension_server->SendWrtMessage(msg_type, msg_ref_id, "success"); } - eina_stringshare_del(msg_ref_id); eina_stringshare_del(msg_id); eina_stringshare_del(msg_value); diff --git a/atom/browser/atom_browser_context.cc b/atom/browser/atom_browser_context.cc index 92a589d75..3f742cbb2 100644 --- a/atom/browser/atom_browser_context.cc +++ b/atom/browser/atom_browser_context.cc @@ -242,6 +242,17 @@ AtomBlobReader* AtomBrowserContext::GetBlobReader() { return blob_reader_.get(); } +net::URLRequestContextGetter* AtomBrowserContext::CreateRequestContext( + content::ProtocolHandlerMap* protocol_handlers, + content::URLRequestInterceptorScopedVector protocol_interceptors) { + brightray::BrowserContext::CreateRequestContext(protocol_handlers, + std::move(protocol_interceptors)); + + CookieManager* manager = Browser::Get()->GetCookieManager(); + manager->SetRequestContextGetter(url_request_context_getter()); + return url_request_context_getter(); +} + // static scoped_refptr AtomBrowserContext::From( const std::string& partition, bool in_memory, diff --git a/atom/browser/atom_browser_context.h b/atom/browser/atom_browser_context.h index e4f6e0d39..dba94e06d 100644 --- a/atom/browser/atom_browser_context.h +++ b/atom/browser/atom_browser_context.h @@ -54,6 +54,9 @@ class AtomBrowserContext : public brightray::BrowserContext, std::vector GetCookieableSchemes() override; net::TransportSecurityState::RequireCTDelegate* GetRequireCTDelegate() override; + net::URLRequestContextGetter* CreateRequestContext( + content::ProtocolHandlerMap* protocol_handlers, + content::URLRequestInterceptorScopedVector request_interceptors) override; // content::BrowserContext: content::DownloadManagerDelegate* GetDownloadManagerDelegate() override; diff --git a/tizen/browser/tizen_browser_parts.cc b/tizen/browser/tizen_browser_parts.cc index 9963419f2..9be83b223 100644 --- a/tizen/browser/tizen_browser_parts.cc +++ b/tizen/browser/tizen_browser_parts.cc @@ -14,6 +14,8 @@ * limitations under the License. */ +#include + #include "atom/common/api/api_messages.h" #include "common/string_utils.h" #include "tizen/browser/tizen_browser_parts.h" @@ -62,6 +64,7 @@ static bool ProcessWellKnownScheme(const std::string& url) { TizenBrowserParts::TizenBrowserParts() : locale_manager_(new common::LocaleManager()), + cookie_manager_(new CookieManager()), is_suspended_(false) { } @@ -86,9 +89,25 @@ void TizenBrowserParts::Initialize() { } else { security_model_version_ = 1; } + + std::unique_ptr path{app_get_data_path(), + std::free}; + if (path.get() != NULL) { + std::string app_data_path = path.get(); + cookie_manager_->SetCookiePolicy(EWK_COOKIE_ACCEPT_POLICY_ALWAYS); + std::string cookie_path = app_data_path + ".cookie"; + LOG(ERROR) << "Cookie path " << cookie_path; + cookie_manager_->SetStoragePath(cookie_path, true , + EWK_COOKIE_PERSISTENT_STORAGE_SQLITE); + } + tizen::VibrationManager::CreateInstance(); } +void TizenBrowserParts::ClearCookie() { + cookie_manager_->DeleteCookiesAsync(); +} + void TizenBrowserParts::GetCSP(std::string &csp_rule, std::string &csp_report_rule) { csp_rule = csp_rule_; csp_report_rule = csp_report_rule_; diff --git a/tizen/browser/tizen_browser_parts.h b/tizen/browser/tizen_browser_parts.h index 973fe1aad..fcce2d944 100644 --- a/tizen/browser/tizen_browser_parts.h +++ b/tizen/browser/tizen_browser_parts.h @@ -22,6 +22,7 @@ #include "tizen/common/application_data.h" #include "tizen/common/locale_manager.h" #include "tizen/common/resource_manager.h" +#include "vendor/brightray/browser/efl/cookie_manager.h" namespace tizen { @@ -39,6 +40,9 @@ class TizenBrowserParts { bool ShouldAllowNavigation(const std::string &url); void Initialize(); + void ClearCookie(); + CookieManager* GetCookieManager() { return cookie_manager_; } + protected: std::unique_ptr locale_manager_; std::unique_ptr resource_manager_; @@ -52,6 +56,7 @@ class TizenBrowserParts { void SetLongPollingTimeout(content::RenderViewHost* render_view_host); common::ApplicationData* app_data_; + CookieManager* cookie_manager_; int security_model_version_; bool is_suspended_; std::string csp_rule_; diff --git a/tizen/extensions/common/xwalk_extension_server.cc b/tizen/extensions/common/xwalk_extension_server.cc index 4c3b9fd14..f86710292 100644 --- a/tizen/extensions/common/xwalk_extension_server.cc +++ b/tizen/extensions/common/xwalk_extension_server.cc @@ -136,6 +136,16 @@ void XWalkExtensionServer::HandleGetExtensions(Ewk_IPC_Wrt_Message_Data* data) { ewk_ipc_wrt_message_data_value_set(data, reply_str.c_str()); } +void XWalkExtensionServer::SendWrtMessage( + Eina_Stringshare* type, Eina_Stringshare* id, const char* val) { + Ewk_IPC_Wrt_Message_Data* ans = ewk_ipc_wrt_message_data_new(); + ewk_ipc_wrt_message_data_type_set(ans, type); + ewk_ipc_wrt_message_data_id_set(ans, id); + ewk_ipc_wrt_message_data_value_set(ans, val); + WrtWidgetHost::Get()->SendWrtMessage(*ans); + ewk_ipc_wrt_message_data_del(ans); +} + void XWalkExtensionServer::HandleCreateInstance( Ewk_IPC_Wrt_Message_Data* data) { Eina_Stringshare* extension_name = ewk_ipc_wrt_message_data_value_get(data); diff --git a/tizen/extensions/common/xwalk_extension_server.h b/tizen/extensions/common/xwalk_extension_server.h index a778a5b17..4fcfa9dae 100644 --- a/tizen/extensions/common/xwalk_extension_server.h +++ b/tizen/extensions/common/xwalk_extension_server.h @@ -29,6 +29,8 @@ class XWalkExtensionServer { void Shutdown(); void LoadUserExtensions(const std::string app_path); + void SendWrtMessage(Eina_Stringshare* type, + Eina_Stringshare* id, const char* val); private: XWalkExtensionServer(); diff --git a/vendor/brightray/brightray.gyp b/vendor/brightray/brightray.gyp index 018a859d3..60a28926e 100644 --- a/vendor/brightray/brightray.gyp +++ b/vendor/brightray/brightray.gyp @@ -26,6 +26,7 @@ '<(libchromiumcontent_src_dir)/tizen_src/chromium_impl', '<(libchromiumcontent_src_dir)/skia/config', '<(libchromiumcontent_src_dir)/third_party/boringssl/src/include', + '<(libchromiumcontent_src_dir)/third_party/leveldatabase/src/include', '<(libchromiumcontent_src_dir)/third_party/skia/include/core', '<(libchromiumcontent_src_dir)/third_party/skia/include/config', '<(libchromiumcontent_src_dir)/third_party/mojo/src', @@ -44,6 +45,7 @@ '<(libchromiumcontent_src_dir)/gpu', '<(libchromiumcontent_src_dir)/skia/config', '<(libchromiumcontent_src_dir)/third_party/boringssl/src/include', + '<(libchromiumcontent_src_dir)/third_party/leveldatabase/src/include', '<(libchromiumcontent_src_dir)/third_party/skia/include/core', '<(libchromiumcontent_src_dir)/third_party/skia/include/config', '<(libchromiumcontent_src_dir)/third_party/icu/source/common', diff --git a/vendor/brightray/browser/browser_context.cc b/vendor/brightray/browser/browser_context.cc index 95530bf44..28e2e54e2 100644 --- a/vendor/brightray/browser/browser_context.cc +++ b/vendor/brightray/browser/browser_context.cc @@ -8,6 +8,9 @@ #include "browser/browser_context.h" +#include "base/files/file_path.h" +#include "base/path_service.h" +#include "base/strings/string_util.h" #include "browser/media/media_device_id_salt.h" #include "browser/brightray_paths.h" #include "browser/browser_client.h" @@ -17,16 +20,11 @@ #include "browser/special_storage_policy.h" #include "browser/zoom_level_delegate.h" #include "common/application_info.h" - -#include "base/files/file_path.h" -#include "base/path_service.h" - #include "components/prefs/json_pref_store.h" #include "components/prefs/pref_registry_simple.h" #include "components/prefs/pref_service.h" #include "components/prefs/pref_service_factory.h" - -#include "base/strings/string_util.h" +#include "content/common/paths_efl.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/resource_context.h" #include "content/public/browser/storage_partition.h" @@ -102,7 +100,11 @@ BrowserContext::BrowserContext(const std::string& partition, bool in_memory) resource_context_(new ResourceContext), storage_policy_(new SpecialStoragePolicy), weak_factory_(this) { +#if defined(OS_TIZEN) + if (!PathService::Get(PathsEfl::DIR_USER_DATA, &path_)) { +#else if (!PathService::Get(DIR_USER_DATA, &path_)) { +#endif PathService::Get(DIR_APP_DATA, &path_); path_ = path_.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName())); PathService::Override(DIR_USER_DATA, path_); diff --git a/vendor/brightray/browser/efl/cookie_manager.cc b/vendor/brightray/browser/efl/cookie_manager.cc new file mode 100644 index 000000000..7f312b6bb --- /dev/null +++ b/vendor/brightray/browser/efl/cookie_manager.cc @@ -0,0 +1,397 @@ +// Copyright 2018 Samsung Electronics. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "cookie_manager.h" + +#include "utility" + +#include "base/bind.h" +#include "base/logging.h" +#include "base/stl_util.h" +#include "base/synchronization/waitable_event.h" +#include "content/public/browser/browser_thread.h" +#include "content/browser/storage_partition_impl.h" +#include "net/base/net_errors.h" +#include "net/base/static_cookie_policy.h" +#include "net/cookies/canonical_cookie.h" +#include "net/cookies/cookie_util.h" +#include "net/cookies/parsed_cookie.h" +#include "net/cookies/cookie_monster.h" +#include "net/cookies/cookie_options.h" +#include "net/url_request/url_request_context.h" +#include "net/url_request/url_request_context_getter.h" +#include "tizen_src/ewk/efl_integration/browser/scoped_allow_wait_for_legacy_web_view_api.h" +#include "url/gurl.h" + +#include + +using content::BrowserThread; +using net::CookieList; +using net::CookieMonster; +using base::AutoLock; + +namespace { + +void TriggerHostPolicyGetCallbackAsyncOnUIThread(Ewk_Cookie_Accept_Policy policy, + Ewk_Cookie_Manager_Policy_Async_Get_Cb callback, + void *data) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + if (callback) + (*callback)(policy, data); +} + +} + +struct CookieManager::EwkGetHostCallback { + public: + EwkGetHostCallback(AsyncHostnamesGetCb callback, void* user_data) + : callback_(callback), + user_data_(user_data) {} + + /* LCOV_EXCL_START */ + void TriggerCallback(const net::CookieList& cookies) { + Eina_List* hostnames = 0; + if (cookies.size()) { + net::CookieList::const_iterator it = cookies.begin(); + while (it != cookies.end()) { + hostnames = eina_list_append(hostnames, eina_stringshare_add(it->Name().c_str())); + ++it; + } + } + (*callback_)(hostnames, NULL, user_data_); + void* item = 0; + EINA_LIST_FREE(hostnames, item) + eina_stringshare_del(static_cast(item)); + } + /* LCOV_EXCL_STOP */ + + private: + AsyncHostnamesGetCb callback_; + void* user_data_; +}; + +CookieManager::CookieManager() + : cookie_policy_(EWK_COOKIE_ACCEPT_POLICY_ALWAYS), + weak_ptr_factory_(this) { +} + +CookieManager::~CookieManager() { + DeleteSessionCookiesOnIOThread(); +} + +void CookieManager::SetRequestContextGetter( + scoped_refptr getter) { + request_context_getter_ = getter; +} + +void CookieManager::DeleteSessionCookiesOnIOThread() { + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); + + net::CookieStore* cookie_store = GetCookieStore(); + if (cookie_store) + cookie_store->DeleteSessionCookiesAsync(base::Callback()); +} + +void CookieManager::DeleteCookiesOnIOThread(const std::string& url, + const std::string& cookie_name) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); + + net::CookieStore* cookie_store = GetCookieStore(); + if (!cookie_store) + return; + + if (url.empty()) { // Delete all cookies. + cookie_store->DeleteAllAsync(net::CookieMonster::DeleteCallback()); + return; + } + + GURL gurl(url); + if (!gurl.is_valid()) + return; + + if (cookie_name.empty()) { + // Delete all matching host cookies. + cookie_store->DeleteAllCreatedBetweenWithPredicateAsync( + base::Time(), + base::Time::Max(), + content::StoragePartitionImpl::CreatePredicateForHostCookies(gurl), + net::CookieMonster::DeleteCallback()); + } else { + // Delete all matching host and domain cookies. + cookie_store->DeleteCookieAsync(gurl, cookie_name, base::Closure()); + } +} + +void CookieManager::DeleteCookiesAsync(const std::string& url, + const std::string& cookie_name) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + BrowserThread::PostTask( + BrowserThread::IO, FROM_HERE, + base::Bind(&CookieManager::DeleteCookiesOnIOThread, + base::Unretained(this), url, cookie_name)); +} + +static void SetStoragePathOnIOThread( + scoped_refptr request_context_getter, + const std::string& path, + bool persist_session_cookies, + Ewk_Cookie_Persistent_Storage file_storage_type) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); + LOG(ERROR)<<__FUNCTION__; + if (request_context_getter.get()) { + base::FilePath storage_path(path); + request_context_getter->SetCookieStoragePath(storage_path, persist_session_cookies, + file_storage_type); + } +} + +void CookieManager::SetStoragePath(const std::string& path, + bool persist_session_cookies, + Ewk_Cookie_Persistent_Storage file_storage_type) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + LOG(ERROR)<<__FUNCTION__; + + scoped_refptr context_getter = GetContextGetter(); + if (context_getter.get()) { + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, + base::Bind(SetStoragePathOnIOThread, + context_getter, + path, + persist_session_cookies, + file_storage_type)); + } +} + +void CookieManager::GetAcceptPolicyAsync(Ewk_Cookie_Manager_Policy_Async_Get_Cb callback, void *data) { + AutoLock lock(lock_); + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, + base::Bind(&TriggerHostPolicyGetCallbackAsyncOnUIThread, + cookie_policy_, + callback, + data)); +} + +/* LCOV_EXCL_START */ +void CookieManager::GetHostNamesWithCookiesAsync(AsyncHostnamesGetCb callback, void *data) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + host_callback_queue_.push(new EwkGetHostCallback(callback, data)); + BrowserThread::PostTask( + BrowserThread::IO, FROM_HERE, + base::Bind(&CookieManager::FetchCookiesOnIOThread, GetSharedThis())); +} + +void CookieManager::FetchCookiesOnIOThread() { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); + + net::CookieStore* cookie_store = GetCookieStore(); + if (cookie_store) { + cookie_store->GetAllCookiesAsync(base::Bind(&CookieManager::OnFetchComplete, + GetSharedThis())); + } else { + OnFetchComplete(net::CookieList()); + } +} + +void CookieManager::OnFetchComplete(const net::CookieList& cookies) { + if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, + base::Bind(&CookieManager::OnFetchComplete, + GetSharedThis(), + cookies)); + return; + } + if (!host_callback_queue_.empty()) { + EwkGetHostCallback* host_callback = host_callback_queue_.front(); + if (host_callback) { + host_callback->TriggerCallback(cookies); + delete host_callback; + } + host_callback_queue_.pop(); + } +} +/* LCOV_EXCL_STOP */ + +bool CookieManager::GetGlobalAllowAccess() { + AutoLock lock(lock_); + if (EWK_COOKIE_ACCEPT_POLICY_ALWAYS == cookie_policy_) + return true; + else + return false; // LCOV_EXCL_LINE +} + +void CookieManager::SetCookiePolicy(Ewk_Cookie_Accept_Policy policy) { + AutoLock lock(lock_); + cookie_policy_ = policy; +} + +/* LCOV_EXCL_START */ +bool CookieManager::ShouldBlockThirdPartyCookies() { + AutoLock lock(lock_); + if (EWK_COOKIE_ACCEPT_POLICY_NO_THIRD_PARTY == cookie_policy_) + return true; + else + return false; +} +/* LCOV_EXCL_STOP */ + +bool CookieManager::AllowCookies(const GURL& url, + const GURL& first_party_url, + bool setting_cookie) { + if (GetGlobalAllowAccess()) + return true; + + if (ShouldBlockThirdPartyCookies()) { + net::StaticCookiePolicy policy(net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES); + int rv; + if (setting_cookie) + rv = policy.CanSetCookie(url, first_party_url); + else + rv = policy.CanGetCookies(url, first_party_url); + + if (net::OK == rv) + return true; + } + + return false; +} + +bool CookieManager::OnCanGetCookies(const net::URLRequest& request, + const net::CookieList& cookie_list) { + return AllowCookies(request.url(), request.first_party_for_cookies(), false); +} + +bool CookieManager::OnCanSetCookie(const net::URLRequest& request, + const std::string& cookie_line, + net::CookieOptions* options) { + return AllowCookies(request.url(), request.first_party_for_cookies(), true); +} + +bool CookieManager::AllowGetCookie(const GURL& url, + const GURL& first_party, + const net::CookieList& cookie_list, + content::ResourceContext* context, + int render_process_id, + int render_frame_id) { + return AllowCookies(url, first_party, false); +} + +bool CookieManager::AllowSetCookie(const GURL& url, + const GURL& first_party, + const std::string& cookie_line, + content::ResourceContext* context, + int render_process_id, + int render_frame_id, + const net::CookieOptions& options) { + return AllowCookies(url, first_party, true); +} + +bool CookieManager::IsFileSchemeCookiesAllowed() { + auto cookie_monster = static_cast(GetCookieStore()); + if (!cookie_monster) + return false; + + AutoLock lock(file_scheme_lock_); + return cookie_monster->IsCookieableScheme(url::kFileScheme); +} + +void CookieManager::SetAcceptFileSchemeCookies(bool accept) { + auto cookie_monster = static_cast(GetCookieStore()); + if (!cookie_monster) + return; // LCOV_EXCL_LINE + + std::vector schemes( + CookieMonster::kDefaultCookieableSchemes, + CookieMonster::kDefaultCookieableSchemes + + CookieMonster::kDefaultCookieableSchemesCount); + if (accept) + schemes.push_back(url::kFileScheme); + + AutoLock lock(file_scheme_lock_); + LOG(ERROR)<<__FUNCTION__; + cookie_monster->SetCookieableSchemes(schemes); +} + +/* LCOV_EXCL_START */ +static void SignalGetCookieValueCompleted(base::WaitableEvent* completion, + std::string* result, + const std::string& value) { + DCHECK(completion); + + *result = value; + completion->Signal(); +} + +static void GetCookieValueOnIOThread(net::CookieStore* cookie_store, + const GURL& host, + std::string* result, + base::WaitableEvent* completion) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); + net::CookieOptions options; + options.set_include_httponly(); + + if (cookie_store) { + cookie_store->GetCookiesWithOptionsAsync(host, + options, + base::Bind(SignalGetCookieValueCompleted, + completion, + result)); + } else { + DCHECK(completion); + completion->Signal(); + } +} + +std::string CookieManager::GetCookiesForURL(const std::string& url) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + net::CookieStore* cookie_store = GetCookieStore(); + if (!cookie_store) + return std::string(); + + std::string cookie_value; + base::WaitableEvent completion(base::WaitableEvent::ResetPolicy::AUTOMATIC, + base::WaitableEvent::InitialState::NOT_SIGNALED); + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, + base::Bind(GetCookieValueOnIOThread, cookie_store, + GURL(url), &cookie_value, &completion)); + // allow wait temporarily. + ScopedAllowWaitForLegacyWebViewApi allow_wait; + completion.Wait(); + return cookie_value; +} +/* LCOV_EXCL_STOP */ + +scoped_refptr CookieManager::GetSharedThis() { + return scoped_refptr(this); +} + +scoped_refptr CookieManager::GetContextGetter() const { + return scoped_refptr(request_context_getter_.get()); +} + +net::CookieStore* CookieManager::GetCookieStore() const { + if (request_context_getter_) + return request_context_getter_->GetURLRequestContext()->cookie_store(); + return nullptr; +} + +#if defined(OS_TIZEN_TV_PRODUCT) || defined(TIZEN_DESKTOP_SUPPORT) +void CookieManager::SetCookiesChangedCallbackOnIOThread( + Ewk_Cookie_Manager_Changes_Watch_Cb callback, + void* data) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); + + auto cookie_monster = static_cast(GetCookieStore()); + if (cookie_monster) + cookie_monster->RegisterCookiesChangedCallback(callback, data); +} + +void CookieManager::SetCookiesChangedCallback( + Ewk_Cookie_Manager_Changes_Watch_Cb callback, + void* data) { + BrowserThread::PostTask( + BrowserThread::IO, FROM_HERE, + base::Bind(&CookieManager::SetCookiesChangedCallbackOnIOThread, + GetSharedThis(), callback, data)); +} +#endif diff --git a/vendor/brightray/browser/efl/cookie_manager.h b/vendor/brightray/browser/efl/cookie_manager.h new file mode 100644 index 000000000..ac615d00a --- /dev/null +++ b/vendor/brightray/browser/efl/cookie_manager.h @@ -0,0 +1,153 @@ +// Copyright 2018 Samsung Electronics. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// cookie manager already existed in |tizen_src/ewk/efl_integration/cookie_manager.cc| +// but it have dependency on |URLRequestContextGetterEfl|, which internaly as +// dependency |ewk_context|. So, it is not possible to directly use this from +// electron. Instead copied |cookie_manager.*| file to electron and added required +// changes in |CookieManager| and atom |URLRequestContextGetter|. + +#ifndef cookie_manager_h +#define cookie_manager_h + +#include +#include + +#include "base/memory/ref_counted.h" +#include "base/synchronization/lock.h" +#include "browser/url_request_context_getter.h" +#include "net/url_request/url_request_context.h" +#include "net/url_request/url_request_context_getter.h" +#include "net/cookies/canonical_cookie.h" +#include "net/cookies/cookie_monster.h" +#include "net/cookies/cookie_options.h" +#include "net/cookies/cookie_util.h" +#include "tizen_src/ewk/efl_integration/public/ewk_cookie_manager.h" +#include "url/gurl.h" + +#if defined(OS_TIZEN_TV_PRODUCT) || defined(TIZEN_DESKTOP_SUPPORT) +#include "tizen_src/ewk/efl_integration/public/ewk_cookie_manager_product.h" +#endif + +namespace content { +class ResourceContext; +} + +struct _Ewk_Error; + +//TODO: Remove unused apis from cookie manager class +class CookieManager : public base::RefCountedThreadSafe { + public: + typedef void (*AsyncHostnamesGetCb)(Eina_List*, _Ewk_Error*, void *); + + CookieManager(); + // Delete all cookies that match the specified parameters. If both |url| and + // values |cookie_name| are specified all host and domain cookies matching + // both will be deleted. If only |url| is specified all host cookies (but not + // domain cookies) irrespective of path will be deleted. If |url| is empty + // all cookies for all hosts and domains will be deleted. This method must be + // called on the IO thread. + void DeleteCookiesAsync(const std::string& url = std::string(), + const std::string& cookie_name = std::string()); + // Sets the directory path that will be used for storing cookie data. If + // |path| is empty data will be stored in memory only. Otherwise, data will + // be stored at the specified |path|. To persist session cookies (cookies + // without an expiry date or validity interval) set |persist_session_cookies| + // to true. Session cookies are generally intended to be transient and most + // Web browsers do not persist them. Returns false if cookies cannot be + // accessed. + void SetStoragePath(const std::string& path, + bool persist_session_cookies, + Ewk_Cookie_Persistent_Storage file_storage); + //get the accept policy asynchronous + void GetAcceptPolicyAsync(Ewk_Cookie_Manager_Policy_Async_Get_Cb callback, void *data); + //get host name asynchronous + void GetHostNamesWithCookiesAsync(AsyncHostnamesGetCb callback, void *data); + + // These manage the global access state shared across requests regardless of + // source (i.e. network or JavaScript). + bool GetGlobalAllowAccess(); + void SetCookiePolicy(Ewk_Cookie_Accept_Policy policy); + // These are the functions called when operating over cookies from the + // network. See NetworkDelegate for further descriptions. + bool OnCanGetCookies(const net::URLRequest& request, + const net::CookieList& cookie_list); + bool OnCanSetCookie(const net::URLRequest& request, + const std::string& cookie_line, + net::CookieOptions* options); + + // These are the functions called when operating over cookies from the + // renderer. See ContentBrowserClient for further descriptions. + bool AllowGetCookie(const GURL& url, + const GURL& first_party, + const net::CookieList& cookie_list, + content::ResourceContext* context, + int render_process_id, + int render_frame_id); + bool AllowSetCookie(const GURL& url, + const GURL& first_party, + const std::string& cookie_line, + content::ResourceContext* context, + int render_process_id, + int render_frame_id, + const net::CookieOptions& options); + bool ShouldBlockThirdPartyCookies(); + //This is synchronous call + std::string GetCookiesForURL(const std::string& url); + + //void SetRequestContextGetter(scoped_refptr getter); + void SetRequestContextGetter(scoped_refptr getter); + + base::WeakPtr GetWeakPtr() { + return weak_ptr_factory_.GetWeakPtr(); + } + + // file scheme + bool IsFileSchemeCookiesAllowed(); + void SetAcceptFileSchemeCookies(bool accept); + +#if defined(OS_TIZEN_TV_PRODUCT) || defined(TIZEN_DESKTOP_SUPPORT) + void SetCookiesChangedCallback(Ewk_Cookie_Manager_Changes_Watch_Cb callback, + void* data); +#endif + + private: + friend class base::RefCountedThreadSafe; + struct EwkGetHostCallback; + + ~CookieManager(); + void DeleteSessionCookiesOnIOThread(); + void DeleteCookiesOnIOThread(const std::string& url, + const std::string& cookie_name); + +#if defined(OS_TIZEN_TV_PRODUCT) || defined(TIZEN_DESKTOP_SUPPORT) + void SetCookiesChangedCallbackOnIOThread( + Ewk_Cookie_Manager_Changes_Watch_Cb callback, + void* data); +#endif + + bool AllowCookies(const GURL& url, + const GURL& first_party_url, + bool setting_cookie); + // Fetch the cookies. This must be called in the IO thread. + void FetchCookiesOnIOThread(); + void OnFetchComplete(const net::CookieList& cookies); + + scoped_refptr GetSharedThis(); + scoped_refptr GetContextGetter() const; + net::CookieStore* GetCookieStore() const; + scoped_refptr request_context_getter_; + //cookie policy information + base::Lock lock_; + Ewk_Cookie_Accept_Policy cookie_policy_; + // This only mutates on the UI thread. + std::queue< EwkGetHostCallback* > host_callback_queue_; + base::WeakPtrFactory weak_ptr_factory_; + // file scheme + base::Lock file_scheme_lock_; + + DISALLOW_COPY_AND_ASSIGN(CookieManager); +}; + +#endif //cookie_manager_h diff --git a/vendor/brightray/browser/url_request_context_getter.cc b/vendor/brightray/browser/url_request_context_getter.cc index fd23b4b84..e2afa004a 100644 --- a/vendor/brightray/browser/url_request_context_getter.cc +++ b/vendor/brightray/browser/url_request_context_getter.cc @@ -13,6 +13,7 @@ #include "common/switches.h" #include "base/command_line.h" +#include "base/files/file_util.h" #include "base/memory/ptr_util.h" #include "base/strings/string_util.h" #include "base/threading/sequenced_worker_pool.h" @@ -28,6 +29,7 @@ #include "net/cert/multi_log_ct_verifier.h" #include "net/cookies/cookie_monster.h" #include "net/dns/mapped_host_resolver.h" +#include "net/extras/sqlite/sqlite_persistent_cookie_store.h" #include "net/http/http_auth_filter.h" #include "net/http/http_auth_handler_factory.h" #include "net/http/http_auth_preferences.h" @@ -59,6 +61,7 @@ #endif using content::BrowserThread; +using net::SQLitePersistentCookieStore; namespace brightray { @@ -376,4 +379,100 @@ URLRequestContextGetter::GetNetworkTaskRunner() const { return BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); } +void URLRequestContextGetter::SetCookieStoragePath( + const base::FilePath& path, + bool persist_session_cookies, + Ewk_Cookie_Persistent_Storage file_storage) { + if (url_request_context_->cookie_store() && + (cookie_store_path_ == path)) { + // The path has not changed so don't do anything. + return; + } + + if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { + BrowserThread::PostTask( + BrowserThread::IO, FROM_HERE, + base::Bind(&URLRequestContextGetter::SetCookieStoragePath, + this, path, persist_session_cookies, file_storage)); + return; + } + + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); + if (file_storage == EWK_COOKIE_PERSISTENT_STORAGE_SQLITE) { + CreateSQLitePersistentCookieStore(path, persist_session_cookies); + } else { + CreatePersistentCookieStore(path, persist_session_cookies); + } +} + +void URLRequestContextGetter::CreateSQLitePersistentCookieStore( + const base::FilePath& path, + bool persist_session_cookies) +{ + using content::CookieStoreConfig; + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); + scoped_refptr persistent_store; + + if (path.empty()) + return; // LCOV_EXCL_LINE + base::ThreadRestrictions::ScopedAllowIO allow_io; + if (base::DirectoryExists(path) || + base::CreateDirectory(path)) { + const base::FilePath& cookie_path = path.AppendASCII("Cookies"); + persistent_store = + new SQLitePersistentCookieStore( + cookie_path, + BrowserThread::GetTaskRunnerForThread(BrowserThread::IO), + BrowserThread::GetTaskRunnerForThread(BrowserThread::DB), + persist_session_cookies, NULL); + } else { + NOTREACHED() << "The cookie storage directory could not be created"; + return; + } + // Set the new cookie store that will be used for all new requests. The old + // cookie store, if any, will be automatically flushed and closed when no + // longer referenced. + std::unique_ptr cookie_monster( + new net::CookieMonster(persistent_store.get(), NULL)); + + if (persistent_store.get() && persist_session_cookies) + cookie_monster->SetPersistSessionCookies(true); + + std::vector schemes; + for (int i = 0; i < net::CookieMonster::kDefaultCookieableSchemesCount; ++i) + schemes.push_back(std::string(net::CookieMonster::kDefaultCookieableSchemes[i])); + schemes.push_back(url::kFileScheme); + cookie_monster->SetCookieableSchemes(schemes); + + storage_->set_cookie_store(std::move(cookie_monster)); + + cookie_store_path_ = path; +} + +void URLRequestContextGetter::CreatePersistentCookieStore(const base::FilePath& path, + bool persist_session_cookies) +{ + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); + cookie_store_path_ = path; + + content::CookieStoreConfig config( + cookie_store_path_.Append(FILE_PATH_LITERAL("Cookies")), + content::CookieStoreConfig::RESTORED_SESSION_COOKIES, + NULL, + NULL); + cookie_store_= CreateCookieStore(config); + net::CookieMonster* cookie_monster = static_cast( + cookie_store_.get()); + cookie_monster->SetPersistSessionCookies(persist_session_cookies); + + std::vector schemes; + for (int i = 0; i < net::CookieMonster::kDefaultCookieableSchemesCount; ++i) + schemes.push_back(std::string(net::CookieMonster::kDefaultCookieableSchemes[i])); + schemes.push_back(url::kFileScheme); + cookie_monster->SetCookieableSchemes(schemes); + + DCHECK(url_request_context_); + url_request_context_->set_cookie_store(cookie_store_.get()); +} + } // namespace brightray diff --git a/vendor/brightray/browser/url_request_context_getter.h b/vendor/brightray/browser/url_request_context_getter.h index c09824d44..5a323a637 100644 --- a/vendor/brightray/browser/url_request_context_getter.h +++ b/vendor/brightray/browser/url_request_context_getter.h @@ -13,6 +13,7 @@ #include "net/http/transport_security_state.h" #include "net/http/url_security_manager.h" #include "net/url_request/url_request_context_getter.h" +#include "tizen_src/ewk/efl_integration/public/ewk_cookie_manager_internal.h" namespace base { class MessageLoop; @@ -83,13 +84,23 @@ class URLRequestContextGetter : public net::URLRequestContextGetter { return delegate_->GetMediaDeviceIDSalt(); } + void SetCookieStoragePath(const base::FilePath& path, + bool persist_session_cookies, + Ewk_Cookie_Persistent_Storage file_storage); private: + void CreateSQLitePersistentCookieStore(const base::FilePath& path, + bool persist_session_cookies); + void CreatePersistentCookieStore(const base::FilePath& path, + bool persist_session_cookies); + Delegate* delegate_; DevToolsNetworkControllerHandle* network_controller_handle_; NetLog* net_log_; base::FilePath base_path_; bool in_memory_; + base::FilePath cookie_store_path_; + std::unique_ptr cookie_store_; scoped_refptr io_task_runner_; scoped_refptr file_task_runner_; diff --git a/vendor/brightray/filenames.gypi b/vendor/brightray/filenames.gypi index 18821ca81..c084d5b9f 100644 --- a/vendor/brightray/filenames.gypi +++ b/vendor/brightray/filenames.gypi @@ -19,8 +19,10 @@ 'browser/devtools_manager_delegate.h', 'browser/devtools_ui.cc', 'browser/devtools_ui.h', - 'browser/efl/notification_presenter_efl.h', + 'browser/efl/cookie_manager.cc', + 'browser/efl/cookie_manager.h', 'browser/efl/notification_presenter_efl.cc', + 'browser/efl/notification_presenter_efl.h', 'browser/inspectable_web_contents.cc', 'browser/inspectable_web_contents.h', 'browser/inspectable_web_contents_delegate.h',