Change AndroidHistoryProviderService to use CancelableTaskTracker (6/6)

Port DeleteHistoryAndBookmarks, DeleteHistory and DeleteSearchTerms to use
base::CancelableTaskTracker instead of CancelableRequestConsumer.

BUG=371818

Review URL: https://codereview.chromium.org/370533003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@284099 0039d316-1c4b-4281-b951-d872f2087c98
parent 9285cdcc
...@@ -851,17 +851,17 @@ class RemoveBookmarksFromAPITask : public HistoryProviderTask { ...@@ -851,17 +851,17 @@ class RemoveBookmarksFromAPITask : public HistoryProviderTask {
const std::vector<base::string16>& selection_args) { const std::vector<base::string16>& selection_args) {
RunAsyncRequestOnUIThreadBlocking( RunAsyncRequestOnUIThreadBlocking(
base::Bind(&AndroidHistoryProviderService::DeleteHistoryAndBookmarks, base::Bind(&AndroidHistoryProviderService::DeleteHistoryAndBookmarks,
base::Unretained(service()), selection, selection_args, base::Unretained(service()),
cancelable_consumer(), selection,
selection_args,
base::Bind(&RemoveBookmarksFromAPITask::OnBookmarksRemoved, base::Bind(&RemoveBookmarksFromAPITask::OnBookmarksRemoved,
base::Unretained(this)))); base::Unretained(this)),
cancelable_tracker()));
return result_; return result_;
} }
private: private:
void OnBookmarksRemoved(AndroidHistoryProviderService::Handle handle, void OnBookmarksRemoved(int removed_row_count) {
bool succeeded,
int removed_row_count) {
result_ = removed_row_count; result_ = removed_row_count;
RequestCompleted(); RequestCompleted();
} }
...@@ -884,17 +884,17 @@ class RemoveHistoryFromAPITask : public HistoryProviderTask { ...@@ -884,17 +884,17 @@ class RemoveHistoryFromAPITask : public HistoryProviderTask {
const std::vector<base::string16>& selection_args) { const std::vector<base::string16>& selection_args) {
RunAsyncRequestOnUIThreadBlocking( RunAsyncRequestOnUIThreadBlocking(
base::Bind(&AndroidHistoryProviderService::DeleteHistory, base::Bind(&AndroidHistoryProviderService::DeleteHistory,
base::Unretained(service()), selection, base::Unretained(service()),
selection_args, cancelable_consumer(), selection,
selection_args,
base::Bind(&RemoveHistoryFromAPITask::OnHistoryRemoved, base::Bind(&RemoveHistoryFromAPITask::OnHistoryRemoved,
base::Unretained(this)))); base::Unretained(this)),
cancelable_tracker()));
return result_; return result_;
} }
private: private:
void OnHistoryRemoved(AndroidHistoryProviderService::Handle handle, void OnHistoryRemoved(int removed_row_count) {
bool succeeded,
int removed_row_count) {
result_ = removed_row_count; result_ = removed_row_count;
RequestCompleted(); RequestCompleted();
} }
...@@ -1094,20 +1094,19 @@ class RemoveSearchTermsFromAPITask : public SearchTermTask { ...@@ -1094,20 +1094,19 @@ class RemoveSearchTermsFromAPITask : public SearchTermTask {
int Run(const std::string& selection, int Run(const std::string& selection,
const std::vector<base::string16>& selection_args) { const std::vector<base::string16>& selection_args) {
RunAsyncRequestOnUIThreadBlocking( RunAsyncRequestOnUIThreadBlocking(base::Bind(
base::Bind(&AndroidHistoryProviderService::DeleteSearchTerms, &AndroidHistoryProviderService::DeleteSearchTerms,
base::Unretained(service()), selection, selection_args, base::Unretained(service()),
cancelable_consumer(), selection,
base::Bind( selection_args,
&RemoveSearchTermsFromAPITask::OnSearchTermsDeleted, base::Bind(&RemoveSearchTermsFromAPITask::OnSearchTermsDeleted,
base::Unretained(this)))); base::Unretained(this)),
cancelable_tracker()));
return result_; return result_;
} }
private: private:
void OnSearchTermsDeleted(AndroidHistoryProviderService::Handle handle, void OnSearchTermsDeleted(int deleted_row_count) {
bool succeeded,
int deleted_row_count) {
result_ = deleted_row_count; result_ = deleted_row_count;
RequestCompleted(); RequestCompleted();
} }
......
...@@ -74,24 +74,29 @@ AndroidHistoryProviderService::UpdateHistoryAndBookmarks( ...@@ -74,24 +74,29 @@ AndroidHistoryProviderService::UpdateHistoryAndBookmarks(
} }
} }
AndroidHistoryProviderService::Handle base::CancelableTaskTracker::TaskId
AndroidHistoryProviderService::DeleteHistoryAndBookmarks( AndroidHistoryProviderService::DeleteHistoryAndBookmarks(
const std::string& selection, const std::string& selection,
const std::vector<base::string16>& selection_args, const std::vector<base::string16>& selection_args,
CancelableRequestConsumerBase* consumer, const DeleteCallback& callback,
const DeleteCallback& callback) { base::CancelableTaskTracker* tracker) {
DeleteRequest* request = new DeleteRequest(callback);
AddRequest(request, consumer);
HistoryService* hs = HistoryService* hs =
HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
if (hs) { if (hs) {
hs->Schedule(HistoryService::PRIORITY_NORMAL, DCHECK(hs->thread_) << "History service being called after cleanup";
&HistoryBackend::DeleteHistoryAndBookmarks, NULL, request, DCHECK(hs->thread_checker_.CalledOnValidThread());
selection, selection_args); return tracker->PostTaskAndReplyWithResult(
hs->thread_->message_loop_proxy().get(),
FROM_HERE,
base::Bind(&HistoryBackend::DeleteHistoryAndBookmarks,
hs->history_backend_.get(),
selection,
selection_args),
callback);
} else { } else {
request->ForwardResultAsync(request->handle(), false, 0); callback.Run(0);
return base::CancelableTaskTracker::kBadTaskId;
} }
return request->handle();
} }
base::CancelableTaskTracker::TaskId base::CancelableTaskTracker::TaskId
...@@ -117,24 +122,29 @@ AndroidHistoryProviderService::InsertHistoryAndBookmark( ...@@ -117,24 +122,29 @@ AndroidHistoryProviderService::InsertHistoryAndBookmark(
} }
} }
AndroidHistoryProviderService::Handle base::CancelableTaskTracker::TaskId
AndroidHistoryProviderService::DeleteHistory( AndroidHistoryProviderService::DeleteHistory(
const std::string& selection, const std::string& selection,
const std::vector<base::string16>& selection_args, const std::vector<base::string16>& selection_args,
CancelableRequestConsumerBase* consumer, const DeleteCallback& callback,
const DeleteCallback& callback) { base::CancelableTaskTracker* tracker) {
DeleteRequest* request = new DeleteRequest(callback);
AddRequest(request, consumer);
HistoryService* hs = HistoryService* hs =
HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
if (hs) { if (hs) {
hs->Schedule(HistoryService::PRIORITY_NORMAL, DCHECK(hs->thread_) << "History service being called after cleanup";
&HistoryBackend::DeleteHistory, NULL, request, selection, DCHECK(hs->thread_checker_.CalledOnValidThread());
selection_args); return tracker->PostTaskAndReplyWithResult(
hs->thread_->message_loop_proxy().get(),
FROM_HERE,
base::Bind(&HistoryBackend::DeleteHistory,
hs->history_backend_.get(),
selection,
selection_args),
callback);
} else { } else {
request->ForwardResultAsync(request->handle(), false, 0); callback.Run(0);
return base::CancelableTaskTracker::kBadTaskId;
} }
return request->handle();
} }
base::CancelableTaskTracker::TaskId base::CancelableTaskTracker::TaskId
...@@ -225,24 +235,29 @@ AndroidHistoryProviderService::UpdateSearchTerms( ...@@ -225,24 +235,29 @@ AndroidHistoryProviderService::UpdateSearchTerms(
} }
} }
AndroidHistoryProviderService::Handle base::CancelableTaskTracker::TaskId
AndroidHistoryProviderService::DeleteSearchTerms( AndroidHistoryProviderService::DeleteSearchTerms(
const std::string& selection, const std::string& selection,
const std::vector<base::string16>& selection_args, const std::vector<base::string16>& selection_args,
CancelableRequestConsumerBase* consumer, const DeleteCallback& callback,
const DeleteCallback& callback) { base::CancelableTaskTracker* tracker) {
DeleteRequest* request = new DeleteRequest(callback);
AddRequest(request, consumer);
HistoryService* hs = HistoryService* hs =
HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
if (hs) { if (hs) {
hs->Schedule(HistoryService::PRIORITY_NORMAL, DCHECK(hs->thread_) << "History service being called after cleanup";
&HistoryBackend::DeleteSearchTerms, NULL, request, selection, DCHECK(hs->thread_checker_.CalledOnValidThread());
selection_args); return tracker->PostTaskAndReplyWithResult(
hs->thread_->message_loop_proxy().get(),
FROM_HERE,
base::Bind(&HistoryBackend::DeleteSearchTerms,
hs->history_backend_.get(),
selection,
selection_args),
callback);
} else { } else {
request->ForwardResultAsync(request->handle(), false, 0); callback.Run(0);
return base::CancelableTaskTracker::kBadTaskId;
} }
return request->handle();
} }
base::CancelableTaskTracker::TaskId base::CancelableTaskTracker::TaskId
......
...@@ -37,12 +37,9 @@ class AndroidHistoryProviderService : public CancelableRequestProvider { ...@@ -37,12 +37,9 @@ class AndroidHistoryProviderService : public CancelableRequestProvider {
// The value is the new row id or 0 if the insertion failed. // The value is the new row id or 0 if the insertion failed.
typedef base::Callback<void(int64)> InsertCallback; typedef base::Callback<void(int64)> InsertCallback;
typedef base::Callback<void( // Callback invoked when a method deleting rows in the database complete.
Handle, // handle // The value is the number of rows deleted or 0 if the deletion failed.
bool, // true if the deletion succeeded. typedef base::Callback<void(int)> DeleteCallback;
int)> // the number of row deleted.
DeleteCallback;
typedef CancelableRequest<DeleteCallback> DeleteRequest;
// Callback invoked when a method moving an |AndroidStatement| is complete. // Callback invoked when a method moving an |AndroidStatement| is complete.
// The value passed to the callback is the new position, or in case of // The value passed to the callback is the new position, or in case of
...@@ -85,12 +82,12 @@ class AndroidHistoryProviderService : public CancelableRequestProvider { ...@@ -85,12 +82,12 @@ class AndroidHistoryProviderService : public CancelableRequestProvider {
// |selection| is the SQL WHERE clause without 'WHERE'. // |selection| is the SQL WHERE clause without 'WHERE'.
// |selection_args| is the arguments for the WHERE clause. // |selection_args| is the arguments for the WHERE clause.
// //
// if |selection| is empty all history and bookmarks are deleted. // If |selection| is empty all history and bookmarks are deleted.
Handle DeleteHistoryAndBookmarks( base::CancelableTaskTracker::TaskId DeleteHistoryAndBookmarks(
const std::string& selection, const std::string& selection,
const std::vector<base::string16>& selection_args, const std::vector<base::string16>& selection_args,
CancelableRequestConsumerBase* consumer, const DeleteCallback& callback,
const DeleteCallback& callback); base::CancelableTaskTracker* tracker);
// Inserts the given values into history backend, and invokes the |callback| // Inserts the given values into history backend, and invokes the |callback|
// to return the result. // to return the result.
...@@ -100,11 +97,12 @@ class AndroidHistoryProviderService : public CancelableRequestProvider { ...@@ -100,11 +97,12 @@ class AndroidHistoryProviderService : public CancelableRequestProvider {
base::CancelableTaskTracker* tracker); base::CancelableTaskTracker* tracker);
// Deletes the matched history and invokes |callback| to return the number of // Deletes the matched history and invokes |callback| to return the number of
// the row deleted from the |callback|. // rows deleted.
Handle DeleteHistory(const std::string& selection, base::CancelableTaskTracker::TaskId DeleteHistory(
const std::vector<base::string16>& selection_args, const std::string& selection,
CancelableRequestConsumerBase* consumer, const std::vector<base::string16>& selection_args,
const DeleteCallback& callback); const DeleteCallback& callback,
base::CancelableTaskTracker* tracker);
// Statement ---------------------------------------------------------------- // Statement ----------------------------------------------------------------
// Moves the statement's current row from |current_pos| to |destination| in DB // Moves the statement's current row from |current_pos| to |destination| in DB
...@@ -148,11 +146,12 @@ class AndroidHistoryProviderService : public CancelableRequestProvider { ...@@ -148,11 +146,12 @@ class AndroidHistoryProviderService : public CancelableRequestProvider {
// |selection| is the SQL WHERE clause without 'WHERE'. // |selection| is the SQL WHERE clause without 'WHERE'.
// |selection_args| is the arguments for WHERE clause. // |selection_args| is the arguments for WHERE clause.
// //
// if |selection| is empty all search be deleted. // If |selection| is empty all search terms will be deleted.
Handle DeleteSearchTerms(const std::string& selection, base::CancelableTaskTracker::TaskId DeleteSearchTerms(
const std::vector<base::string16>& selection_args, const std::string& selection,
CancelableRequestConsumerBase* consumer, const std::vector<base::string16>& selection_args,
const DeleteCallback& callback); const DeleteCallback& callback,
base::CancelableTaskTracker* tracker);
// Runs the query and invokes the |callback| to return the result. // Runs the query and invokes the |callback| to return the result.
// //
......
...@@ -122,10 +122,8 @@ class CallbackHelper : public base::RefCountedThreadSafe<CallbackHelper> { ...@@ -122,10 +122,8 @@ class CallbackHelper : public base::RefCountedThreadSafe<CallbackHelper> {
base::MessageLoop::current()->Quit(); base::MessageLoop::current()->Quit();
} }
void OnDeleted(AndroidHistoryProviderService::Handle handle, void OnDeleted(int count) {
bool success, success_ = count != 0;
int count) {
success_ = success;
count_ = count; count_ = count;
base::MessageLoop::current()->Quit(); base::MessageLoop::current()->Quit();
} }
...@@ -207,9 +205,11 @@ TEST_F(AndroidHistoryProviderServiceTest, TestHistoryAndBookmark) { ...@@ -207,9 +205,11 @@ TEST_F(AndroidHistoryProviderServiceTest, TestHistoryAndBookmark) {
EXPECT_EQ(1, callback->count()); EXPECT_EQ(1, callback->count());
// Delete the row. // Delete the row.
service_->DeleteHistoryAndBookmarks(std::string(), service_->DeleteHistoryAndBookmarks(
std::string(),
std::vector<base::string16>(), std::vector<base::string16>(),
&cancelable_consumer_, Bind(&CallbackHelper::OnDeleted, callback.get())); Bind(&CallbackHelper::OnDeleted, callback.get()),
&cancelable_tracker_);
base::MessageLoop::current()->Run(); base::MessageLoop::current()->Run();
EXPECT_TRUE(callback->success()); EXPECT_TRUE(callback->success());
EXPECT_EQ(1, callback->count()); EXPECT_EQ(1, callback->count());
...@@ -274,8 +274,10 @@ TEST_F(AndroidHistoryProviderServiceTest, TestSearchTerm) { ...@@ -274,8 +274,10 @@ TEST_F(AndroidHistoryProviderServiceTest, TestSearchTerm) {
EXPECT_EQ(1, callback->count()); EXPECT_EQ(1, callback->count());
// Delete the row. // Delete the row.
service_->DeleteSearchTerms(std::string(), std::vector<base::string16>(), service_->DeleteSearchTerms(std::string(),
&cancelable_consumer_, Bind(&CallbackHelper::OnDeleted, callback.get())); std::vector<base::string16>(),
Bind(&CallbackHelper::OnDeleted, callback.get()),
&cancelable_tracker_);
base::MessageLoop::current()->Run(); base::MessageLoop::current()->Run();
EXPECT_TRUE(callback->success()); EXPECT_TRUE(callback->success());
EXPECT_EQ(1, callback->count()); EXPECT_EQ(1, callback->count());
......
...@@ -311,14 +311,19 @@ class HistoryBackend : public base::RefCountedThreadSafe<HistoryBackend>, ...@@ -311,14 +311,19 @@ class HistoryBackend : public base::RefCountedThreadSafe<HistoryBackend>,
const std::string& selection, const std::string& selection,
const std::vector<base::string16>& selection_args); const std::vector<base::string16>& selection_args);
void DeleteHistoryAndBookmarks( // Deletes the specified rows and returns the number of rows deleted.
scoped_refptr<DeleteRequest> request, //
// |selection| is the SQL WHERE clause without 'WHERE'.
// |selection_args| is the arguments for the WHERE clause.
//
// If |selection| is empty all history and bookmarks are deleted.
int DeleteHistoryAndBookmarks(
const std::string& selection, const std::string& selection,
const std::vector<base::string16>& selection_args); const std::vector<base::string16>& selection_args);
void DeleteHistory(scoped_refptr<DeleteRequest> request, // Deletes the matched history and returns the number of rows deleted.
const std::string& selection, int DeleteHistory(const std::string& selection,
const std::vector<base::string16>& selection_args); const std::vector<base::string16>& selection_args);
// Statement ---------------------------------------------------------------- // Statement ----------------------------------------------------------------
// Move the statement's current position. // Move the statement's current position.
...@@ -342,9 +347,14 @@ class HistoryBackend : public base::RefCountedThreadSafe<HistoryBackend>, ...@@ -342,9 +347,14 @@ class HistoryBackend : public base::RefCountedThreadSafe<HistoryBackend>,
const std::string& selection, const std::string& selection,
const std::vector<base::string16> selection_args); const std::vector<base::string16> selection_args);
void DeleteSearchTerms(scoped_refptr<DeleteRequest> request, // Deletes the matched rows and returns the number of deleted rows.
const std::string& selection, //
const std::vector<base::string16> selection_args); // |selection| is the SQL WHERE clause without 'WHERE'.
// |selection_args| is the arguments for WHERE clause.
//
// If |selection| is empty all search terms will be deleted.
int DeleteSearchTerms(const std::string& selection,
const std::vector<base::string16> selection_args);
// Returns the result of the given query. // Returns the result of the given query.
// //
......
...@@ -41,36 +41,25 @@ int HistoryBackend::UpdateHistoryAndBookmarks( ...@@ -41,36 +41,25 @@ int HistoryBackend::UpdateHistoryAndBookmarks(
return count; return count;
} }
void HistoryBackend::DeleteHistoryAndBookmarks( int HistoryBackend::DeleteHistoryAndBookmarks(
scoped_refptr<DeleteRequest> request,
const std::string& selection, const std::string& selection,
const std::vector<base::string16>& selection_args) { const std::vector<base::string16>& selection_args) {
if (request->canceled())
return;
int count = 0; int count = 0;
bool result = false; if (android_provider_backend_) {
if (android_provider_backend_) android_provider_backend_->DeleteHistoryAndBookmarks(
result = android_provider_backend_->DeleteHistoryAndBookmarks(selection, selection, selection_args, &count);
selection_args, &count); }
return count;
request->ForwardResult(request->handle(), result, count);
} }
void HistoryBackend::DeleteHistory( int HistoryBackend::DeleteHistory(
scoped_refptr<DeleteRequest> request,
const std::string& selection, const std::string& selection,
const std::vector<base::string16>& selection_args) { const std::vector<base::string16>& selection_args) {
if (request->canceled())
return;
int count = 0; int count = 0;
bool result = false;
if (android_provider_backend_) { if (android_provider_backend_) {
result = android_provider_backend_->DeleteHistory(selection, selection_args, android_provider_backend_->DeleteHistory(selection, selection_args, &count);
&count);
} }
request->ForwardResult(request->handle(), result, count); return count;
} }
// Statement ------------------------------------------------------------------- // Statement -------------------------------------------------------------------
...@@ -119,21 +108,15 @@ int HistoryBackend::UpdateSearchTerms( ...@@ -119,21 +108,15 @@ int HistoryBackend::UpdateSearchTerms(
return count; return count;
} }
void HistoryBackend::DeleteSearchTerms( int HistoryBackend::DeleteSearchTerms(
scoped_refptr<DeleteRequest> request,
const std::string& selection, const std::string& selection,
const std::vector<base::string16> selection_args) { const std::vector<base::string16> selection_args) {
if (request->canceled())
return;
int count = 0; int count = 0;
bool result = false;
if (android_provider_backend_) { if (android_provider_backend_) {
result = android_provider_backend_->DeleteSearchTerms(selection, android_provider_backend_->DeleteSearchTerms(
selection_args, &count); selection, selection_args, &count);
} }
return count;
request->ForwardResult(request->handle(), result, count);
} }
AndroidStatement* HistoryBackend::QuerySearchTerms( AndroidStatement* HistoryBackend::QuerySearchTerms(
......
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