| 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/browser/fileapi/file_system_dir_url_request_job.h" | 5 #include "storage/browser/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/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 16 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 18 #include "net/base/net_util.h" | 18 #include "net/base/net_util.h" |
| 19 #include "net/url_request/url_request.h" | 19 #include "net/url_request/url_request.h" |
| 20 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 21 #include "webkit/browser/fileapi/file_system_context.h" | 21 #include "storage/browser/fileapi/file_system_context.h" |
| 22 #include "webkit/browser/fileapi/file_system_operation_runner.h" | 22 #include "storage/browser/fileapi/file_system_operation_runner.h" |
| 23 #include "webkit/browser/fileapi/file_system_url.h" | 23 #include "storage/browser/fileapi/file_system_url.h" |
| 24 #include "webkit/common/fileapi/directory_entry.h" | 24 #include "storage/common/fileapi/directory_entry.h" |
| 25 #include "webkit/common/fileapi/file_system_util.h" | 25 #include "storage/common/fileapi/file_system_util.h" |
| 26 | 26 |
| 27 using net::NetworkDelegate; | 27 using net::NetworkDelegate; |
| 28 using net::URLRequest; | 28 using net::URLRequest; |
| 29 using net::URLRequestJob; | 29 using net::URLRequestJob; |
| 30 using net::URLRequestStatus; | 30 using net::URLRequestStatus; |
| 31 | 31 |
| 32 namespace fileapi { | 32 namespace storage { |
| 33 | 33 |
| 34 FileSystemDirURLRequestJob::FileSystemDirURLRequestJob( | 34 FileSystemDirURLRequestJob::FileSystemDirURLRequestJob( |
| 35 URLRequest* request, | 35 URLRequest* request, |
| 36 NetworkDelegate* network_delegate, | 36 NetworkDelegate* network_delegate, |
| 37 const std::string& storage_domain, | 37 const std::string& storage_domain, |
| 38 FileSystemContext* file_system_context) | 38 FileSystemContext* file_system_context) |
| 39 : URLRequestJob(request, network_delegate), | 39 : URLRequestJob(request, network_delegate), |
| 40 storage_domain_(storage_domain), | 40 storage_domain_(storage_domain), |
| 41 file_system_context_(file_system_context), | 41 file_system_context_(file_system_context), |
| 42 weak_factory_(this) { | 42 weak_factory_(this) { |
| 43 } | 43 } |
| 44 | 44 |
| 45 FileSystemDirURLRequestJob::~FileSystemDirURLRequestJob() { | 45 FileSystemDirURLRequestJob::~FileSystemDirURLRequestJob() { |
| 46 } | 46 } |
| 47 | 47 |
| 48 bool FileSystemDirURLRequestJob::ReadRawData(net::IOBuffer* dest, int dest_size, | 48 bool FileSystemDirURLRequestJob::ReadRawData(net::IOBuffer* dest, |
| 49 int *bytes_read) { | 49 int dest_size, |
| 50 int* bytes_read) { |
| 50 int count = std::min(dest_size, static_cast<int>(data_.size())); | 51 int count = std::min(dest_size, static_cast<int>(data_.size())); |
| 51 if (count > 0) { | 52 if (count > 0) { |
| 52 memcpy(dest->data(), data_.data(), count); | 53 memcpy(dest->data(), data_.data(), count); |
| 53 data_.erase(0, count); | 54 data_.erase(0, count); |
| 54 } | 55 } |
| 55 *bytes_read = count; | 56 *bytes_read = count; |
| 56 return true; | 57 return true; |
| 57 } | 58 } |
| 58 | 59 |
| 59 void FileSystemDirURLRequestJob::Start() { | 60 void FileSystemDirURLRequestJob::Start() { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 87 request_, | 88 request_, |
| 88 storage_domain_, | 89 storage_domain_, |
| 89 base::Bind(&FileSystemDirURLRequestJob::DidAttemptAutoMount, | 90 base::Bind(&FileSystemDirURLRequestJob::DidAttemptAutoMount, |
| 90 weak_factory_.GetWeakPtr())); | 91 weak_factory_.GetWeakPtr())); |
| 91 return; | 92 return; |
| 92 } | 93 } |
| 93 if (!file_system_context_->CanServeURLRequest(url_)) { | 94 if (!file_system_context_->CanServeURLRequest(url_)) { |
| 94 // In incognito mode the API is not usable and there should be no data. | 95 // In incognito mode the API is not usable and there should be no data. |
| 95 if (url_.is_valid() && VirtualPath::IsRootPath(url_.virtual_path())) { | 96 if (url_.is_valid() && VirtualPath::IsRootPath(url_.virtual_path())) { |
| 96 // Return an empty directory if the filesystem root is queried. | 97 // Return an empty directory if the filesystem root is queried. |
| 97 DidReadDirectory(base::File::FILE_OK, | 98 DidReadDirectory( |
| 98 std::vector<DirectoryEntry>(), | 99 base::File::FILE_OK, std::vector<DirectoryEntry>(), false); |
| 99 false); | |
| 100 return; | 100 return; |
| 101 } | 101 } |
| 102 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 102 NotifyDone( |
| 103 net::ERR_FILE_NOT_FOUND)); | 103 URLRequestStatus(URLRequestStatus::FAILED, net::ERR_FILE_NOT_FOUND)); |
| 104 return; | 104 return; |
| 105 } | 105 } |
| 106 file_system_context_->operation_runner()->ReadDirectory( | 106 file_system_context_->operation_runner()->ReadDirectory( |
| 107 url_, | 107 url_, base::Bind(&FileSystemDirURLRequestJob::DidReadDirectory, this)); |
| 108 base::Bind(&FileSystemDirURLRequestJob::DidReadDirectory, this)); | |
| 109 } | 108 } |
| 110 | 109 |
| 111 void FileSystemDirURLRequestJob::DidAttemptAutoMount(base::File::Error result) { | 110 void FileSystemDirURLRequestJob::DidAttemptAutoMount(base::File::Error result) { |
| 112 if (result >= 0 && | 111 if (result >= 0 && |
| 113 file_system_context_->CrackURL(request_->url()).is_valid()) { | 112 file_system_context_->CrackURL(request_->url()).is_valid()) { |
| 114 StartAsync(); | 113 StartAsync(); |
| 115 } else { | 114 } else { |
| 116 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 115 NotifyDone( |
| 117 net::ERR_FILE_NOT_FOUND)); | 116 URLRequestStatus(URLRequestStatus::FAILED, net::ERR_FILE_NOT_FOUND)); |
| 118 } | 117 } |
| 119 } | 118 } |
| 120 | 119 |
| 121 void FileSystemDirURLRequestJob::DidReadDirectory( | 120 void FileSystemDirURLRequestJob::DidReadDirectory( |
| 122 base::File::Error result, | 121 base::File::Error result, |
| 123 const std::vector<DirectoryEntry>& entries, | 122 const std::vector<DirectoryEntry>& entries, |
| 124 bool has_more) { | 123 bool has_more) { |
| 125 if (result != base::File::FILE_OK) { | 124 if (result != base::File::FILE_OK) { |
| 126 int rv = net::ERR_FILE_NOT_FOUND; | 125 int rv = net::ERR_FILE_NOT_FOUND; |
| 127 if (result == base::File::FILE_ERROR_INVALID_URL) | 126 if (result == base::File::FILE_ERROR_INVALID_URL) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 139 relative_path = | 138 relative_path = |
| 140 base::FilePath(FILE_PATH_LITERAL("/") + relative_path.value()); | 139 base::FilePath(FILE_PATH_LITERAL("/") + relative_path.value()); |
| 141 #endif | 140 #endif |
| 142 const base::string16& title = relative_path.LossyDisplayName(); | 141 const base::string16& title = relative_path.LossyDisplayName(); |
| 143 data_.append(net::GetDirectoryListingHeader(title)); | 142 data_.append(net::GetDirectoryListingHeader(title)); |
| 144 } | 143 } |
| 145 | 144 |
| 146 typedef std::vector<DirectoryEntry>::const_iterator EntryIterator; | 145 typedef std::vector<DirectoryEntry>::const_iterator EntryIterator; |
| 147 for (EntryIterator it = entries.begin(); it != entries.end(); ++it) { | 146 for (EntryIterator it = entries.begin(); it != entries.end(); ++it) { |
| 148 const base::string16& name = base::FilePath(it->name).LossyDisplayName(); | 147 const base::string16& name = base::FilePath(it->name).LossyDisplayName(); |
| 149 data_.append(net::GetDirectoryListingEntry( | 148 data_.append(net::GetDirectoryListingEntry(name, |
| 150 name, std::string(), it->is_directory, it->size, | 149 std::string(), |
| 151 it->last_modified_time)); | 150 it->is_directory, |
| 151 it->size, |
| 152 it->last_modified_time)); |
| 152 } | 153 } |
| 153 | 154 |
| 154 if (!has_more) { | 155 if (!has_more) { |
| 155 set_expected_content_size(data_.size()); | 156 set_expected_content_size(data_.size()); |
| 156 NotifyHeadersComplete(); | 157 NotifyHeadersComplete(); |
| 157 } | 158 } |
| 158 } | 159 } |
| 159 | 160 |
| 160 } // namespace fileapi | 161 } // namespace storage |
| OLD | NEW |