summaryrefslogtreecommitdiffstats
path: root/net/base/upload_data.cc
diff options
context:
space:
mode:
authorsatorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-02-10 07:39:11 +0000
committersatorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-02-10 07:39:11 +0000
commit1dce708bbe7fdef15436e20f36f805f53bfdcb73 (patch)
tree3b7852c44e541e6d755fb1044c857d37bc622252 /net/base/upload_data.cc
parent9caded3f4f06300155f117134f59b5d85b98119f (diff)
downloadchromium_src-1dce708bbe7fdef15436e20f36f805f53bfdcb73.zip
chromium_src-1dce708bbe7fdef15436e20f36f805f53bfdcb73.tar.gz
chromium_src-1dce708bbe7fdef15436e20f36f805f53bfdcb73.tar.bz2
net: Make UploadData::GetContentLength() asynchronous.
However, the asynchronous version is not used yet. The synchronous version is kept as GetContentLengthSync(). The existing code is changed to use the synchronous version. TEST=net_unittests BUG=72001,112607 Review URL: https://chromiumcodereview.appspot.com/9321003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@121411 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base/upload_data.cc')
-rw-r--r--net/base/upload_data.cc60
1 files changed, 39 insertions, 21 deletions
diff --git a/net/base/upload_data.cc b/net/base/upload_data.cc
index bffe13b..d445307 100644
--- a/net/base/upload_data.cc
+++ b/net/base/upload_data.cc
@@ -6,15 +6,29 @@
#include <algorithm>
+#include "base/bind.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "base/threading/thread_restrictions.h"
+#include "base/threading/worker_pool.h"
+#include "base/tracked_objects.h"
#include "net/base/file_stream.h"
#include "net/base/net_errors.h"
namespace net {
+namespace {
+
+// Helper function for GetContentLength().
+void OnGetContentLengthComplete(
+ const UploadData::ContentLengthCallback& callback,
+ uint64* content_length) {
+ callback.Run(*content_length);
+}
+
+} // namespace
+
UploadData::Element::Element()
: type_(TYPE_BYTES),
file_range_offset_(0),
@@ -73,15 +87,8 @@ uint64 UploadData::Element::GetContentLength() {
return 0;
int64 length = 0;
-
- {
- // TODO(tzik):
- // file_util::GetFileSize may cause blocking IO.
- // Temporary allow until fix: http://crbug.com/72001.
- base::ThreadRestrictions::ScopedAllowIO allow_io;
- if (!file_util::GetFileSize(file_path_, &length))
+ if (!file_util::GetFileSize(file_path_, &length))
return 0;
- }
if (file_range_offset_ >= static_cast<uint64>(length))
return 0; // range is beyond eof
@@ -101,11 +108,6 @@ FileStream* UploadData::Element::NewFileStreamForReading() {
return file;
}
- // TODO(tzik):
- // FileStream::Open and FileStream::Seek may cause blocking IO.
- // Temporary allow until fix: http://crbug.com/72001.
- base::ThreadRestrictions::ScopedAllowIO allow_io;
-
scoped_ptr<FileStream> file(new FileStream(NULL));
int64 rv = file->OpenSync(
file_path_,
@@ -172,15 +174,21 @@ void UploadData::set_chunk_callback(ChunkCallback* callback) {
chunk_callback_ = callback;
}
-uint64 UploadData::GetContentLength() {
- if (is_chunked_)
- return 0;
+void UploadData::GetContentLength(const ContentLengthCallback& callback) {
+ uint64* result = new uint64(0);
+ const bool task_is_slow = true;
+ const bool posted = base::WorkerPool::PostTaskAndReply(
+ FROM_HERE,
+ base::Bind(&UploadData::DoGetContentLength, this, result),
+ base::Bind(&OnGetContentLengthComplete, callback, base::Owned(result)),
+ task_is_slow);
+ DCHECK(posted);
+}
- uint64 len = 0;
- std::vector<Element>::iterator it = elements_.begin();
- for (; it != elements_.end(); ++it)
- len += (*it).GetContentLength();
- return len;
+uint64 UploadData::GetContentLengthSync() {
+ uint64 content_length = 0;
+ DoGetContentLength(&content_length);
+ return content_length;
}
bool UploadData::IsInMemory() const {
@@ -203,6 +211,16 @@ void UploadData::SetElements(const std::vector<Element>& elements) {
elements_ = elements;
}
+void UploadData::DoGetContentLength(uint64* content_length) {
+ *content_length = 0;
+
+ if (is_chunked_)
+ return;
+
+ for (size_t i = 0; i < elements_.size(); ++i)
+ *content_length += elements_[i].GetContentLength();
+}
+
UploadData::~UploadData() {
}