Change AndroidHistoryProviderService to use CancelableTaskTracker (5/6)

Port UpdateHistoryAndBookmarks and UpdateSearchTerms to use a
base::CancelableTaskTracker instead of a CancelableRequestConsumer.

BUG=371818

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@284069 0039d316-1c4b-4281-b951-d872f2087c98
parent 13f8141b
......@@ -817,17 +817,18 @@ class UpdateBookmarksFromAPITask : public HistoryProviderTask {
const std::vector<base::string16>& selection_args) {
RunAsyncRequestOnUIThreadBlocking(
base::Bind(&AndroidHistoryProviderService::UpdateHistoryAndBookmarks,
base::Unretained(service()), row, selection,
selection_args, cancelable_consumer(),
base::Unretained(service()),
row,
selection,
selection_args,
base::Bind(&UpdateBookmarksFromAPITask::OnBookmarksUpdated,
base::Unretained(this))));
base::Unretained(this)),
cancelable_tracker()));
return result_;
}
private:
void OnBookmarksUpdated(AndroidHistoryProviderService::Handle handle,
bool succeeded,
int updated_row_count) {
void OnBookmarksUpdated(int updated_row_count) {
result_ = updated_row_count;
RequestCompleted();
}
......@@ -1063,15 +1064,12 @@ class UpdateSearchTermsFromAPITask : public SearchTermTask {
internal_row,
selection,
selection_args,
cancelable_consumer(),
base::Bind(&UpdateSearchTermsFromAPITask::OnSearchTermsUpdated,
base::Unretained(this)));
base::Unretained(this)),
cancelable_tracker());
}
void OnSearchTermsUpdated(AndroidHistoryProviderService::Handle handle,
bool succeeded,
int updated_row_count) {
void OnSearchTermsUpdated(int updated_row_count) {
result_ = updated_row_count;
RequestCompleted();
}
......
......@@ -47,25 +47,31 @@ AndroidHistoryProviderService::QueryHistoryAndBookmarks(
}
}
AndroidHistoryProviderService::Handle
base::CancelableTaskTracker::TaskId
AndroidHistoryProviderService::UpdateHistoryAndBookmarks(
const history::HistoryAndBookmarkRow& row,
const std::string& selection,
const std::vector<base::string16>& selection_args,
CancelableRequestConsumerBase* consumer,
const UpdateCallback& callback) {
UpdateRequest* request = new UpdateRequest(callback);
AddRequest(request, consumer);
const UpdateCallback& callback,
base::CancelableTaskTracker* tracker) {
HistoryService* hs =
HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
if (hs) {
hs->Schedule(HistoryService::PRIORITY_NORMAL,
&HistoryBackend::UpdateHistoryAndBookmarks, NULL, request, row,
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::UpdateHistoryAndBookmarks,
hs->history_backend_.get(),
row,
selection,
selection_args),
callback);
} else {
request->ForwardResultAsync(request->handle(), false, 0);
callback.Run(0);
return base::CancelableTaskTracker::kBadTaskId;
}
return request->handle();
}
AndroidHistoryProviderService::Handle
......@@ -192,25 +198,31 @@ AndroidHistoryProviderService::InsertSearchTerm(
}
}
AndroidHistoryProviderService::Handle
base::CancelableTaskTracker::TaskId
AndroidHistoryProviderService::UpdateSearchTerms(
const history::SearchRow& row,
const std::string& selection,
const std::vector<base::string16>& selection_args,
CancelableRequestConsumerBase* consumer,
const UpdateCallback& callback) {
UpdateRequest* request = new UpdateRequest(callback);
AddRequest(request, consumer);
const UpdateCallback& callback,
base::CancelableTaskTracker* tracker) {
HistoryService* hs =
HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
if (hs) {
hs->Schedule(HistoryService::PRIORITY_NORMAL,
&HistoryBackend::UpdateSearchTerms, NULL, request, row, 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::UpdateSearchTerms,
hs->history_backend_.get(),
row,
selection,
selection_args),
callback);
} else {
request->ForwardResultAsync(request->handle(), false, 0);
callback.Run(0);
return base::CancelableTaskTracker::kBadTaskId;
}
return request->handle();
}
AndroidHistoryProviderService::Handle
......
......@@ -29,12 +29,9 @@ class AndroidHistoryProviderService : public CancelableRequestProvider {
// complete. The pointer is NULL if the creation failed.
typedef base::Callback<void(history::AndroidStatement*)> QueryCallback;
typedef base::Callback<void(
Handle, // handle
bool, // true if the update succeeded.
int)> // the number of row updated.
UpdateCallback;
typedef CancelableRequest<UpdateCallback> UpdateRequest;
// Callback invoked when a method updating rows in the database complete.
// The parameter is the number of rows updated or 0 if the update failed.
typedef base::Callback<void(int)> UpdateCallback;
// Callback invoked when a method inserting rows in the database complete.
// The value is the new row id or 0 if the insertion failed.
......@@ -75,12 +72,12 @@ class AndroidHistoryProviderService : public CancelableRequestProvider {
// |row| is the value to update.
// |selection| is the SQL WHERE clause without 'WHERE'.
// |selection_args| is the arguments for the WHERE clause.
Handle UpdateHistoryAndBookmarks(
base::CancelableTaskTracker::TaskId UpdateHistoryAndBookmarks(
const history::HistoryAndBookmarkRow& row,
const std::string& selection,
const std::vector<base::string16>& selection_args,
CancelableRequestConsumerBase* consumer,
const UpdateCallback& callback);
const UpdateCallback& callback,
base::CancelableTaskTracker* tracker);
// Deletes the specified rows and invokes the |callback| to return the number
// of row deleted on success.
......@@ -138,11 +135,12 @@ class AndroidHistoryProviderService : public CancelableRequestProvider {
// |row| is the value need to update.
// |selection| is the SQL WHERE clause without 'WHERE'.
// |selection_args| is the arguments for WHERE clause.
Handle UpdateSearchTerms(const history::SearchRow& row,
base::CancelableTaskTracker::TaskId UpdateSearchTerms(
const history::SearchRow& row,
const std::string& selection,
const std::vector<base::string16>& selection_args,
CancelableRequestConsumerBase* consumer,
const UpdateCallback& callback);
const UpdateCallback& callback,
base::CancelableTaskTracker* tracker);
// Deletes the matched rows and the number of deleted rows is returned to
// the |callback| on success.
......
......@@ -116,10 +116,8 @@ class CallbackHelper : public base::RefCountedThreadSafe<CallbackHelper> {
base::MessageLoop::current()->Quit();
}
void OnUpdated(AndroidHistoryProviderService::Handle handle,
bool success,
int count) {
success_ = success;
void OnUpdated(int count) {
success_ = count != 0;
count_ = count;
base::MessageLoop::current()->Quit();
}
......@@ -198,9 +196,12 @@ TEST_F(AndroidHistoryProviderServiceTest, TestHistoryAndBookmark) {
// Update the row.
HistoryAndBookmarkRow update_row;
update_row.set_visit_count(3);
service_->UpdateHistoryAndBookmarks(update_row, std::string(),
std::vector<base::string16>(), &cancelable_consumer_,
Bind(&CallbackHelper::OnUpdated, callback.get()));
service_->UpdateHistoryAndBookmarks(
update_row,
std::string(),
std::vector<base::string16>(),
Bind(&CallbackHelper::OnUpdated, callback.get()),
&cancelable_tracker_);
base::MessageLoop::current()->Run();
EXPECT_TRUE(callback->success());
EXPECT_EQ(1, callback->count());
......@@ -263,9 +264,11 @@ TEST_F(AndroidHistoryProviderServiceTest, TestSearchTerm) {
// Update the row.
SearchRow update_row;
update_row.set_search_time(Time::Now());
service_->UpdateSearchTerms(update_row, std::string(),
std::vector<base::string16>(), &cancelable_consumer_,
Bind(&CallbackHelper::OnUpdated, callback.get()));
service_->UpdateSearchTerms(update_row,
std::string(),
std::vector<base::string16>(),
Bind(&CallbackHelper::OnUpdated, callback.get()),
&cancelable_tracker_);
base::MessageLoop::current()->Run();
EXPECT_TRUE(callback->success());
EXPECT_EQ(1, callback->count());
......
......@@ -301,8 +301,12 @@ class HistoryBackend : public base::RefCountedThreadSafe<HistoryBackend>,
const std::vector<base::string16>& selection_args,
const std::string& sort_order);
void UpdateHistoryAndBookmarks(
scoped_refptr<UpdateRequest> request,
// Returns the number of row updated by the update query.
//
// |row| is the value to update.
// |selection| is the SQL WHERE clause without 'WHERE'.
// |selection_args| is the arguments for the WHERE clause.
int UpdateHistoryAndBookmarks(
const HistoryAndBookmarkRow& row,
const std::string& selection,
const std::vector<base::string16>& selection_args);
......@@ -329,8 +333,12 @@ class HistoryBackend : public base::RefCountedThreadSafe<HistoryBackend>,
// Inserts the given values and returns the SearchTermID of the inserted row.
SearchTermID InsertSearchTerm(const SearchRow& row);
void UpdateSearchTerms(scoped_refptr<UpdateRequest> request,
const SearchRow& row,
// Returns the number of row updated by the update query.
//
// |row| is the value to update.
// |selection| is the SQL WHERE clause without 'WHERE'.
// |selection_args| is the arguments for the WHERE clause.
int UpdateSearchTerms(const SearchRow& row,
const std::string& selection,
const std::vector<base::string16> selection_args);
......
......@@ -29,22 +29,16 @@ AndroidStatement* HistoryBackend::QueryHistoryAndBookmarks(
return statement;
}
void HistoryBackend::UpdateHistoryAndBookmarks(
scoped_refptr<UpdateRequest> request,
int HistoryBackend::UpdateHistoryAndBookmarks(
const HistoryAndBookmarkRow& row,
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_->UpdateHistoryAndBookmarks(row,
selection, selection_args, &count);
android_provider_backend_->UpdateHistoryAndBookmarks(
row, selection, selection_args, &count);
}
request->ForwardResult(request->handle(), result, count);
return count;
}
void HistoryBackend::DeleteHistoryAndBookmarks(
......@@ -113,21 +107,16 @@ SearchTermID HistoryBackend::InsertSearchTerm(const SearchRow& row) {
return id;
}
void HistoryBackend::UpdateSearchTerms(
scoped_refptr<UpdateRequest> request,
int HistoryBackend::UpdateSearchTerms(
const SearchRow& row,
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_->UpdateSearchTerms(row, selection,
selection_args, &count);
android_provider_backend_->UpdateSearchTerms(
row, selection, selection_args, &count);
}
request->ForwardResult(request->handle(), result, count);
return count;
}
void HistoryBackend::DeleteSearchTerms(
......
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