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