| 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 "storage/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" |  | 
|   18 #include "net/base/net_util.h" |   17 #include "net/base/net_util.h" | 
|   19 #include "net/url_request/url_request.h" |   18 #include "net/url_request/url_request.h" | 
|   20 #include "storage/browser/fileapi/file_system_context.h" |   19 #include "storage/browser/fileapi/file_system_context.h" | 
|   21 #include "storage/browser/fileapi/file_system_operation_runner.h" |   20 #include "storage/browser/fileapi/file_system_operation_runner.h" | 
|   22 #include "storage/common/fileapi/directory_entry.h" |   21 #include "storage/common/fileapi/directory_entry.h" | 
|   23 #include "storage/common/fileapi/file_system_util.h" |   22 #include "storage/common/fileapi/file_system_util.h" | 
|   24 #include "url/gurl.h" |   23 #include "url/gurl.h" | 
|   25  |   24  | 
|   26 using net::NetworkDelegate; |   25 using net::NetworkDelegate; | 
|   27 using net::URLRequest; |   26 using net::URLRequest; | 
|   28 using net::URLRequestJob; |   27 using net::URLRequestJob; | 
|   29 using net::URLRequestStatus; |   28 using net::URLRequestStatus; | 
|   30  |   29  | 
|   31 namespace storage { |   30 namespace storage { | 
|   32  |   31  | 
|   33 FileSystemDirURLRequestJob::FileSystemDirURLRequestJob( |   32 FileSystemDirURLRequestJob::FileSystemDirURLRequestJob( | 
|   34     URLRequest* request, |   33     URLRequest* request, | 
|   35     NetworkDelegate* network_delegate, |   34     NetworkDelegate* network_delegate, | 
|   36     const std::string& storage_domain, |   35     const std::string& storage_domain, | 
|   37     FileSystemContext* file_system_context) |   36     FileSystemContext* file_system_context) | 
|   38     : URLRequestJob(request, network_delegate), |   37     : URLRequestJob(request, network_delegate), | 
|   39       storage_domain_(storage_domain), |   38       storage_domain_(storage_domain), | 
|   40       file_system_context_(file_system_context), |   39       file_system_context_(file_system_context), | 
|   41       weak_factory_(this) { |   40       weak_factory_(this) { | 
|   42 } |   41 } | 
|   43  |   42  | 
|   44 FileSystemDirURLRequestJob::~FileSystemDirURLRequestJob() { |   43 FileSystemDirURLRequestJob::~FileSystemDirURLRequestJob() { | 
|   45 } |   44 } | 
|   46  |   45  | 
|   47 bool FileSystemDirURLRequestJob::ReadRawData(net::IOBuffer* dest, |   46 int FileSystemDirURLRequestJob::ReadRawData(net::IOBuffer* dest, | 
|   48                                              int dest_size, |   47                                             int dest_size) { | 
|   49                                              int* bytes_read) { |   48   int count = std::min(dest_size, base::checked_cast<int>(data_.size())); | 
|   50   int count = std::min(dest_size, static_cast<int>(data_.size())); |  | 
|   51   if (count > 0) { |   49   if (count > 0) { | 
|   52     memcpy(dest->data(), data_.data(), count); |   50     memcpy(dest->data(), data_.data(), count); | 
|   53     data_.erase(0, count); |   51     data_.erase(0, count); | 
|   54   } |   52   } | 
|   55   *bytes_read = count; |   53   return count; | 
|   56   return true; |  | 
|   57 } |   54 } | 
|   58  |   55  | 
|   59 void FileSystemDirURLRequestJob::Start() { |   56 void FileSystemDirURLRequestJob::Start() { | 
|   60   base::MessageLoop::current()->PostTask( |   57   base::MessageLoop::current()->PostTask( | 
|   61       FROM_HERE, |   58       FROM_HERE, | 
|   62       base::Bind(&FileSystemDirURLRequestJob::StartAsync, |   59       base::Bind(&FileSystemDirURLRequestJob::StartAsync, | 
|   63                  weak_factory_.GetWeakPtr())); |   60                  weak_factory_.GetWeakPtr())); | 
|   64 } |   61 } | 
|   65  |   62  | 
|   66 void FileSystemDirURLRequestJob::Kill() { |   63 void FileSystemDirURLRequestJob::Kill() { | 
| (...skipping 25 matching lines...) Expand all  Loading... | 
|   92   } |   89   } | 
|   93   if (!file_system_context_->CanServeURLRequest(url_)) { |   90   if (!file_system_context_->CanServeURLRequest(url_)) { | 
|   94     // In incognito mode the API is not usable and there should be no data. |   91     // In incognito mode the API is not usable and there should be no data. | 
|   95     if (url_.is_valid() && VirtualPath::IsRootPath(url_.virtual_path())) { |   92     if (url_.is_valid() && VirtualPath::IsRootPath(url_.virtual_path())) { | 
|   96       // Return an empty directory if the filesystem root is queried. |   93       // Return an empty directory if the filesystem root is queried. | 
|   97       DidReadDirectory(base::File::FILE_OK, |   94       DidReadDirectory(base::File::FILE_OK, | 
|   98                        std::vector<DirectoryEntry>(), |   95                        std::vector<DirectoryEntry>(), | 
|   99                        false); |   96                        false); | 
|  100       return; |   97       return; | 
|  101     } |   98     } | 
|  102     NotifyDone( |   99     NotifyStartError(URLRequestStatus::FromError(net::ERR_FILE_NOT_FOUND)); | 
|  103         URLRequestStatus(URLRequestStatus::FAILED, net::ERR_FILE_NOT_FOUND)); |  | 
|  104     return; |  100     return; | 
|  105   } |  101   } | 
|  106   file_system_context_->operation_runner()->ReadDirectory( |  102   file_system_context_->operation_runner()->ReadDirectory( | 
|  107       url_, |  103       url_, | 
|  108       base::Bind(&FileSystemDirURLRequestJob::DidReadDirectory, this)); |  104       base::Bind(&FileSystemDirURLRequestJob::DidReadDirectory, this)); | 
|  109 } |  105 } | 
|  110  |  106  | 
|  111 void FileSystemDirURLRequestJob::DidAttemptAutoMount(base::File::Error result) { |  107 void FileSystemDirURLRequestJob::DidAttemptAutoMount(base::File::Error result) { | 
|  112   if (result >= 0 && |  108   if (result >= 0 && | 
|  113       file_system_context_->CrackURL(request_->url()).is_valid()) { |  109       file_system_context_->CrackURL(request_->url()).is_valid()) { | 
|  114     StartAsync(); |  110     StartAsync(); | 
|  115   } else { |  111   } else { | 
|  116     NotifyDone( |  112     NotifyStartError(URLRequestStatus::FromError(net::ERR_FILE_NOT_FOUND)); | 
|  117         URLRequestStatus(URLRequestStatus::FAILED, net::ERR_FILE_NOT_FOUND)); |  | 
|  118   } |  113   } | 
|  119 } |  114 } | 
|  120  |  115  | 
|  121 void FileSystemDirURLRequestJob::DidReadDirectory( |  116 void FileSystemDirURLRequestJob::DidReadDirectory( | 
|  122     base::File::Error result, |  117     base::File::Error result, | 
|  123     const std::vector<DirectoryEntry>& entries, |  118     const std::vector<DirectoryEntry>& entries, | 
|  124     bool has_more) { |  119     bool has_more) { | 
|  125   if (result != base::File::FILE_OK) { |  120   if (result != base::File::FILE_OK) { | 
|  126     int rv = net::ERR_FILE_NOT_FOUND; |  121     int rv = net::ERR_FILE_NOT_FOUND; | 
|  127     if (result == base::File::FILE_ERROR_INVALID_URL) |  122     if (result == base::File::FILE_ERROR_INVALID_URL) | 
|  128       rv = net::ERR_INVALID_URL; |  123       rv = net::ERR_INVALID_URL; | 
|  129     NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); |  124     NotifyStartError(URLRequestStatus::FromError(rv)); | 
|  130     return; |  125     return; | 
|  131   } |  126   } | 
|  132  |  127  | 
|  133   if (!request_) |  128   if (!request_) | 
|  134     return; |  129     return; | 
|  135  |  130  | 
|  136   if (data_.empty()) { |  131   if (data_.empty()) { | 
|  137     base::FilePath relative_path = url_.path(); |  132     base::FilePath relative_path = url_.path(); | 
|  138 #if defined(OS_POSIX) |  133 #if defined(OS_POSIX) | 
|  139     relative_path = |  134     relative_path = | 
| (...skipping 28 matching lines...) Expand all  Loading... | 
|  168 } |  163 } | 
|  169  |  164  | 
|  170 void FileSystemDirURLRequestJob::DidGetMetadata( |  165 void FileSystemDirURLRequestJob::DidGetMetadata( | 
|  171     size_t index, |  166     size_t index, | 
|  172     base::File::Error result, |  167     base::File::Error result, | 
|  173     const base::File::Info& file_info) { |  168     const base::File::Info& file_info) { | 
|  174   if (result != base::File::FILE_OK) { |  169   if (result != base::File::FILE_OK) { | 
|  175     int rv = net::ERR_FILE_NOT_FOUND; |  170     int rv = net::ERR_FILE_NOT_FOUND; | 
|  176     if (result == base::File::FILE_ERROR_INVALID_URL) |  171     if (result == base::File::FILE_ERROR_INVALID_URL) | 
|  177       rv = net::ERR_INVALID_URL; |  172       rv = net::ERR_INVALID_URL; | 
|  178     NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); |  173     NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, rv)); | 
|  179   } |  174   } | 
|  180  |  175  | 
|  181   if (!request_) |  176   if (!request_) | 
|  182     return; |  177     return; | 
|  183  |  178  | 
|  184   const DirectoryEntry& entry = entries_[index]; |  179   const DirectoryEntry& entry = entries_[index]; | 
|  185   const base::string16& name = base::FilePath(entry.name).LossyDisplayName(); |  180   const base::string16& name = base::FilePath(entry.name).LossyDisplayName(); | 
|  186   data_.append(net::GetDirectoryListingEntry(name, std::string(), |  181   data_.append(net::GetDirectoryListingEntry(name, std::string(), | 
|  187                                              entry.is_directory, file_info.size, |  182                                              entry.is_directory, file_info.size, | 
|  188                                              file_info.last_modified)); |  183                                              file_info.last_modified)); | 
|  189  |  184  | 
|  190   if (index < entries_.size() - 1) { |  185   if (index < entries_.size() - 1) { | 
|  191     GetMetadata(index + 1); |  186     GetMetadata(index + 1); | 
|  192   } else { |  187   } else { | 
|  193     set_expected_content_size(data_.size()); |  188     set_expected_content_size(data_.size()); | 
|  194     NotifyHeadersComplete(); |  189     NotifyHeadersComplete(); | 
|  195   } |  190   } | 
|  196 } |  191 } | 
|  197  |  192  | 
|  198 }  // namespace storage |  193 }  // namespace storage | 
| OLD | NEW |