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