| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "mojo/shell/local_fetcher.h" | 5 #include "mojo/shell/local_fetcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/strings/string_util.h" |
| 11 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 12 #include "base/trace_event/trace_event.h" | 13 #include "base/trace_event/trace_event.h" |
| 13 #include "mojo/common/common_type_converters.h" | 14 #include "mojo/common/common_type_converters.h" |
| 14 #include "mojo/common/data_pipe_utils.h" | 15 #include "mojo/common/data_pipe_utils.h" |
| 15 #include "mojo/common/url_type_converters.h" | 16 #include "mojo/common/url_type_converters.h" |
| 16 #include "mojo/util/filename_util.h" | 17 #include "mojo/util/filename_util.h" |
| 17 #include "url/url_util.h" | 18 #include "url/url_util.h" |
| 18 | 19 |
| 19 namespace mojo { | 20 namespace mojo { |
| 20 namespace shell { | 21 namespace shell { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 DataPipe data_pipe; | 57 DataPipe data_pipe; |
| 57 response->body = data_pipe.consumer_handle.Pass(); | 58 response->body = data_pipe.consumer_handle.Pass(); |
| 58 int64 file_size; | 59 int64 file_size; |
| 59 if (base::GetFileSize(path_, &file_size)) { | 60 if (base::GetFileSize(path_, &file_size)) { |
| 60 response->headers = Array<HttpHeaderPtr>(1); | 61 response->headers = Array<HttpHeaderPtr>(1); |
| 61 HttpHeaderPtr header = HttpHeader::New(); | 62 HttpHeaderPtr header = HttpHeader::New(); |
| 62 header->name = "Content-Length"; | 63 header->name = "Content-Length"; |
| 63 header->value = base::StringPrintf("%" PRId64, file_size); | 64 header->value = base::StringPrintf("%" PRId64, file_size); |
| 64 response->headers[0] = header.Pass(); | 65 response->headers[0] = header.Pass(); |
| 65 } | 66 } |
| 67 response->mime_type = String::From(MimeType()); |
| 66 common::CopyFromFile(path_, data_pipe.producer_handle.Pass(), skip, | 68 common::CopyFromFile(path_, data_pipe.producer_handle.Pass(), skip, |
| 67 task_runner, base::Bind(&IgnoreResult)); | 69 task_runner, base::Bind(&IgnoreResult)); |
| 68 return response.Pass(); | 70 return response.Pass(); |
| 69 } | 71 } |
| 70 | 72 |
| 71 void LocalFetcher::AsPath( | 73 void LocalFetcher::AsPath( |
| 72 base::TaskRunner* task_runner, | 74 base::TaskRunner* task_runner, |
| 73 base::Callback<void(const base::FilePath&, bool)> callback) { | 75 base::Callback<void(const base::FilePath&, bool)> callback) { |
| 74 // Async for consistency with network case. | 76 // Async for consistency with network case. |
| 75 base::MessageLoop::current()->PostTask( | 77 base::MessageLoop::current()->PostTask( |
| 76 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_))); | 78 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_))); |
| 77 } | 79 } |
| 78 | 80 |
| 79 std::string LocalFetcher::MimeType() { | 81 std::string LocalFetcher::MimeType() { |
| 80 return ""; | 82 // TODO(msw): Expand support; use net::GetMimeTypeFromExtension or similar. |
| 83 std::string extension(base::FilePath(path_.Extension()).AsUTF8Unsafe()); |
| 84 if (base::EqualsCaseInsensitiveASCII(extension, ".html") || |
| 85 base::EqualsCaseInsensitiveASCII(extension, ".htm") || |
| 86 base::EqualsCaseInsensitiveASCII(extension, ".shtml") || |
| 87 base::EqualsCaseInsensitiveASCII(extension, ".shtm") || |
| 88 base::EqualsCaseInsensitiveASCII(extension, ".ehtml")) { |
| 89 return "text/html"; |
| 90 } |
| 91 return std::string(); |
| 81 } | 92 } |
| 82 | 93 |
| 83 bool LocalFetcher::HasMojoMagic() { | 94 bool LocalFetcher::HasMojoMagic() { |
| 84 std::string magic; | 95 std::string magic; |
| 85 ReadFileToString(path_, &magic, strlen(kMojoMagic)); | 96 ReadFileToString(path_, &magic, strlen(kMojoMagic)); |
| 86 return magic == kMojoMagic; | 97 return magic == kMojoMagic; |
| 87 } | 98 } |
| 88 | 99 |
| 89 bool LocalFetcher::PeekFirstLine(std::string* line) { | 100 bool LocalFetcher::PeekFirstLine(std::string* line) { |
| 90 std::string start_of_file; | 101 std::string start_of_file; |
| 91 ReadFileToString(path_, &start_of_file, kMaxShebangLength); | 102 ReadFileToString(path_, &start_of_file, kMaxShebangLength); |
| 92 size_t return_position = start_of_file.find('\n'); | 103 size_t return_position = start_of_file.find('\n'); |
| 93 if (return_position == std::string::npos) | 104 if (return_position == std::string::npos) |
| 94 return false; | 105 return false; |
| 95 *line = start_of_file.substr(0, return_position + 1); | 106 *line = start_of_file.substr(0, return_position + 1); |
| 96 return true; | 107 return true; |
| 97 } | 108 } |
| 98 | 109 |
| 99 } // namespace shell | 110 } // namespace shell |
| 100 } // namespace mojo | 111 } // namespace mojo |
| OLD | NEW |