| Index: chrome/browser/sessions/session_data_deleter.cc
|
| diff --git a/chrome/browser/sessions/session_data_deleter.cc b/chrome/browser/sessions/session_data_deleter.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..7fbaec30c7452de03598d36a58f91241a7c9baf1
|
| --- /dev/null
|
| +++ b/chrome/browser/sessions/session_data_deleter.cc
|
| @@ -0,0 +1,161 @@
|
| +// Copyright 2013 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/command_line.h"
|
| +#include "chrome/browser/browser_shutdown.h"
|
| +#include "chrome/browser/prefs/session_startup_pref.h"
|
| +#include "chrome/browser/ui/startup/startup_browser_creator.h"
|
| +#include "content/public/browser/browser_context.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +#include "content/public/browser/dom_storage_context.h"
|
| +#include "content/public/browser/local_storage_usage_info.h"
|
| +#include "content/public/browser/storage_partition.h"
|
| +#include "net/cookies/cookie_monster.h"
|
| +#include "net/cookies/cookie_store.h"
|
| +#include "net/cookies/cookie_util.h"
|
| +#include "net/url_request/url_request_context.h"
|
| +#include "net/url_request/url_request_context_getter.h"
|
| +#include "webkit/browser/quota/special_storage_policy.h"
|
| +
|
| +namespace {
|
| +
|
| +void CookieDeleted(bool success) {
|
| + DCHECK(success);
|
| +}
|
| +
|
| +class SessionDataDeleter
|
| + : public base::RefCountedThreadSafe<SessionDataDeleter> {
|
| + public:
|
| + SessionDataDeleter(content::BrowserContext* context,
|
| + bool delete_all_session_cookies);
|
| +
|
| + void Run(content::StoragePartition* storage_partition);
|
| +
|
| + private:
|
| + friend class base::RefCountedThreadSafe<SessionDataDeleter>;
|
| + ~SessionDataDeleter();
|
| +
|
| + // Deletes the local storage described by |usages| for origins which are
|
| + // session-only.
|
| + void ClearSessionOnlyLocalStorage(
|
| + content::StoragePartition* storage_partition,
|
| + const std::vector<content::LocalStorageUsageInfo>& usages);
|
| +
|
| + // Deletes all cookies that are session only if |delete_all_session_cookies_|
|
| + // is true. Once completed or skipped, this arranges for
|
| + // DeleteSessionOnlyOriginCookies to be called with a list of all remaining
|
| + // cookies.
|
| + void DeleteSessionCookiesOnIOThread(
|
| + net::URLRequestContextGetter* request_context_getter);
|
| +
|
| + // Called when all session-only cookies have been deleted.
|
| + void DeleteSessionCookiesDone(int num_deleted);
|
| +
|
| + // Deletes the cookies in |cookies| that are for origins which are
|
| + // session-only.
|
| + void DeleteSessionOnlyOriginCookies(const net::CookieList& cookies);
|
| +
|
| + scoped_refptr<net::CookieMonster> cookie_monster_;
|
| + scoped_refptr<quota::SpecialStoragePolicy> storage_policy_;
|
| + const bool delete_all_session_cookies_;
|
| +};
|
| +
|
| +SessionDataDeleter::SessionDataDeleter(content::BrowserContext* context,
|
| + bool delete_all_session_cookies)
|
| + : storage_policy_(context->GetSpecialStoragePolicy()),
|
| + delete_all_session_cookies_(delete_all_session_cookies) {}
|
| +
|
| +void SessionDataDeleter::Run(content::StoragePartition* storage_partition) {
|
| + storage_partition->GetDOMStorageContext()->GetLocalStorageUsage(
|
| + base::Bind(&SessionDataDeleter::ClearSessionOnlyLocalStorage,
|
| + this,
|
| + storage_partition));
|
| + content::BrowserThread::PostTask(
|
| + content::BrowserThread::IO,
|
| + FROM_HERE,
|
| + base::Bind(
|
| + &SessionDataDeleter::DeleteSessionCookiesOnIOThread,
|
| + this,
|
| + make_scoped_refptr(storage_partition->GetURLRequestContext())));
|
| +}
|
| +
|
| +SessionDataDeleter::~SessionDataDeleter() {}
|
| +
|
| +void SessionDataDeleter::ClearSessionOnlyLocalStorage(
|
| + content::StoragePartition* storage_partition,
|
| + const std::vector<content::LocalStorageUsageInfo>& usages) {
|
| + for (std::vector<content::LocalStorageUsageInfo>::const_iterator it =
|
| + usages.begin();
|
| + it != usages.end();
|
| + ++it) {
|
| + if (storage_policy_->IsStorageSessionOnly(it->origin))
|
| + storage_partition->GetDOMStorageContext()->DeleteLocalStorage(it->origin);
|
| + }
|
| +}
|
| +
|
| +void SessionDataDeleter::DeleteSessionCookiesOnIOThread(
|
| + net::URLRequestContextGetter* request_context_getter) {
|
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
|
| + cookie_monster_ = request_context_getter->GetURLRequestContext()
|
| + ->cookie_store()
|
| + ->GetCookieMonster();
|
| + if (delete_all_session_cookies_) {
|
| + cookie_monster_->DeleteSessionCookiesAsync(
|
| + base::Bind(&SessionDataDeleter::DeleteSessionCookiesDone, this));
|
| + } else {
|
| + cookie_monster_->GetCookieMonster()->GetAllCookiesAsync(
|
| + base::Bind(&SessionDataDeleter::DeleteSessionOnlyOriginCookies, this));
|
| + }
|
| +}
|
| +
|
| +void SessionDataDeleter::DeleteSessionCookiesDone(int num_deleted) {
|
| + cookie_monster_->GetAllCookiesAsync(
|
| + base::Bind(&SessionDataDeleter::DeleteSessionOnlyOriginCookies, this));
|
| +}
|
| +
|
| +void SessionDataDeleter::DeleteSessionOnlyOriginCookies(
|
| + const net::CookieList& cookies) {
|
| + for (net::CookieList::const_iterator it = cookies.begin();
|
| + it != cookies.end();
|
| + ++it) {
|
| + if (storage_policy_->IsStorageSessionOnly(
|
| + net::cookie_util::CookieOriginToURL(it->Domain(),
|
| + it->IsSecure()))) {
|
| + cookie_monster_->DeleteCanonicalCookieAsync(*it,
|
| + base::Bind(CookieDeleted));
|
| + }
|
| + }
|
| +}
|
| +
|
| +void ClearSessionOnlyDataForStoragePartition(
|
| + content::BrowserContext* context,
|
| + bool delete_all_session_cookies,
|
| + content::StoragePartition* storage_partition) {
|
| + scoped_refptr<SessionDataDeleter> deleter(
|
| + new SessionDataDeleter(context, delete_all_session_cookies));
|
| + deleter->Run(storage_partition);
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +void DeleteSessionOnlyData(Profile* profile) {
|
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
|
| + if (browser_shutdown::IsTryingToQuit())
|
| + return;
|
| +
|
| +#if defined(OS_ANDROID)
|
| + SessionStartupPref::Type startup_pref_type =
|
| + SessionStartupPref::GetDefaultStartupType();
|
| +#else
|
| + SessionStartupPref::Type startup_pref_type =
|
| + StartupBrowserCreator::GetSessionStartupPref(
|
| + *CommandLine::ForCurrentProcess(), profile).type;
|
| +#endif
|
| + Profile::ForEachStoragePartition(
|
| + profile,
|
| + base::Bind(ClearSessionOnlyDataForStoragePartition,
|
| + profile,
|
| + startup_pref_type != SessionStartupPref::LAST));
|
| +}
|
|
|