Commit c2789e7e authored by Randy Smith's avatar Randy Smith Committed by Commit Bot

Switch SessionDataDeleter cookie access over to Mojo.

Bug: 721395
Change-Id: I06b833910c11c345b0995c0ff3f7de5f64c56b82
Reviewed-on: https://chromium-review.googlesource.com/760037Reviewed-by: default avatarScott Violet <sky@chromium.org>
Commit-Queue: Randy Smith <rdsmith@chromium.org>
Cr-Commit-Position: refs/heads/master@{#516337}
parent aa260864
...@@ -19,27 +19,19 @@ ...@@ -19,27 +19,19 @@
#include "content/public/browser/dom_storage_context.h" #include "content/public/browser/dom_storage_context.h"
#include "content/public/browser/local_storage_usage_info.h" #include "content/public/browser/local_storage_usage_info.h"
#include "content/public/browser/storage_partition.h" #include "content/public/browser/storage_partition.h"
#include "net/cookies/cookie_store.h"
#include "net/cookies/cookie_util.h" #include "net/cookies/cookie_util.h"
#include "net/url_request/url_request_context.h" #include "services/network/public/interfaces/cookie_manager.mojom.h"
#include "net/url_request/url_request_context_getter.h"
#include "storage/browser/quota/special_storage_policy.h" #include "storage/browser/quota/special_storage_policy.h"
namespace { namespace {
void CookieDeleted(uint32_t num_cookies_deleted) {
DCHECK_EQ(1u, num_cookies_deleted);
}
class SessionDataDeleter class SessionDataDeleter
: public base::RefCountedThreadSafe<SessionDataDeleter> { : public base::RefCountedThreadSafe<SessionDataDeleter> {
public: public:
SessionDataDeleter(storage::SpecialStoragePolicy* storage_policy, SessionDataDeleter(storage::SpecialStoragePolicy* storage_policy,
bool delete_only_by_session_only_policy); bool delete_only_by_session_only_policy);
void Run( void Run(content::StoragePartition* storage_partition);
content::StoragePartition* storage_partition,
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter);
private: private:
friend class base::RefCountedThreadSafe<SessionDataDeleter>; friend class base::RefCountedThreadSafe<SessionDataDeleter>;
...@@ -51,22 +43,13 @@ class SessionDataDeleter ...@@ -51,22 +43,13 @@ class SessionDataDeleter
content::StoragePartition* storage_partition, content::StoragePartition* storage_partition,
const std::vector<content::LocalStorageUsageInfo>& usages); const std::vector<content::LocalStorageUsageInfo>& usages);
// Deletes all cookies that are session only if // Takes the result of a CookieManager::GetAllCookies() method, and
// |delete_only_by_session_only_policy_| is false. Once completed or skipped, // initiates deletion of all cookies that are session only by the
// this arranges for DeleteSessionOnlyOriginCookies to be called with a list // storage policy of the constructor.
// of all remaining cookies. void DeleteSessionOnlyOriginCookies(
void DeleteSessionCookiesOnIOThread( const std::vector<net::CanonicalCookie>& cookies);
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter);
// Called when all session-only cookies have been deleted.
void DeleteSessionCookiesDone(net::CookieStore* cookie_store,
uint32_t num_deleted);
// Deletes the cookies in |cookies| that are for origins which are
// session-only.
void DeleteSessionOnlyOriginCookies(net::CookieStore* cookie_store,
const net::CookieList& cookies);
network::mojom::CookieManagerPtr cookie_manager_;
scoped_refptr<storage::SpecialStoragePolicy> storage_policy_; scoped_refptr<storage::SpecialStoragePolicy> storage_policy_;
const bool delete_only_by_session_only_policy_; const bool delete_only_by_session_only_policy_;
...@@ -80,19 +63,65 @@ SessionDataDeleter::SessionDataDeleter( ...@@ -80,19 +63,65 @@ SessionDataDeleter::SessionDataDeleter(
delete_only_by_session_only_policy_(delete_only_by_session_only_policy) { delete_only_by_session_only_policy_(delete_only_by_session_only_policy) {
} }
void SessionDataDeleter::Run( void SessionDataDeleter::Run(content::StoragePartition* storage_partition) {
content::StoragePartition* storage_partition,
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) {
if (storage_policy_.get() && storage_policy_->HasSessionOnlyOrigins()) { if (storage_policy_.get() && storage_policy_->HasSessionOnlyOrigins()) {
storage_partition->GetDOMStorageContext()->GetLocalStorageUsage( storage_partition->GetDOMStorageContext()->GetLocalStorageUsage(
base::Bind(&SessionDataDeleter::ClearSessionOnlyLocalStorage, base::Bind(&SessionDataDeleter::ClearSessionOnlyLocalStorage,
this, this,
storage_partition)); storage_partition));
} }
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE, storage_partition->GetNetworkContext()->GetCookieManager(
base::Bind(&SessionDataDeleter::DeleteSessionCookiesOnIOThread, this, mojo::MakeRequest(&cookie_manager_));
url_request_context_getter));
if (!delete_only_by_session_only_policy_) {
network::mojom::CookieDeletionFilterPtr filter(
network::mojom::CookieDeletionFilter::New());
filter->session_control =
network::mojom::CookieDeletionSessionControl::SESSION_COOKIES;
cookie_manager_->DeleteCookies(
std::move(filter),
// Fire and forget
network::mojom::CookieManager::DeleteCookiesCallback());
}
if (!storage_policy_.get() || !storage_policy_->HasSessionOnlyOrigins())
return;
cookie_manager_->GetAllCookies(
base::Bind(&SessionDataDeleter::DeleteSessionOnlyOriginCookies, this));
// Note that from this point on |*this| is kept alive by scoped_refptr<>
// references automatically taken by |Bind()|, so when the last callback
// created by Bind() is released (after execution of that function), the
// object will be deleted. This may result in any callbacks passed to
// |*cookie_manager_.get()| methods not being executed because of the
// destruction of the CookieManagerPtr, but the model of the
// SessionDataDeleter is "fire-and-forget" and all such callbacks are
// empty, so that is ok. Mojo guarantees that all messages pushed onto a
// pipe will be executed by the server side of the pipe even if the client
// side of the pipe is closed, so all deletion requested will still occur.
}
void SessionDataDeleter::DeleteSessionOnlyOriginCookies(
const std::vector<net::CanonicalCookie>& cookies) {
base::Time yesterday(base::Time::Now() - base::TimeDelta::FromDays(1));
for (const auto& cookie : cookies) {
GURL url =
net::cookie_util::CookieOriginToURL(cookie.Domain(), cookie.IsSecure());
if (!storage_policy_->IsStorageSessionOnly(url))
continue;
// Delete a single cookie by setting its expiration time into the past.
cookie_manager_->SetCanonicalCookie(
net::CanonicalCookie(cookie.Name(), cookie.Value(), cookie.Domain(),
cookie.Path(), cookie.CreationDate(), yesterday,
cookie.LastAccessDate(), cookie.IsSecure(),
cookie.IsHttpOnly(), cookie.SameSite(),
cookie.Priority()),
true /* secure_source */, true /* modify_http_only */,
// Fire and forget
network::mojom::CookieManager::SetCanonicalCookieCallback());
}
} }
SessionDataDeleter::~SessionDataDeleter() {} SessionDataDeleter::~SessionDataDeleter() {}
...@@ -110,54 +139,6 @@ void SessionDataDeleter::ClearSessionOnlyLocalStorage( ...@@ -110,54 +139,6 @@ void SessionDataDeleter::ClearSessionOnlyLocalStorage(
} }
} }
void SessionDataDeleter::DeleteSessionCookiesOnIOThread(
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
net::URLRequestContext* request_context =
url_request_context_getter->GetURLRequestContext();
// Shouldn't happen, but best not to depend on the URLRequestContext's
// lifetime
if (!request_context)
return;
net::CookieStore* cookie_store = request_context->cookie_store();
// If these callbacks are invoked, |cookie_store| is guaranteed to still
// exist, since deleting the CookieStore will cancel pending callbacks.
if (delete_only_by_session_only_policy_) {
cookie_store->GetAllCookiesAsync(
base::Bind(&SessionDataDeleter::DeleteSessionOnlyOriginCookies, this,
base::Unretained(cookie_store)));
} else {
cookie_store->DeleteSessionCookiesAsync(
base::Bind(&SessionDataDeleter::DeleteSessionCookiesDone, this,
base::Unretained(cookie_store)));
}
}
void SessionDataDeleter::DeleteSessionCookiesDone(
net::CookieStore* cookie_store,
uint32_t num_deleted) {
// If these callbacks are invoked, |cookie_store| is gauranteed to still
// exist, since deleting the CookieStore will cancel pending callbacks.
cookie_store->GetAllCookiesAsync(
base::Bind(&SessionDataDeleter::DeleteSessionOnlyOriginCookies, this,
base::Unretained(cookie_store)));
}
void SessionDataDeleter::DeleteSessionOnlyOriginCookies(
net::CookieStore* cookie_store,
const net::CookieList& cookies) {
if (!storage_policy_.get() || !storage_policy_->HasSessionOnlyOrigins())
return;
for (const auto& cookie : cookies) {
GURL url =
net::cookie_util::CookieOriginToURL(cookie.Domain(), cookie.IsSecure());
if (!storage_policy_->IsStorageSessionOnly(url))
continue;
cookie_store->DeleteCanonicalCookieAsync(cookie, base::Bind(CookieDeleted));
}
}
} // namespace } // namespace
void DeleteSessionOnlyData(Profile* profile) { void DeleteSessionOnlyData(Profile* profile) {
...@@ -179,6 +160,5 @@ void DeleteSessionOnlyData(Profile* profile) { ...@@ -179,6 +160,5 @@ void DeleteSessionOnlyData(Profile* profile) {
scoped_refptr<SessionDataDeleter> deleter( scoped_refptr<SessionDataDeleter> deleter(
new SessionDataDeleter(profile->GetSpecialStoragePolicy(), new SessionDataDeleter(profile->GetSpecialStoragePolicy(),
startup_pref_type == SessionStartupPref::LAST)); startup_pref_type == SessionStartupPref::LAST));
deleter->Run(Profile::GetDefaultStoragePartition(profile), deleter->Run(Profile::GetDefaultStoragePartition(profile));
profile->GetRequestContext());
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment