| 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/string_util.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/trace_event/trace_event.h" | 13 #include "base/trace_event/trace_event.h" |
| 14 #include "mojo/common/common_type_converters.h" | 14 #include "mojo/common/common_type_converters.h" |
| 15 #include "mojo/common/data_pipe_utils.h" | 15 #include "mojo/common/data_pipe_utils.h" |
| 16 #include "mojo/common/url_type_converters.h" | 16 #include "mojo/common/url_type_converters.h" |
| 17 #include "mojo/services/network/public/interfaces/network_service.mojom.h" |
| 17 #include "mojo/util/filename_util.h" | 18 #include "mojo/util/filename_util.h" |
| 18 #include "url/url_util.h" | 19 #include "url/url_util.h" |
| 19 | 20 |
| 20 namespace mojo { | 21 namespace mojo { |
| 21 namespace shell { | 22 namespace shell { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 void IgnoreResult(bool result) { | 26 void IgnoreResult(bool result) { |
| 26 } | 27 } |
| 27 | 28 |
| 28 } // namespace | 29 } // namespace |
| 29 | 30 |
| 30 // A loader for local files. | 31 // A loader for local files. |
| 31 LocalFetcher::LocalFetcher(const GURL& url, | 32 LocalFetcher::LocalFetcher(NetworkService* network_service, |
| 33 const GURL& url, |
| 32 const GURL& url_without_query, | 34 const GURL& url_without_query, |
| 33 const FetchCallback& loader_callback) | 35 const FetchCallback& loader_callback) |
| 34 : Fetcher(loader_callback), | 36 : Fetcher(loader_callback), |
| 35 url_(url), | 37 url_(url), |
| 36 path_(util::UrlToFilePath(url_without_query)) { | 38 path_(util::UrlToFilePath(url_without_query)) { |
| 37 TRACE_EVENT1("mojo_shell", "LocalFetcher::LocalFetcher", "url", url.spec()); | 39 TRACE_EVENT1("mojo_shell", "LocalFetcher::LocalFetcher", "url", url.spec()); |
| 40 const std::string ext(base::FilePath(path_.Extension()).AsUTF8Unsafe()); |
| 41 if (network_service && !base::EqualsCaseInsensitiveASCII(ext, ".mojo")) { |
| 42 network_service->GetMimeTypeFromFile( |
| 43 path_.AsUTF8Unsafe(), |
| 44 base::Bind(&LocalFetcher::GetMimeTypeFromFileCallback, |
| 45 base::Unretained(this))); |
| 46 } else { |
| 47 loader_callback_.Run(make_scoped_ptr(this)); |
| 48 } |
| 49 } |
| 50 |
| 51 void LocalFetcher::GetMimeTypeFromFileCallback(const mojo::String& mime_type) { |
| 52 mime_type_ = mime_type.To<std::string>(); |
| 38 loader_callback_.Run(make_scoped_ptr(this)); | 53 loader_callback_.Run(make_scoped_ptr(this)); |
| 39 } | 54 } |
| 40 | 55 |
| 41 const GURL& LocalFetcher::GetURL() const { | 56 const GURL& LocalFetcher::GetURL() const { |
| 42 return url_; | 57 return url_; |
| 43 } | 58 } |
| 44 | 59 |
| 45 GURL LocalFetcher::GetRedirectURL() const { | 60 GURL LocalFetcher::GetRedirectURL() const { |
| 46 return GURL::EmptyGURL(); | 61 return GURL::EmptyGURL(); |
| 47 } | 62 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 72 | 87 |
| 73 void LocalFetcher::AsPath( | 88 void LocalFetcher::AsPath( |
| 74 base::TaskRunner* task_runner, | 89 base::TaskRunner* task_runner, |
| 75 base::Callback<void(const base::FilePath&, bool)> callback) { | 90 base::Callback<void(const base::FilePath&, bool)> callback) { |
| 76 // Async for consistency with network case. | 91 // Async for consistency with network case. |
| 77 base::MessageLoop::current()->PostTask( | 92 base::MessageLoop::current()->PostTask( |
| 78 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_))); | 93 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_))); |
| 79 } | 94 } |
| 80 | 95 |
| 81 std::string LocalFetcher::MimeType() { | 96 std::string LocalFetcher::MimeType() { |
| 82 // TODO(msw): Expand support; use net::GetMimeTypeFromExtension or similar. | 97 return mime_type_; |
| 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(); | |
| 92 } | 98 } |
| 93 | 99 |
| 94 bool LocalFetcher::HasMojoMagic() { | 100 bool LocalFetcher::HasMojoMagic() { |
| 95 std::string magic; | 101 std::string magic; |
| 96 ReadFileToString(path_, &magic, strlen(kMojoMagic)); | 102 ReadFileToString(path_, &magic, strlen(kMojoMagic)); |
| 97 return magic == kMojoMagic; | 103 return magic == kMojoMagic; |
| 98 } | 104 } |
| 99 | 105 |
| 100 bool LocalFetcher::PeekFirstLine(std::string* line) { | 106 bool LocalFetcher::PeekFirstLine(std::string* line) { |
| 101 std::string start_of_file; | 107 std::string start_of_file; |
| 102 ReadFileToString(path_, &start_of_file, kMaxShebangLength); | 108 ReadFileToString(path_, &start_of_file, kMaxShebangLength); |
| 103 size_t return_position = start_of_file.find('\n'); | 109 size_t return_position = start_of_file.find('\n'); |
| 104 if (return_position == std::string::npos) | 110 if (return_position == std::string::npos) |
| 105 return false; | 111 return false; |
| 106 *line = start_of_file.substr(0, return_position + 1); | 112 *line = start_of_file.substr(0, return_position + 1); |
| 107 return true; | 113 return true; |
| 108 } | 114 } |
| 109 | 115 |
| 110 } // namespace shell | 116 } // namespace shell |
| 111 } // namespace mojo | 117 } // namespace mojo |
| OLD | NEW |