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, int dest_size, | 46 int FileSystemDirURLRequestJob::ReadRawData(net::IOBuffer* dest, |
48 int *bytes_read) { | 47 int dest_size) { |
49 int count = std::min(dest_size, static_cast<int>(data_.size())); | 48 int count = std::min(dest_size, base::checked_cast<int>(data_.size())); |
50 if (count > 0) { | 49 if (count > 0) { |
51 memcpy(dest->data(), data_.data(), count); | 50 memcpy(dest->data(), data_.data(), count); |
52 data_.erase(0, count); | 51 data_.erase(0, count); |
53 } | 52 } |
54 *bytes_read = count; | 53 return count; |
55 return true; | |
56 } | 54 } |
57 | 55 |
58 void FileSystemDirURLRequestJob::Start() { | 56 void FileSystemDirURLRequestJob::Start() { |
59 base::MessageLoop::current()->PostTask( | 57 base::MessageLoop::current()->PostTask( |
60 FROM_HERE, | 58 FROM_HERE, |
61 base::Bind(&FileSystemDirURLRequestJob::StartAsync, | 59 base::Bind(&FileSystemDirURLRequestJob::StartAsync, |
62 weak_factory_.GetWeakPtr())); | 60 weak_factory_.GetWeakPtr())); |
63 } | 61 } |
64 | 62 |
65 void FileSystemDirURLRequestJob::Kill() { | 63 void FileSystemDirURLRequestJob::Kill() { |
(...skipping 25 matching lines...) Expand all Loading... |
91 } | 89 } |
92 if (!file_system_context_->CanServeURLRequest(url_)) { | 90 if (!file_system_context_->CanServeURLRequest(url_)) { |
93 // 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. |
94 if (url_.is_valid() && VirtualPath::IsRootPath(url_.virtual_path())) { | 92 if (url_.is_valid() && VirtualPath::IsRootPath(url_.virtual_path())) { |
95 // Return an empty directory if the filesystem root is queried. | 93 // Return an empty directory if the filesystem root is queried. |
96 DidReadDirectory(base::File::FILE_OK, | 94 DidReadDirectory(base::File::FILE_OK, |
97 std::vector<DirectoryEntry>(), | 95 std::vector<DirectoryEntry>(), |
98 false); | 96 false); |
99 return; | 97 return; |
100 } | 98 } |
101 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 99 NotifyStartError(URLRequestStatus::FromError(net::ERR_FILE_NOT_FOUND)); |
102 net::ERR_FILE_NOT_FOUND)); | |
103 return; | 100 return; |
104 } | 101 } |
105 file_system_context_->operation_runner()->ReadDirectory( | 102 file_system_context_->operation_runner()->ReadDirectory( |
106 url_, | 103 url_, |
107 base::Bind(&FileSystemDirURLRequestJob::DidReadDirectory, this)); | 104 base::Bind(&FileSystemDirURLRequestJob::DidReadDirectory, this)); |
108 } | 105 } |
109 | 106 |
110 void FileSystemDirURLRequestJob::DidAttemptAutoMount(base::File::Error result) { | 107 void FileSystemDirURLRequestJob::DidAttemptAutoMount(base::File::Error result) { |
111 if (result >= 0 && | 108 if (result >= 0 && |
112 file_system_context_->CrackURL(request_->url()).is_valid()) { | 109 file_system_context_->CrackURL(request_->url()).is_valid()) { |
113 StartAsync(); | 110 StartAsync(); |
114 } else { | 111 } else { |
115 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 112 NotifyStartError(URLRequestStatus::FromError(net::ERR_FILE_NOT_FOUND)); |
116 net::ERR_FILE_NOT_FOUND)); | |
117 } | 113 } |
118 } | 114 } |
119 | 115 |
120 void FileSystemDirURLRequestJob::DidReadDirectory( | 116 void FileSystemDirURLRequestJob::DidReadDirectory( |
121 base::File::Error result, | 117 base::File::Error result, |
122 const std::vector<DirectoryEntry>& entries, | 118 const std::vector<DirectoryEntry>& entries, |
123 bool has_more) { | 119 bool has_more) { |
124 if (result != base::File::FILE_OK) { | 120 if (result != base::File::FILE_OK) { |
125 int rv = net::ERR_FILE_NOT_FOUND; | 121 int rv = net::ERR_FILE_NOT_FOUND; |
126 if (result == base::File::FILE_ERROR_INVALID_URL) | 122 if (result == base::File::FILE_ERROR_INVALID_URL) |
127 rv = net::ERR_INVALID_URL; | 123 rv = net::ERR_INVALID_URL; |
128 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); | 124 NotifyStartError(URLRequestStatus::FromError(rv)); |
129 return; | 125 return; |
130 } | 126 } |
131 | 127 |
132 if (!request_) | 128 if (!request_) |
133 return; | 129 return; |
134 | 130 |
135 if (data_.empty()) { | 131 if (data_.empty()) { |
136 base::FilePath relative_path = url_.path(); | 132 base::FilePath relative_path = url_.path(); |
137 #if defined(OS_POSIX) | 133 #if defined(OS_POSIX) |
138 relative_path = | 134 relative_path = |
(...skipping 11 matching lines...) Expand all Loading... |
150 it->last_modified_time)); | 146 it->last_modified_time)); |
151 } | 147 } |
152 | 148 |
153 if (!has_more) { | 149 if (!has_more) { |
154 set_expected_content_size(data_.size()); | 150 set_expected_content_size(data_.size()); |
155 NotifyHeadersComplete(); | 151 NotifyHeadersComplete(); |
156 } | 152 } |
157 } | 153 } |
158 | 154 |
159 } // namespace storage | 155 } // namespace storage |
OLD | NEW |