Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(783)

Side by Side Diff: chrome/browser/sessions/session_data_deleter.cc

Issue 25414005: Clear session-only data when all browser windows are closed. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/net/chrome_url_request_context.h"
9 #include "chrome/browser/prefs/session_startup_pref.h"
10 #include "chrome/browser/profiles/profile_io_data.h"
11 #include "chrome/browser/ui/startup/startup_browser_creator.h"
12 #include "content/public/browser/browser_context.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/dom_storage_context.h"
15 #include "content/public/browser/local_storage_usage_info.h"
16 #include "content/public/browser/storage_partition.h"
17 #include "net/cookies/cookie_monster.h"
18 #include "net/cookies/cookie_store.h"
19 #include "net/cookies/cookie_util.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(quota::SpecialStoragePolicy* storage_policy,
32 bool delete_all_session_cookies);
33
34 void Run(content::StoragePartition* storage_partition,
35 ProfileIOData* profile_io_data);
36
37 private:
38 friend class base::RefCountedThreadSafe<SessionDataDeleter>;
39 ~SessionDataDeleter();
40
41 // Deletes the local storage described by |usages| for origins which are
42 // session-only.
43 void ClearSessionOnlyLocalStorage(
44 content::StoragePartition* storage_partition,
45 const std::vector<content::LocalStorageUsageInfo>& usages);
46
47 // Deletes all cookies that are session only if |delete_all_session_cookies_|
48 // is true. Once completed or skipped, this arranges for
49 // DeleteSessionOnlyOriginCookies to be called with a list of all remaining
50 // cookies.
51 void DeleteSessionCookiesOnIOThread(
52 ProfileIOData* profile_io_data);
53
54 // Called when all session-only cookies have been deleted.
55 void DeleteSessionCookiesDone(int num_deleted);
56
57 // Deletes the cookies in |cookies| that are for origins which are
58 // session-only.
59 void DeleteSessionOnlyOriginCookies(const net::CookieList& cookies);
60
61 base::WeakPtr<ChromeURLRequestContext> request_context_;
62 scoped_refptr<quota::SpecialStoragePolicy> storage_policy_;
63 const bool delete_all_session_cookies_;
64
65 DISALLOW_COPY_AND_ASSIGN(SessionDataDeleter);
66 };
67
68 SessionDataDeleter::SessionDataDeleter(
69 quota::SpecialStoragePolicy* storage_policy,
70 bool delete_all_session_cookies)
71 : storage_policy_(storage_policy),
72 delete_all_session_cookies_(delete_all_session_cookies) {}
73
74 void SessionDataDeleter::Run(content::StoragePartition* storage_partition,
75 ProfileIOData* profile_io_data) {
76 storage_partition->GetDOMStorageContext()->GetLocalStorageUsage(
77 base::Bind(&SessionDataDeleter::ClearSessionOnlyLocalStorage,
78 this,
79 storage_partition));
80 content::BrowserThread::PostTask(
81 content::BrowserThread::IO,
82 FROM_HERE,
83 base::Bind(&SessionDataDeleter::DeleteSessionCookiesOnIOThread,
84 this,
85 profile_io_data));
86 }
87
88 SessionDataDeleter::~SessionDataDeleter() {}
89
90 void SessionDataDeleter::ClearSessionOnlyLocalStorage(
91 content::StoragePartition* storage_partition,
92 const std::vector<content::LocalStorageUsageInfo>& usages) {
93 for (std::vector<content::LocalStorageUsageInfo>::const_iterator it =
94 usages.begin();
95 it != usages.end();
96 ++it) {
97 if (storage_policy_->IsStorageSessionOnly(it->origin))
98 storage_partition->GetDOMStorageContext()->DeleteLocalStorage(it->origin);
99 }
100 }
101
102 void SessionDataDeleter::DeleteSessionCookiesOnIOThread(
103 ProfileIOData* profile_io_data) {
104 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
105 ChromeURLRequestContext* request_context =
106 profile_io_data->GetMainRequestContext();
107 request_context_ = request_context->GetWeakPtr();
108 net::CookieMonster* cookie_monster =
109 request_context_->cookie_store()->GetCookieMonster();
110 if (delete_all_session_cookies_) {
111 cookie_monster->DeleteSessionCookiesAsync(
112 base::Bind(&SessionDataDeleter::DeleteSessionCookiesDone, this));
113 } else {
114 cookie_monster->GetCookieMonster()->GetAllCookiesAsync(
benwells 2013/10/15 08:28:51 Nit: You don't need this GetCookieMonster, I think
Sam McNally 2013/10/15 22:53:05 Done.
115 base::Bind(&SessionDataDeleter::DeleteSessionOnlyOriginCookies, this));
116 }
117 }
118
119 void SessionDataDeleter::DeleteSessionCookiesDone(int num_deleted) {
120 ChromeURLRequestContext* request_context = request_context_.get();
121 if (!request_context)
122 return;
123
124 request_context->cookie_store()->GetCookieMonster()->GetAllCookiesAsync(
125 base::Bind(&SessionDataDeleter::DeleteSessionOnlyOriginCookies, this));
126 }
127
128 void SessionDataDeleter::DeleteSessionOnlyOriginCookies(
129 const net::CookieList& cookies) {
130 ChromeURLRequestContext* request_context = request_context_.get();
131 if (!request_context)
132 return;
133
134 net::CookieMonster* cookie_monster =
135 request_context->cookie_store()->GetCookieMonster();
136 for (net::CookieList::const_iterator it = cookies.begin();
137 it != cookies.end();
138 ++it) {
139 if (storage_policy_->IsStorageSessionOnly(
140 net::cookie_util::CookieOriginToURL(it->Domain(),
141 it->IsSecure()))) {
142 cookie_monster->DeleteCanonicalCookieAsync(*it,
143 base::Bind(CookieDeleted));
144 }
145 }
146 }
147
148 } // namespace
149
150 void DeleteSessionOnlyData(Profile* profile) {
151 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
152 if (browser_shutdown::IsTryingToQuit())
153 return;
154
155 #if defined(OS_ANDROID)
156 SessionStartupPref::Type startup_pref_type =
157 SessionStartupPref::GetDefaultStartupType();
158 #else
159 SessionStartupPref::Type startup_pref_type =
160 StartupBrowserCreator::GetSessionStartupPref(
161 *CommandLine::ForCurrentProcess(), profile).type;
162 #endif
163
164 scoped_refptr<SessionDataDeleter> deleter(
165 new SessionDataDeleter(profile->GetSpecialStoragePolicy(),
166 startup_pref_type != SessionStartupPref::LAST));
167 deleter->Run(
168 Profile::GetDefaultStoragePartition(profile),
169 ProfileIOData::FromResourceContext(profile->GetResourceContext()));
170 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698