-
Notifications
You must be signed in to change notification settings - Fork 903
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clear browsing data on exit when instructed by the user.
Fixes brave/brave-browser#492 1. Adds a new "On exit" tab to chrome://settings/clearBrowserData dialog with the same options as on the "Advanced" tab and a "Save" button. By default all "On exit" options are turned off. When an option is modified from the current state the Save button becomes enabled and allows the user to save the "on exit" options. The options are saved to profile's preferences: new *OnExit preferences have been added for each option. 2. Clear browsing data on exit functionality is triggered from Browser destructor. The code checks if user selected any on exit options and if the browser object being destroyed is the last one for the current profile. If these conditions apply then a BrowsingDataRemover is called. 3. Adds BraveClearDataOnExitTest and BraveClearDataOnExitTwoBrowsersTest browser tests that verify that: - the removal of the browsing data is triggered only when an "on exit" preference is set; - the removal flags are constrcuted correctly; - the removal completes on exit; - the removal is called only when the last active browser session for the profile is being closed (tests with OTR/Guest/multiple profiles being present).
- Loading branch information
Showing
15 changed files
with
885 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
source_set("browsing_data") { | ||
sources = [ | ||
"brave_clear_browsing_data.cc", | ||
"brave_clear_browsing_data.h", | ||
] | ||
|
||
deps = [ | ||
"//chrome/browser", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "brave/browser/browsing_data/brave_clear_browsing_data.h" | ||
#include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h" | ||
#include "chrome/browser/profiles/profile.h" | ||
#include "chrome/browser/ui/browser.h" | ||
#include "chrome/browser/ui/browser_list.h" | ||
#include "chrome/common/pref_names.h" | ||
#include "components/prefs/pref_service.h" | ||
#include "components/browsing_data/core/pref_names.h" | ||
|
||
namespace { | ||
|
||
// Gets the count of sessions with the given |profile|. Off-the-record sessions | ||
// aren't counted as they clean up after themselves. | ||
bool IsLastActiveSessionForProfile(Profile* profile) { | ||
BrowserList* list = BrowserList::GetInstance(); | ||
int count = | ||
std::count_if(list->begin(), list->end(), [profile](Browser* browser) { | ||
return !browser->profile()->IsOffTheRecord() && | ||
browser->profile()->IsSameProfile(profile); | ||
}); | ||
return (count == 0); | ||
} | ||
|
||
bool GetClearBrowsingDataOnExitSettings(const Profile* profile, | ||
int& remove_mask, | ||
int& origin_mask) { | ||
const PrefService* prefs = profile->GetPrefs(); | ||
remove_mask = 0; | ||
origin_mask = 0; | ||
|
||
int site_data_mask = ChromeBrowsingDataRemoverDelegate::DATA_TYPE_SITE_DATA; | ||
// Don't try to clear LSO data if it's not supported. | ||
if (!prefs->GetBoolean(prefs::kClearPluginLSODataEnabled)) | ||
site_data_mask &= ~ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA; | ||
|
||
if (prefs->GetBoolean(browsing_data::prefs::kDeleteBrowsingHistoryOnExit) && | ||
prefs->GetBoolean(prefs::kAllowDeletingBrowserHistory)) | ||
remove_mask |= ChromeBrowsingDataRemoverDelegate::DATA_TYPE_HISTORY; | ||
|
||
if (prefs->GetBoolean(browsing_data::prefs::kDeleteDownloadHistoryOnExit) && | ||
prefs->GetBoolean(prefs::kAllowDeletingBrowserHistory)) | ||
remove_mask |= content::BrowsingDataRemover::DATA_TYPE_DOWNLOADS; | ||
|
||
if (prefs->GetBoolean(browsing_data::prefs::kDeleteCacheOnExit)) | ||
remove_mask |= content::BrowsingDataRemover::DATA_TYPE_CACHE; | ||
|
||
if (prefs->GetBoolean(browsing_data::prefs::kDeleteCookiesOnExit)) { | ||
remove_mask |= site_data_mask; | ||
origin_mask |= content::BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB; | ||
} | ||
|
||
if (prefs->GetBoolean(browsing_data::prefs::kDeletePasswordsOnExit)) | ||
remove_mask |= ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PASSWORDS; | ||
|
||
if (prefs->GetBoolean(browsing_data::prefs::kDeleteFormDataOnExit)) | ||
remove_mask |= ChromeBrowsingDataRemoverDelegate::DATA_TYPE_FORM_DATA; | ||
|
||
if (prefs->GetBoolean(browsing_data::prefs::kDeleteHostedAppsDataOnExit)) { | ||
remove_mask |= site_data_mask; | ||
origin_mask |= content::BrowsingDataRemover::ORIGIN_TYPE_PROTECTED_WEB; | ||
} | ||
|
||
if (prefs->GetBoolean(browsing_data::prefs::kDeleteMediaLicensesOnExit)) | ||
remove_mask |= content::BrowsingDataRemover::DATA_TYPE_MEDIA_LICENSES; | ||
|
||
if (prefs->GetBoolean(browsing_data::prefs::kDeleteSiteSettingsOnExit)) | ||
remove_mask |= | ||
ChromeBrowsingDataRemoverDelegate::DATA_TYPE_CONTENT_SETTINGS; | ||
|
||
return (remove_mask != 0); | ||
} | ||
|
||
} // namespace | ||
|
||
namespace content { | ||
|
||
BraveClearBrowsingData::OnExitTestingCallback* | ||
BraveClearBrowsingData::on_exit_testing_callback_ = nullptr; | ||
|
||
//static | ||
void BraveClearBrowsingData::ClearOnExit(Profile* profile) { | ||
DCHECK(profile); | ||
|
||
// Off-the-record profiles clean up after themselves. | ||
if (profile->IsOffTheRecord()) | ||
return; | ||
|
||
// Check if any settings to clear data on exit have been turned on. | ||
int remove_mask = 0; | ||
int origin_mask = 0; | ||
if (!GetClearBrowsingDataOnExitSettings(profile, remove_mask, origin_mask)) | ||
return; | ||
|
||
// Check if this is the last browser for this profile. | ||
if (!IsLastActiveSessionForProfile(profile)) | ||
return; | ||
|
||
// Get data remover for this profile. | ||
content::BrowsingDataRemover* remover = | ||
content::BrowserContext::GetBrowsingDataRemover(profile); | ||
|
||
// If testing, let the test decide if it will do the actual removal and wait. | ||
if (!on_exit_testing_callback_ || | ||
!on_exit_testing_callback_->BeforeClearOnExitRemoveData( | ||
remover, remove_mask, origin_mask)) | ||
remover->Remove(base::Time(), base::Time::Max(), remove_mask, origin_mask); | ||
} | ||
|
||
//static | ||
void BraveClearBrowsingData::SetOnExitTestingCallback( | ||
OnExitTestingCallback* callback) { | ||
on_exit_testing_callback_ = callback; | ||
} | ||
|
||
} // namespace content | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVE_BROWSER_BROWSING_DATA_BRAVE_CLEAR_BROWSING_DATA_H_ | ||
#define BRAVE_BROWSER_BROWSING_DATA_BRAVE_CLEAR_BROWSING_DATA_H_ | ||
|
||
namespace content { | ||
class BrowsingDataRemover; | ||
} | ||
class Profile; | ||
class BraveClearDataOnExitTest; | ||
|
||
namespace content { | ||
|
||
class BraveClearBrowsingData { | ||
public: | ||
// Clears browsing data for the given |profile| if there are no more sessions | ||
// active for the |profile|. *OnExit preferences determine what gets cleared. | ||
static void ClearOnExit(Profile* profile); | ||
|
||
protected: | ||
friend class ::BraveClearDataOnExitTest; | ||
|
||
struct OnExitTestingCallback { | ||
// Called from ClearOnExit right before the call to BrowsingDataRemover to | ||
// remove data. Return true to indicate that the removal should not proceed. | ||
// Used for testing only. | ||
virtual bool BeforeClearOnExitRemoveData( | ||
content::BrowsingDataRemover* remover, | ||
int remove_mask, | ||
int origin_mask) = 0; | ||
}; | ||
|
||
// Used for testing only. | ||
static void SetOnExitTestingCallback(OnExitTestingCallback* callback); | ||
|
||
private: | ||
static OnExitTestingCallback* on_exit_testing_callback_; | ||
}; | ||
|
||
} // namespace content | ||
|
||
#endif //BRAVE_BROWSER_BROWSING_DATA_BRAVE_CLEAR_BROWSING_DATA_H_ |
Oops, something went wrong.