| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/file_util_proxy.h" | 11 #include "base/file_util_proxy.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/platform_file.h" | 13 #include "base/platform_file.h" |
| 14 #include "base/sys_string_conversions.h" | 14 #include "base/sys_string_conversions.h" |
| 15 #include "base/time.h" | 15 #include "base/time.h" |
| 16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 18 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
| 19 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
| 20 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 21 #include "net/base/net_util.h" | 21 #include "net/base/net_util.h" |
| 22 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" |
| 23 #include "webkit/fileapi/file_system_callback_dispatcher.h" | |
| 24 #include "webkit/fileapi/file_system_context.h" | 23 #include "webkit/fileapi/file_system_context.h" |
| 25 #include "webkit/fileapi/file_system_operation.h" | 24 #include "webkit/fileapi/file_system_operation.h" |
| 26 #include "webkit/fileapi/file_system_util.h" | 25 #include "webkit/fileapi/file_system_util.h" |
| 27 | 26 |
| 28 using net::URLRequest; | 27 using net::URLRequest; |
| 29 using net::URLRequestJob; | 28 using net::URLRequestJob; |
| 30 using net::URLRequestStatus; | 29 using net::URLRequestStatus; |
| 31 | 30 |
| 32 namespace fileapi { | 31 namespace fileapi { |
| 33 | 32 |
| 34 static FilePath GetRelativePath(const GURL& url) { | 33 static FilePath GetRelativePath(const GURL& url) { |
| 35 FilePath relative_path; | 34 FilePath relative_path; |
| 36 GURL unused_url; | 35 GURL unused_url; |
| 37 FileSystemType unused_type; | 36 FileSystemType unused_type; |
| 38 CrackFileSystemURL(url, &unused_url, &unused_type, &relative_path); | 37 CrackFileSystemURL(url, &unused_url, &unused_type, &relative_path); |
| 39 return relative_path; | 38 return relative_path; |
| 40 } | 39 } |
| 41 | 40 |
| 42 class FileSystemDirURLRequestJob::CallbackDispatcher | |
| 43 : public FileSystemCallbackDispatcher { | |
| 44 public: | |
| 45 // An instance of this class must be created by Create() | |
| 46 // (so that we do not leak ownership). | |
| 47 static scoped_ptr<FileSystemCallbackDispatcher> Create( | |
| 48 FileSystemDirURLRequestJob* job) { | |
| 49 return scoped_ptr<FileSystemCallbackDispatcher>( | |
| 50 new CallbackDispatcher(job)); | |
| 51 } | |
| 52 | |
| 53 // fileapi::FileSystemCallbackDispatcher overrides. | |
| 54 virtual void DidSucceed() OVERRIDE { | |
| 55 NOTREACHED(); | |
| 56 } | |
| 57 | |
| 58 virtual void DidReadMetadata(const base::PlatformFileInfo& file_info, | |
| 59 const FilePath& platform_path) OVERRIDE { | |
| 60 NOTREACHED(); | |
| 61 } | |
| 62 | |
| 63 virtual void DidReadDirectory( | |
| 64 const std::vector<base::FileUtilProxy::Entry>& entries, | |
| 65 bool has_more) OVERRIDE { | |
| 66 job_->DidReadDirectory(entries, has_more); | |
| 67 } | |
| 68 | |
| 69 virtual void DidWrite(int64 bytes, bool complete) OVERRIDE { | |
| 70 NOTREACHED(); | |
| 71 } | |
| 72 | |
| 73 virtual void DidOpenFileSystem(const std::string& name, | |
| 74 const GURL& root_path) OVERRIDE { | |
| 75 NOTREACHED(); | |
| 76 } | |
| 77 | |
| 78 virtual void DidFail(base::PlatformFileError error_code) OVERRIDE { | |
| 79 int rv = net::ERR_FILE_NOT_FOUND; | |
| 80 if (error_code == base::PLATFORM_FILE_ERROR_INVALID_URL) | |
| 81 rv = net::ERR_INVALID_URL; | |
| 82 job_->NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); | |
| 83 } | |
| 84 | |
| 85 private: | |
| 86 explicit CallbackDispatcher(FileSystemDirURLRequestJob* job) : job_(job) { | |
| 87 DCHECK(job_); | |
| 88 } | |
| 89 | |
| 90 // TODO(adamk): Get rid of the need for refcounting here by | |
| 91 // allowing FileSystemOperations to be cancelled. | |
| 92 scoped_refptr<FileSystemDirURLRequestJob> job_; | |
| 93 DISALLOW_COPY_AND_ASSIGN(CallbackDispatcher); | |
| 94 }; | |
| 95 | |
| 96 FileSystemDirURLRequestJob::FileSystemDirURLRequestJob( | 41 FileSystemDirURLRequestJob::FileSystemDirURLRequestJob( |
| 97 URLRequest* request, FileSystemContext* file_system_context, | 42 URLRequest* request, FileSystemContext* file_system_context, |
| 98 scoped_refptr<base::MessageLoopProxy> file_thread_proxy) | 43 scoped_refptr<base::MessageLoopProxy> file_thread_proxy) |
| 99 : URLRequestJob(request), | 44 : URLRequestJob(request), |
| 100 file_system_context_(file_system_context), | 45 file_system_context_(file_system_context), |
| 101 file_thread_proxy_(file_thread_proxy), | 46 file_thread_proxy_(file_thread_proxy), |
| 102 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 47 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 103 } | 48 } |
| 104 | 49 |
| 105 FileSystemDirURLRequestJob::~FileSystemDirURLRequestJob() { | 50 FileSystemDirURLRequestJob::~FileSystemDirURLRequestJob() { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 | 86 |
| 142 void FileSystemDirURLRequestJob::StartAsync() { | 87 void FileSystemDirURLRequestJob::StartAsync() { |
| 143 if (!request_) | 88 if (!request_) |
| 144 return; | 89 return; |
| 145 FileSystemOperationInterface* operation = GetNewOperation(request_->url()); | 90 FileSystemOperationInterface* operation = GetNewOperation(request_->url()); |
| 146 if (!operation) { | 91 if (!operation) { |
| 147 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 92 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, |
| 148 net::ERR_INVALID_URL)); | 93 net::ERR_INVALID_URL)); |
| 149 return; | 94 return; |
| 150 } | 95 } |
| 151 operation->ReadDirectory(request_->url()); | 96 operation->ReadDirectory( |
| 97 request_->url(), |
| 98 base::Bind(&FileSystemDirURLRequestJob::DidReadDirectory, this)); |
| 152 } | 99 } |
| 153 | 100 |
| 154 void FileSystemDirURLRequestJob::DidReadDirectory( | 101 void FileSystemDirURLRequestJob::DidReadDirectory( |
| 102 base::PlatformFileError result, |
| 155 const std::vector<base::FileUtilProxy::Entry>& entries, | 103 const std::vector<base::FileUtilProxy::Entry>& entries, |
| 156 bool has_more) { | 104 bool has_more) { |
| 105 if (result != base::PLATFORM_FILE_OK) { |
| 106 int rv = net::ERR_FILE_NOT_FOUND; |
| 107 if (result == base::PLATFORM_FILE_ERROR_INVALID_URL) |
| 108 rv = net::ERR_INVALID_URL; |
| 109 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); |
| 110 return; |
| 111 } |
| 112 |
| 157 if (!request_) | 113 if (!request_) |
| 158 return; | 114 return; |
| 159 | 115 |
| 160 if (data_.empty()) { | 116 if (data_.empty()) { |
| 161 FilePath relative_path = GetRelativePath(request_->url()); | 117 FilePath relative_path = GetRelativePath(request_->url()); |
| 162 #if defined(OS_WIN) | 118 #if defined(OS_WIN) |
| 163 const string16& title = relative_path.value(); | 119 const string16& title = relative_path.value(); |
| 164 #elif defined(OS_POSIX) | 120 #elif defined(OS_POSIX) |
| 165 const string16& title = ASCIIToUTF16("/") + | 121 const string16& title = ASCIIToUTF16("/") + |
| 166 WideToUTF16(base::SysNativeMBToWide(relative_path.value())); | 122 WideToUTF16(base::SysNativeMBToWide(relative_path.value())); |
| 167 #endif | 123 #endif |
| 168 data_.append(net::GetDirectoryListingHeader(title)); | 124 data_.append(net::GetDirectoryListingHeader(title)); |
| 169 } | 125 } |
| 170 | 126 |
| 171 typedef std::vector<base::FileUtilProxy::Entry>::const_iterator EntryIterator; | 127 typedef std::vector<base::FileUtilProxy::Entry>::const_iterator EntryIterator; |
| 172 for (EntryIterator it = entries.begin(); it != entries.end(); ++it) { | 128 for (EntryIterator it = entries.begin(); it != entries.end(); ++it) { |
| 173 #if defined(OS_WIN) | 129 #if defined(OS_WIN) |
| 174 const string16& name = it->name; | 130 const string16& name = it->name; |
| 175 #elif defined(OS_POSIX) | 131 #elif defined(OS_POSIX) |
| 176 const string16& name = | 132 const string16& name = |
| 177 WideToUTF16(base::SysNativeMBToWide(it->name)); | 133 WideToUTF16(base::SysNativeMBToWide(it->name)); |
| 178 #endif | 134 #endif |
| 179 data_.append(net::GetDirectoryListingEntry( | 135 data_.append(net::GetDirectoryListingEntry( |
| 180 name, std::string(), it->is_directory, it->size, | 136 name, std::string(), it->is_directory, it->size, |
| 181 it->last_modified_time)); | 137 it->last_modified_time)); |
| 182 } | 138 } |
| 183 | 139 |
| 184 if (has_more) { | 140 if (has_more) { |
| 185 GetNewOperation(request_->url())->ReadDirectory(request_->url()); | 141 GetNewOperation(request_->url())->ReadDirectory( |
| 142 request_->url(), |
| 143 base::Bind(&FileSystemDirURLRequestJob::DidReadDirectory, this)); |
| 186 } else { | 144 } else { |
| 187 set_expected_content_size(data_.size()); | 145 set_expected_content_size(data_.size()); |
| 188 NotifyHeadersComplete(); | 146 NotifyHeadersComplete(); |
| 189 } | 147 } |
| 190 } | 148 } |
| 191 | 149 |
| 192 FileSystemOperationInterface* | 150 FileSystemOperationInterface* |
| 193 FileSystemDirURLRequestJob::GetNewOperation(const GURL& url) { | 151 FileSystemDirURLRequestJob::GetNewOperation(const GURL& url) { |
| 194 return file_system_context_->CreateFileSystemOperation( | 152 return file_system_context_->CreateFileSystemOperation( |
| 195 url, | 153 url, |
| 196 CallbackDispatcher::Create(this), | |
| 197 file_thread_proxy_); | 154 file_thread_proxy_); |
| 198 } | 155 } |
| 199 | 156 |
| 200 } // namespace fileapi | 157 } // namespace fileapi |
| OLD | NEW |