wip-remove-site-data-after-page-exit.patch 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. From: uazo <uazo@users.noreply.github.com>
  2. Date: Wed, 6 Jul 2022 10:38:09 +0000
  3. Subject: [wip] remove site data after page exit
  4. ---
  5. .../permissions/last_tab_standing_tracker.cc | 48 ++++++++++++++++++-
  6. .../permissions/last_tab_standing_tracker.h | 3 +-
  7. .../last_tab_standing_tracker_tab_helper.cc | 4 +-
  8. net/http/http_cache.cc | 21 +++++++-
  9. net/http/http_cache.h | 2 +-
  10. .../conditional_cache_deletion_helper.cc | 6 ++-
  11. 6 files changed, 77 insertions(+), 7 deletions(-)
  12. diff --git a/chrome/browser/permissions/last_tab_standing_tracker.cc b/chrome/browser/permissions/last_tab_standing_tracker.cc
  13. --- a/chrome/browser/permissions/last_tab_standing_tracker.cc
  14. +++ b/chrome/browser/permissions/last_tab_standing_tracker.cc
  15. @@ -10,6 +10,11 @@
  16. #include "components/content_settings/core/browser/host_content_settings_map.h"
  17. #include "components/content_settings/core/common/content_settings_utils.h"
  18. #include "components/permissions/permissions_client.h"
  19. +#include "content/public/browser/storage_partition.h"
  20. +#include "services/network/network_context.h"
  21. +#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
  22. +#include "content/public/browser/clear_site_data_utils.h"
  23. +#include "content/public/browser/browsing_data_filter_builder.h"
  24. namespace {
  25. // Remove all sessions content setting by origin and type
  26. @@ -32,6 +37,16 @@ namespace {
  27. }
  28. }
  29. }
  30. +
  31. + bool DoesOriginMatchPredicate(
  32. + base::OnceCallback<bool(const url::Origin&)> predicate,
  33. + const url::Origin& origin,
  34. + storage::SpecialStoragePolicy* policy) {
  35. + if (!std::move(predicate).Run(origin))
  36. + return false;
  37. +
  38. + return true;
  39. + }
  40. }
  41. LastTabStandingTracker::LastTabStandingTracker(content::BrowserContext* context)
  42. @@ -69,7 +84,7 @@ void LastTabStandingTracker::WebContentsLoadedOrigin(
  43. }
  44. void LastTabStandingTracker::WebContentsUnloadedOrigin(
  45. - const url::Origin& origin) {
  46. + const url::Origin& origin, content::WebContents* web_contents) {
  47. if (origin.opaque())
  48. return;
  49. if (origin == url::Origin::Create(GURL("chrome://newtab/")) ||
  50. @@ -86,5 +101,36 @@ void LastTabStandingTracker::WebContentsUnloadedOrigin(
  51. RemoveSessionSettings(content_settings, origin, ContentSettingsType::GEOLOCATION);
  52. RemoveSessionSettings(content_settings, origin, ContentSettingsType::MEDIASTREAM_MIC);
  53. RemoveSessionSettings(content_settings, origin, ContentSettingsType::MEDIASTREAM_CAMERA);
  54. +
  55. + content::SiteInstance* site_instance = web_contents->GetSiteInstance();
  56. + content::StoragePartition* storage_partition =
  57. + context_->GetStoragePartition(site_instance, /*can_create*/false);
  58. + DCHECK(storage_partition);
  59. +
  60. + network::mojom::ClearDataFilterPtr filter = network::mojom::ClearDataFilter::New();
  61. + filter->type = network::mojom::ClearDataFilter_Type::DELETE_MATCHES;
  62. + filter->domains.push_back(net::registry_controlled_domains::GetDomainAndRegistry(
  63. + origin,
  64. + net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
  65. + storage_partition->GetNetworkContext()->ClearHttpCache(
  66. + base::Time(), base::Time::Now(),
  67. + std::move(filter),
  68. + base::BindOnce([](){}));
  69. +
  70. + auto filter_builder = content::BrowsingDataFilterBuilder::Create(
  71. + content::BrowsingDataFilterBuilder::Mode::kDelete);
  72. + filter_builder->AddOrigin(origin);
  73. + auto cookie_delete_filter = network::mojom::CookieDeletionFilter::New();
  74. + cookie_delete_filter->session_control =
  75. + network::mojom::CookieDeletionSessionControl::IGNORE_CONTROL;
  76. + content::StoragePartition::OriginMatcherFunction matcher_function =
  77. + base::BindRepeating(&DoesOriginMatchPredicate,
  78. + filter_builder->BuildOriginFilter());
  79. + storage_partition->ClearData(
  80. + content::StoragePartition::REMOVE_DATA_MASK_ALL, 0,
  81. + matcher_function,
  82. + std::move(cookie_delete_filter), /*perform_cleanup*/true,
  83. + /*remove_since*/base::Time(), base::Time::Max(),
  84. + base::DoNothing());
  85. }
  86. }
  87. diff --git a/chrome/browser/permissions/last_tab_standing_tracker.h b/chrome/browser/permissions/last_tab_standing_tracker.h
  88. --- a/chrome/browser/permissions/last_tab_standing_tracker.h
  89. +++ b/chrome/browser/permissions/last_tab_standing_tracker.h
  90. @@ -12,6 +12,7 @@
  91. #include "chrome/browser/profiles/profile.h"
  92. #include "components/keyed_service/core/keyed_service.h"
  93. #include "url/origin.h"
  94. +#include "content/public/browser/web_contents.h"
  95. // This class keep tracks of all open tabs. And notifies its observers when
  96. // all tabs of a particular origin have been closed or navigated away from.
  97. @@ -24,7 +25,7 @@ class LastTabStandingTracker : public KeyedService {
  98. LastTabStandingTracker& operator=(const LastTabStandingTracker&) = delete;
  99. void WebContentsLoadedOrigin(const url::Origin& origin);
  100. - void WebContentsUnloadedOrigin(const url::Origin& origin);
  101. + void WebContentsUnloadedOrigin(const url::Origin& origin, content::WebContents* web_contents);
  102. void AddObserver(LastTabStandingTrackerObserver* observer);
  103. void RemoveObserver(LastTabStandingTrackerObserver* observer);
  104. diff --git a/chrome/browser/permissions/last_tab_standing_tracker_tab_helper.cc b/chrome/browser/permissions/last_tab_standing_tracker_tab_helper.cc
  105. --- a/chrome/browser/permissions/last_tab_standing_tracker_tab_helper.cc
  106. +++ b/chrome/browser/permissions/last_tab_standing_tracker_tab_helper.cc
  107. @@ -14,7 +14,7 @@ void LastTabStandingTrackerTabHelper::WebContentsDestroyed() {
  108. if (last_committed_origin_) {
  109. LastTabStandingTrackerFactory::GetForBrowserContext(
  110. web_contents()->GetBrowserContext())
  111. - ->WebContentsUnloadedOrigin(*last_committed_origin_);
  112. + ->WebContentsUnloadedOrigin(*last_committed_origin_, web_contents());
  113. }
  114. }
  115. @@ -27,7 +27,7 @@ void LastTabStandingTrackerTabHelper::PrimaryPageChanged(content::Page& page) {
  116. web_contents()->GetBrowserContext());
  117. if (last_committed_origin_) {
  118. last_tab_standing_tracker->WebContentsUnloadedOrigin(
  119. - *last_committed_origin_);
  120. + *last_committed_origin_, web_contents());
  121. }
  122. last_tab_standing_tracker->WebContentsLoadedOrigin(new_origin);
  123. last_committed_origin_ = std::move(new_origin);
  124. diff --git a/net/http/http_cache.cc b/net/http/http_cache.cc
  125. --- a/net/http/http_cache.cc
  126. +++ b/net/http/http_cache.cc
  127. @@ -413,7 +413,7 @@ HttpCache::SetHttpNetworkTransactionFactoryForTesting(
  128. }
  129. // static
  130. -std::string HttpCache::GetResourceURLFromHttpCacheKey(const std::string& key) {
  131. +std::string HttpCache::GetResourceURLFromHttpCacheKey(const std::string& key, bool top_frame) {
  132. // The key format is:
  133. // credential_key/post_key/[isolation_key]url
  134. @@ -437,11 +437,30 @@ std::string HttpCache::GetResourceURLFromHttpCacheKey(const std::string& key) {
  135. // HttpUtil::SpecForRequest method, which has a DCHECK to ensure that
  136. // the original resource url is valid, and hence will not contain the
  137. // unescaped whitespace of |kDoubleKeySeparator|.
  138. +
  139. + if (top_frame) {
  140. + // Get TopFrame url from (double) isolation key
  141. + // example:
  142. + // 1/0/_dk_https://google.com https://google.com https://www.google.com/async/...
  143. + // 1/0/_dk_s_https://google.com https://google.com https://www.google.com/async/...
  144. + std::string::size_type top_frame_pos = pos + strlen(kDoubleKeyPrefix);
  145. + auto subframeDocumentResourcePrefixLength = strlen(kSubframeDocumentResourcePrefix);
  146. + if (key.substr(top_frame_pos, subframeDocumentResourcePrefixLength) == kSubframeDocumentResourcePrefix) {
  147. + // skip kSubframeDocumentResourcePrefix if present
  148. + top_frame_pos += subframeDocumentResourcePrefixLength;
  149. + }
  150. + // find kDoubleKeySeparator ('space') from top_frame_pos
  151. + pos = key.find(kDoubleKeySeparator, top_frame_pos);
  152. + DCHECK_NE(pos, std::string::npos);
  153. + return key.substr(top_frame_pos, pos - top_frame_pos);
  154. + }
  155. +
  156. pos = key.rfind(kDoubleKeySeparator);
  157. DCHECK_NE(pos, std::string::npos);
  158. pos += strlen(kDoubleKeySeparator);
  159. DCHECK_LE(pos, key.size() - 1);
  160. } else if (pos == key.find(kSingleKeyPrefix, pos)) {
  161. + if (top_frame) NOTREACHED();
  162. pos = key.rfind(kSingleKeySeparator);
  163. DCHECK_NE(pos, std::string::npos);
  164. pos += strlen(kSingleKeySeparator);
  165. diff --git a/net/http/http_cache.h b/net/http/http_cache.h
  166. --- a/net/http/http_cache.h
  167. +++ b/net/http/http_cache.h
  168. @@ -257,7 +257,7 @@ class NET_EXPORT HttpCache : public HttpTransactionFactory {
  169. std::unique_ptr<HttpTransactionFactory> new_network_layer);
  170. // Get the URL from the entry's cache key.
  171. - static std::string GetResourceURLFromHttpCacheKey(const std::string& key);
  172. + static std::string GetResourceURLFromHttpCacheKey(const std::string& key, bool top_frame = false);
  173. // Function to generate cache key for testing.
  174. static std::string GenerateCacheKeyForTest(const HttpRequestInfo* request);
  175. diff --git a/services/network/conditional_cache_deletion_helper.cc b/services/network/conditional_cache_deletion_helper.cc
  176. --- a/services/network/conditional_cache_deletion_helper.cc
  177. +++ b/services/network/conditional_cache_deletion_helper.cc
  178. @@ -22,8 +22,12 @@ bool EntryPredicateFromURLsAndTime(
  179. std::string entry_key(entry->GetKey());
  180. std::string url_string(
  181. net::HttpCache::GetResourceURLFromHttpCacheKey(entry_key));
  182. + std::string top_frame_url(
  183. + net::HttpCache::GetResourceURLFromHttpCacheKey(entry_key, true));
  184. return (entry->GetLastUsed() >= begin_time &&
  185. - entry->GetLastUsed() < end_time && url_matcher.Run(GURL(url_string)));
  186. + entry->GetLastUsed() < end_time) && (
  187. + url_matcher.Run(GURL(url_string)) ||
  188. + url_matcher.Run(GURL(top_frame_url)));
  189. }
  190. } // namespace
  191. --
  192. 2.25.1