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

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

Issue 1701063002: CookieStore: Remove reference counting. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@threadsafe
Patch Set: More IWYU fixes Created 4 years, 9 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stddef.h> 5 #include <stddef.h>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
11 #include "build/build_config.h" 11 #include "build/build_config.h"
12 #include "chrome/browser/browser_shutdown.h" 12 #include "chrome/browser/browser_shutdown.h"
13 #include "chrome/browser/prefs/session_startup_pref.h" 13 #include "chrome/browser/prefs/session_startup_pref.h"
14 #include "chrome/browser/profiles/profile_io_data.h" 14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/startup/startup_browser_creator.h" 15 #include "chrome/browser/ui/startup/startup_browser_creator.h"
16 #include "content/public/browser/browser_context.h" 16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/dom_storage_context.h" 18 #include "content/public/browser/dom_storage_context.h"
19 #include "content/public/browser/local_storage_usage_info.h" 19 #include "content/public/browser/local_storage_usage_info.h"
20 #include "content/public/browser/storage_partition.h" 20 #include "content/public/browser/storage_partition.h"
21 #include "net/cookies/cookie_store.h" 21 #include "net/cookies/cookie_store.h"
22 #include "net/cookies/cookie_util.h" 22 #include "net/cookies/cookie_util.h"
23 #include "net/url_request/url_request_context.h" 23 #include "net/url_request/url_request_context.h"
24 #include "net/url_request/url_request_context_getter.h"
24 #include "storage/browser/quota/special_storage_policy.h" 25 #include "storage/browser/quota/special_storage_policy.h"
25 26
26 namespace { 27 namespace {
27 28
28 void CookieDeleted(int num_cookies_deleted) { 29 void CookieDeleted(int num_cookies_deleted) {
29 DCHECK_EQ(1, num_cookies_deleted); 30 DCHECK_EQ(1, num_cookies_deleted);
30 } 31 }
31 32
32 class SessionDataDeleter 33 class SessionDataDeleter
33 : public base::RefCountedThreadSafe<SessionDataDeleter> { 34 : public base::RefCountedThreadSafe<SessionDataDeleter> {
34 public: 35 public:
35 SessionDataDeleter(storage::SpecialStoragePolicy* storage_policy, 36 SessionDataDeleter(storage::SpecialStoragePolicy* storage_policy,
36 bool delete_only_by_session_only_policy); 37 bool delete_only_by_session_only_policy);
37 38
38 void Run(content::StoragePartition* storage_partition, 39 void Run(
39 ProfileIOData* profile_io_data); 40 content::StoragePartition* storage_partition,
41 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter);
40 42
41 private: 43 private:
42 friend class base::RefCountedThreadSafe<SessionDataDeleter>; 44 friend class base::RefCountedThreadSafe<SessionDataDeleter>;
43 ~SessionDataDeleter(); 45 ~SessionDataDeleter();
44 46
45 // Deletes the local storage described by |usages| for origins which are 47 // Deletes the local storage described by |usages| for origins which are
46 // session-only. 48 // session-only.
47 void ClearSessionOnlyLocalStorage( 49 void ClearSessionOnlyLocalStorage(
48 content::StoragePartition* storage_partition, 50 content::StoragePartition* storage_partition,
49 const std::vector<content::LocalStorageUsageInfo>& usages); 51 const std::vector<content::LocalStorageUsageInfo>& usages);
50 52
51 // Deletes all cookies that are session only if 53 // Deletes all cookies that are session only if
52 // |delete_only_by_session_only_policy_| is false. Once completed or skipped, 54 // |delete_only_by_session_only_policy_| is false. Once completed or skipped,
53 // this arranges for DeleteSessionOnlyOriginCookies to be called with a list 55 // this arranges for DeleteSessionOnlyOriginCookies to be called with a list
54 // of all remaining cookies. 56 // of all remaining cookies.
55 void DeleteSessionCookiesOnIOThread(ProfileIOData* profile_io_data); 57 void DeleteSessionCookiesOnIOThread(
58 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter);
56 59
57 // Called when all session-only cookies have been deleted. 60 // Called when all session-only cookies have been deleted.
58 void DeleteSessionCookiesDone(int num_deleted); 61 void DeleteSessionCookiesDone(net::CookieStore* cookie_store,
62 int num_deleted);
59 63
60 // Deletes the cookies in |cookies| that are for origins which are 64 // Deletes the cookies in |cookies| that are for origins which are
61 // session-only. 65 // session-only.
62 void DeleteSessionOnlyOriginCookies(const net::CookieList& cookies); 66 void DeleteSessionOnlyOriginCookies(net::CookieStore* cookie_store,
67 const net::CookieList& cookies);
63 68
64 scoped_refptr<net::CookieStore> cookie_store_;
65 scoped_refptr<storage::SpecialStoragePolicy> storage_policy_; 69 scoped_refptr<storage::SpecialStoragePolicy> storage_policy_;
66 const bool delete_only_by_session_only_policy_; 70 const bool delete_only_by_session_only_policy_;
67 71
68 DISALLOW_COPY_AND_ASSIGN(SessionDataDeleter); 72 DISALLOW_COPY_AND_ASSIGN(SessionDataDeleter);
69 }; 73 };
70 74
71 SessionDataDeleter::SessionDataDeleter( 75 SessionDataDeleter::SessionDataDeleter(
72 storage::SpecialStoragePolicy* storage_policy, 76 storage::SpecialStoragePolicy* storage_policy,
73 bool delete_only_by_session_only_policy) 77 bool delete_only_by_session_only_policy)
74 : storage_policy_(storage_policy), 78 : storage_policy_(storage_policy),
75 delete_only_by_session_only_policy_(delete_only_by_session_only_policy) { 79 delete_only_by_session_only_policy_(delete_only_by_session_only_policy) {
76 } 80 }
77 81
78 void SessionDataDeleter::Run(content::StoragePartition* storage_partition, 82 void SessionDataDeleter::Run(
79 ProfileIOData* profile_io_data) { 83 content::StoragePartition* storage_partition,
84 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) {
80 if (storage_policy_.get() && storage_policy_->HasSessionOnlyOrigins()) { 85 if (storage_policy_.get() && storage_policy_->HasSessionOnlyOrigins()) {
81 storage_partition->GetDOMStorageContext()->GetLocalStorageUsage( 86 storage_partition->GetDOMStorageContext()->GetLocalStorageUsage(
82 base::Bind(&SessionDataDeleter::ClearSessionOnlyLocalStorage, 87 base::Bind(&SessionDataDeleter::ClearSessionOnlyLocalStorage,
83 this, 88 this,
84 storage_partition)); 89 storage_partition));
85 } 90 }
86 content::BrowserThread::PostTask( 91 content::BrowserThread::PostTask(
87 content::BrowserThread::IO, 92 content::BrowserThread::IO, FROM_HERE,
88 FROM_HERE, 93 base::Bind(&SessionDataDeleter::DeleteSessionCookiesOnIOThread, this,
89 base::Bind(&SessionDataDeleter::DeleteSessionCookiesOnIOThread, 94 url_request_context_getter));
sky 2016/03/03 00:58:56 Is URLRequestContextGetter thread safe?
mmenke 2016/03/03 01:14:40 It's refcounted.
90 this,
91 profile_io_data));
92 } 95 }
93 96
94 SessionDataDeleter::~SessionDataDeleter() {} 97 SessionDataDeleter::~SessionDataDeleter() {}
95 98
96 void SessionDataDeleter::ClearSessionOnlyLocalStorage( 99 void SessionDataDeleter::ClearSessionOnlyLocalStorage(
97 content::StoragePartition* storage_partition, 100 content::StoragePartition* storage_partition,
98 const std::vector<content::LocalStorageUsageInfo>& usages) { 101 const std::vector<content::LocalStorageUsageInfo>& usages) {
99 DCHECK(storage_policy_.get()); 102 DCHECK(storage_policy_.get());
100 DCHECK(storage_policy_->HasSessionOnlyOrigins()); 103 DCHECK(storage_policy_->HasSessionOnlyOrigins());
101 for (size_t i = 0; i < usages.size(); ++i) { 104 for (size_t i = 0; i < usages.size(); ++i) {
102 const content::LocalStorageUsageInfo& usage = usages[i]; 105 const content::LocalStorageUsageInfo& usage = usages[i];
103 if (!storage_policy_->IsStorageSessionOnly(usage.origin)) 106 if (!storage_policy_->IsStorageSessionOnly(usage.origin))
104 continue; 107 continue;
105 storage_partition->GetDOMStorageContext()->DeleteLocalStorage(usage.origin); 108 storage_partition->GetDOMStorageContext()->DeleteLocalStorage(usage.origin);
106 } 109 }
107 } 110 }
108 111
109 void SessionDataDeleter::DeleteSessionCookiesOnIOThread( 112 void SessionDataDeleter::DeleteSessionCookiesOnIOThread(
110 ProfileIOData* profile_io_data) { 113 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) {
111 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 114 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
112 net::URLRequestContext* request_context = 115 net::URLRequestContext* request_context =
113 profile_io_data->GetMainRequestContext(); 116 url_request_context_getter->GetURLRequestContext();
114 cookie_store_ = request_context->cookie_store(); 117 // Shouldn't happen, but best not to depend on the URLRequestContext's
sky 2016/03/03 00:58:56 I don't understand this comment. Either it can ret
mmenke 2016/03/03 01:14:40 It can, but only if ProfileIOData has been destroy
118 // lifetime
119 if (!request_context)
120 return;
121 net::CookieStore* cookie_store = request_context->cookie_store();
122 // If these callbacks are invoked, |cookie_store| is guaranteed to still
123 // exist, since deleting the CookieStore will cancel pending callbacks.
115 if (delete_only_by_session_only_policy_) { 124 if (delete_only_by_session_only_policy_) {
116 cookie_store_->GetAllCookiesAsync( 125 cookie_store->GetAllCookiesAsync(
117 base::Bind(&SessionDataDeleter::DeleteSessionOnlyOriginCookies, this)); 126 base::Bind(&SessionDataDeleter::DeleteSessionOnlyOriginCookies, this,
127 base::Unretained(cookie_store)));
118 } else { 128 } else {
119 cookie_store_->DeleteSessionCookiesAsync( 129 cookie_store->DeleteSessionCookiesAsync(
120 base::Bind(&SessionDataDeleter::DeleteSessionCookiesDone, this)); 130 base::Bind(&SessionDataDeleter::DeleteSessionCookiesDone, this,
131 base::Unretained(cookie_store)));
121 } 132 }
122 } 133 }
123 134
124 void SessionDataDeleter::DeleteSessionCookiesDone(int num_deleted) { 135 void SessionDataDeleter::DeleteSessionCookiesDone(
125 cookie_store_->GetAllCookiesAsync( 136 net::CookieStore* cookie_store,
126 base::Bind(&SessionDataDeleter::DeleteSessionOnlyOriginCookies, this)); 137 int num_deleted) {
138 // If these callbacks are invoked, |cookie_store| is gauranteed to still
139 // exist, since deleting the CookieStore will cancel pending callbacks.
140 cookie_store->GetAllCookiesAsync(
141 base::Bind(&SessionDataDeleter::DeleteSessionOnlyOriginCookies, this,
142 base::Unretained(cookie_store)));
127 } 143 }
128 144
129 void SessionDataDeleter::DeleteSessionOnlyOriginCookies( 145 void SessionDataDeleter::DeleteSessionOnlyOriginCookies(
146 net::CookieStore* cookie_store,
130 const net::CookieList& cookies) { 147 const net::CookieList& cookies) {
131 if (!storage_policy_.get() || !storage_policy_->HasSessionOnlyOrigins()) 148 if (!storage_policy_.get() || !storage_policy_->HasSessionOnlyOrigins())
132 return; 149 return;
133 150
134 for (net::CookieList::const_iterator it = cookies.begin(); 151 for (const auto& cookie : cookies) {
135 it != cookies.end();
136 ++it) {
137 GURL url = 152 GURL url =
138 net::cookie_util::CookieOriginToURL(it->Domain(), it->IsSecure()); 153 net::cookie_util::CookieOriginToURL(cookie.Domain(), cookie.IsSecure());
139 if (!storage_policy_->IsStorageSessionOnly(url)) 154 if (!storage_policy_->IsStorageSessionOnly(url))
140 continue; 155 continue;
141 cookie_store_->DeleteCanonicalCookieAsync(*it, base::Bind(CookieDeleted)); 156 cookie_store->DeleteCanonicalCookieAsync(cookie, base::Bind(CookieDeleted));
142 } 157 }
143 } 158 }
144 159
145 } // namespace 160 } // namespace
146 161
147 void DeleteSessionOnlyData(Profile* profile) { 162 void DeleteSessionOnlyData(Profile* profile) {
148 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 163 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
149 if (browser_shutdown::IsTryingToQuit()) 164 if (browser_shutdown::IsTryingToQuit())
150 return; 165 return;
151 166
152 // TODO: Remove Athena special casing once the AthenaSessionRestore is in 167 // TODO: Remove Athena special casing once the AthenaSessionRestore is in
153 // place. 168 // place.
154 #if defined(OS_ANDROID) 169 #if defined(OS_ANDROID)
155 SessionStartupPref::Type startup_pref_type = 170 SessionStartupPref::Type startup_pref_type =
156 SessionStartupPref::GetDefaultStartupType(); 171 SessionStartupPref::GetDefaultStartupType();
157 #else 172 #else
158 SessionStartupPref::Type startup_pref_type = 173 SessionStartupPref::Type startup_pref_type =
159 StartupBrowserCreator::GetSessionStartupPref( 174 StartupBrowserCreator::GetSessionStartupPref(
160 *base::CommandLine::ForCurrentProcess(), profile).type; 175 *base::CommandLine::ForCurrentProcess(), profile).type;
161 #endif 176 #endif
162 177
163 scoped_refptr<SessionDataDeleter> deleter( 178 scoped_refptr<SessionDataDeleter> deleter(
164 new SessionDataDeleter(profile->GetSpecialStoragePolicy(), 179 new SessionDataDeleter(profile->GetSpecialStoragePolicy(),
165 startup_pref_type == SessionStartupPref::LAST)); 180 startup_pref_type == SessionStartupPref::LAST));
166 deleter->Run( 181 deleter->Run(Profile::GetDefaultStoragePartition(profile),
167 Profile::GetDefaultStoragePartition(profile), 182 profile->GetRequestContext());
168 ProfileIOData::FromResourceContext(profile->GetResourceContext()));
169 } 183 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698