Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "webkit/fileapi/file_system_dir_url_request_job.h" | 5 #include "webkit/fileapi/file_system_dir_url_request_job.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/file_util_proxy.h" | 10 #include "base/file_util_proxy.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/platform_file.h" | 12 #include "base/platform_file.h" |
| 13 #include "base/sys_string_conversions.h" | 13 #include "base/sys_string_conversions.h" |
| 14 #include "base/time.h" | 14 #include "base/time.h" |
| 15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 17 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
| 18 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
| 19 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 20 #include "net/base/net_util.h" | 20 #include "net/base/net_util.h" |
| 21 #include "net/url_request/url_request.h" | 21 #include "net/url_request/url_request.h" |
| 22 #include "webkit/fileapi/file_system_callback_dispatcher.h" | |
| 23 #include "webkit/fileapi/file_system_operation.h" | |
| 22 #include "webkit/fileapi/file_system_path_manager.h" | 24 #include "webkit/fileapi/file_system_path_manager.h" |
| 23 #include "webkit/fileapi/file_system_util.h" | 25 #include "webkit/fileapi/file_system_util.h" |
| 24 | 26 |
| 25 using net::URLRequest; | 27 using net::URLRequest; |
| 26 using net::URLRequestJob; | 28 using net::URLRequestJob; |
| 27 using net::URLRequestStatus; | 29 using net::URLRequestStatus; |
| 28 | 30 |
| 29 namespace fileapi { | 31 namespace fileapi { |
| 30 | 32 |
| 31 FileSystemDirURLRequestJob::FileSystemDirURLRequestJob( | 33 FileSystemDirURLRequestJob::FileSystemDirURLRequestJob( |
| 32 URLRequest* request, FileSystemPathManager* path_manager, | 34 URLRequest* request, FileSystemContext* file_system_context, |
| 33 scoped_refptr<base::MessageLoopProxy> file_thread_proxy) | 35 scoped_refptr<base::MessageLoopProxy> file_thread_proxy) |
| 34 : URLRequestJob(request), | 36 : FileSystemURLRequestJobBase(request, file_system_context, |
| 35 path_manager_(path_manager), | 37 file_thread_proxy), |
| 36 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), | 38 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), |
| 37 ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)), | 39 ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)) { |
| 38 file_thread_proxy_(file_thread_proxy) { | |
| 39 } | 40 } |
| 40 | 41 |
| 41 FileSystemDirURLRequestJob::~FileSystemDirURLRequestJob() { | 42 FileSystemDirURLRequestJob::~FileSystemDirURLRequestJob() { |
| 42 } | 43 } |
| 43 | 44 |
| 44 void FileSystemDirURLRequestJob::Start() { | |
| 45 MessageLoop::current()->PostTask(FROM_HERE, | |
| 46 method_factory_.NewRunnableMethod( | |
| 47 &FileSystemDirURLRequestJob::StartAsync)); | |
| 48 } | |
| 49 | |
| 50 void FileSystemDirURLRequestJob::Kill() { | |
| 51 URLRequestJob::Kill(); | |
| 52 callback_factory_.RevokeAll(); | |
| 53 } | |
| 54 | |
| 55 bool FileSystemDirURLRequestJob::ReadRawData(net::IOBuffer* dest, int dest_size, | 45 bool FileSystemDirURLRequestJob::ReadRawData(net::IOBuffer* dest, int dest_size, |
| 56 int *bytes_read) { | 46 int *bytes_read) { |
| 57 | 47 |
| 58 int count = std::min(dest_size, static_cast<int>(data_.size())); | 48 int count = std::min(dest_size, static_cast<int>(data_.size())); |
| 59 if (count > 0) { | 49 if (count > 0) { |
| 60 memcpy(dest->data(), data_.data(), count); | 50 memcpy(dest->data(), data_.data(), count); |
| 61 data_.erase(0, count); | 51 data_.erase(0, count); |
| 62 } | 52 } |
| 63 *bytes_read = count; | 53 *bytes_read = count; |
| 64 return true; | 54 return true; |
| 65 } | 55 } |
| 66 | 56 |
| 57 void FileSystemDirURLRequestJob::Start() { | |
| 58 MessageLoop::current()->PostTask(FROM_HERE, | |
| 59 method_factory_.NewRunnableMethod( | |
| 60 &FileSystemURLRequestJobBase::StartAsync)); | |
| 61 } | |
| 62 | |
| 63 void FileSystemDirURLRequestJob::Kill() { | |
| 64 URLRequestJob::Kill(); | |
| 65 callback_factory_.RevokeAll(); | |
| 66 } | |
| 67 | |
| 68 void FileSystemDirURLRequestJob::DidGetLocalPath( | |
| 69 const FilePath& local_path) { | |
| 70 relative_file_path_ = local_path; | |
|
ericu
2011/04/19 20:30:49
You're calling it relative_file_path here, but abs
zel
2011/04/19 23:14:00
Yes, good catch. Done.
| |
| 71 base::FileUtilProxy::ReadDirectory(file_thread_proxy_, relative_file_path_, | |
| 72 callback_factory_.NewCallback( | |
| 73 &FileSystemDirURLRequestJob::DidReadDirectory)); | |
| 74 } | |
| 75 | |
| 67 bool FileSystemDirURLRequestJob::GetMimeType(std::string* mime_type) const { | 76 bool FileSystemDirURLRequestJob::GetMimeType(std::string* mime_type) const { |
| 68 *mime_type = "text/html"; | 77 *mime_type = "text/html"; |
| 69 return true; | 78 return true; |
| 70 } | 79 } |
| 71 | 80 |
| 72 bool FileSystemDirURLRequestJob::GetCharset(std::string* charset) { | 81 bool FileSystemDirURLRequestJob::GetCharset(std::string* charset) { |
| 73 *charset = "utf-8"; | 82 *charset = "utf-8"; |
| 74 return true; | 83 return true; |
| 75 } | 84 } |
| 76 | 85 |
| 77 void FileSystemDirURLRequestJob::StartAsync() { | |
| 78 GURL origin_url; | |
| 79 FileSystemType type; | |
| 80 if (!CrackFileSystemURL(request_->url(), &origin_url, &type, | |
| 81 &relative_dir_path_)) { | |
| 82 NotifyFailed(net::ERR_INVALID_URL); | |
| 83 return; | |
| 84 } | |
| 85 | |
| 86 path_manager_->GetFileSystemRootPath( | |
| 87 origin_url, type, false, // create | |
| 88 callback_factory_.NewCallback( | |
| 89 &FileSystemDirURLRequestJob::DidGetRootPath)); | |
| 90 } | |
| 91 | |
| 92 void FileSystemDirURLRequestJob::DidGetRootPath(bool success, | |
| 93 const FilePath& root_path, | |
| 94 const std::string& name) { | |
| 95 if (!success) { | |
| 96 NotifyFailed(net::ERR_FILE_NOT_FOUND); | |
| 97 return; | |
| 98 } | |
| 99 | |
| 100 absolute_dir_path_ = root_path.Append(relative_dir_path_); | |
| 101 | |
| 102 // We assume it's a directory if we've gotten here: either the path | |
| 103 // ends with '/', or FileSystemDirURLRequestJob already statted it and | |
| 104 // found it to be a directory. | |
| 105 base::FileUtilProxy::ReadDirectory(file_thread_proxy_, absolute_dir_path_, | |
| 106 callback_factory_.NewCallback( | |
| 107 &FileSystemDirURLRequestJob::DidReadDirectory)); | |
| 108 } | |
| 109 | |
| 110 void FileSystemDirURLRequestJob::DidReadDirectory( | 86 void FileSystemDirURLRequestJob::DidReadDirectory( |
| 111 base::PlatformFileError error_code, | 87 base::PlatformFileError error_code, |
| 112 const std::vector<base::FileUtilProxy::Entry>& entries) { | 88 const std::vector<base::FileUtilProxy::Entry>& entries) { |
| 113 if (error_code != base::PLATFORM_FILE_OK) { | 89 if (error_code != base::PLATFORM_FILE_OK) { |
| 114 NotifyFailed(error_code); | 90 NotifyFailed(error_code); |
| 115 return; | 91 return; |
| 116 } | 92 } |
| 117 | 93 |
| 118 #if defined(OS_WIN) | 94 #if defined(OS_WIN) |
| 119 const string16& title = relative_dir_path_.value(); | 95 const string16& title = relative_file_path_.value(); |
| 120 #elif defined(OS_POSIX) | 96 #elif defined(OS_POSIX) |
| 121 const string16& title = WideToUTF16( | 97 const string16& title = WideToUTF16( |
| 122 base::SysNativeMBToWide(relative_dir_path_.value())); | 98 base::SysNativeMBToWide(relative_file_path_.value())); |
| 123 #endif | 99 #endif |
| 124 data_.append(net::GetDirectoryListingHeader(ASCIIToUTF16("/") + title)); | 100 data_.append(net::GetDirectoryListingHeader(ASCIIToUTF16("/") + title)); |
| 125 | 101 |
| 126 typedef std::vector<base::FileUtilProxy::Entry>::const_iterator EntryIterator; | 102 typedef std::vector<base::FileUtilProxy::Entry>::const_iterator EntryIterator; |
| 127 for (EntryIterator it = entries.begin(); it != entries.end(); ++it) { | 103 for (EntryIterator it = entries.begin(); it != entries.end(); ++it) { |
| 128 #if defined(OS_WIN) | 104 #if defined(OS_WIN) |
| 129 const string16& name = it->name; | 105 const string16& name = it->name; |
| 130 #elif defined(OS_POSIX) | 106 #elif defined(OS_POSIX) |
| 131 const string16& name = | 107 const string16& name = |
| 132 WideToUTF16(base::SysNativeMBToWide(it->name)); | 108 WideToUTF16(base::SysNativeMBToWide(it->name)); |
| 133 #endif | 109 #endif |
| 134 // TODO(adamk): Add file size? | 110 // TODO(adamk): Add file size? |
| 135 data_.append(net::GetDirectoryListingEntry( | 111 data_.append(net::GetDirectoryListingEntry( |
| 136 name, std::string(), it->is_directory, 0, base::Time())); | 112 name, std::string(), it->is_directory, 0, base::Time())); |
| 137 } | 113 } |
| 138 | 114 |
| 139 set_expected_content_size(data_.size()); | 115 set_expected_content_size(data_.size()); |
| 140 NotifyHeadersComplete(); | 116 NotifyHeadersComplete(); |
| 141 } | 117 } |
| 142 | 118 |
| 143 void FileSystemDirURLRequestJob::NotifyFailed(int rv) { | |
| 144 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); | |
| 145 } | |
| 146 | |
| 147 } // namespace fileapi | 119 } // namespace fileapi |
| OLD | NEW |