Commit f4339316 authored by ioanap's avatar ioanap Committed by Commit bot

Add support for time based deletion of browsing data on iOS

Change existing browsing data deletion methods to take a browsing_data::TimePeriod as a parameter.

Keep old methods around until all call sites are changed downstream.

BUG=642334

Review-Url: https://codereview.chromium.org/2270063005
Cr-Commit-Position: refs/heads/master@{#419433}
parent b22ae2a3
...@@ -78,7 +78,8 @@ class AccountConsistencyService : public KeyedService, ...@@ -78,7 +78,8 @@ class AccountConsistencyService : public KeyedService,
// Does nothing if the cookie is not set on |domain|. // Does nothing if the cookie is not set on |domain|.
void RemoveChromeConnectedCookieFromDomain(const std::string& domain); void RemoveChromeConnectedCookieFromDomain(const std::string& domain);
// Notifies the AccountConsistencyService that browsing data has been removed. // Notifies the AccountConsistencyService that browsing data has been removed
// for any time period.
void OnBrowsingDataRemoved(); void OnBrowsingDataRemoved();
private: private:
......
...@@ -17,8 +17,9 @@ BrowsingDataRemoverHelper::~BrowsingDataRemoverHelper() { ...@@ -17,8 +17,9 @@ BrowsingDataRemoverHelper::~BrowsingDataRemoverHelper() {
BrowsingDataRemoverHelper::BrowsingDataRemovalInfo::BrowsingDataRemovalInfo( BrowsingDataRemoverHelper::BrowsingDataRemovalInfo::BrowsingDataRemovalInfo(
int remove_mask, int remove_mask,
browsing_data::TimePeriod time_period,
const base::Closure& callback) const base::Closure& callback)
: remove_mask(remove_mask) { : remove_mask(remove_mask), time_period(time_period) {
callbacks.push_back(callback); callbacks.push_back(callback);
} }
...@@ -27,6 +28,7 @@ BrowsingDataRemoverHelper::BrowsingDataRemovalInfo::~BrowsingDataRemovalInfo() { ...@@ -27,6 +28,7 @@ BrowsingDataRemoverHelper::BrowsingDataRemovalInfo::~BrowsingDataRemovalInfo() {
void BrowsingDataRemoverHelper::Remove(ios::ChromeBrowserState* browser_state, void BrowsingDataRemoverHelper::Remove(ios::ChromeBrowserState* browser_state,
int remove_mask, int remove_mask,
browsing_data::TimePeriod time_period,
const base::Closure& callback) { const base::Closure& callback) {
DCHECK(browser_state); DCHECK(browser_state);
DCHECK(!browser_state->IsOffTheRecord()); DCHECK(!browser_state->IsOffTheRecord());
...@@ -43,7 +45,7 @@ void BrowsingDataRemoverHelper::Remove(ios::ChromeBrowserState* browser_state, ...@@ -43,7 +45,7 @@ void BrowsingDataRemoverHelper::Remove(ios::ChromeBrowserState* browser_state,
auto pending_removals_iter = pending_removals_.find(browser_state); auto pending_removals_iter = pending_removals_.find(browser_state);
if (pending_removals_iter == pending_removals_.end()) { if (pending_removals_iter == pending_removals_.end()) {
std::unique_ptr<BrowsingDataRemovalInfo> removal_info( std::unique_ptr<BrowsingDataRemovalInfo> removal_info(
new BrowsingDataRemovalInfo(remove_mask, callback)); new BrowsingDataRemovalInfo(remove_mask, time_period, callback));
pending_removals_[browser_state] = std::move(removal_info); pending_removals_[browser_state] = std::move(removal_info);
} else { } else {
pending_removals_iter->second->remove_mask |= remove_mask; pending_removals_iter->second->remove_mask |= remove_mask;
...@@ -51,11 +53,17 @@ void BrowsingDataRemoverHelper::Remove(ios::ChromeBrowserState* browser_state, ...@@ -51,11 +53,17 @@ void BrowsingDataRemoverHelper::Remove(ios::ChromeBrowserState* browser_state,
} }
} else { } else {
std::unique_ptr<BrowsingDataRemovalInfo> removal_info( std::unique_ptr<BrowsingDataRemovalInfo> removal_info(
new BrowsingDataRemovalInfo(remove_mask, callback)); new BrowsingDataRemovalInfo(remove_mask, time_period, callback));
DoRemove(browser_state, std::move(removal_info)); DoRemove(browser_state, std::move(removal_info));
} }
} }
void BrowsingDataRemoverHelper::Remove(ios::ChromeBrowserState* browser_state,
int remove_mask,
const base::Closure& callback) {
Remove(browser_state, remove_mask, browsing_data::ALL_TIME, callback);
}
void BrowsingDataRemoverHelper::OnIOSChromeBrowsingDataRemoverDone() { void BrowsingDataRemoverHelper::OnIOSChromeBrowsingDataRemoverDone() {
current_remover_ = nullptr; current_remover_ = nullptr;
...@@ -90,7 +98,7 @@ void BrowsingDataRemoverHelper::DoRemove( ...@@ -90,7 +98,7 @@ void BrowsingDataRemoverHelper::DoRemove(
// IOSChromeBrowsingDataRemover deletes itself. // IOSChromeBrowsingDataRemover deletes itself.
IOSChromeBrowsingDataRemover* remover = IOSChromeBrowsingDataRemover* remover =
IOSChromeBrowsingDataRemover::CreateForPeriod( IOSChromeBrowsingDataRemover::CreateForPeriod(
browser_state, IOSChromeBrowsingDataRemover::EVERYTHING); browser_state, current_removal_info_->time_period);
remover->AddObserver(this); remover->AddObserver(this);
current_remover_ = remover; current_remover_ = remover;
int remove_mask = current_removal_info_->remove_mask; int remove_mask = current_removal_info_->remove_mask;
......
...@@ -30,6 +30,13 @@ class BrowsingDataRemoverHelper ...@@ -30,6 +30,13 @@ class BrowsingDataRemoverHelper
// |callback| is called on the main thread. // |callback| is called on the main thread.
// Note: Removal operations are not necessarily processed in the sequence that // Note: Removal operations are not necessarily processed in the sequence that
// they are received in. // they are received in.
void Remove(ios::ChromeBrowserState* browser_state,
int remove_mask,
browsing_data::TimePeriod time_period,
const base::Closure& callback);
// DEPRECATED: Same as above, but setting the |time_period| to ALL_TIME.
// TODO(ioanap): Remove after all call sites are changed.
void Remove(ios::ChromeBrowserState* browser_state, void Remove(ios::ChromeBrowserState* browser_state,
int remove_mask, int remove_mask,
const base::Closure& callback); const base::Closure& callback);
...@@ -39,11 +46,15 @@ class BrowsingDataRemoverHelper ...@@ -39,11 +46,15 @@ class BrowsingDataRemoverHelper
// a ChromeBrowserState. // a ChromeBrowserState.
struct BrowsingDataRemovalInfo { struct BrowsingDataRemovalInfo {
// Creates a BrowsingDataRemovalInfo with a single callback |callback|. // Creates a BrowsingDataRemovalInfo with a single callback |callback|.
BrowsingDataRemovalInfo(int remove_mask, const base::Closure& callback); BrowsingDataRemovalInfo(int remove_mask,
browsing_data::TimePeriod time_period,
const base::Closure& callback);
~BrowsingDataRemovalInfo(); ~BrowsingDataRemovalInfo();
// The mask of all the types of browsing data that needs to be removed. // The mask of all the types of browsing data that needs to be removed.
// Obtained from BrowsingDataRemoved::RemoveDataMask. // Obtained from BrowsingDataRemoved::RemoveDataMask.
int remove_mask; int remove_mask;
// Time period for which the user wants to remove the data.
browsing_data::TimePeriod time_period;
// The vector of callbacks that need to be run when browsing data is // The vector of callbacks that need to be run when browsing data is
// actually removed. // actually removed.
std::vector<base::Closure> callbacks; std::vector<base::Closure> callbacks;
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "base/sequenced_task_runner_helpers.h" #include "base/sequenced_task_runner_helpers.h"
#include "base/task/cancelable_task_tracker.h" #include "base/task/cancelable_task_tracker.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "components/browsing_data/core/browsing_data_utils.h"
#include "components/prefs/pref_member.h" #include "components/prefs/pref_member.h"
#include "components/search_engines/template_url_service.h" #include "components/search_engines/template_url_service.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -33,9 +34,6 @@ class URLRequestContextGetter; ...@@ -33,9 +34,6 @@ class URLRequestContextGetter;
class IOSChromeBrowsingDataRemover { class IOSChromeBrowsingDataRemover {
public: public:
// Time period ranges available when doing browsing data removals.
enum TimePeriod { EVERYTHING };
// Mask used for Remove. // Mask used for Remove.
enum RemoveDataMask { enum RemoveDataMask {
REMOVE_APPCACHE = 1 << 0, REMOVE_APPCACHE = 1 << 0,
...@@ -119,10 +117,7 @@ class IOSChromeBrowsingDataRemover { ...@@ -119,10 +117,7 @@ class IOSChromeBrowsingDataRemover {
// itself once finished. // itself once finished.
static IOSChromeBrowsingDataRemover* CreateForPeriod( static IOSChromeBrowsingDataRemover* CreateForPeriod(
ios::ChromeBrowserState* browser_state, ios::ChromeBrowserState* browser_state,
TimePeriod period); browsing_data::TimePeriod period);
// Calculate the begin time for the deletion range specified by |time_period|.
static base::Time CalculateBeginDeleteTime(TimePeriod time_period);
// Is the IOSChromeBrowsingDataRemover currently in the process of removing // Is the IOSChromeBrowsingDataRemover currently in the process of removing
// data? // data?
......
...@@ -90,15 +90,26 @@ IOSChromeBrowsingDataRemover::NotificationDetails::~NotificationDetails() {} ...@@ -90,15 +90,26 @@ IOSChromeBrowsingDataRemover::NotificationDetails::~NotificationDetails() {}
// Static. // Static.
IOSChromeBrowsingDataRemover* IOSChromeBrowsingDataRemover::CreateForPeriod( IOSChromeBrowsingDataRemover* IOSChromeBrowsingDataRemover::CreateForPeriod(
ios::ChromeBrowserState* browser_state, ios::ChromeBrowserState* browser_state,
TimePeriod period) { browsing_data::TimePeriod period) {
switch (period) { switch (period) {
case EVERYTHING: case browsing_data::LAST_HOUR:
base::RecordAction(UserMetricsAction("ClearBrowsingData_LastHour"));
break;
case browsing_data::LAST_DAY:
base::RecordAction(UserMetricsAction("ClearBrowsingData_LastDay"));
break;
case browsing_data::LAST_WEEK:
base::RecordAction(UserMetricsAction("ClearBrowsingData_LastWeek"));
break;
case browsing_data::FOUR_WEEKS:
base::RecordAction(UserMetricsAction("ClearBrowsingData_LastMonth"));
break;
case browsing_data::ALL_TIME:
base::RecordAction(UserMetricsAction("ClearBrowsingData_Everything")); base::RecordAction(UserMetricsAction("ClearBrowsingData_Everything"));
break; break;
} }
return new IOSChromeBrowsingDataRemover( return new IOSChromeBrowsingDataRemover(
browser_state, browser_state, browsing_data::CalculateBeginDeleteTime(period),
IOSChromeBrowsingDataRemover::CalculateBeginDeleteTime(period),
base::Time::Max()); base::Time::Max());
} }
...@@ -303,9 +314,9 @@ void IOSChromeBrowsingDataRemover::RemoveImpl(int remove_mask) { ...@@ -303,9 +314,9 @@ void IOSChromeBrowsingDataRemover::RemoveImpl(int remove_mask) {
base::RecordAction(UserMetricsAction("ClearBrowsingData_Cache")); base::RecordAction(UserMetricsAction("ClearBrowsingData_Cache"));
waiting_for_clear_cache_ = true; waiting_for_clear_cache_ = true;
DCHECK(delete_begin_.is_null()) << "Partial clearing not supported";
ClearHttpCache(browser_state_->GetRequestContext(), ClearHttpCache(browser_state_->GetRequestContext(),
WebThread::GetTaskRunnerForThread(WebThread::IO), WebThread::GetTaskRunnerForThread(WebThread::IO),
delete_begin_, delete_end_,
base::Bind(&IOSChromeBrowsingDataRemover::OnClearedCache, base::Bind(&IOSChromeBrowsingDataRemover::OnClearedCache,
base::Unretained(this))); base::Unretained(this)));
} }
...@@ -350,21 +361,6 @@ void IOSChromeBrowsingDataRemover::OnHistoryDeletionDone() { ...@@ -350,21 +361,6 @@ void IOSChromeBrowsingDataRemover::OnHistoryDeletionDone() {
NotifyAndDeleteIfDone(); NotifyAndDeleteIfDone();
} }
base::Time IOSChromeBrowsingDataRemover::CalculateBeginDeleteTime(
TimePeriod time_period) {
base::TimeDelta diff;
base::Time delete_begin_time = base::Time::Now();
switch (time_period) {
case EVERYTHING:
delete_begin_time = base::Time();
break;
default:
NOTREACHED() << L"Missing item";
break;
}
return delete_begin_time - diff;
}
bool IOSChromeBrowsingDataRemover::AllDone() { bool IOSChromeBrowsingDataRemover::AllDone() {
return !waiting_for_clear_autofill_origin_urls_ && return !waiting_for_clear_autofill_origin_urls_ &&
!waiting_for_clear_cache_ && !waiting_for_clear_channel_ids_ && !waiting_for_clear_cache_ && !waiting_for_clear_channel_ids_ &&
......
...@@ -40,7 +40,8 @@ void BrowserStateDataRemover::RemoveBrowserStateData(ProceduralBlock callback) { ...@@ -40,7 +40,8 @@ void BrowserStateDataRemover::RemoveBrowserStateData(ProceduralBlock callback) {
base::scoped_nsobject<ClearBrowsingDataCommand> command( base::scoped_nsobject<ClearBrowsingDataCommand> command(
[[ClearBrowsingDataCommand alloc] [[ClearBrowsingDataCommand alloc]
initWithBrowserState:browser_state_ initWithBrowserState:browser_state_
mask:kRemoveAllDataMask]); mask:kRemoveAllDataMask
timePeriod:browsing_data::ALL_TIME]);
UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow]; UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
DCHECK(mainWindow); DCHECK(mainWindow);
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import "components/browsing_data/core/browsing_data_utils.h"
#import "ios/chrome/browser/ui/commands/generic_chrome_command.h" #import "ios/chrome/browser/ui/commands/generic_chrome_command.h"
namespace ios { namespace ios {
...@@ -20,9 +21,16 @@ class ChromeBrowserState; ...@@ -20,9 +21,16 @@ class ChromeBrowserState;
- (instancetype)initWithTag:(NSInteger)tag NS_UNAVAILABLE; - (instancetype)initWithTag:(NSInteger)tag NS_UNAVAILABLE;
// Initializes a command intented to clear browsing data for |browserState| // Initializes a command intented to clear browsing data for |browserState|
// that correspong to removal mask |mask|. // that corresponds to removal mask |mask| for the time period |timePeriod|.
- (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState
mask:(int)mask NS_DESIGNATED_INITIALIZER; mask:(int)mask
timePeriod:(browsing_data::TimePeriod)timePeriod
NS_DESIGNATED_INITIALIZER;
// DEPRECATED: Same as above, but setting |timePeriod| to ALL_TIME.
// TODO(ioanap): Remove after all call sites are changed.
- (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState
mask:(int)mask;
// When executed this command will remove browsing data for this BrowserState. // When executed this command will remove browsing data for this BrowserState.
@property(nonatomic, readonly) ios::ChromeBrowserState* browserState; @property(nonatomic, readonly) ios::ChromeBrowserState* browserState;
...@@ -30,6 +38,9 @@ class ChromeBrowserState; ...@@ -30,6 +38,9 @@ class ChromeBrowserState;
// Removal mask: see BrowsingDataRemover::RemoveDataMask. // Removal mask: see BrowsingDataRemover::RemoveDataMask.
@property(nonatomic, readonly) int mask; @property(nonatomic, readonly) int mask;
// Time period for which the browsing data will be removed.
@property(nonatomic, readonly) browsing_data::TimePeriod timePeriod;
@end @end
#endif // IOS_CHROME_BROWSER_UI_COMMANDS_CLEAR_BROWSING_DATA_COMMAND_H_ #endif // IOS_CHROME_BROWSER_UI_COMMANDS_CLEAR_BROWSING_DATA_COMMAND_H_
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
@synthesize browserState = _browserState; @synthesize browserState = _browserState;
@synthesize mask = _mask; @synthesize mask = _mask;
@synthesize timePeriod = _timePeriod;
- (instancetype)initWithTag:(NSInteger)tag { - (instancetype)initWithTag:(NSInteger)tag {
NOTREACHED(); NOTREACHED();
...@@ -18,14 +19,23 @@ ...@@ -18,14 +19,23 @@
} }
- (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState
mask:(int)mask { mask:(int)mask
timePeriod:(browsing_data::TimePeriod)timePeriod {
self = [super initWithTag:IDC_CLEAR_BROWSING_DATA_IOS]; self = [super initWithTag:IDC_CLEAR_BROWSING_DATA_IOS];
if (self) { if (self) {
DCHECK(browserState); DCHECK(browserState);
_browserState = browserState; _browserState = browserState;
_mask = mask; _mask = mask;
_timePeriod = timePeriod;
} }
return self; return self;
} }
- (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState
mask:(int)mask {
return [self initWithBrowserState:browserState
mask:mask
timePeriod:browsing_data::ALL_TIME];
}
@end @end
...@@ -506,7 +506,7 @@ std::string CrNetEnvironment::user_agent() { ...@@ -506,7 +506,7 @@ std::string CrNetEnvironment::user_agent() {
void CrNetEnvironment::ClearCache(ClearCacheCallback callback) { void CrNetEnvironment::ClearCache(ClearCacheCallback callback) {
PostToNetworkThread( PostToNetworkThread(
FROM_HERE, FROM_HERE, base::Bind(&net::ClearHttpCache, main_context_getter_,
base::Bind(&net::ClearHttpCache, main_context_getter_, network_io_thread_->task_runner(), base::Time(),
network_io_thread_->task_runner(), base::BindBlock(callback))); base::Time::Max(), base::BindBlock(callback)));
} }
...@@ -33,11 +33,14 @@ void PostCallback(const scoped_refptr<base::TaskRunner>& task_runner, ...@@ -33,11 +33,14 @@ void PostCallback(const scoped_refptr<base::TaskRunner>& task_runner,
// Clears the disk_cache::Backend on the IO thread and deletes |backend|. // Clears the disk_cache::Backend on the IO thread and deletes |backend|.
void DoomHttpCache(std::unique_ptr<disk_cache::Backend*> backend, void DoomHttpCache(std::unique_ptr<disk_cache::Backend*> backend,
const scoped_refptr<base::TaskRunner>& client_task_runner, const scoped_refptr<base::TaskRunner>& client_task_runner,
const base::Time& delete_begin,
const base::Time& delete_end,
const net::CompletionCallback& callback, const net::CompletionCallback& callback,
int error) { int error) {
// |*backend| may be null in case of error. // |*backend| may be null in case of error.
if (*backend) { if (*backend) {
(*backend)->DoomAllEntries( (*backend)->DoomEntriesBetween(
delete_begin, delete_end,
base::Bind(&PostCallback, client_task_runner, callback)); base::Bind(&PostCallback, client_task_runner, callback));
} else { } else {
client_task_runner->PostTask(FROM_HERE, base::Bind(callback, error)); client_task_runner->PostTask(FROM_HERE, base::Bind(callback, error));
...@@ -49,6 +52,8 @@ void DoomHttpCache(std::unique_ptr<disk_cache::Backend*> backend, ...@@ -49,6 +52,8 @@ void DoomHttpCache(std::unique_ptr<disk_cache::Backend*> backend,
void ClearHttpCacheOnIOThread( void ClearHttpCacheOnIOThread(
const scoped_refptr<net::URLRequestContextGetter>& getter, const scoped_refptr<net::URLRequestContextGetter>& getter,
const scoped_refptr<base::TaskRunner>& client_task_runner, const scoped_refptr<base::TaskRunner>& client_task_runner,
const base::Time& delete_begin,
const base::Time& delete_end,
const net::CompletionCallback& callback) { const net::CompletionCallback& callback) {
net::HttpCache* http_cache = net::HttpCache* http_cache =
getter->GetURLRequestContext()->http_transaction_factory()->GetCache(); getter->GetURLRequestContext()->http_transaction_factory()->GetCache();
...@@ -72,7 +77,7 @@ void ClearHttpCacheOnIOThread( ...@@ -72,7 +77,7 @@ void ClearHttpCacheOnIOThread(
disk_cache::Backend** backend_ptr = backend.get(); disk_cache::Backend** backend_ptr = backend.get();
net::CompletionCallback doom_callback = net::CompletionCallback doom_callback =
base::Bind(&DoomHttpCache, base::Passed(std::move(backend)), base::Bind(&DoomHttpCache, base::Passed(std::move(backend)),
client_task_runner, callback); client_task_runner, delete_begin, delete_end, callback);
int rv = http_cache->GetBackend(backend_ptr, doom_callback); int rv = http_cache->GetBackend(backend_ptr, doom_callback);
...@@ -89,10 +94,14 @@ namespace net { ...@@ -89,10 +94,14 @@ namespace net {
void ClearHttpCache(const scoped_refptr<net::URLRequestContextGetter>& getter, void ClearHttpCache(const scoped_refptr<net::URLRequestContextGetter>& getter,
const scoped_refptr<base::TaskRunner>& network_task_runner, const scoped_refptr<base::TaskRunner>& network_task_runner,
const base::Time& delete_begin,
const base::Time& delete_end,
const net::CompletionCallback& callback) { const net::CompletionCallback& callback) {
DCHECK(delete_end != base::Time());
network_task_runner->PostTask( network_task_runner->PostTask(
FROM_HERE, base::Bind(&ClearHttpCacheOnIOThread, getter, FROM_HERE, base::Bind(&ClearHttpCacheOnIOThread, getter,
base::ThreadTaskRunnerHandle::Get(), callback)); base::ThreadTaskRunnerHandle::Get(), delete_begin,
delete_end, callback));
} }
} // namespace net } // namespace net
...@@ -19,6 +19,8 @@ class URLRequestContextGetter; ...@@ -19,6 +19,8 @@ class URLRequestContextGetter;
// Clears the HTTP cache and calls |closure| back. // Clears the HTTP cache and calls |closure| back.
void ClearHttpCache(const scoped_refptr<net::URLRequestContextGetter>& getter, void ClearHttpCache(const scoped_refptr<net::URLRequestContextGetter>& getter,
const scoped_refptr<base::TaskRunner>& network_task_runner, const scoped_refptr<base::TaskRunner>& network_task_runner,
const base::Time& delete_begin,
const base::Time& delete_end,
const net::CompletionCallback& callback); const net::CompletionCallback& callback);
} // namespace net } // namespace net
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment