| 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" |
| 23 #include "webkit/fileapi/file_system_context.h" | 24 #include "webkit/fileapi/file_system_context.h" |
| 24 #include "webkit/fileapi/file_system_operation.h" | 25 #include "webkit/fileapi/file_system_operation.h" |
| 25 #include "webkit/fileapi/file_system_util.h" | 26 #include "webkit/fileapi/file_system_util.h" |
| 26 | 27 |
| 27 using net::URLRequest; | 28 using net::URLRequest; |
| 28 using net::URLRequestJob; | 29 using net::URLRequestJob; |
| 29 using net::URLRequestStatus; | 30 using net::URLRequestStatus; |
| 30 | 31 |
| 31 namespace fileapi { | 32 namespace fileapi { |
| 32 | 33 |
| 33 static FilePath GetRelativePath(const GURL& url) { | 34 static FilePath GetRelativePath(const GURL& url) { |
| 34 FilePath relative_path; | 35 FilePath relative_path; |
| 35 GURL unused_url; | 36 GURL unused_url; |
| 36 FileSystemType unused_type; | 37 FileSystemType unused_type; |
| 37 CrackFileSystemURL(url, &unused_url, &unused_type, &relative_path); | 38 CrackFileSystemURL(url, &unused_url, &unused_type, &relative_path); |
| 38 return relative_path; | 39 return relative_path; |
| 39 } | 40 } |
| 40 | 41 |
| 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 |
| 41 FileSystemDirURLRequestJob::FileSystemDirURLRequestJob( | 96 FileSystemDirURLRequestJob::FileSystemDirURLRequestJob( |
| 42 URLRequest* request, FileSystemContext* file_system_context, | 97 URLRequest* request, FileSystemContext* file_system_context, |
| 43 scoped_refptr<base::MessageLoopProxy> file_thread_proxy) | 98 scoped_refptr<base::MessageLoopProxy> file_thread_proxy) |
| 44 : URLRequestJob(request), | 99 : URLRequestJob(request), |
| 45 file_system_context_(file_system_context), | 100 file_system_context_(file_system_context), |
| 46 file_thread_proxy_(file_thread_proxy), | 101 file_thread_proxy_(file_thread_proxy), |
| 47 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 102 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 48 } | 103 } |
| 49 | 104 |
| 50 FileSystemDirURLRequestJob::~FileSystemDirURLRequestJob() { | 105 FileSystemDirURLRequestJob::~FileSystemDirURLRequestJob() { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 | 141 |
| 87 void FileSystemDirURLRequestJob::StartAsync() { | 142 void FileSystemDirURLRequestJob::StartAsync() { |
| 88 if (!request_) | 143 if (!request_) |
| 89 return; | 144 return; |
| 90 FileSystemOperationInterface* operation = GetNewOperation(request_->url()); | 145 FileSystemOperationInterface* operation = GetNewOperation(request_->url()); |
| 91 if (!operation) { | 146 if (!operation) { |
| 92 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 147 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, |
| 93 net::ERR_INVALID_URL)); | 148 net::ERR_INVALID_URL)); |
| 94 return; | 149 return; |
| 95 } | 150 } |
| 96 operation->ReadDirectory( | 151 operation->ReadDirectory(request_->url()); |
| 97 request_->url(), | |
| 98 base::Bind(&FileSystemDirURLRequestJob::DidReadDirectory, this)); | |
| 99 } | 152 } |
| 100 | 153 |
| 101 void FileSystemDirURLRequestJob::DidReadDirectory( | 154 void FileSystemDirURLRequestJob::DidReadDirectory( |
| 102 base::PlatformFileError result, | |
| 103 const std::vector<base::FileUtilProxy::Entry>& entries, | 155 const std::vector<base::FileUtilProxy::Entry>& entries, |
| 104 bool has_more) { | 156 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 | |
| 113 if (!request_) | 157 if (!request_) |
| 114 return; | 158 return; |
| 115 | 159 |
| 116 if (data_.empty()) { | 160 if (data_.empty()) { |
| 117 FilePath relative_path = GetRelativePath(request_->url()); | 161 FilePath relative_path = GetRelativePath(request_->url()); |
| 118 #if defined(OS_WIN) | 162 #if defined(OS_WIN) |
| 119 const string16& title = relative_path.value(); | 163 const string16& title = relative_path.value(); |
| 120 #elif defined(OS_POSIX) | 164 #elif defined(OS_POSIX) |
| 121 const string16& title = ASCIIToUTF16("/") + | 165 const string16& title = ASCIIToUTF16("/") + |
| 122 WideToUTF16(base::SysNativeMBToWide(relative_path.value())); | 166 WideToUTF16(base::SysNativeMBToWide(relative_path.value())); |
| 123 #endif | 167 #endif |
| 124 data_.append(net::GetDirectoryListingHeader(title)); | 168 data_.append(net::GetDirectoryListingHeader(title)); |
| 125 } | 169 } |
| 126 | 170 |
| 127 typedef std::vector<base::FileUtilProxy::Entry>::const_iterator EntryIterator; | 171 typedef std::vector<base::FileUtilProxy::Entry>::const_iterator EntryIterator; |
| 128 for (EntryIterator it = entries.begin(); it != entries.end(); ++it) { | 172 for (EntryIterator it = entries.begin(); it != entries.end(); ++it) { |
| 129 #if defined(OS_WIN) | 173 #if defined(OS_WIN) |
| 130 const string16& name = it->name; | 174 const string16& name = it->name; |
| 131 #elif defined(OS_POSIX) | 175 #elif defined(OS_POSIX) |
| 132 const string16& name = | 176 const string16& name = |
| 133 WideToUTF16(base::SysNativeMBToWide(it->name)); | 177 WideToUTF16(base::SysNativeMBToWide(it->name)); |
| 134 #endif | 178 #endif |
| 135 data_.append(net::GetDirectoryListingEntry( | 179 data_.append(net::GetDirectoryListingEntry( |
| 136 name, std::string(), it->is_directory, it->size, | 180 name, std::string(), it->is_directory, it->size, |
| 137 it->last_modified_time)); | 181 it->last_modified_time)); |
| 138 } | 182 } |
| 139 | 183 |
| 140 if (has_more) { | 184 if (has_more) { |
| 141 GetNewOperation(request_->url())->ReadDirectory( | 185 GetNewOperation(request_->url())->ReadDirectory(request_->url()); |
| 142 request_->url(), | |
| 143 base::Bind(&FileSystemDirURLRequestJob::DidReadDirectory, this)); | |
| 144 } else { | 186 } else { |
| 145 set_expected_content_size(data_.size()); | 187 set_expected_content_size(data_.size()); |
| 146 NotifyHeadersComplete(); | 188 NotifyHeadersComplete(); |
| 147 } | 189 } |
| 148 } | 190 } |
| 149 | 191 |
| 150 FileSystemOperationInterface* | 192 FileSystemOperationInterface* |
| 151 FileSystemDirURLRequestJob::GetNewOperation(const GURL& url) { | 193 FileSystemDirURLRequestJob::GetNewOperation(const GURL& url) { |
| 152 return file_system_context_->CreateFileSystemOperation( | 194 return file_system_context_->CreateFileSystemOperation( |
| 153 url, | 195 url, |
| 196 CallbackDispatcher::Create(this), |
| 154 file_thread_proxy_); | 197 file_thread_proxy_); |
| 155 } | 198 } |
| 156 | 199 |
| 157 } // namespace fileapi | 200 } // namespace fileapi |
| OLD | NEW |