diff options
Diffstat (limited to 'net')
-rw-r--r-- | net/base/directory_lister.cc | 11 | ||||
-rw-r--r-- | net/base/directory_lister.h | 14 | ||||
-rw-r--r-- | net/base/directory_lister_unittest.cc | 5 | ||||
-rw-r--r-- | net/base/net_util.cc | 8 | ||||
-rw-r--r-- | net/base/net_util.h | 3 | ||||
-rw-r--r-- | net/base/net_util_posix.cc | 25 | ||||
-rw-r--r-- | net/base/net_util_win.cc | 23 | ||||
-rw-r--r-- | net/url_request/url_request_file_dir_job.cc | 10 | ||||
-rw-r--r-- | net/url_request/url_request_file_dir_job.h | 5 | ||||
-rw-r--r-- | net/url_request/url_request_file_job.cc | 25 | ||||
-rw-r--r-- | net/url_request/url_request_file_job.h | 3 |
11 files changed, 72 insertions, 60 deletions
diff --git a/net/base/directory_lister.cc b/net/base/directory_lister.cc index a04aec2..a5d893a 100644 --- a/net/base/directory_lister.cc +++ b/net/base/directory_lister.cc @@ -32,14 +32,14 @@ class DirectoryDataEvent : public Task { int count, error; }; -DirectoryLister::DirectoryLister(const std::wstring& dir, +DirectoryLister::DirectoryLister(const FilePath& dir, DirectoryListerDelegate* delegate) : dir_(dir), delegate_(delegate), message_loop_(NULL), thread_(NULL), canceled_(false) { - DCHECK(!dir.empty()); + DCHECK(!dir.value().empty()); } DirectoryLister::~DirectoryLister() { @@ -77,18 +77,17 @@ void DirectoryLister::Cancel() { void DirectoryLister::ThreadMain() { DirectoryDataEvent* e = new DirectoryDataEvent(this); - if (!file_util::DirectoryExists(directory())) { + if (!file_util::DirectoryExists(dir_)) { e->error = net::ERR_FILE_NOT_FOUND; message_loop_->PostTask(FROM_HERE, e); Release(); return; } - file_util::FileEnumerator file_enum(directory(), false, + file_util::FileEnumerator file_enum(dir_.ToWStringHack(), false, file_util::FileEnumerator::FILES_AND_DIRECTORIES); - std::wstring filename; - while (!was_canceled() && !(filename = file_enum.Next()).empty()) { + while (!canceled_ && !(file_enum.Next().empty())) { file_enum.GetFindInfo(&e->data[e->count]); if (++e->count == kFilesPerEvent) { diff --git a/net/base/directory_lister.h b/net/base/directory_lister.h index 06d1b37..0608b63 100644 --- a/net/base/directory_lister.h +++ b/net/base/directory_lister.h @@ -7,6 +7,7 @@ #include <string> +#include "base/file_path.h" #include "base/file_util.h" #include "base/platform_thread.h" #include "base/ref_counted.h" @@ -33,7 +34,7 @@ class DirectoryLister : public base::RefCountedThreadSafe<DirectoryLister>, virtual void OnListDone(int error) = 0; }; - DirectoryLister(const std::wstring& dir, DirectoryListerDelegate* delegate); + DirectoryLister(const FilePath& dir, DirectoryListerDelegate* delegate); ~DirectoryLister(); // Call this method to start the directory enumeration thread. @@ -41,31 +42,24 @@ class DirectoryLister : public base::RefCountedThreadSafe<DirectoryLister>, // Call this method to asynchronously stop directory enumeration. The // delegate will receive the OnListDone notification with an error code of - // ERROR_OPERATION_ABORTED. + // net::ERR_ABORTED. void Cancel(); // The delegate pointer may be modified at any time. DirectoryListerDelegate* delegate() const { return delegate_; } void set_delegate(DirectoryListerDelegate* d) { delegate_ = d; } - // Returns the directory being enumerated. - const std::wstring& directory() const { return dir_; } - - // Returns true if the directory enumeration was canceled. - bool was_canceled() const { return canceled_; } - // PlatformThread::Delegate implementation void ThreadMain(); private: friend class DirectoryDataEvent; - friend class ThreadDelegate; void OnReceivedData(const file_util::FileEnumerator::FindInfo* data, int count); void OnDone(int error); - std::wstring dir_; + FilePath dir_; DirectoryListerDelegate* delegate_; MessageLoop* message_loop_; PlatformThreadHandle thread_; diff --git a/net/base/directory_lister_unittest.cc b/net/base/directory_lister_unittest.cc index 8f80466..a0c0c5b 100644 --- a/net/base/directory_lister_unittest.cc +++ b/net/base/directory_lister_unittest.cc @@ -35,7 +35,7 @@ TEST(DirectoryListerTest, BigDirTest) { ListerDelegate delegate; scoped_refptr<net::DirectoryLister> lister = - new net::DirectoryLister(path.ToWStringHack(), &delegate); + new net::DirectoryLister(path, &delegate); lister->Start(); @@ -50,7 +50,7 @@ TEST(DirectoryListerTest, CancelTest) { ListerDelegate delegate; scoped_refptr<net::DirectoryLister> lister = - new net::DirectoryLister(path.ToWStringHack(), &delegate); + new net::DirectoryLister(path, &delegate); lister->Start(); lister->Cancel(); @@ -58,5 +58,4 @@ TEST(DirectoryListerTest, CancelTest) { MessageLoop::current()->Run(); EXPECT_EQ(delegate.error(), net::ERR_ABORTED); - EXPECT_EQ(lister->was_canceled(), true); } diff --git a/net/base/net_util.cc b/net/base/net_util.cc index 9636df5..959ab28 100644 --- a/net/base/net_util.cc +++ b/net/base/net_util.cc @@ -943,4 +943,12 @@ int SetNonBlocking(int fd) { #endif } +// Deprecated. +bool FileURLToFilePath(const GURL& gurl, std::wstring* file_path) { + FilePath path; + bool rv = FileURLToFilePath(gurl, &path); + *file_path = path.ToWStringHack(); + return rv; +} + } // namespace net diff --git a/net/base/net_util.h b/net/base/net_util.h index f8f49b8..fb6841f 100644 --- a/net/base/net_util.h +++ b/net/base/net_util.h @@ -17,6 +17,7 @@ #include "googleurl/src/url_canon.h" #include "googleurl/src/url_parse.h" +class FilePath; class GURL; namespace base { @@ -33,6 +34,8 @@ GURL FilePathToFileURL(const std::wstring& file_path); // file URL must be well-formed (GURL::is_valid() must return true); we don't // handle degenerate cases here. Returns true on success, false if it isn't a // valid file URL. On failure, *file_path will be empty. +bool FileURLToFilePath(const GURL& url, FilePath* file_path); +// Deprecated temporary compatibility function. bool FileURLToFilePath(const GURL& url, std::wstring* file_path); // Return the value of the HTTP response header with name 'name'. 'headers' diff --git a/net/base/net_util_posix.cc b/net/base/net_util_posix.cc index 2554e4b..4bfa1fd 100644 --- a/net/base/net_util_posix.cc +++ b/net/base/net_util_posix.cc @@ -4,42 +4,43 @@ #include "net/base/net_util.h" +#include "base/file_path.h" #include "base/string_util.h" #include "googleurl/src/gurl.h" #include "net/base/escape.h" namespace net { -bool FileURLToFilePath(const GURL& url, std::wstring* file_path) { - file_path->clear(); +bool FileURLToFilePath(const GURL& url, FilePath* path) { + *path = FilePath(); + std::string& file_path_str = const_cast<std::string&>(path->value()); + file_path_str.clear(); if (!url.is_valid()) return false; // Firefox seems to ignore the "host" of a file url if there is one. That is, // file://foo/bar.txt maps to /bar.txt. - std::string path = url.path(); + std::string old_path = url.path(); - if (path.empty()) + if (old_path.empty()) return false; // GURL stores strings as percent-encoded 8-bit, this will undo if possible. - path = UnescapeURLComponent(path, + old_path = UnescapeURLComponent(old_path, UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); // Collapse multiple path slashes into a single path slash. std::string new_path; do { - new_path = path; + new_path = old_path; ReplaceSubstringsAfterOffset(&new_path, 0, "//", "/"); - path.swap(new_path); - } while (new_path != path); + old_path.swap(new_path); + } while (new_path != old_path); - // TODO(tc): This should actually be 8-bit to wide. We may lose data if the - // string isn't UTF-8. - file_path->assign(UTF8ToWide(path)); + file_path_str.assign(old_path); - return !file_path->empty(); + return !file_path_str.empty(); } } // namespace net diff --git a/net/base/net_util_win.cc b/net/base/net_util_win.cc index 7ea217c..6993142 100644 --- a/net/base/net_util_win.cc +++ b/net/base/net_util_win.cc @@ -4,6 +4,7 @@ #include "net/base/net_util.h" +#include "base/file_path.h" #include "base/string_piece.h" #include "base/string_util.h" #include "base/sys_string_conversions.h" @@ -12,8 +13,10 @@ namespace net { -bool FileURLToFilePath(const GURL& url, std::wstring* file_path) { - file_path->clear(); +bool FileURLToFilePath(const GURL& url, FilePath* file_path) { + *file_path = FilePath(); + std::wstring& file_path_str = const_cast<std::wstring&>(file_path->value()); + file_path_str.clear(); if (!url.is_valid()) return false; @@ -49,14 +52,14 @@ bool FileURLToFilePath(const GURL& url, std::wstring* file_path) { // are giving the conversion function a nonempty string, and it may fail if // the given string is not in the current encoding and give us an empty // string back. We detect this and report failure. - *file_path = base::SysNativeMBToWide(path); - return !file_path->empty(); + file_path_str = base::SysNativeMBToWide(path); + return !file_path_str.empty(); } - file_path->assign(UTF8ToWide(path)); + file_path_str.assign(UTF8ToWide(path)); // Now we have an unescaped filename, but are still not sure about its // encoding. For example, each character could be part of a UTF-8 string. - if (file_path->empty() || !IsString8Bit(*file_path)) { + if (file_path_str.empty() || !IsString8Bit(file_path_str)) { // assume our 16-bit encoding is correct if it won't fit into an 8-bit // string return true; @@ -64,23 +67,23 @@ bool FileURLToFilePath(const GURL& url, std::wstring* file_path) { // Convert our narrow string into the native wide path. std::string narrow; - if (!WideToLatin1(*file_path, &narrow)) { + if (!WideToLatin1(file_path_str, &narrow)) { NOTREACHED() << "Should have filtered out non-8-bit strings above."; return false; } if (IsStringUTF8(narrow)) { // Our string actually looks like it could be UTF-8, convert to 8-bit // UTF-8 and then to the corresponding wide string. - *file_path = UTF8ToWide(narrow); + file_path_str = UTF8ToWide(narrow); } else { // Our wide string contains only 8-bit characters and it's not UTF-8, so // we assume it's in the native codepage. - *file_path = base::SysNativeMBToWide(narrow); + file_path_str = base::SysNativeMBToWide(narrow); } // Fail if 8-bit -> wide conversion failed and gave us an empty string back // (we already filtered out empty strings above). - return !file_path->empty(); + return !file_path_str.empty(); } } // namespace net diff --git a/net/url_request/url_request_file_dir_job.cc b/net/url_request/url_request_file_dir_job.cc index 3685107..4f94a42 100644 --- a/net/url_request/url_request_file_dir_job.cc +++ b/net/url_request/url_request_file_dir_job.cc @@ -17,10 +17,9 @@ #endif using std::string; -using std::wstring; URLRequestFileDirJob::URLRequestFileDirJob(URLRequest* request, - const wstring& dir_path) + const FilePath& dir_path) : URLRequestJob(request), dir_path_(dir_path), canceled_(false), @@ -104,7 +103,12 @@ void URLRequestFileDirJob::OnListFile( // We wait to write out the header until we get the first file, so that we // can catch errors from DirectoryLister and show an error page. if (!wrote_header_) { - data_.append(net::GetDirectoryListingHeader(WideToUTF8(dir_path_))); +#if defined(OS_WIN) + const std::string& title = WideToUTF8(dir_path_.value()); +#elif defined(OS_POSIX) + const std::string& title = dir_path_.value(); +#endif + data_.append(net::GetDirectoryListingHeader(title)); wrote_header_ = true; } diff --git a/net/url_request/url_request_file_dir_job.h b/net/url_request/url_request_file_dir_job.h index afbb56b..657a587 100644 --- a/net/url_request/url_request_file_dir_job.h +++ b/net/url_request/url_request_file_dir_job.h @@ -5,6 +5,7 @@ #ifndef NET_URL_REQUEST_URL_REQUEST_FILE_DIR_JOB_H__ #define NET_URL_REQUEST_URL_REQUEST_FILE_DIR_JOB_H__ +#include "base/file_path.h" #include "base/file_util.h" #include "net/base/directory_lister.h" #include "net/url_request/url_request_job.h" @@ -13,7 +14,7 @@ class URLRequestFileDirJob : public URLRequestJob, public net::DirectoryLister::DirectoryListerDelegate { public: - URLRequestFileDirJob(URLRequest* request, const std::wstring& dir_path); + URLRequestFileDirJob(URLRequest* request, const FilePath& dir_path); virtual ~URLRequestFileDirJob(); // URLRequestJob methods: @@ -39,7 +40,7 @@ class URLRequestFileDirJob bool FillReadBuffer(char *buf, int buf_size, int *bytes_read); scoped_refptr<net::DirectoryLister> lister_; - std::wstring dir_path_; + FilePath dir_path_; std::string data_; bool canceled_; diff --git a/net/url_request/url_request_file_job.cc b/net/url_request/url_request_file_job.cc index 5c823d8..3c37468 100644 --- a/net/url_request/url_request_file_job.cc +++ b/net/url_request/url_request_file_job.cc @@ -38,7 +38,7 @@ class URLRequestFileJob::AsyncResolver : : owner_(owner), owner_loop_(MessageLoop::current()) { } - void Resolve(const std::wstring& file_path) { + void Resolve(const FilePath& file_path) { file_util::FileInfo file_info; bool exists = file_util::GetFileInfo(file_path, &file_info); AutoLock locked(lock_); @@ -71,9 +71,9 @@ class URLRequestFileJob::AsyncResolver : // static URLRequestJob* URLRequestFileJob::Factory( URLRequest* request, const std::string& scheme) { - std::wstring file_path; + FilePath file_path; if (net::FileURLToFilePath(request->url(), &file_path)) { - if (file_path[file_path.size() - 1] == file_util::kPathSeparator) { + if (file_util::DirectoryExists(file_path)) { // Only directories have trailing slashes. return new URLRequestFileDirJob(request, file_path); } @@ -102,7 +102,7 @@ URLRequestFileJob::~URLRequestFileJob() { void URLRequestFileJob::Start() { #if defined(OS_WIN) // Resolve UNC paths on a background thread. - if (!file_path_.compare(0, 2, L"\\\\")) { + if (!file_path_.value().compare(0, 2, L"\\\\")) { DCHECK(!async_resolver_); async_resolver_ = new AsyncResolver(this); WorkerPool::PostTask(FROM_HERE, NewRunnableMethod( @@ -154,7 +154,7 @@ bool URLRequestFileJob::ReadRawData( bool URLRequestFileJob::GetMimeType(std::string* mime_type) { DCHECK(request_); - return net::GetMimeTypeFromFile(file_path_, mime_type); + return net::GetMimeTypeFromFile(file_path_.ToWStringHack(), mime_type); } void URLRequestFileJob::DidResolve( @@ -176,7 +176,7 @@ void URLRequestFileJob::DidResolve( int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ | base::PLATFORM_FILE_ASYNC; - rv = stream_.Open(file_path_, flags); + rv = stream_.Open(file_path_.ToWStringHack(), flags); } if (rv == net::OK) { @@ -214,16 +214,15 @@ bool URLRequestFileJob::IsRedirectResponse( } #if defined(OS_WIN) - // Follow a Windows shortcut. - size_t found; - found = file_path_.find_last_of('.'); + std::wstring extension = + file_util::GetFileExtensionFromPath(file_path_.value()); - // We just resolve .lnk file, ignor others. - if (found == std::string::npos || - !LowerCaseEqualsASCII(file_path_.substr(found), ".lnk")) + // Follow a Windows shortcut. + // We just resolve .lnk file, ignore others. + if (!LowerCaseEqualsASCII(extension, "lnk")) return false; - std::wstring new_path = file_path_; + std::wstring new_path = file_path_.value(); bool resolved; resolved = file_util::ResolveShortcut(&new_path); diff --git a/net/url_request/url_request_file_job.h b/net/url_request/url_request_file_job.h index 5d52850..e6c1850 100644 --- a/net/url_request/url_request_file_job.h +++ b/net/url_request/url_request_file_job.h @@ -5,6 +5,7 @@ #ifndef NET_URL_REQUEST_URL_REQUEST_FILE_JOB_H_ #define NET_URL_REQUEST_URL_REQUEST_FILE_JOB_H_ +#include "base/file_path.h" #include "base/file_util.h" #include "net/base/completion_callback.h" #include "net/base/file_stream.h" @@ -27,7 +28,7 @@ class URLRequestFileJob : public URLRequestJob { protected: // The OS-specific full path name of the file - std::wstring file_path_; + FilePath file_path_; private: void DidResolve(bool exists, const file_util::FileInfo& file_info); |