diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-24 23:37:04 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-24 23:37:04 +0000 |
commit | 498c1a6b2dc98869e0703127565247830227d075 (patch) | |
tree | 24de83f7cde502e02b2a0f331154c28aee2ee6ac /net/base | |
parent | 0f3cb003bed09985526391ff19f22ef9e0958401 (diff) | |
download | chromium_src-498c1a6b2dc98869e0703127565247830227d075.zip chromium_src-498c1a6b2dc98869e0703127565247830227d075.tar.gz chromium_src-498c1a6b2dc98869e0703127565247830227d075.tar.bz2 |
Clean up some net/ code. Change a number of wstrings to FilePaths.
Review URL: http://codereview.chromium.org/12409
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@5944 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-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 |
7 files changed, 48 insertions, 41 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 |