| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/command_line.h" |
| 7 #include "chrome/browser/browser_shutdown.h" |
| 8 #include "chrome/browser/prefs/session_startup_pref.h" |
| 9 #include "chrome/browser/ui/startup/startup_browser_creator.h" |
| 10 #include "content/public/browser/browser_context.h" |
| 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/public/browser/dom_storage_context.h" |
| 13 #include "content/public/browser/local_storage_usage_info.h" |
| 14 #include "content/public/browser/storage_partition.h" |
| 15 #include "net/cookies/cookie_monster.h" |
| 16 #include "net/cookies/cookie_store.h" |
| 17 #include "net/cookies/cookie_util.h" |
| 18 #include "net/url_request/url_request_context.h" |
| 19 #include "net/url_request/url_request_context_getter.h" |
| 20 #include "webkit/browser/quota/special_storage_policy.h" |
| 21 |
| 22 namespace { |
| 23 |
| 24 void CookieDeleted(bool success) { |
| 25 DCHECK(success); |
| 26 } |
| 27 |
| 28 class SessionDataDeleter |
| 29 : public base::RefCountedThreadSafe<SessionDataDeleter> { |
| 30 public: |
| 31 SessionDataDeleter(content::BrowserContext* context, |
| 32 bool delete_all_session_cookies); |
| 33 |
| 34 void Run(content::StoragePartition* storage_partition); |
| 35 |
| 36 private: |
| 37 friend class base::RefCountedThreadSafe<SessionDataDeleter>; |
| 38 ~SessionDataDeleter(); |
| 39 |
| 40 // Deletes the local storage described by |usages| for origins which are |
| 41 // session-only. |
| 42 void ClearSessionOnlyLocalStorage( |
| 43 content::StoragePartition* storage_partition, |
| 44 const std::vector<content::LocalStorageUsageInfo>& usages); |
| 45 |
| 46 // Deletes all cookies that are session only if |delete_all_session_cookies_| |
| 47 // is true. Once completed or skipped, this arranges for |
| 48 // DeleteSessionOnlyOriginCookies to be called with a list of all remaining |
| 49 // cookies. |
| 50 void DeleteSessionCookiesOnIOThread( |
| 51 net::URLRequestContextGetter* request_context_getter); |
| 52 |
| 53 // Called when all session-only cookies have been deleted. |
| 54 void DeleteSessionCookiesDone(int num_deleted); |
| 55 |
| 56 // Deletes the cookies in |cookies| that are for origins which are |
| 57 // session-only. |
| 58 void DeleteSessionOnlyOriginCookies(const net::CookieList& cookies); |
| 59 |
| 60 scoped_refptr<net::CookieMonster> cookie_monster_; |
| 61 scoped_refptr<quota::SpecialStoragePolicy> storage_policy_; |
| 62 const bool delete_all_session_cookies_; |
| 63 }; |
| 64 |
| 65 SessionDataDeleter::SessionDataDeleter(content::BrowserContext* context, |
| 66 bool delete_all_session_cookies) |
| 67 : storage_policy_(context->GetSpecialStoragePolicy()), |
| 68 delete_all_session_cookies_(delete_all_session_cookies) {} |
| 69 |
| 70 void SessionDataDeleter::Run(content::StoragePartition* storage_partition) { |
| 71 storage_partition->GetDOMStorageContext()->GetLocalStorageUsage( |
| 72 base::Bind(&SessionDataDeleter::ClearSessionOnlyLocalStorage, |
| 73 this, |
| 74 storage_partition)); |
| 75 content::BrowserThread::PostTask( |
| 76 content::BrowserThread::IO, |
| 77 FROM_HERE, |
| 78 base::Bind( |
| 79 &SessionDataDeleter::DeleteSessionCookiesOnIOThread, |
| 80 this, |
| 81 make_scoped_refptr(storage_partition->GetURLRequestContext()))); |
| 82 } |
| 83 |
| 84 SessionDataDeleter::~SessionDataDeleter() {} |
| 85 |
| 86 void SessionDataDeleter::ClearSessionOnlyLocalStorage( |
| 87 content::StoragePartition* storage_partition, |
| 88 const std::vector<content::LocalStorageUsageInfo>& usages) { |
| 89 for (std::vector<content::LocalStorageUsageInfo>::const_iterator it = |
| 90 usages.begin(); |
| 91 it != usages.end(); |
| 92 ++it) { |
| 93 if (storage_policy_->IsStorageSessionOnly(it->origin)) |
| 94 storage_partition->GetDOMStorageContext()->DeleteLocalStorage(it->origin); |
| 95 } |
| 96 } |
| 97 |
| 98 void SessionDataDeleter::DeleteSessionCookiesOnIOThread( |
| 99 net::URLRequestContextGetter* request_context_getter) { |
| 100 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 101 cookie_monster_ = request_context_getter->GetURLRequestContext() |
| 102 ->cookie_store() |
| 103 ->GetCookieMonster(); |
| 104 if (delete_all_session_cookies_) { |
| 105 cookie_monster_->DeleteSessionCookiesAsync( |
| 106 base::Bind(&SessionDataDeleter::DeleteSessionCookiesDone, this)); |
| 107 } else { |
| 108 cookie_monster_->GetCookieMonster()->GetAllCookiesAsync( |
| 109 base::Bind(&SessionDataDeleter::DeleteSessionOnlyOriginCookies, this)); |
| 110 } |
| 111 } |
| 112 |
| 113 void SessionDataDeleter::DeleteSessionCookiesDone(int num_deleted) { |
| 114 cookie_monster_->GetAllCookiesAsync( |
| 115 base::Bind(&SessionDataDeleter::DeleteSessionOnlyOriginCookies, this)); |
| 116 } |
| 117 |
| 118 void SessionDataDeleter::DeleteSessionOnlyOriginCookies( |
| 119 const net::CookieList& cookies) { |
| 120 for (net::CookieList::const_iterator it = cookies.begin(); |
| 121 it != cookies.end(); |
| 122 ++it) { |
| 123 if (storage_policy_->IsStorageSessionOnly( |
| 124 net::cookie_util::CookieOriginToURL(it->Domain(), |
| 125 it->IsSecure()))) { |
| 126 cookie_monster_->DeleteCanonicalCookieAsync(*it, |
| 127 base::Bind(CookieDeleted)); |
| 128 } |
| 129 } |
| 130 } |
| 131 |
| 132 void ClearSessionOnlyDataForStoragePartition( |
| 133 content::BrowserContext* context, |
| 134 bool delete_all_session_cookies, |
| 135 content::StoragePartition* storage_partition) { |
| 136 scoped_refptr<SessionDataDeleter> deleter( |
| 137 new SessionDataDeleter(context, delete_all_session_cookies)); |
| 138 deleter->Run(storage_partition); |
| 139 } |
| 140 |
| 141 } // namespace |
| 142 |
| 143 void DeleteSessionOnlyData(Profile* profile) { |
| 144 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 145 if (browser_shutdown::IsTryingToQuit()) |
| 146 return; |
| 147 |
| 148 #if defined(OS_ANDROID) |
| 149 SessionStartupPref::Type startup_pref_type = |
| 150 SessionStartupPref::GetDefaultStartupType(); |
| 151 #else |
| 152 SessionStartupPref::Type startup_pref_type = |
| 153 StartupBrowserCreator::GetSessionStartupPref( |
| 154 *CommandLine::ForCurrentProcess(), profile).type; |
| 155 #endif |
| 156 Profile::ForEachStoragePartition( |
| 157 profile, |
| 158 base::Bind(ClearSessionOnlyDataForStoragePartition, |
| 159 profile, |
| 160 startup_pref_type != SessionStartupPref::LAST)); |
| 161 } |
| OLD | NEW |