Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/browser/file_url_loader_factory.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | #include <string> |
| 9 | #include <utility> |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 10 | #include <vector> |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 11 | |
| 12 | #include "base/bind.h" |
| 13 | #include "base/files/file.h" |
| 14 | #include "base/files/file_path.h" |
| 15 | #include "base/files/file_util.h" |
| 16 | #include "base/macros.h" |
| 17 | #include "base/numerics/safe_conversions.h" |
| 18 | #include "base/strings/string16.h" |
| 19 | #include "base/strings/string_util.h" |
| 20 | #include "base/strings/sys_string_conversions.h" |
| 21 | #include "base/strings/utf_string_conversions.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 22 | #include "base/task/post_task.h" |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 23 | #include "base/time/time.h" |
| 24 | #include "build/build_config.h" |
| 25 | #include "content/public/browser/content_browser_client.h" |
Ken Rockot | 6414c4d9 | 2017-11-08 19:58:32 | [diff] [blame] | 26 | #include "content/public/browser/file_url_loader.h" |
Matt Menke | fcbb1bd7 | 2018-01-31 21:53:12 | [diff] [blame] | 27 | #include "content/public/common/content_client.h" |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 28 | #include "mojo/public/cpp/bindings/strong_binding.h" |
| 29 | #include "mojo/public/cpp/system/data_pipe.h" |
| 30 | #include "mojo/public/cpp/system/file_data_pipe_producer.h" |
| 31 | #include "mojo/public/cpp/system/string_data_pipe_producer.h" |
| 32 | #include "net/base/directory_lister.h" |
| 33 | #include "net/base/directory_listing.h" |
| 34 | #include "net/base/filename_util.h" |
| 35 | #include "net/base/mime_sniffer.h" |
| 36 | #include "net/base/mime_util.h" |
| 37 | #include "net/base/net_errors.h" |
| 38 | #include "net/http/http_byte_range.h" |
| 39 | #include "net/http/http_util.h" |
| 40 | #include "net/url_request/redirect_info.h" |
John Abd-El-Malek | 1df6179 | 2018-01-12 20:40:45 | [diff] [blame] | 41 | #include "services/network/public/cpp/resource_request.h" |
Ken Rockot | 54311e6 | 2018-02-10 19:01:52 | [diff] [blame] | 42 | #include "services/network/public/mojom/url_loader.mojom.h" |
Chris Mumford | b5df8ef2 | 2017-12-20 17:47:42 | [diff] [blame] | 43 | #include "storage/common/fileapi/file_system_util.h" |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 44 | #include "url/gurl.h" |
| 45 | |
| 46 | #if defined(OS_WIN) |
| 47 | #include "base/win/shortcut.h" |
| 48 | #endif |
| 49 | |
| 50 | namespace content { |
| 51 | namespace { |
| 52 | |
| 53 | constexpr size_t kDefaultFileUrlPipeSize = 65536; |
| 54 | |
| 55 | // Because this makes things simpler. |
| 56 | static_assert(kDefaultFileUrlPipeSize >= net::kMaxBytesToSniff, |
| 57 | "Default file data pipe size must be at least as large as a MIME-" |
| 58 | "type sniffing buffer."); |
| 59 | |
Ken Rockot | 6414c4d9 | 2017-11-08 19:58:32 | [diff] [blame] | 60 | // Policy to control how a FileURLLoader will handle directory URLs. |
| 61 | enum class DirectoryLoadingPolicy { |
| 62 | // File paths which refer to directories are allowed and will load as an |
| 63 | // HTML directory listing. |
| 64 | kRespondWithListing, |
| 65 | |
| 66 | // File paths which refer to directories are treated as non-existent and |
| 67 | // will result in FILE_NOT_FOUND errors. |
| 68 | kFail, |
| 69 | }; |
| 70 | |
| 71 | // Policy to control whether or not file access constraints imposed by content |
| 72 | // or its embedder should be honored by a FileURLLoader. |
| 73 | enum class FileAccessPolicy { |
| 74 | // Enforces file acccess policy defined by content and/or its embedder. |
| 75 | kRestricted, |
| 76 | |
| 77 | // Ignores file access policy, allowing contents to be loaded from any |
| 78 | // resolvable file path given. |
| 79 | kUnrestricted, |
| 80 | }; |
| 81 | |
| 82 | // Policy to control whether or not a FileURLLoader should follow filesystem |
| 83 | // links (e.g. Windows shortcuts) where applicable. |
| 84 | enum class LinkFollowingPolicy { |
| 85 | kFollow, |
| 86 | kDoNotFollow, |
| 87 | }; |
| 88 | |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 89 | GURL AppendUrlSeparator(const GURL& url) { |
| 90 | std::string new_path = url.path() + '/'; |
| 91 | GURL::Replacements replacements; |
| 92 | replacements.SetPathStr(new_path); |
| 93 | return url.ReplaceComponents(replacements); |
| 94 | } |
| 95 | |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 96 | class FileURLDirectoryLoader |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 97 | : public network::mojom::URLLoader, |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 98 | public net::DirectoryLister::DirectoryListerDelegate { |
| 99 | public: |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 100 | static void CreateAndStart( |
| 101 | const base::FilePath& profile_path, |
| 102 | const network::ResourceRequest& request, |
| 103 | network::mojom::URLLoaderRequest loader, |
| 104 | network::mojom::URLLoaderClientPtrInfo client_info, |
| 105 | std::unique_ptr<FileURLLoaderObserver> observer, |
| 106 | scoped_refptr<net::HttpResponseHeaders> response_headers) { |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 107 | // Owns itself. Will live as long as its URLLoader and URLLoaderClientPtr |
| 108 | // bindings are alive - essentially until either the client gives up or all |
| 109 | // file data has been sent to it. |
| 110 | auto* file_url_loader = new FileURLDirectoryLoader; |
| 111 | file_url_loader->Start(profile_path, request, std::move(loader), |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 112 | std::move(client_info), std::move(observer), |
| 113 | std::move(response_headers)); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 114 | } |
| 115 | |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 116 | // network::mojom::URLLoader: |
Jun Cai | 605ff0e7 | 2018-06-12 22:29:20 | [diff] [blame] | 117 | void FollowRedirect(const base::Optional<std::vector<std::string>>& |
| 118 | to_be_removed_request_headers, |
| 119 | const base::Optional<net::HttpRequestHeaders>& |
Chong Zhang | f19dde9 | 2018-05-23 04:33:59 | [diff] [blame] | 120 | modified_request_headers) override {} |
arthursonzogni | 2e1524a7 | 2017-12-18 16:53:26 | [diff] [blame] | 121 | void ProceedWithResponse() override { NOTREACHED(); } |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 122 | void SetPriority(net::RequestPriority priority, |
| 123 | int32_t intra_priority_value) override {} |
| 124 | void PauseReadingBodyFromNet() override {} |
| 125 | void ResumeReadingBodyFromNet() override {} |
| 126 | |
| 127 | private: |
| 128 | FileURLDirectoryLoader() : binding_(this) {} |
| 129 | ~FileURLDirectoryLoader() override = default; |
| 130 | |
| 131 | void Start(const base::FilePath& profile_path, |
John Abd-El-Malek | 1df6179 | 2018-01-12 20:40:45 | [diff] [blame] | 132 | const network::ResourceRequest& request, |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 133 | network::mojom::URLLoaderRequest loader, |
| 134 | network::mojom::URLLoaderClientPtrInfo client_info, |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 135 | std::unique_ptr<content::FileURLLoaderObserver> observer, |
| 136 | scoped_refptr<net::HttpResponseHeaders> response_headers) { |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 137 | binding_.Bind(std::move(loader)); |
| 138 | binding_.set_connection_error_handler(base::BindOnce( |
| 139 | &FileURLDirectoryLoader::OnConnectionError, base::Unretained(this))); |
| 140 | |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 141 | network::mojom::URLLoaderClientPtr client; |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 142 | client.Bind(std::move(client_info)); |
| 143 | |
| 144 | if (!net::FileURLToFilePath(request.url, &path_)) { |
Takashi Toyoshima | aa27866 | 2017-11-20 11:11:26 | [diff] [blame] | 145 | client->OnComplete(network::URLLoaderCompletionStatus(net::ERR_FAILED)); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 146 | return; |
| 147 | } |
| 148 | |
| 149 | base::File::Info info; |
| 150 | if (!base::GetFileInfo(path_, &info) || !info.is_directory) { |
Takashi Toyoshima | aa27866 | 2017-11-20 11:11:26 | [diff] [blame] | 151 | client->OnComplete( |
| 152 | network::URLLoaderCompletionStatus(net::ERR_FILE_NOT_FOUND)); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 153 | return; |
| 154 | } |
| 155 | |
| 156 | if (!GetContentClient()->browser()->IsFileAccessAllowed( |
| 157 | path_, base::MakeAbsoluteFilePath(path_), profile_path)) { |
Takashi Toyoshima | aa27866 | 2017-11-20 11:11:26 | [diff] [blame] | 158 | client->OnComplete( |
| 159 | network::URLLoaderCompletionStatus(net::ERR_ACCESS_DENIED)); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 160 | return; |
| 161 | } |
| 162 | |
| 163 | mojo::DataPipe pipe(kDefaultFileUrlPipeSize); |
| 164 | if (!pipe.consumer_handle.is_valid()) { |
Takashi Toyoshima | aa27866 | 2017-11-20 11:11:26 | [diff] [blame] | 165 | client->OnComplete(network::URLLoaderCompletionStatus(net::ERR_FAILED)); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 166 | return; |
| 167 | } |
| 168 | |
John Abd-El-Malek | 4624803 | 2018-01-17 19:11:23 | [diff] [blame] | 169 | network::ResourceResponseHead head; |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 170 | head.mime_type = "text/html"; |
| 171 | head.charset = "utf-8"; |
Marijn Kruisselbrink | 9ebd7ba | 2018-06-11 23:18:04 | [diff] [blame] | 172 | client->OnReceiveResponse(head); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 173 | client->OnStartLoadingResponseBody(std::move(pipe.consumer_handle)); |
| 174 | client_ = std::move(client); |
| 175 | |
| 176 | lister_ = std::make_unique<net::DirectoryLister>(path_, this); |
| 177 | lister_->Start(); |
| 178 | |
| 179 | data_producer_ = std::make_unique<mojo::StringDataPipeProducer>( |
| 180 | std::move(pipe.producer_handle)); |
| 181 | } |
| 182 | |
| 183 | void OnConnectionError() { |
| 184 | binding_.Close(); |
| 185 | MaybeDeleteSelf(); |
| 186 | } |
| 187 | |
| 188 | void MaybeDeleteSelf() { |
| 189 | if (!binding_.is_bound() && !client_.is_bound() && !lister_) |
| 190 | delete this; |
| 191 | } |
| 192 | |
| 193 | // net::DirectoryLister::DirectoryListerDelegate: |
| 194 | void OnListFile( |
| 195 | const net::DirectoryLister::DirectoryListerData& data) override { |
| 196 | if (!wrote_header_) { |
| 197 | wrote_header_ = true; |
| 198 | |
| 199 | #if defined(OS_WIN) |
| 200 | const base::string16& title = path_.value(); |
Fabrice de Gans-Riberi | 91fa3505 | 2018-05-07 19:33:48 | [diff] [blame] | 201 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 202 | const base::string16& title = |
| 203 | base::WideToUTF16(base::SysNativeMBToWide(path_.value())); |
| 204 | #endif |
| 205 | pending_data_.append(net::GetDirectoryListingHeader(title)); |
| 206 | |
| 207 | // If not a top-level directory, add a link to the parent directory. To |
| 208 | // figure this out, first normalize |path_| by stripping trailing |
| 209 | // separators. Then compare the result to its DirName(). For the top-level |
| 210 | // directory, e.g. "/" or "c:\\", the normalized path is equal to its own |
| 211 | // DirName(). |
| 212 | base::FilePath stripped_path = path_.StripTrailingSeparators(); |
| 213 | if (stripped_path != stripped_path.DirName()) |
| 214 | pending_data_.append(net::GetParentDirectoryLink()); |
| 215 | } |
| 216 | |
| 217 | // Skip current / parent links from the listing. |
| 218 | base::FilePath filename = data.info.GetName(); |
| 219 | if (filename.value() != base::FilePath::kCurrentDirectory && |
| 220 | filename.value() != base::FilePath::kParentDirectory) { |
| 221 | #if defined(OS_WIN) |
| 222 | std::string raw_bytes; // Empty on Windows means UTF-8 encoded name. |
Fabrice de Gans-Riberi | 91fa3505 | 2018-05-07 19:33:48 | [diff] [blame] | 223 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 224 | const std::string& raw_bytes = filename.value(); |
| 225 | #endif |
| 226 | pending_data_.append(net::GetDirectoryListingEntry( |
| 227 | filename.LossyDisplayName(), raw_bytes, data.info.IsDirectory(), |
| 228 | data.info.GetSize(), data.info.GetLastModifiedTime())); |
| 229 | } |
| 230 | |
| 231 | MaybeTransferPendingData(); |
| 232 | } |
| 233 | |
| 234 | void OnListDone(int error) override { |
| 235 | listing_result_ = error; |
| 236 | lister_.reset(); |
| 237 | MaybeDeleteSelf(); |
| 238 | } |
| 239 | |
| 240 | void MaybeTransferPendingData() { |
| 241 | if (transfer_in_progress_) |
| 242 | return; |
| 243 | |
| 244 | transfer_in_progress_ = true; |
| 245 | data_producer_->Write(pending_data_, |
Daniel Murphy | 0ecba61 | 2018-02-07 20:56:18 | [diff] [blame] | 246 | mojo::StringDataPipeProducer::AsyncWritingMode:: |
| 247 | STRING_MAY_BE_INVALIDATED_BEFORE_COMPLETION, |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 248 | base::BindOnce(&FileURLDirectoryLoader::OnDataWritten, |
| 249 | base::Unretained(this))); |
| 250 | // The producer above will have already copied any parts of |pending_data_| |
| 251 | // that couldn't be written immediately, so we can wipe it out here to begin |
| 252 | // accumulating more data. |
| 253 | pending_data_.clear(); |
| 254 | } |
| 255 | |
| 256 | void OnDataWritten(MojoResult result) { |
| 257 | transfer_in_progress_ = false; |
| 258 | |
| 259 | int completion_status; |
| 260 | if (result == MOJO_RESULT_OK) { |
| 261 | if (!pending_data_.empty()) { |
| 262 | // Keep flushing the data buffer as long as it's non-empty and pipe |
| 263 | // writes are succeeding. |
| 264 | MaybeTransferPendingData(); |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | // If there's no pending data but the lister is still active, we simply |
| 269 | // wait for more listing results. |
| 270 | if (lister_) |
| 271 | return; |
| 272 | |
| 273 | // At this point we know the listing is complete and all available data |
| 274 | // has been transferred. We inherit the status of the listing operation. |
| 275 | completion_status = listing_result_; |
| 276 | } else { |
| 277 | completion_status = net::ERR_FAILED; |
| 278 | } |
| 279 | |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 280 | // All the data has been written now. Close the data pipe. The consumer will |
| 281 | // be notified that there will be no more data to read from now. |
| 282 | data_producer_.reset(); |
| 283 | |
Takashi Toyoshima | aa27866 | 2017-11-20 11:11:26 | [diff] [blame] | 284 | client_->OnComplete(network::URLLoaderCompletionStatus(completion_status)); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 285 | client_.reset(); |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 286 | |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 287 | MaybeDeleteSelf(); |
| 288 | } |
| 289 | |
| 290 | base::FilePath path_; |
| 291 | std::unique_ptr<net::DirectoryLister> lister_; |
| 292 | bool wrote_header_ = false; |
| 293 | int listing_result_; |
| 294 | |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 295 | mojo::Binding<network::mojom::URLLoader> binding_; |
| 296 | network::mojom::URLLoaderClientPtr client_; |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 297 | |
| 298 | std::unique_ptr<mojo::StringDataPipeProducer> data_producer_; |
| 299 | std::string pending_data_; |
| 300 | bool transfer_in_progress_ = false; |
| 301 | |
| 302 | DISALLOW_COPY_AND_ASSIGN(FileURLDirectoryLoader); |
| 303 | }; |
| 304 | |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 305 | class FileURLLoader : public network::mojom::URLLoader { |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 306 | public: |
Ken Rockot | a0dfaca1 | 2018-02-15 07:26:25 | [diff] [blame] | 307 | static void CreateAndStart( |
| 308 | const base::FilePath& profile_path, |
| 309 | const network::ResourceRequest& request, |
| 310 | network::mojom::URLLoaderRequest loader, |
| 311 | network::mojom::URLLoaderClientPtrInfo client_info, |
| 312 | DirectoryLoadingPolicy directory_loading_policy, |
| 313 | FileAccessPolicy file_access_policy, |
| 314 | LinkFollowingPolicy link_following_policy, |
| 315 | std::unique_ptr<FileURLLoaderObserver> observer, |
| 316 | scoped_refptr<net::HttpResponseHeaders> extra_response_headers) { |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 317 | // Owns itself. Will live as long as its URLLoader and URLLoaderClientPtr |
| 318 | // bindings are alive - essentially until either the client gives up or all |
| 319 | // file data has been sent to it. |
| 320 | auto* file_url_loader = new FileURLLoader; |
Ken Rockot | a0dfaca1 | 2018-02-15 07:26:25 | [diff] [blame] | 321 | file_url_loader->Start( |
| 322 | profile_path, request, std::move(loader), std::move(client_info), |
| 323 | directory_loading_policy, file_access_policy, link_following_policy, |
| 324 | std::move(observer), std::move(extra_response_headers)); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 325 | } |
| 326 | |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 327 | // network::mojom::URLLoader: |
Jun Cai | 605ff0e7 | 2018-06-12 22:29:20 | [diff] [blame] | 328 | void FollowRedirect(const base::Optional<std::vector<std::string>>& |
| 329 | to_be_removed_request_headers, |
| 330 | const base::Optional<net::HttpRequestHeaders>& |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 331 | modified_request_headers) override { |
| 332 | std::unique_ptr<RedirectData> redirect_data = std::move(redirect_data_); |
| 333 | if (redirect_data->is_directory) { |
| 334 | FileURLDirectoryLoader::CreateAndStart( |
| 335 | redirect_data->profile_path, redirect_data->request, |
| 336 | binding_.Unbind(), client_.PassInterface(), |
| 337 | std::move(redirect_data->observer), |
| 338 | std::move(redirect_data->extra_response_headers)); |
| 339 | } else { |
| 340 | FileURLLoader::CreateAndStart( |
| 341 | redirect_data->profile_path, redirect_data->request, |
| 342 | binding_.Unbind(), client_.PassInterface(), |
| 343 | redirect_data->directory_loading_policy, |
| 344 | redirect_data->file_access_policy, |
| 345 | redirect_data->link_following_policy, |
| 346 | std::move(redirect_data->observer), |
| 347 | std::move(redirect_data->extra_response_headers)); |
| 348 | } |
| 349 | MaybeDeleteSelf(); |
| 350 | } |
arthursonzogni | 2e1524a7 | 2017-12-18 16:53:26 | [diff] [blame] | 351 | void ProceedWithResponse() override {} |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 352 | void SetPriority(net::RequestPriority priority, |
| 353 | int32_t intra_priority_value) override {} |
| 354 | void PauseReadingBodyFromNet() override {} |
| 355 | void ResumeReadingBodyFromNet() override {} |
| 356 | |
| 357 | private: |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 358 | // Used to save outstanding redirect data while waiting for FollowRedirect |
| 359 | // to be called. Values default to their most restrictive in case they are |
| 360 | // not set. |
| 361 | struct RedirectData { |
| 362 | bool is_directory = false; |
| 363 | base::FilePath profile_path; |
| 364 | network::ResourceRequest request; |
| 365 | network::mojom::URLLoaderRequest loader; |
| 366 | DirectoryLoadingPolicy directory_loading_policy = |
| 367 | DirectoryLoadingPolicy::kFail; |
| 368 | FileAccessPolicy file_access_policy = FileAccessPolicy::kRestricted; |
| 369 | LinkFollowingPolicy link_following_policy = |
| 370 | LinkFollowingPolicy::kDoNotFollow; |
| 371 | std::unique_ptr<FileURLLoaderObserver> observer; |
| 372 | scoped_refptr<net::HttpResponseHeaders> extra_response_headers; |
| 373 | }; |
| 374 | |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 375 | FileURLLoader() : binding_(this) {} |
| 376 | ~FileURLLoader() override = default; |
| 377 | |
| 378 | void Start(const base::FilePath& profile_path, |
John Abd-El-Malek | 1df6179 | 2018-01-12 20:40:45 | [diff] [blame] | 379 | const network::ResourceRequest& request, |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 380 | network::mojom::URLLoaderRequest loader, |
| 381 | network::mojom::URLLoaderClientPtrInfo client_info, |
Ken Rockot | 6414c4d9 | 2017-11-08 19:58:32 | [diff] [blame] | 382 | DirectoryLoadingPolicy directory_loading_policy, |
| 383 | FileAccessPolicy file_access_policy, |
Chris Mumford | b5df8ef2 | 2017-12-20 17:47:42 | [diff] [blame] | 384 | LinkFollowingPolicy link_following_policy, |
Ken Rockot | a0dfaca1 | 2018-02-15 07:26:25 | [diff] [blame] | 385 | std::unique_ptr<FileURLLoaderObserver> observer, |
| 386 | scoped_refptr<net::HttpResponseHeaders> extra_response_headers) { |
John Abd-El-Malek | 4624803 | 2018-01-17 19:11:23 | [diff] [blame] | 387 | network::ResourceResponseHead head; |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 388 | head.request_start = base::TimeTicks::Now(); |
| 389 | head.response_start = base::TimeTicks::Now(); |
Ken Rockot | a0dfaca1 | 2018-02-15 07:26:25 | [diff] [blame] | 390 | head.headers = extra_response_headers; |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 391 | binding_.Bind(std::move(loader)); |
| 392 | binding_.set_connection_error_handler(base::BindOnce( |
| 393 | &FileURLLoader::OnConnectionError, base::Unretained(this))); |
| 394 | |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 395 | client_.Bind(std::move(client_info)); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 396 | |
| 397 | base::FilePath path; |
| 398 | if (!net::FileURLToFilePath(request.url, &path)) { |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 399 | OnClientComplete(net::ERR_FAILED, std::move(observer)); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 400 | return; |
| 401 | } |
| 402 | |
| 403 | base::File::Info info; |
| 404 | if (!base::GetFileInfo(path, &info)) { |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 405 | OnClientComplete(net::ERR_FILE_NOT_FOUND, std::move(observer)); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 406 | return; |
| 407 | } |
| 408 | |
Ken Rockot | 6414c4d9 | 2017-11-08 19:58:32 | [diff] [blame] | 409 | if (info.is_directory) { |
| 410 | if (directory_loading_policy == DirectoryLoadingPolicy::kFail) { |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 411 | OnClientComplete(net::ERR_FILE_NOT_FOUND, std::move(observer)); |
Ken Rockot | 6414c4d9 | 2017-11-08 19:58:32 | [diff] [blame] | 412 | return; |
| 413 | } |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 414 | |
Ken Rockot | 6414c4d9 | 2017-11-08 19:58:32 | [diff] [blame] | 415 | DCHECK_EQ(directory_loading_policy, |
| 416 | DirectoryLoadingPolicy::kRespondWithListing); |
| 417 | |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 418 | net::RedirectInfo redirect_info; |
| 419 | redirect_info.new_method = "GET"; |
| 420 | redirect_info.status_code = 301; |
| 421 | redirect_info.new_url = path.EndsWithSeparator() |
| 422 | ? request.url |
| 423 | : AppendUrlSeparator(request.url); |
| 424 | head.encoded_data_length = 0; |
Ken Rockot | 6414c4d9 | 2017-11-08 19:58:32 | [diff] [blame] | 425 | |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 426 | redirect_data_ = std::make_unique<RedirectData>(); |
| 427 | redirect_data_->is_directory = true; |
| 428 | redirect_data_->profile_path = std::move(profile_path); |
| 429 | redirect_data_->request = request; |
| 430 | redirect_data_->directory_loading_policy = directory_loading_policy; |
| 431 | redirect_data_->file_access_policy = file_access_policy; |
| 432 | redirect_data_->link_following_policy = link_following_policy; |
| 433 | redirect_data_->request.url = redirect_info.new_url; |
| 434 | redirect_data_->observer = std::move(observer); |
| 435 | redirect_data_->extra_response_headers = |
| 436 | std::move(extra_response_headers); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 437 | |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 438 | client_->OnReceiveRedirect(redirect_info, head); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 439 | return; |
| 440 | } |
| 441 | |
| 442 | #if defined(OS_WIN) |
| 443 | base::FilePath shortcut_target; |
Ken Rockot | 6414c4d9 | 2017-11-08 19:58:32 | [diff] [blame] | 444 | if (link_following_policy == LinkFollowingPolicy::kFollow && |
| 445 | base::LowerCaseEqualsASCII(path.Extension(), ".lnk") && |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 446 | base::win::ResolveShortcut(path, &shortcut_target, nullptr)) { |
| 447 | // Follow Windows shortcuts |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 448 | redirect_data_ = std::make_unique<RedirectData>(); |
| 449 | if (!base::GetFileInfo(shortcut_target, &info)) { |
| 450 | OnClientComplete(net::ERR_FILE_NOT_FOUND, std::move(observer)); |
| 451 | return; |
| 452 | } |
| 453 | |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 454 | GURL new_url = net::FilePathToFileURL(shortcut_target); |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 455 | if (info.is_directory && !path.EndsWithSeparator()) |
| 456 | new_url = AppendUrlSeparator(new_url); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 457 | |
| 458 | net::RedirectInfo redirect_info; |
| 459 | redirect_info.new_method = "GET"; |
| 460 | redirect_info.status_code = 301; |
| 461 | redirect_info.new_url = new_url; |
| 462 | head.encoded_data_length = 0; |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 463 | |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 464 | redirect_data_->is_directory = info.is_directory; |
| 465 | redirect_data_->profile_path = std::move(profile_path); |
| 466 | redirect_data_->request = request; |
| 467 | redirect_data_->directory_loading_policy = directory_loading_policy; |
| 468 | redirect_data_->file_access_policy = file_access_policy; |
| 469 | redirect_data_->link_following_policy = link_following_policy; |
| 470 | redirect_data_->request.url = redirect_info.new_url; |
| 471 | redirect_data_->observer = std::move(observer); |
| 472 | redirect_data_->extra_response_headers = |
| 473 | std::move(extra_response_headers); |
| 474 | |
| 475 | client_->OnReceiveRedirect(redirect_info, head); |
| 476 | return; |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 477 | } |
| 478 | #endif // defined(OS_WIN) |
| 479 | |
Ken Rockot | 6414c4d9 | 2017-11-08 19:58:32 | [diff] [blame] | 480 | if (file_access_policy == FileAccessPolicy::kRestricted && |
| 481 | !GetContentClient()->browser()->IsFileAccessAllowed( |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 482 | path, base::MakeAbsoluteFilePath(path), profile_path)) { |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 483 | OnClientComplete(net::ERR_ACCESS_DENIED, std::move(observer)); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 484 | return; |
| 485 | } |
| 486 | |
| 487 | mojo::DataPipe pipe(kDefaultFileUrlPipeSize); |
| 488 | if (!pipe.consumer_handle.is_valid()) { |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 489 | OnClientComplete(net::ERR_FAILED, std::move(observer)); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 490 | return; |
| 491 | } |
| 492 | |
| 493 | // Should never be possible for this to be a directory. If FileURLLoader |
| 494 | // is used to respond to a directory request, it must be because the URL |
| 495 | // path didn't have a trailing path separator. In that case we finish with |
| 496 | // a redirect above which will in turn be handled by FileURLDirectoryLoader. |
| 497 | DCHECK(!info.is_directory); |
Chris Mumford | b5df8ef2 | 2017-12-20 17:47:42 | [diff] [blame] | 498 | if (observer) |
| 499 | observer->OnStart(); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 500 | |
| 501 | base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 502 | if (!file.IsValid()) { |
Mikhail Atuchin | 9af3ff8c | 2018-04-19 21:15:10 | [diff] [blame] | 503 | if (observer) { |
| 504 | observer->OnBytesRead(nullptr, 0u, file.error_details()); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 505 | observer->OnDoneReading(); |
Mikhail Atuchin | 9af3ff8c | 2018-04-19 21:15:10 | [diff] [blame] | 506 | } |
| 507 | net::Error net_error = net::FileErrorToNetError(file.error_details()); |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 508 | client_->OnComplete(network::URLLoaderCompletionStatus(net_error)); |
| 509 | client_.reset(); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 510 | MaybeDeleteSelf(); |
| 511 | return; |
| 512 | } |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 513 | char initial_read_buffer[net::kMaxBytesToSniff]; |
| 514 | int initial_read_result = |
| 515 | file.ReadAtCurrentPos(initial_read_buffer, net::kMaxBytesToSniff); |
| 516 | if (initial_read_result < 0) { |
Chris Mumford | b5df8ef2 | 2017-12-20 17:47:42 | [diff] [blame] | 517 | base::File::Error read_error = base::File::GetLastFileError(); |
| 518 | DCHECK_NE(base::File::FILE_OK, read_error); |
| 519 | if (observer) { |
Mikhail Atuchin | 9af3ff8c | 2018-04-19 21:15:10 | [diff] [blame] | 520 | // This can happen when the file is unreadable (which can happen during |
| 521 | // corruption). We need to be sure to inform |
| 522 | // the observer that we've finished reading so that it can proceed. |
Chris Mumford | b5df8ef2 | 2017-12-20 17:47:42 | [diff] [blame] | 523 | observer->OnBytesRead(nullptr, 0u, read_error); |
| 524 | observer->OnDoneReading(); |
| 525 | } |
Mikhail Atuchin | 9af3ff8c | 2018-04-19 21:15:10 | [diff] [blame] | 526 | net::Error net_error = net::FileErrorToNetError(read_error); |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 527 | client_->OnComplete(network::URLLoaderCompletionStatus(net_error)); |
| 528 | client_.reset(); |
| 529 | MaybeDeleteSelf(); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 530 | return; |
Chris Mumford | b5df8ef2 | 2017-12-20 17:47:42 | [diff] [blame] | 531 | } else if (observer) { |
| 532 | observer->OnBytesRead(initial_read_buffer, initial_read_result, |
| 533 | base::File::FILE_OK); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 534 | } |
| 535 | size_t initial_read_size = static_cast<size_t>(initial_read_result); |
| 536 | |
| 537 | std::string range_header; |
| 538 | net::HttpByteRange byte_range; |
| 539 | if (request.headers.GetHeader(net::HttpRequestHeaders::kRange, |
| 540 | &range_header)) { |
| 541 | // Handle a simple Range header for a single range. |
| 542 | std::vector<net::HttpByteRange> ranges; |
| 543 | bool fail = false; |
| 544 | if (net::HttpUtil::ParseRangeHeader(range_header, &ranges) && |
| 545 | ranges.size() == 1) { |
| 546 | byte_range = ranges[0]; |
| 547 | if (!byte_range.ComputeBounds(info.size)) |
| 548 | fail = true; |
| 549 | } else { |
| 550 | fail = true; |
| 551 | } |
| 552 | |
| 553 | if (fail) { |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 554 | OnClientComplete(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, |
| 555 | std::move(observer)); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 556 | return; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | size_t first_byte_to_send = 0; |
| 561 | size_t total_bytes_to_send = static_cast<size_t>(info.size); |
Antonio Gomes | 402dbf5 | 2018-08-02 11:06:41 | [diff] [blame] | 562 | |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 563 | if (byte_range.IsValid()) { |
| 564 | first_byte_to_send = |
| 565 | static_cast<size_t>(byte_range.first_byte_position()); |
| 566 | total_bytes_to_send = |
| 567 | static_cast<size_t>(byte_range.last_byte_position()) - |
| 568 | first_byte_to_send + 1; |
| 569 | } |
| 570 | |
Eric Roman | 7f6604c | 2018-10-02 22:50:02 | [diff] [blame^] | 571 | total_bytes_written_ = static_cast<size_t>(total_bytes_to_send); |
| 572 | |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 573 | head.content_length = base::saturated_cast<int64_t>(total_bytes_to_send); |
| 574 | |
| 575 | if (first_byte_to_send < initial_read_size) { |
| 576 | // Write any data we read for MIME sniffing, constraining by range where |
| 577 | // applicable. This will always fit in the pipe (see assertion near |
| 578 | // |kDefaultFileUrlPipeSize| definition). |
| 579 | uint32_t write_size = std::min( |
| 580 | static_cast<uint32_t>(initial_read_size - first_byte_to_send), |
| 581 | static_cast<uint32_t>(total_bytes_to_send)); |
| 582 | const uint32_t expected_write_size = write_size; |
| 583 | MojoResult result = pipe.producer_handle->WriteData( |
| 584 | &initial_read_buffer[first_byte_to_send], &write_size, |
| 585 | MOJO_WRITE_DATA_FLAG_NONE); |
| 586 | if (result != MOJO_RESULT_OK || write_size != expected_write_size) { |
Chris Mumford | b5df8ef2 | 2017-12-20 17:47:42 | [diff] [blame] | 587 | OnFileWritten(std::move(observer), result); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 588 | return; |
| 589 | } |
| 590 | |
| 591 | // Discount the bytes we just sent from the total range. |
| 592 | first_byte_to_send = initial_read_size; |
| 593 | total_bytes_to_send -= write_size; |
| 594 | } |
| 595 | |
| 596 | if (!net::GetMimeTypeFromFile(path, &head.mime_type)) { |
Matt Menke | fcbb1bd7 | 2018-01-31 21:53:12 | [diff] [blame] | 597 | net::SniffMimeType( |
| 598 | initial_read_buffer, initial_read_result, request.url, head.mime_type, |
| 599 | GetContentClient()->browser()->ForceSniffingFileUrlsForHtml() |
| 600 | ? net::ForceSniffFileUrlsForHtml::kEnabled |
| 601 | : net::ForceSniffFileUrlsForHtml::kDisabled, |
| 602 | &head.mime_type); |
Makoto Shimazu | 21950e8 | 2018-08-02 12:37:14 | [diff] [blame] | 603 | head.did_mime_sniff = true; |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 604 | } |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 605 | if (head.headers) { |
| 606 | head.headers->AddHeader( |
| 607 | base::StringPrintf("%s: %s", net::HttpRequestHeaders::kContentType, |
| 608 | head.mime_type.c_str())); |
| 609 | } |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 610 | client_->OnReceiveResponse(head); |
| 611 | client_->OnStartLoadingResponseBody(std::move(pipe.consumer_handle)); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 612 | |
| 613 | if (total_bytes_to_send == 0) { |
| 614 | // There's definitely no more data, so we're already done. |
Chris Mumford | b5df8ef2 | 2017-12-20 17:47:42 | [diff] [blame] | 615 | OnFileWritten(std::move(observer), MOJO_RESULT_OK); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 616 | return; |
| 617 | } |
| 618 | |
| 619 | // In case of a range request, seek to the appropriate position before |
| 620 | // sending the remaining bytes asynchronously. Under normal conditions |
| 621 | // (i.e., no range request) this Seek is effectively a no-op. |
Chris Mumford | b5df8ef2 | 2017-12-20 17:47:42 | [diff] [blame] | 622 | int new_position = file.Seek(base::File::FROM_BEGIN, |
| 623 | static_cast<int64_t>(first_byte_to_send)); |
| 624 | if (observer) |
| 625 | observer->OnSeekComplete(new_position); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 626 | |
| 627 | data_producer_ = std::make_unique<mojo::FileDataPipeProducer>( |
Chris Mumford | b5df8ef2 | 2017-12-20 17:47:42 | [diff] [blame] | 628 | std::move(pipe.producer_handle), std::move(observer)); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 629 | data_producer_->WriteFromFile( |
| 630 | std::move(file), total_bytes_to_send, |
Chris Mumford | b5df8ef2 | 2017-12-20 17:47:42 | [diff] [blame] | 631 | base::BindOnce(&FileURLLoader::OnFileWritten, base::Unretained(this), |
| 632 | nullptr)); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | void OnConnectionError() { |
| 636 | binding_.Close(); |
| 637 | MaybeDeleteSelf(); |
| 638 | } |
| 639 | |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 640 | void OnClientComplete(net::Error net_error, |
| 641 | std::unique_ptr<FileURLLoaderObserver> observer) { |
| 642 | client_->OnComplete(network::URLLoaderCompletionStatus(net_error)); |
| 643 | client_.reset(); |
| 644 | if (observer) |
| 645 | observer->OnDoneReading(); |
| 646 | MaybeDeleteSelf(); |
| 647 | } |
| 648 | |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 649 | void MaybeDeleteSelf() { |
| 650 | if (!binding_.is_bound() && !client_.is_bound()) |
| 651 | delete this; |
| 652 | } |
| 653 | |
Chris Mumford | b5df8ef2 | 2017-12-20 17:47:42 | [diff] [blame] | 654 | void OnFileWritten(std::unique_ptr<FileURLLoaderObserver> observer, |
| 655 | MojoResult result) { |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 656 | // All the data has been written now. Close the data pipe. The consumer will |
| 657 | // be notified that there will be no more data to read from now. |
| 658 | data_producer_.reset(); |
Chris Mumford | b5df8ef2 | 2017-12-20 17:47:42 | [diff] [blame] | 659 | if (observer) |
| 660 | observer->OnDoneReading(); |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame] | 661 | |
Antonio Gomes | 402dbf5 | 2018-08-02 11:06:41 | [diff] [blame] | 662 | if (result == MOJO_RESULT_OK) { |
| 663 | network::URLLoaderCompletionStatus status(net::OK); |
| 664 | status.encoded_data_length = total_bytes_written_; |
| 665 | status.encoded_body_length = total_bytes_written_; |
| 666 | status.decoded_body_length = total_bytes_written_; |
| 667 | client_->OnComplete(status); |
| 668 | } else { |
Takashi Toyoshima | aa27866 | 2017-11-20 11:11:26 | [diff] [blame] | 669 | client_->OnComplete(network::URLLoaderCompletionStatus(net::ERR_FAILED)); |
Antonio Gomes | 402dbf5 | 2018-08-02 11:06:41 | [diff] [blame] | 670 | } |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 671 | client_.reset(); |
| 672 | MaybeDeleteSelf(); |
| 673 | } |
| 674 | |
| 675 | std::unique_ptr<mojo::FileDataPipeProducer> data_producer_; |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 676 | mojo::Binding<network::mojom::URLLoader> binding_; |
| 677 | network::mojom::URLLoaderClientPtr client_; |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 678 | std::unique_ptr<RedirectData> redirect_data_; |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 679 | |
Eric Roman | 7f6604c | 2018-10-02 22:50:02 | [diff] [blame^] | 680 | // In case of successful loads, this holds the total number of bytes written |
| 681 | // to the response (this may be smaller than the total size of the file when |
| 682 | // a byte range was requested). |
Antonio Gomes | 402dbf5 | 2018-08-02 11:06:41 | [diff] [blame] | 683 | // It is used to set some of the URLLoaderCompletionStatus data passed back |
| 684 | // to the URLLoaderClients (eg SimpleURLLoader). |
| 685 | size_t total_bytes_written_ = 0; |
| 686 | |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 687 | DISALLOW_COPY_AND_ASSIGN(FileURLLoader); |
| 688 | }; |
| 689 | |
| 690 | } // namespace |
| 691 | |
| 692 | FileURLLoaderFactory::FileURLLoaderFactory( |
| 693 | const base::FilePath& profile_path, |
| 694 | scoped_refptr<base::SequencedTaskRunner> task_runner) |
| 695 | : profile_path_(profile_path), task_runner_(std::move(task_runner)) {} |
| 696 | |
| 697 | FileURLLoaderFactory::~FileURLLoaderFactory() = default; |
| 698 | |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 699 | void FileURLLoaderFactory::CreateLoaderAndStart( |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 700 | network::mojom::URLLoaderRequest loader, |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 701 | int32_t routing_id, |
| 702 | int32_t request_id, |
| 703 | uint32_t options, |
John Abd-El-Malek | 1df6179 | 2018-01-12 20:40:45 | [diff] [blame] | 704 | const network::ResourceRequest& request, |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 705 | network::mojom::URLLoaderClientPtr client, |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 706 | const net::MutableNetworkTrafficAnnotationTag& traffic_annotation) { |
| 707 | base::FilePath file_path; |
Chris Mumford | 90b1a0d | 2018-09-26 20:10:43 | [diff] [blame] | 708 | if (!net::FileURLToFilePath(request.url, &file_path)) { |
| 709 | client->OnComplete( |
| 710 | network::URLLoaderCompletionStatus(net::ERR_INVALID_URL)); |
| 711 | return; |
| 712 | } |
| 713 | if (file_path.EndsWithSeparator() && file_path.IsAbsolute()) { |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 714 | task_runner_->PostTask( |
| 715 | FROM_HERE, |
| 716 | base::BindOnce(&FileURLDirectoryLoader::CreateAndStart, profile_path_, |
Chris Mumford | b5df8ef2 | 2017-12-20 17:47:42 | [diff] [blame] | 717 | request, std::move(loader), client.PassInterface(), |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 718 | std::unique_ptr<FileURLLoaderObserver>(), nullptr)); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 719 | } else { |
| 720 | task_runner_->PostTask( |
| 721 | FROM_HERE, |
| 722 | base::BindOnce(&FileURLLoader::CreateAndStart, profile_path_, request, |
Ken Rockot | 6414c4d9 | 2017-11-08 19:58:32 | [diff] [blame] | 723 | std::move(loader), client.PassInterface(), |
| 724 | DirectoryLoadingPolicy::kRespondWithListing, |
| 725 | FileAccessPolicy::kRestricted, |
Chris Mumford | b5df8ef2 | 2017-12-20 17:47:42 | [diff] [blame] | 726 | LinkFollowingPolicy::kFollow, |
Ken Rockot | a0dfaca1 | 2018-02-15 07:26:25 | [diff] [blame] | 727 | std::unique_ptr<FileURLLoaderObserver>(), |
| 728 | nullptr /* extra_response_headers */)); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 732 | void FileURLLoaderFactory::Clone( |
| 733 | network::mojom::URLLoaderFactoryRequest loader) { |
Ken Rockot | 6414c4d9 | 2017-11-08 19:58:32 | [diff] [blame] | 734 | bindings_.AddBinding(this, std::move(loader)); |
| 735 | } |
| 736 | |
Ken Rockot | a0dfaca1 | 2018-02-15 07:26:25 | [diff] [blame] | 737 | void CreateFileURLLoader( |
| 738 | const network::ResourceRequest& request, |
| 739 | network::mojom::URLLoaderRequest loader, |
| 740 | network::mojom::URLLoaderClientPtr client, |
| 741 | std::unique_ptr<FileURLLoaderObserver> observer, |
| 742 | scoped_refptr<net::HttpResponseHeaders> extra_response_headers) { |
Ken Rockot | 6414c4d9 | 2017-11-08 19:58:32 | [diff] [blame] | 743 | auto task_runner = base::CreateSequencedTaskRunnerWithTraits( |
Gabriel Charette | b10aeeb | 2018-07-26 20:15:00 | [diff] [blame] | 744 | {base::MayBlock(), base::TaskPriority::BEST_EFFORT, |
Ken Rockot | 6414c4d9 | 2017-11-08 19:58:32 | [diff] [blame] | 745 | base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}); |
| 746 | task_runner->PostTask( |
| 747 | FROM_HERE, |
| 748 | base::BindOnce(&FileURLLoader::CreateAndStart, base::FilePath(), request, |
| 749 | std::move(loader), client.PassInterface(), |
| 750 | DirectoryLoadingPolicy::kFail, |
| 751 | FileAccessPolicy::kUnrestricted, |
Ken Rockot | a0dfaca1 | 2018-02-15 07:26:25 | [diff] [blame] | 752 | LinkFollowingPolicy::kDoNotFollow, std::move(observer), |
| 753 | std::move(extra_response_headers))); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 754 | } |
| 755 | |
Antonio Gomes | e1a0219 | 2018-08-01 15:39:44 | [diff] [blame] | 756 | std::unique_ptr<network::mojom::URLLoaderFactory> CreateFileURLLoaderFactory( |
| 757 | const base::FilePath& profile_path) { |
| 758 | return std::make_unique<content::FileURLLoaderFactory>( |
| 759 | profile_path, base::CreateSequencedTaskRunnerWithTraits( |
| 760 | {base::MayBlock(), base::TaskPriority::BEST_EFFORT, |
| 761 | base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN})); |
| 762 | } |
| 763 | |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 764 | } // namespace content |