Change AndroidHistoryProviderService to use CancelableTaskTracker (3/6)

Change AndroidHistoryProviderService::MoveStatement to use
base::CancelableTaskTracker instead of CancelableRequestConsumer.

BUG=371818

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@283770 0039d316-1c4b-4281-b951-d872f2087c98
parent d7554b1a
...@@ -126,25 +126,31 @@ AndroidHistoryProviderService::DeleteHistory( ...@@ -126,25 +126,31 @@ AndroidHistoryProviderService::DeleteHistory(
return request->handle(); return request->handle();
} }
AndroidHistoryProviderService::Handle base::CancelableTaskTracker::TaskId
AndroidHistoryProviderService::MoveStatement( AndroidHistoryProviderService::MoveStatement(
history::AndroidStatement* statement, history::AndroidStatement* statement,
int current_pos, int current_pos,
int destination, int destination,
CancelableRequestConsumerBase* consumer, const MoveStatementCallback& callback,
const MoveStatementCallback& callback) { base::CancelableTaskTracker* tracker) {
MoveStatementRequest* request = new MoveStatementRequest(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::MoveStatement, NULL, request, statement, DCHECK(hs->thread_checker_.CalledOnValidThread());
current_pos, destination); return tracker->PostTaskAndReplyWithResult(
hs->thread_->message_loop_proxy().get(),
FROM_HERE,
base::Bind(&HistoryBackend::MoveStatement,
hs->history_backend_.get(),
statement,
current_pos,
destination),
callback);
} else { } else {
request->ForwardResultAsync(request->handle(), current_pos); callback.Run(current_pos);
return base::CancelableTaskTracker::kBadTaskId;
} }
return request->handle();
} }
void AndroidHistoryProviderService::CloseStatement( void AndroidHistoryProviderService::CloseStatement(
......
...@@ -50,11 +50,10 @@ class AndroidHistoryProviderService : public CancelableRequestProvider { ...@@ -50,11 +50,10 @@ class AndroidHistoryProviderService : public CancelableRequestProvider {
DeleteCallback; DeleteCallback;
typedef CancelableRequest<DeleteCallback> DeleteRequest; typedef CancelableRequest<DeleteCallback> DeleteRequest;
typedef base::Callback<void( // Callback invoked when a method moving an |AndroidStatement| is complete.
Handle, // handle // The value passed to the callback is the new position, or in case of
int)> // the new position. // failure, the old position.
MoveStatementCallback; typedef base::Callback<void(int)> MoveStatementCallback;
typedef CancelableRequest<MoveStatementCallback> MoveStatementRequest;
// History and Bookmarks ---------------------------------------------------- // History and Bookmarks ----------------------------------------------------
// //
...@@ -116,11 +115,12 @@ class AndroidHistoryProviderService : public CancelableRequestProvider { ...@@ -116,11 +115,12 @@ class AndroidHistoryProviderService : public CancelableRequestProvider {
// 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
// thread. The new position is returned to the callback. The result supplied // thread. The new position is returned to the callback. The result supplied
// the callback is constrained by the number of rows might. // the callback is constrained by the number of rows might.
Handle MoveStatement(history::AndroidStatement* statement, base::CancelableTaskTracker::TaskId MoveStatement(
int current_pos, history::AndroidStatement* statement,
int destination, int current_pos,
CancelableRequestConsumerBase* consumer, int destination,
const MoveStatementCallback& callback); const MoveStatementCallback& callback,
base::CancelableTaskTracker* tracker);
// Closes the statement in db thread. The AndroidHistoryProviderService takes // Closes the statement in db thread. The AndroidHistoryProviderService takes
// the ownership of |statement|. // the ownership of |statement|.
......
...@@ -134,8 +134,7 @@ class CallbackHelper : public base::RefCountedThreadSafe<CallbackHelper> { ...@@ -134,8 +134,7 @@ class CallbackHelper : public base::RefCountedThreadSafe<CallbackHelper> {
base::MessageLoop::current()->Quit(); base::MessageLoop::current()->Quit();
} }
void OnStatementMoved(AndroidHistoryProviderService::Handle handle, void OnStatementMoved(int cursor_position) {
int cursor_position) {
cursor_position_ = cursor_position; cursor_position_ = cursor_position;
base::MessageLoop::current()->Quit(); base::MessageLoop::current()->Quit();
} }
...@@ -184,8 +183,12 @@ TEST_F(AndroidHistoryProviderServiceTest, TestHistoryAndBookmark) { ...@@ -184,8 +183,12 @@ TEST_F(AndroidHistoryProviderServiceTest, TestHistoryAndBookmark) {
// Move the cursor to the begining and verify whether we could get // Move the cursor to the begining and verify whether we could get
// the same result. // the same result.
AndroidStatement* statement = callback->statement(); AndroidStatement* statement = callback->statement();
service_->MoveStatement(statement, 0, -1, &cancelable_consumer_, service_->MoveStatement(
Bind(&CallbackHelper::OnStatementMoved, callback.get())); statement,
0,
-1,
Bind(&CallbackHelper::OnStatementMoved, callback.get()),
&cancelable_tracker_);
base::MessageLoop::current()->Run(); base::MessageLoop::current()->Run();
EXPECT_EQ(-1, callback->cursor_position()); EXPECT_EQ(-1, callback->cursor_position());
EXPECT_TRUE(callback->statement()->statement()->Step()); EXPECT_TRUE(callback->statement()->statement()->Step());
...@@ -244,8 +247,12 @@ TEST_F(AndroidHistoryProviderServiceTest, TestSearchTerm) { ...@@ -244,8 +247,12 @@ TEST_F(AndroidHistoryProviderServiceTest, TestSearchTerm) {
// Move the cursor to the begining and verify whether we could get // Move the cursor to the begining and verify whether we could get
// the same result. // the same result.
AndroidStatement* statement = callback->statement(); AndroidStatement* statement = callback->statement();
service_->MoveStatement(statement, 0, -1, &cancelable_consumer_, service_->MoveStatement(
Bind(&CallbackHelper::OnStatementMoved, callback.get())); statement,
0,
-1,
Bind(&CallbackHelper::OnStatementMoved, callback.get()),
&cancelable_tracker_);
base::MessageLoop::current()->Run(); base::MessageLoop::current()->Run();
EXPECT_EQ(-1, callback->cursor_position()); EXPECT_EQ(-1, callback->cursor_position());
EXPECT_TRUE(callback->statement()->statement()->Step()); EXPECT_TRUE(callback->statement()->statement()->Step());
......
...@@ -167,7 +167,6 @@ void SQLiteCursor::DestroyOnUIThread() { ...@@ -167,7 +167,6 @@ void SQLiteCursor::DestroyOnUIThread() {
// Consumer requests were set in the UI thread. They must be cancelled // Consumer requests were set in the UI thread. They must be cancelled
// using the same thread. // using the same thread.
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
consumer_.reset();
tracker_.reset(); tracker_.reset();
service_->CloseStatement(statement_); service_->CloseStatement(statement_);
delete this; delete this;
...@@ -218,8 +217,7 @@ void SQLiteCursor::OnFaviconData( ...@@ -218,8 +217,7 @@ void SQLiteCursor::OnFaviconData(
test_observer_->OnGetFaviconResult(); test_observer_->OnGetFaviconResult();
} }
void SQLiteCursor::OnMoved(AndroidHistoryProviderService::Handle handle, void SQLiteCursor::OnMoved(int pos) {
int pos) {
position_ = pos; position_ = pos;
event_.Signal(); event_.Signal();
if (test_observer_) if (test_observer_)
...@@ -236,9 +234,12 @@ SQLiteCursor::JavaColumnType SQLiteCursor::GetColumnTypeInternal(int column) { ...@@ -236,9 +234,12 @@ SQLiteCursor::JavaColumnType SQLiteCursor::GetColumnTypeInternal(int column) {
void SQLiteCursor::RunMoveStatementOnUIThread(int pos) { void SQLiteCursor::RunMoveStatementOnUIThread(int pos) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!consumer_.get()) if (!tracker_.get())
consumer_.reset(new CancelableRequestConsumer()); tracker_.reset(new base::CancelableTaskTracker());
service_->MoveStatement( service_->MoveStatement(
statement_, position_, pos, consumer_.get(), statement_,
base::Bind(&SQLiteCursor::OnMoved, base::Unretained(this))); position_,
pos,
base::Bind(&SQLiteCursor::OnMoved, base::Unretained(this)),
tracker_.get());
} }
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/synchronization/waitable_event.h" #include "base/synchronization/waitable_event.h"
#include "base/task/cancelable_task_tracker.h" #include "base/task/cancelable_task_tracker.h"
#include "chrome/browser/common/cancelable_request.h"
#include "chrome/browser/history/android/android_history_provider_service.h" #include "chrome/browser/history/android/android_history_provider_service.h"
#include "chrome/browser/history/history_types.h" #include "chrome/browser/history/history_types.h"
#include "components/favicon_base/favicon_callback.h" #include "components/favicon_base/favicon_callback.h"
...@@ -159,7 +158,7 @@ class SQLiteCursor { ...@@ -159,7 +158,7 @@ class SQLiteCursor {
void OnFaviconData(const favicon_base::FaviconRawBitmapResult& bitmap_result); void OnFaviconData(const favicon_base::FaviconRawBitmapResult& bitmap_result);
// The callback function of MoveTo(). // The callback function of MoveTo().
void OnMoved(AndroidHistoryProviderService::Handle handle, int pos); void OnMoved(int pos);
JavaColumnType GetColumnTypeInternal(int column); JavaColumnType GetColumnTypeInternal(int column);
...@@ -182,7 +181,6 @@ class SQLiteCursor { ...@@ -182,7 +181,6 @@ class SQLiteCursor {
FaviconService* favicon_service_; FaviconService* favicon_service_;
// Live on UI thread. // Live on UI thread.
scoped_ptr<CancelableRequestConsumer> consumer_;
scoped_ptr<base::CancelableTaskTracker> tracker_; scoped_ptr<base::CancelableTaskTracker> tracker_;
// The count of result rows. // The count of result rows.
......
...@@ -314,10 +314,9 @@ class HistoryBackend : public base::RefCountedThreadSafe<HistoryBackend>, ...@@ -314,10 +314,9 @@ class HistoryBackend : public base::RefCountedThreadSafe<HistoryBackend>,
// Statement ---------------------------------------------------------------- // Statement ----------------------------------------------------------------
// Move the statement's current position. // Move the statement's current position.
void MoveStatement(scoped_refptr<MoveStatementRequest> request, int MoveStatement(history::AndroidStatement* statement,
history::AndroidStatement* statement, int current_pos,
int current_pos, int destination);
int destination);
// Close the given statement. The ownership is transfered. // Close the given statement. The ownership is transfered.
void CloseStatement(AndroidStatement* statement); void CloseStatement(AndroidStatement* statement);
......
...@@ -86,11 +86,9 @@ void HistoryBackend::DeleteHistory( ...@@ -86,11 +86,9 @@ void HistoryBackend::DeleteHistory(
// Statement ------------------------------------------------------------------- // Statement -------------------------------------------------------------------
void HistoryBackend::MoveStatement( int HistoryBackend::MoveStatement(history::AndroidStatement* statement,
scoped_refptr<MoveStatementRequest> request, int current_pos,
history::AndroidStatement* statement, int destination) {
int current_pos,
int destination) {
DCHECK_LE(-1, current_pos); DCHECK_LE(-1, current_pos);
DCHECK_LE(-1, destination); DCHECK_LE(-1, destination);
...@@ -104,7 +102,7 @@ void HistoryBackend::MoveStatement( ...@@ -104,7 +102,7 @@ void HistoryBackend::MoveStatement(
break; break;
} }
request->ForwardResult(request->handle(), cur); return cur;
} }
void HistoryBackend::CloseStatement(AndroidStatement* statement) { void HistoryBackend::CloseStatement(AndroidStatement* statement) {
......
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