|
5 | 5 |
|
6 | 6 | #include "brave/browser/ephemeral_storage/brave_ephemeral_storage_service_delegate.h" |
7 | 7 |
|
| 8 | +#include <memory> |
8 | 9 | #include <utility> |
| 10 | +#include <vector> |
9 | 11 |
|
10 | 12 | #include "base/check.h" |
| 13 | +#include "base/containers/flat_set.h" |
| 14 | +#include "base/functional/bind.h" |
| 15 | +#include "base/functional/callback_helpers.h" |
11 | 16 | #include "base/logging.h" |
| 17 | +#include "base/memory/raw_ptr.h" |
12 | 18 | #include "base/memory/scoped_refptr.h" |
| 19 | +#include "brave/browser/brave_shields/brave_shields_settings_service_factory.h" |
| 20 | +#include "brave/browser/ephemeral_storage/ephemeral_storage_tab_helper.h" |
| 21 | +#include "brave/components/brave_shields/core/browser/brave_shields_settings_service.h" |
13 | 22 | #include "brave/components/brave_shields/core/browser/brave_shields_utils.h" |
14 | 23 | #include "chrome/browser/browsing_data/chrome_browsing_data_remover_constants.h" |
15 | 24 | #include "chrome/browser/profiles/profile.h" |
| 25 | +#include "chrome/browser/ui/browser_finder.h" |
| 26 | +#include "chrome/browser/ui/browser_window/public/browser_window_interface_iterator.h" |
16 | 27 | #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 28 | +#include "components/tabs/public/tab_interface.h" |
17 | 29 | #include "content/public/browser/browser_context.h" |
18 | 30 | #include "content/public/browser/browsing_data_filter_builder.h" |
19 | 31 | #include "content/public/browser/browsing_data_remover.h" |
20 | | -#include "content/public/browser/dom_storage_context.h" |
| 32 | +#include "content/public/browser/render_process_host.h" |
| 33 | +#include "content/public/browser/web_contents.h" |
21 | 34 | #include "net/base/features.h" |
22 | 35 | #include "net/base/schemeful_site.h" |
| 36 | +#include "net/base/url_util.h" |
23 | 37 | #include "url/origin.h" |
24 | 38 |
|
25 | 39 | #if !BUILDFLAG(IS_ANDROID) |
| 40 | +#include "brave/browser/ui/brave_browser.h" |
26 | 41 | #include "chrome/browser/ui/browser.h" |
27 | 42 | #include "chrome/browser/ui/browser_list.h" |
| 43 | +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 44 | +#else |
| 45 | +#include "chrome/browser/android/tab_android.h" |
| 46 | +#include "chrome/browser/ui/android/tab_model/tab_model.h" |
| 47 | +#include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
| 48 | +#include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
28 | 49 | #endif |
29 | 50 |
|
| 51 | +namespace { |
| 52 | + |
| 53 | +bool PrepareTabForFirstPartyStorageCleanup(tabs::TabHandle tab_handle, |
| 54 | + const std::string& etldplusone) { |
| 55 | + if (tab_handle == tabs::TabHandle::Null()) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + auto* tab = tab_handle.Get(); |
| 59 | + if (!tab) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + content::WebContents* contents = tab->GetContents(); |
| 64 | + if (!contents) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + const auto tab_tld = |
| 69 | + net::URLToEphemeralStorageDomain(contents->GetLastCommittedURL()); |
| 70 | + if (tab_tld.empty() || tab_tld != etldplusone) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + if (auto* ephemeral_storage_tab_helper = |
| 74 | + ephemeral_storage::EphemeralStorageTabHelper::FromWebContents( |
| 75 | + contents)) { |
| 76 | + ephemeral_storage_tab_helper->EnforceFirstPartyStorageCleanup(); |
| 77 | + return true; |
| 78 | + } |
| 79 | + return false; |
| 80 | +} |
| 81 | + |
| 82 | +} // namespace |
| 83 | + |
30 | 84 | namespace ephemeral_storage { |
31 | 85 |
|
32 | 86 | BraveEphemeralStorageServiceDelegate::BraveEphemeralStorageServiceDelegate( |
33 | 87 | content::BrowserContext* context, |
34 | 88 | HostContentSettingsMap* host_content_settings_map, |
35 | | - scoped_refptr<content_settings::CookieSettings> cookie_settings) |
| 89 | + scoped_refptr<content_settings::CookieSettings> cookie_settings, |
| 90 | + brave_shields::BraveShieldsSettingsService* shields_settings_service) |
36 | 91 | : context_(context), |
37 | 92 | host_content_settings_map_(host_content_settings_map), |
38 | | - cookie_settings_(std::move(cookie_settings)) { |
| 93 | + cookie_settings_(std::move(cookie_settings)), |
| 94 | + shields_settings_service_(shields_settings_service) { |
39 | 95 | DCHECK(context_); |
40 | 96 | DCHECK(host_content_settings_map_); |
41 | 97 | DCHECK(cookie_settings_); |
| 98 | + CHECK(shields_settings_service_); |
42 | 99 | } |
43 | 100 |
|
44 | 101 | BraveEphemeralStorageServiceDelegate::~BraveEphemeralStorageServiceDelegate() { |
@@ -145,4 +202,70 @@ void BraveEphemeralStorageServiceDelegate::OnBrowserAdded(Browser* browser) { |
145 | 202 | } |
146 | 203 | #endif |
147 | 204 |
|
| 205 | +void BraveEphemeralStorageServiceDelegate:: |
| 206 | + PrepareTabsForFirstPartyStorageCleanup( |
| 207 | + const std::string& ephemeral_domain) { |
| 208 | + auto* profile = Profile::FromBrowserContext(context_); |
| 209 | + CHECK(profile); |
| 210 | + |
| 211 | +#if !BUILDFLAG(IS_ANDROID) |
| 212 | + for (auto* browser : GetAllBrowserWindowInterfaces()) { |
| 213 | + if (profile != browser->GetProfile()) { |
| 214 | + continue; |
| 215 | + } |
| 216 | + auto* tab_strip = browser->GetTabStripModel(); |
| 217 | + if (!tab_strip) { |
| 218 | + continue; |
| 219 | + } |
| 220 | + |
| 221 | + base::flat_set<tabs::TabHandle> tab_handlers; |
| 222 | + for (auto* tab : *tab_strip) { |
| 223 | + if (!tab || !PrepareTabForFirstPartyStorageCleanup(tab->GetHandle(), |
| 224 | + ephemeral_domain)) { |
| 225 | + continue; |
| 226 | + } |
| 227 | + tab_handlers.emplace(tab->GetHandle()); |
| 228 | + } |
| 229 | + static_cast<BraveBrowser*>(browser)->SetTabsToIgnoreBeforeUnloadHandlers( |
| 230 | + tab_handlers); |
| 231 | + |
| 232 | + for (auto& tab_handle : tab_handlers) { |
| 233 | + if (tab_handle == tabs::TabHandle::Null() || !tab_handle.Get()) { |
| 234 | + continue; |
| 235 | + } |
| 236 | + |
| 237 | + // initiate the closing of the tab |
| 238 | + tab_handle.Get()->Close(); |
| 239 | + } |
| 240 | + } |
| 241 | +#else |
| 242 | + for (TabModel* model : TabModelList::models()) { |
| 243 | + const size_t tab_count = model->GetTabCount(); |
| 244 | + std::vector<tabs::TabHandle> tabs_to_close; |
| 245 | + for (size_t index = 0; index < tab_count; index++) { |
| 246 | + auto* tab = model->GetTabAt(index); |
| 247 | + // Do not process tabs from other profiles. |
| 248 | + if (!tab || profile != tab->profile()) { |
| 249 | + continue; |
| 250 | + } |
| 251 | + |
| 252 | + if (!PrepareTabForFirstPartyStorageCleanup(tab->GetHandle(), |
| 253 | + ephemeral_domain)) { |
| 254 | + continue; |
| 255 | + } |
| 256 | + tabs_to_close.emplace_back(tab->GetHandle()); |
| 257 | + } |
| 258 | + for (auto& tab_handle : tabs_to_close) { |
| 259 | + tab_handle.Get()->Close(); |
| 260 | + } |
| 261 | + } |
| 262 | +#endif |
| 263 | +} |
| 264 | + |
| 265 | +bool BraveEphemeralStorageServiceDelegate:: |
| 266 | + IsShieldsDisabledOnAnyHostMatchingDomainOf(const GURL& url) const { |
| 267 | + return shields_settings_service_->IsShieldsDisabledOnAnyHostMatchingDomainOf( |
| 268 | + url); |
| 269 | +} |
| 270 | + |
148 | 271 | } // namespace ephemeral_storage |
0 commit comments