Skip to content

Commit

Permalink
Clear browsing data on exit when instructed by the user.
Browse files Browse the repository at this point in the history
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
mkarolin committed Jan 7, 2019
1 parent 0017c41 commit edd9a56
Show file tree
Hide file tree
Showing 15 changed files with 885 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
<message name="IDS_SETTINGS_MANAGE_EXTENSIONS_LABEL" desc="The label of manage extensions link in settings">
Manage Extensions
</message>
<!-- Brave Clear Browsing Data On Exit Settings -->
<message name="IDS_SETTINGS_BRAVE_ON_EXIT" desc="Clear Browsing Data dialog On exit tab label">
On exit
</message>
</messages>
<includes>
<include name="IDR_BRAVE_TAG_SERVICES_POLYFILL" file="resources/js/tag_services_polyfill.js" type="BINDATA" />
Expand Down
1 change: 1 addition & 0 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ source_set("browser") {
"//content/public/browser",
"//content/public/common",
"//services/service_manager/embedder",
"browsing_data",
"download",
"extensions",
"importer",
Expand Down
10 changes: 10 additions & 0 deletions browser/browsing_data/BUILD.gn
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",
]
}
120 changes: 120 additions & 0 deletions browser/browsing_data/brave_clear_browsing_data.cc
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

44 changes: 44 additions & 0 deletions browser/browsing_data/brave_clear_browsing_data.h
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_
Loading

0 comments on commit edd9a56

Please sign in to comment.