diff options
author | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-22 05:10:24 +0000 |
---|---|---|
committer | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-22 05:10:24 +0000 |
commit | 72a3ce556f70637bfa196901c92652d023b96f7f (patch) | |
tree | f62634f2d3f4faf2aec3d2231de59d63f96ccd36 /webkit/appcache | |
parent | 0c7ae740f862bae6dc245d378b9bc515f62e73ab (diff) | |
download | chromium_src-72a3ce556f70637bfa196901c92652d023b96f7f.zip chromium_src-72a3ce556f70637bfa196901c92652d023b96f7f.tar.gz chromium_src-72a3ce556f70637bfa196901c92652d023b96f7f.tar.bz2 |
base::Bind: More low-hanging fruit in webkit/appcache.
BUG=none
TEST=none
R=csilv
Review URL: http://codereview.chromium.org/8949063
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@115486 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/appcache')
-rw-r--r-- | webkit/appcache/appcache_disk_cache.cc | 2 | ||||
-rw-r--r-- | webkit/appcache/appcache_response.cc | 24 | ||||
-rw-r--r-- | webkit/appcache/appcache_response.h | 4 | ||||
-rw-r--r-- | webkit/appcache/appcache_response_unittest.cc | 26 | ||||
-rw-r--r-- | webkit/appcache/appcache_service.cc | 36 | ||||
-rw-r--r-- | webkit/appcache/appcache_service_unittest.cc | 2 | ||||
-rw-r--r-- | webkit/appcache/appcache_storage_impl_unittest.cc | 47 | ||||
-rw-r--r-- | webkit/appcache/appcache_url_request_job_unittest.cc | 51 |
8 files changed, 72 insertions, 120 deletions
diff --git a/webkit/appcache/appcache_disk_cache.cc b/webkit/appcache/appcache_disk_cache.cc index 7cdc4ae..c0c9468 100644 --- a/webkit/appcache/appcache_disk_cache.cc +++ b/webkit/appcache/appcache_disk_cache.cc @@ -248,7 +248,7 @@ void AppCacheDiskCache::OnCreateBackendComplete(int rv) { init_callback_.Reset(); } - // Service pending calls that were queued up while we were initailizating. + // Service pending calls that were queued up while we were initializing. for (PendingCalls::const_iterator iter = pending_calls_.begin(); iter < pending_calls_.end(); ++iter) { int rv = net::ERR_FAILED; diff --git a/webkit/appcache/appcache_response.cc b/webkit/appcache/appcache_response.cc index 05fdbf6..b1dd410 100644 --- a/webkit/appcache/appcache_response.cc +++ b/webkit/appcache/appcache_response.cc @@ -88,13 +88,13 @@ AppCacheResponseIO::~AppCacheResponseIO() { entry_->Close(); } -void AppCacheResponseIO::ScheduleIOOldCompletionCallback(int result) { +void AppCacheResponseIO::ScheduleIOCompletionCallback(int result) { MessageLoop::current()->PostTask( FROM_HERE, base::Bind(&AppCacheResponseIO::OnIOComplete, weak_factory_.GetWeakPtr(), result)); } -void AppCacheResponseIO::InvokeUserOldCompletionCallback(int result) { +void AppCacheResponseIO::InvokeUserCompletionCallback(int result) { // Clear the user callback and buffers prior to invoking the callback // so the caller can schedule additional operations in the callback. buffer_ = NULL; @@ -112,7 +112,7 @@ void AppCacheResponseIO::ReadRaw(int index, int offset, base::Bind(&AppCacheResponseIO::OnRawIOComplete, weak_factory_.GetWeakPtr())); if (rv != net::ERR_IO_PENDING) - ScheduleIOOldCompletionCallback(rv); + ScheduleIOCompletionCallback(rv); } void AppCacheResponseIO::WriteRaw(int index, int offset, @@ -123,7 +123,7 @@ void AppCacheResponseIO::WriteRaw(int index, int offset, base::Bind(&AppCacheResponseIO::OnRawIOComplete, weak_factory_.GetWeakPtr())); if (rv != net::ERR_IO_PENDING) - ScheduleIOOldCompletionCallback(rv); + ScheduleIOCompletionCallback(rv); } void AppCacheResponseIO::OnRawIOComplete(int result) { @@ -162,13 +162,13 @@ void AppCacheResponseReader::ReadInfo(HttpResponseInfoIOBuffer* info_buf, void AppCacheResponseReader::ContinueReadInfo() { if (!entry_) { - ScheduleIOOldCompletionCallback(net::ERR_CACHE_MISS); + ScheduleIOCompletionCallback(net::ERR_CACHE_MISS); return; } int size = entry_->GetSize(kResponseInfoIndex); if (size <= 0) { - ScheduleIOOldCompletionCallback(net::ERR_CACHE_MISS); + ScheduleIOCompletionCallback(net::ERR_CACHE_MISS); return; } @@ -193,7 +193,7 @@ void AppCacheResponseReader::ReadData(net::IOBuffer* buf, int buf_len, void AppCacheResponseReader::ContinueReadData() { if (!entry_) { - ScheduleIOOldCompletionCallback(net::ERR_CACHE_MISS); + ScheduleIOCompletionCallback(net::ERR_CACHE_MISS); return; } @@ -221,7 +221,7 @@ void AppCacheResponseReader::OnIOComplete(int result) { bool response_truncated = false; if (!info->InitFromPickle(pickle, &response_truncated) || !info->headers) { - InvokeUserOldCompletionCallback(net::ERR_FAILED); + InvokeUserCompletionCallback(net::ERR_FAILED); return; } DCHECK(!response_truncated); @@ -235,7 +235,7 @@ void AppCacheResponseReader::OnIOComplete(int result) { read_position_ += result; } } - InvokeUserOldCompletionCallback(result); + InvokeUserCompletionCallback(result); } void AppCacheResponseReader::OpenEntryIfNeededAndContinue() { @@ -305,7 +305,7 @@ void AppCacheResponseWriter::WriteInfo( void AppCacheResponseWriter::ContinueWriteInfo() { if (!entry_) { - ScheduleIOOldCompletionCallback(net::ERR_FAILED); + ScheduleIOCompletionCallback(net::ERR_FAILED); return; } @@ -335,7 +335,7 @@ void AppCacheResponseWriter::WriteData( void AppCacheResponseWriter::ContinueWriteData() { if (!entry_) { - ScheduleIOOldCompletionCallback(net::ERR_FAILED); + ScheduleIOCompletionCallback(net::ERR_FAILED); return; } WriteRaw(kResponseContentIndex, write_position_, buffer_, write_amount_); @@ -349,7 +349,7 @@ void AppCacheResponseWriter::OnIOComplete(int result) { else info_size_ = result; } - InvokeUserOldCompletionCallback(result); + InvokeUserCompletionCallback(result); } void AppCacheResponseWriter::CreateEntryIfNeededAndContinue() { diff --git a/webkit/appcache/appcache_response.h b/webkit/appcache/appcache_response.h index b7d8a7d..0ef61f0 100644 --- a/webkit/appcache/appcache_response.h +++ b/webkit/appcache/appcache_response.h @@ -122,8 +122,8 @@ class APPCACHE_EXPORT AppCacheResponseIO { virtual void OnIOComplete(int result) = 0; bool IsIOPending() { return !callback_.is_null(); } - void ScheduleIOOldCompletionCallback(int result); - void InvokeUserOldCompletionCallback(int result); + void ScheduleIOCompletionCallback(int result); + void InvokeUserCompletionCallback(int result); void ReadRaw(int index, int offset, net::IOBuffer* buf, int buf_len); void WriteRaw(int index, int offset, net::IOBuffer* buf, int buf_len); diff --git a/webkit/appcache/appcache_response_unittest.cc b/webkit/appcache/appcache_response_unittest.cc index 4a4ac28..6d989af 100644 --- a/webkit/appcache/appcache_response_unittest.cc +++ b/webkit/appcache/appcache_response_unittest.cc @@ -53,24 +53,13 @@ class AppCacheResponseTest : public testing::Test { AppCacheResponseTest* test_; }; - // Helper class run a test on our io_thread. The io_thread - // is spun up once and reused for all tests. + // Helper callback to run a test on our io_thread. The io_thread is spun up + // once and reused for all tests. template <class Method> - class WrapperTask : public Task { - public: - WrapperTask(AppCacheResponseTest* test, Method method) - : test_(test), method_(method) { - } - - virtual void Run() { - test_->SetUpTest(); - (test_->*method_)(); - } - - private: - AppCacheResponseTest* test_; - Method method_; - }; + void MethodWrapper(Method method) { + SetUpTest(); + (this->*method)(); + } static void SetUpTestCase() { io_thread_.reset(new base::Thread("AppCacheResponseTest Thread")); @@ -88,7 +77,8 @@ class AppCacheResponseTest : public testing::Test { void RunTestOnIOThread(Method method) { test_finished_event_ .reset(new base::WaitableEvent(false, false)); io_thread_->message_loop()->PostTask( - FROM_HERE, new WrapperTask<Method>(this, method)); + FROM_HERE, base::Bind(&AppCacheResponseTest::MethodWrapper<Method>, + base::Unretained(this), method)); test_finished_event_->Wait(); } diff --git a/webkit/appcache/appcache_service.cc b/webkit/appcache/appcache_service.cc index 79ee7ac..55ae766 100644 --- a/webkit/appcache/appcache_service.cc +++ b/webkit/appcache/appcache_service.cc @@ -24,6 +24,14 @@ namespace appcache { +namespace { + +void DeferredCallback(const net::CompletionCallback& callback, int rv) { + callback.Run(rv); +} + +} // namespace + AppCacheInfoCollection::AppCacheInfoCollection() {} AppCacheInfoCollection::~AppCacheInfoCollection() {} @@ -52,7 +60,7 @@ class AppCacheService::NewAsyncHelper if (!callback_.is_null()) { // Defer to guarantee async completion. MessageLoop::current()->PostTask( - FROM_HERE, base::Bind(&DeferredCallCallback, callback_, rv)); + FROM_HERE, base::Bind(&DeferredCallback, callback_, rv)); } callback_.Reset(); } @@ -80,8 +88,9 @@ class AppCacheService::AsyncHelper : public AppCacheStorage::Delegate { public: AsyncHelper( - AppCacheService* service, net::OldCompletionCallback* callback) - : service_(service), callback_(callback) { + AppCacheService* service, const net::CompletionCallback& callback) + : service_(service), + callback_(callback) { service_->pending_helpers_.insert(this); } @@ -95,27 +104,22 @@ class AppCacheService::AsyncHelper protected: void CallCallback(int rv) { - if (callback_) { + if (!callback_.is_null()) { // Defer to guarantee async completion. MessageLoop::current()->PostTask( - FROM_HERE, base::Bind(&DeferredCallCallback, callback_, rv)); + FROM_HERE, base::Bind(&DeferredCallback, callback_, rv)); } - callback_ = NULL; - } - - static void DeferredCallCallback(net::OldCompletionCallback* callback, - int rv) { - callback->Run(rv); + callback_.Reset(); } AppCacheService* service_; - net::OldCompletionCallback* callback_; + net::CompletionCallback callback_; }; void AppCacheService::AsyncHelper::Cancel() { - if (callback_) { - callback_->Run(net::ERR_ABORTED); - callback_ = NULL; + if (!callback_.is_null()) { + callback_.Run(net::ERR_ABORTED); + callback_.Reset(); } service_->storage()->CancelDelegateCallbacks(this); service_ = NULL; @@ -339,7 +343,7 @@ class AppCacheService::CheckResponseHelper : AsyncHelper { CheckResponseHelper( AppCacheService* service, const GURL& manifest_url, int64 cache_id, int64 response_id) - : AsyncHelper(service, NULL), + : AsyncHelper(service, net::CompletionCallback()), manifest_url_(manifest_url), cache_id_(cache_id), response_id_(response_id), diff --git a/webkit/appcache/appcache_service_unittest.cc b/webkit/appcache/appcache_service_unittest.cc index 9bc74e5..4397c6f 100644 --- a/webkit/appcache/appcache_service_unittest.cc +++ b/webkit/appcache/appcache_service_unittest.cc @@ -69,7 +69,7 @@ class MockResponseReader : public AppCacheResponseReader { void ScheduleUserCallback(int result) { MessageLoop::current()->PostTask( FROM_HERE, - base::Bind(&MockResponseReader::InvokeUserOldCompletionCallback, + base::Bind(&MockResponseReader::InvokeUserCompletionCallback, weak_factory_.GetWeakPtr(), result)); } diff --git a/webkit/appcache/appcache_storage_impl_unittest.cc b/webkit/appcache/appcache_storage_impl_unittest.cc index cfaabf2..cdfe149 100644 --- a/webkit/appcache/appcache_storage_impl_unittest.cc +++ b/webkit/appcache/appcache_storage_impl_unittest.cc @@ -202,37 +202,27 @@ class AppCacheStorageImplTest : public testing::Test { scoped_refptr<MockQuotaManager> mock_manager_; }; - // Helper class run a test on our io_thread. The io_thread - // is spun up once and reused for all tests. template <class Method> - class WrapperTask : public Task { - public: - WrapperTask(AppCacheStorageImplTest* test, Method method) - : test_(test), method_(method) { - } - - virtual void Run() OVERRIDE { - test_->SetUpTest(); - - // Ensure InitTask execution prior to conducting a test. - test_->FlushDbThreadTasks(); - - // We also have to wait for InitTask completion call to be performed - // on the IO thread prior to running the test. Its guaranteed to be - // queued by this time. - MessageLoop::current()->PostTask( - FROM_HERE, base::Bind(&RunMethod, test_, method_)); - } + void RunMethod(Method method) { + (this->*method)(); + } - static void RunMethod(AppCacheStorageImplTest* test, Method method) { - (test->*method)(); - } + // Helper callback to run a test on our io_thread. The io_thread is spun up + // once and reused for all tests. + template <class Method> + void MethodWrapper(Method method) { + SetUpTest(); - private: - AppCacheStorageImplTest* test_; - Method method_; - }; + // Ensure InitTask execution prior to conducting a test. + FlushDbThreadTasks(); + // We also have to wait for InitTask completion call to be performed + // on the IO thread prior to running the test. Its guaranteed to be + // queued by this time. + MessageLoop::current()->PostTask( + FROM_HERE, base::Bind(&AppCacheStorageImplTest::RunMethod<Method>, + base::Unretained(this), method)); + } static void SetUpTestCase() { io_thread.reset(new base::Thread("AppCacheTest.IOThread")); @@ -257,7 +247,8 @@ class AppCacheStorageImplTest : public testing::Test { void RunTestOnIOThread(Method method) { test_finished_event_ .reset(new base::WaitableEvent(false, false)); io_thread->message_loop()->PostTask( - FROM_HERE, new WrapperTask<Method>(this, method)); + FROM_HERE, base::Bind(&AppCacheStorageImplTest::MethodWrapper<Method>, + base::Unretained(this), method)); test_finished_event_->Wait(); } diff --git a/webkit/appcache/appcache_url_request_job_unittest.cc b/webkit/appcache/appcache_url_request_job_unittest.cc index a7ff8b0..43ae76b 100644 --- a/webkit/appcache/appcache_url_request_job_unittest.cc +++ b/webkit/appcache/appcache_url_request_job_unittest.cc @@ -33,7 +33,6 @@ static const char kHttpBasicBody[] = "Hello"; static const int kNumBlocks = 4; static const int kBlockSize = 1024; -static const int kNoSuchResponseId = 123; class AppCacheURLRequestJobTest : public testing::Test { public: @@ -139,24 +138,13 @@ class AppCacheURLRequestJobTest : public testing::Test { } } - // Helper class run a test on our io_thread. The io_thread - // is spun up once and reused for all tests. + // Helper callback to run a test on our io_thread. The io_thread is spun up + // once and reused for all tests. template <class Method> - class WrapperTask : public Task { - public: - WrapperTask(AppCacheURLRequestJobTest* test, Method method) - : test_(test), method_(method) { - } - - virtual void Run() { - test_->SetUpTest(); - (test_->*method_)(); - } - - private: - AppCacheURLRequestJobTest* test_; - Method method_; - }; + void MethodWrapper(Method method) { + SetUpTest(); + (this->*method)(); + } static void SetUpTestCase() { io_thread_.reset(new base::Thread("AppCacheURLRequestJobTest Thread")); @@ -168,16 +156,14 @@ class AppCacheURLRequestJobTest : public testing::Test { io_thread_.reset(NULL); } - AppCacheURLRequestJobTest() - : ALLOW_THIS_IN_INITIALIZER_LIST(read_info_callback_( - this, &AppCacheURLRequestJobTest::OnReadInfoComplete)) { - } + AppCacheURLRequestJobTest() {} template <class Method> void RunTestOnIOThread(Method method) { test_finished_event_ .reset(new base::WaitableEvent(false, false)); io_thread_->message_loop()->PostTask( - FROM_HERE, new WrapperTask<Method>(this, method)); + FROM_HERE, base::Bind(&AppCacheURLRequestJobTest::MethodWrapper<Method>, + base::Unretained(this), method)); test_finished_event_->Wait(); } @@ -192,12 +178,8 @@ class AppCacheURLRequestJobTest : public testing::Test { expected_read_result_ = 0; expected_write_result_ = 0; written_response_id_ = 0; - should_delete_reader_in_completion_callback_ = false; - should_delete_writer_in_completion_callback_ = false; reader_deletion_count_down_ = 0; writer_deletion_count_down_ = 0; - read_callback_was_called_ = false; - write_callback_was_called_ = false; } void TearDownTest() { @@ -317,12 +299,7 @@ class AppCacheURLRequestJobTest : public testing::Test { void OnWriteComplete(int result) { EXPECT_FALSE(writer_->IsWritePending()); - write_callback_was_called_ = true; EXPECT_EQ(expected_write_result_, result); - if (should_delete_writer_in_completion_callback_ && - --writer_deletion_count_down_ == 0) { - writer_.reset(); - } ScheduleNextTask(); } @@ -334,12 +311,7 @@ class AppCacheURLRequestJobTest : public testing::Test { void OnReadComplete(int result) { EXPECT_FALSE(reader_->IsReadPending()); - read_callback_was_called_ = true; EXPECT_EQ(expected_read_result_, result); - if (should_delete_reader_in_completion_callback_ && - --reader_deletion_count_down_ == 0) { - reader_.reset(); - } ScheduleNextTask(); } @@ -765,19 +737,14 @@ class AppCacheURLRequestJobTest : public testing::Test { scoped_refptr<HttpResponseInfoIOBuffer> read_info_buffer_; scoped_refptr<IOBuffer> read_buffer_; int expected_read_result_; - net::OldCompletionCallbackImpl<AppCacheURLRequestJobTest> read_info_callback_; - bool should_delete_reader_in_completion_callback_; int reader_deletion_count_down_; - bool read_callback_was_called_; int64 written_response_id_; scoped_ptr<AppCacheResponseWriter> writer_; scoped_refptr<HttpResponseInfoIOBuffer> write_info_buffer_; scoped_refptr<IOBuffer> write_buffer_; int expected_write_result_; - bool should_delete_writer_in_completion_callback_; int writer_deletion_count_down_; - bool write_callback_was_called_; net::URLRequest::ProtocolFactory* orig_http_factory_; scoped_ptr<net::URLRequest> request_; |