diff options
author | darin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-29 17:29:38 +0000 |
---|---|---|
committer | darin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-29 17:29:38 +0000 |
commit | 9396b257950a859afb01dea086c13645884e39e4 (patch) | |
tree | ee0c2f81bc9c4f6098ccdb882054a50aff5c6c4f /net/url_request | |
parent | 1843ebdfbc05e6b91ce4070647271dfbe090d687 (diff) | |
download | chromium_src-9396b257950a859afb01dea086c13645884e39e4.zip chromium_src-9396b257950a859afb01dea086c13645884e39e4.tar.gz chromium_src-9396b257950a859afb01dea086c13645884e39e4.tar.bz2 |
Define FileInputStream and use it to make UpdateDataStream and URLRequestFileJob portable.
file_input_stream_posix.cc is not implemented in this CL. That is saved for a follow-up.
R=mark
Review URL: http://codereview.chromium.org/4101
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@2673 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/url_request')
-rw-r--r-- | net/url_request/url_request_file_job.cc | 301 | ||||
-rw-r--r-- | net/url_request/url_request_file_job.h | 54 | ||||
-rw-r--r-- | net/url_request/url_request_unittest.cc | 19 | ||||
-rw-r--r-- | net/url_request/url_request_unittest.h | 3 |
4 files changed, 135 insertions, 242 deletions
diff --git a/net/url_request/url_request_file_job.cc b/net/url_request/url_request_file_job.cc index 4e23d78..6139827 100644 --- a/net/url_request/url_request_file_job.cc +++ b/net/url_request/url_request_file_job.cc @@ -17,38 +17,60 @@ // signal from the OS that the overlapped read has completed. It does so by // leveraging the MessageLoop::WatchObject API. -#include <process.h> -#include <windows.h> - #include "net/url_request/url_request_file_job.h" -#include "base/file_util.h" +#include "base/compiler_specific.h" +#include "base/message_loop.h" #include "base/string_util.h" +#include "base/worker_pool.h" #include "googleurl/src/gurl.h" #include "net/base/mime_util.h" #include "net/base/net_errors.h" #include "net/base/net_util.h" -#include "net/base/wininet_util.h" #include "net/url_request/url_request.h" #include "net/url_request/url_request_file_dir_job.h" -using net::WinInetUtil; +#if defined(OS_WIN) +class URLRequestFileJob::AsyncResolver : + public base::RefCountedThreadSafe<URLRequestFileJob::AsyncResolver> { + public: + explicit AsyncResolver(URLRequestFileJob* owner) + : owner_(owner), owner_loop_(MessageLoop::current()) { + } + + void Resolve(const std::wstring& file_path) { + file_util::FileInfo file_info; + bool exists = file_util::GetFileInfo(file_path, &file_info); + AutoLock locked(lock_); + if (owner_loop_) { + owner_loop_->PostTask(FROM_HERE, NewRunnableMethod( + this, &AsyncResolver::ReturnResults, exists, file_info)); + } + } + + void Cancel() { + owner_ = NULL; -namespace { + AutoLock locked(lock_); + owner_loop_ = NULL; + } -// Thread used to run ResolveFile. The parameter is a pointer to the -// URLRequestFileJob object. -DWORD WINAPI NetworkFileThread(LPVOID param) { - URLRequestFileJob* job = reinterpret_cast<URLRequestFileJob*>(param); - job->ResolveFile(); - return 0; -} + private: + void ReturnResults(bool exists, const file_util::FileInfo& file_info) { + if (owner_) + owner_->DidResolve(exists, file_info); + } + + URLRequestFileJob* owner_; -} // namespace + Lock lock_; + MessageLoop* owner_loop_; +}; +#endif // static -URLRequestJob* URLRequestFileJob::Factory(URLRequest* request, - const std::string& scheme) { +URLRequestJob* URLRequestFileJob::Factory( + URLRequest* request, const std::string& scheme) { std::wstring file_path; if (net::FileURLToFilePath(request->url(), &file_path)) { if (file_path[file_path.size() - 1] == file_util::kPathSeparator) { @@ -66,131 +88,65 @@ URLRequestJob* URLRequestFileJob::Factory(URLRequest* request, URLRequestFileJob::URLRequestFileJob(URLRequest* request) : URLRequestJob(request), - handle_(INVALID_HANDLE_VALUE), - is_waiting_(false), - is_directory_(false), - is_not_found_(false), - network_file_thread_(NULL), - loop_(NULL) { - memset(&overlapped_, 0, sizeof(overlapped_)); + ALLOW_THIS_IN_INITIALIZER_LIST( + io_callback_(this, &URLRequestFileJob::DidRead)), + is_directory_(false) { } URLRequestFileJob::~URLRequestFileJob() { - CloseHandles(); - - // The thread might still be running. We need to kill it because it holds - // a reference to this object. - if (network_file_thread_) { - TerminateThread(network_file_thread_, 0); - CloseHandle(network_file_thread_); - } -} - -// This function can be called on the main thread or on the network file thread. -// When the request is done, we call StartAsync on the main thread. -void URLRequestFileJob::ResolveFile() { - WIN32_FILE_ATTRIBUTE_DATA file_attributes = {0}; - if (!GetFileAttributesEx(file_path_.c_str(), - GetFileExInfoStandard, - &file_attributes)) { - is_not_found_ = true; - } else { - if (file_attributes.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { - is_directory_ = true; - } else { - // Set the file size as expected size to be read. - ULARGE_INTEGER file_size; - file_size.HighPart = file_attributes.nFileSizeHigh; - file_size.LowPart = file_attributes.nFileSizeLow; - - set_expected_content_size(file_size.QuadPart); - } - } - - - // We need to protect the loop_ pointer with a lock because if we are running - // on the network file thread, it is possible that the main thread is - // executing Kill() at this moment. Kill() sets the loop_ to NULL because it - // might be going away. - AutoLock lock(loop_lock_); - if (loop_) { - // Start reading asynchronously so that all error reporting and data - // callbacks happen as they would for network requests. - loop_->PostTask(FROM_HERE, NewRunnableMethod( - this, &URLRequestFileJob::StartAsync)); - } +#if defined(OS_WIN) + DCHECK(!async_resolver_); +#endif } void URLRequestFileJob::Start() { - // This is the loop on which we should execute StartAsync. This is used in - // ResolveFile(). - loop_ = MessageLoop::current(); - + // Resolve UNC paths on a background thread. if (!file_path_.compare(0, 2, L"\\\\")) { - // This is on a network share. It might be slow to check if it's a directory - // or a file. We need to do it in another thread. - network_file_thread_ = CreateThread(NULL, 0, &NetworkFileThread, this, 0, - NULL); + DCHECK(!async_resolver_); + async_resolver_ = new AsyncResolver(this); + WorkerPool::PostTask(FROM_HERE, NewRunnableMethod( + async_resolver_.get(), &AsyncResolver::Resolve, file_path_), true); } else { - // We can call the function directly because it's going to be fast. - ResolveFile(); + file_util::FileInfo file_info; + bool exists = file_util::GetFileInfo(file_path_, &file_info); + + // Continue asynchronously. + MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( + this, &URLRequestFileJob::DidResolve, exists, file_info)); } } void URLRequestFileJob::Kill() { - // If we are killed while waiting for an overlapped result... - if (is_waiting_) { - MessageLoopForIO::current()->WatchObject(overlapped_.hEvent, NULL); - is_waiting_ = false; - Release(); + stream_.Close(); + +#if defined(OS_WIN) + if (async_resolver_) { + async_resolver_->Cancel(); + async_resolver_ = NULL; } - CloseHandles(); - URLRequestJob::Kill(); +#endif - // It is possible that the network file thread is running and will invoke - // StartAsync() on loop_. We set loop_ to NULL here because the message loop - // might be going away and we don't want the other thread to call StartAsync() - // on this loop anymore. We protect loop_ with a lock in case the other thread - // is currenly invoking the call. - AutoLock lock(loop_lock_); - loop_ = NULL; + URLRequestJob::Kill(); } -bool URLRequestFileJob::ReadRawData(char* dest, int dest_size, - int *bytes_read) { +bool URLRequestFileJob::ReadRawData( + char* dest, int dest_size, int *bytes_read) { DCHECK_NE(dest_size, 0); DCHECK(bytes_read); - DCHECK(!is_waiting_); - - *bytes_read = 0; - DWORD bytes; - if (ReadFile(handle_, dest, dest_size, &bytes, &overlapped_)) { - // data is immediately available - overlapped_.Offset += bytes; - *bytes_read = static_cast<int>(bytes); - DCHECK(!is_waiting_); - DCHECK(!is_done()); + int rv = stream_.Read(dest, dest_size, &io_callback_); + if (rv >= 0) { + // Data is immediately available. + *bytes_read = rv; return true; } - // otherwise, a read error occured. - DWORD err = GetLastError(); - if (err == ERROR_IO_PENDING) { - // OK, wait for the object to become signaled - MessageLoopForIO::current()->WatchObject(overlapped_.hEvent, this); - is_waiting_ = true; + // Otherwise, a read error occured. We may just need to wait... + if (rv == net::ERR_IO_PENDING) { SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); - AddRef(); - return false; - } - if (err == ERROR_HANDLE_EOF) { - // OK, nothing more to read - return true; + } else { + NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); } - - NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, - WinInetUtil::OSErrorToNetError(err))); return false; } @@ -199,99 +155,49 @@ bool URLRequestFileJob::GetMimeType(std::string* mime_type) { return net::GetMimeTypeFromFile(file_path_, mime_type); } -void URLRequestFileJob::CloseHandles() { - DCHECK(!is_waiting_); - if (handle_ != INVALID_HANDLE_VALUE) { - CloseHandle(handle_); - handle_ = INVALID_HANDLE_VALUE; - } - if (overlapped_.hEvent) { - CloseHandle(overlapped_.hEvent); - overlapped_.hEvent = NULL; - } -} - -void URLRequestFileJob::StartAsync() { - if (network_file_thread_) { - CloseHandle(network_file_thread_); - network_file_thread_ = NULL; - } - - // The request got killed, we need to stop. - if (!loop_) - return; +void URLRequestFileJob::DidResolve( + bool exists, const file_util::FileInfo& file_info) { +#if defined(OS_WIN) + async_resolver_ = NULL; +#endif // We may have been orphaned... if (!request_) return; - // This is not a file, this is a directory. - if (is_directory_) { - NotifyHeadersComplete(); - return; + is_directory_ = file_info.is_directory; + + int rv = net::OK; + if (!exists) { + rv = net::ERR_FILE_NOT_FOUND; + } else if (!is_directory_) { + rv = stream_.Open(file_path_, true); } - if (is_not_found_) { - // some kind of invalid file URI - NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, - net::ERR_FILE_NOT_FOUND)); + if (rv == net::OK) { + set_expected_content_size(file_info.size); + NotifyHeadersComplete(); } else { - handle_ = CreateFile(file_path_.c_str(), - GENERIC_READ|SYNCHRONIZE, - FILE_SHARE_READ | FILE_SHARE_WRITE, - NULL, - OPEN_EXISTING, - FILE_FLAG_OVERLAPPED | FILE_FLAG_SEQUENTIAL_SCAN, - NULL); - if (handle_ == INVALID_HANDLE_VALUE) { - NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, - WinInetUtil::OSErrorToNetError(GetLastError()))); - } else { - // Get setup for overlapped reads (w/ a manual reset event) - overlapped_.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); - } + NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); } - - NotifyHeadersComplete(); } -void URLRequestFileJob::OnObjectSignaled(HANDLE object) { - DCHECK(overlapped_.hEvent == object); - DCHECK(is_waiting_); - - // We'll resume watching this handle if need be when we do - // another IO. - MessageLoopForIO::current()->WatchObject(object, NULL); - is_waiting_ = false; - - DWORD bytes_read = 0; - if (!GetOverlappedResult(handle_, &overlapped_, &bytes_read, FALSE)) { - DWORD err = GetLastError(); - if (err == ERROR_HANDLE_EOF) { - // successfully read all data - NotifyDone(URLRequestStatus()); - } else { - NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, - WinInetUtil::OSErrorToNetError(err))); - } - } else if (bytes_read) { - overlapped_.Offset += bytes_read; - // clear the IO_PENDING status - SetStatus(URLRequestStatus()); - } else { - // there was no more data so we're done +void URLRequestFileJob::DidRead(int result) { + if (result > 0) { + SetStatus(URLRequestStatus()); // Clear the IO_PENDING status + } else if (result == 0) { NotifyDone(URLRequestStatus()); + } else { + NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); } - NotifyReadComplete(bytes_read); - - Release(); // Balance with AddRef from FillBuf; may destroy |this| + NotifyReadComplete(result); } -bool URLRequestFileJob::IsRedirectResponse(GURL* location, - int* http_status_code) { +bool URLRequestFileJob::IsRedirectResponse( + GURL* location, int* http_status_code) { if (is_directory_) { - // This happens when we discovered the file is a directory, so needs a slash - // at the end of the path. + // This happens when we discovered the file is a directory, so needs a + // slash at the end of the path. std::string new_path = request_->url().path(); new_path.push_back('/'); GURL::Replacements replacements; @@ -302,6 +208,8 @@ bool URLRequestFileJob::IsRedirectResponse(GURL* location, return true; } +#if defined(OS_WIN) + // Follow a Windows shortcut. size_t found; found = file_path_.find_last_of('.'); @@ -321,5 +229,8 @@ bool URLRequestFileJob::IsRedirectResponse(GURL* location, *location = net::FilePathToFileURL(new_path); *http_status_code = 301; return true; +#else + return false; +#endif } diff --git a/net/url_request/url_request_file_job.h b/net/url_request/url_request_file_job.h index 638f4ff..92e326c 100644 --- a/net/url_request/url_request_file_job.h +++ b/net/url_request/url_request_file_job.h @@ -5,16 +5,14 @@ #ifndef NET_URL_REQUEST_URL_REQUEST_FILE_JOB_H_ #define NET_URL_REQUEST_URL_REQUEST_FILE_JOB_H_ -#include "base/lock.h" -#include "base/message_loop.h" -#include "base/thread.h" +#include "base/file_util.h" +#include "net/base/completion_callback.h" +#include "net/base/file_input_stream.h" #include "net/url_request/url_request.h" #include "net/url_request/url_request_job.h" -#include "testing/gtest/include/gtest/gtest_prod.h" // A request job that handles reading file URLs -class URLRequestFileJob : public URLRequestJob, - protected MessageLoopForIO::Watcher { +class URLRequestFileJob : public URLRequestJob { public: URLRequestFileJob(URLRequest* request); virtual ~URLRequestFileJob(); @@ -23,13 +21,8 @@ class URLRequestFileJob : public URLRequestJob, virtual void Kill(); virtual bool ReadRawData(char* buf, int buf_size, int *bytes_read); virtual bool IsRedirectResponse(GURL* location, int* http_status_code); - virtual bool GetMimeType(std::string* mime_type); - // Checks the status of the file. Set is_directory_ and is_not_found_ - // accordingly. Call StartAsync on the main message loop when it's done. - void ResolveFile(); - static URLRequest::ProtocolFactory Factory; protected: @@ -37,37 +30,20 @@ class URLRequestFileJob : public URLRequestJob, std::wstring file_path_; private: - // The net util test wants to run our FileURLToFilePath function. - FRIEND_TEST(NetUtilTest, FileURLConversion); - - void CloseHandles(); - void StartAsync(); - - // MessageLoop::Watcher callback - virtual void OnObjectSignaled(HANDLE object); + void DidResolve(bool exists, const file_util::FileInfo& file_info); + void DidRead(int result); - // We use overlapped reads to ensure that reads from network file systems do - // not hang the application thread. - HANDLE handle_; - OVERLAPPED overlapped_; - bool is_waiting_; // true when waiting for overlapped result - bool is_directory_; // true when the file request is for a direcotry. - bool is_not_found_; // true when the file requested does not exist. + net::CompletionCallbackImpl<URLRequestFileJob> io_callback_; + net::FileInputStream stream_; + bool is_directory_; - // This lock ensure that the network_file_thread is not using the loop_ after - // is has been set to NULL in Kill(). - Lock loop_lock_; +#if defined(OS_WIN) + class AsyncResolver; + friend class AsyncResolver; + scoped_refptr<AsyncResolver> async_resolver_; +#endif - // Main message loop where StartAsync has to be called. - MessageLoop* loop_; - - // Thread used to query the attributes of files on the network. - // We need to do it on a separate thread because it can be really - // slow. - HANDLE network_file_thread_; - - DISALLOW_EVIL_CONSTRUCTORS(URLRequestFileJob); + DISALLOW_COPY_AND_ASSIGN(URLRequestFileJob); }; #endif // NET_URL_REQUEST_URL_REQUEST_FILE_JOB_H_ - diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc index cfffe46..9bf467f 100644 --- a/net/url_request/url_request_unittest.cc +++ b/net/url_request/url_request_unittest.cc @@ -2,13 +2,16 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "net/url_request/url_request_unittest.h" + +#if defined(OS_WIN) #include <windows.h> #include <shlobj.h> +#endif + #include <algorithm> #include <string> -#include "net/url_request/url_request_unittest.h" - #include "base/message_loop.h" #include "base/path_service.h" #include "base/process_util.h" @@ -53,7 +56,7 @@ std::string TestNetResourceProvider(int key) { return "header"; } -} +} // namespace TEST(URLRequestTest, GetTest_NoCache) { TestServer server(L""); @@ -308,7 +311,7 @@ TEST(URLRequestTest, PostFileTest) { std::wstring dir; PathService::Get(base::DIR_EXE, &dir); - _wchdir(dir.c_str()); + file_util::SetCurrentDirectory(dir); std::wstring path; PathService::Get(base::DIR_SOURCE_ROOT, &path); @@ -390,13 +393,13 @@ TEST(URLRequestTest, FileTest) { MessageLoop::current()->Run(); - WIN32_FILE_ATTRIBUTE_DATA data; - GetFileAttributesEx(app_path.c_str(), GetFileExInfoStandard, &data); + int64 file_size; + file_util::GetFileSize(app_path, &file_size); EXPECT_TRUE(!r.is_pending()); EXPECT_EQ(1, d.response_started_count()); EXPECT_FALSE(d.received_data_before_response()); - EXPECT_EQ(d.bytes_received(), data.nFileSizeLow); + EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); } #ifndef NDEBUG DCHECK_EQ(url_request_metrics.object_count,0); @@ -510,6 +513,7 @@ TEST(URLRequestTest, BZip2ContentTest_IncrementalHeader) { EXPECT_EQ(got_content, got_bz2_content); } +#if defined(OS_WIN) TEST(URLRequestTest, ResolveShortcutTest) { std::wstring app_path; PathService::Get(base::DIR_SOURCE_ROOT, &app_path); @@ -580,6 +584,7 @@ TEST(URLRequestTest, ResolveShortcutTest) { DCHECK_EQ(url_request_metrics.object_count,0); #endif } +#endif // defined(OS_WIN) TEST(URLRequestTest, ContentTypeNormalizationTest) { TestServer server(L"net/data/url_request_unittest"); diff --git a/net/url_request/url_request_unittest.h b/net/url_request/url_request_unittest.h index f17e7fc..a7214fd 100644 --- a/net/url_request/url_request_unittest.h +++ b/net/url_request/url_request_unittest.h @@ -11,6 +11,7 @@ #include "base/file_util.h" #include "base/message_loop.h" #include "base/path_service.h" +#include "base/platform_thread.h" #include "base/process_util.h" #include "base/string_util.h" #include "base/thread.h" @@ -297,7 +298,7 @@ class TestServer : public process_util::ProcessFilter { bool success; while ((success = MakeGETRequest("hello.html")) == false && retries > 0) { retries--; - ::Sleep(500); + PlatformThread::Sleep(500); } ASSERT_TRUE(success) << "Webserver not starting properly."; |