| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/fetcher/local_fetcher.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <utility> | |
| 11 | |
| 12 #include "base/bind.h" | |
| 13 #include "base/command_line.h" | |
| 14 #include "base/files/file_util.h" | |
| 15 #include "base/format_macros.h" | |
| 16 #include "base/message_loop/message_loop.h" | |
| 17 #include "base/strings/string_util.h" | |
| 18 #include "base/strings/stringprintf.h" | |
| 19 #include "base/trace_event/trace_event.h" | |
| 20 #include "mojo/common/common_type_converters.h" | |
| 21 #include "mojo/common/data_pipe_utils.h" | |
| 22 #include "mojo/common/url_type_converters.h" | |
| 23 #include "mojo/services/network/public/interfaces/network_service.mojom.h" | |
| 24 #include "mojo/shell/switches.h" | |
| 25 #include "mojo/util/filename_util.h" | |
| 26 #include "url/url_util.h" | |
| 27 | |
| 28 namespace mojo { | |
| 29 namespace fetcher { | |
| 30 | |
| 31 namespace { | |
| 32 void IgnoreResult(bool result) { | |
| 33 } | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 // A loader for local files. | |
| 38 LocalFetcher::LocalFetcher(NetworkService* network_service, | |
| 39 const GURL& url, | |
| 40 const GURL& url_without_query, | |
| 41 const base::FilePath& shell_file_root, | |
| 42 const FetchCallback& loader_callback) | |
| 43 : Fetcher(loader_callback), | |
| 44 url_(url), | |
| 45 path_(util::UrlToFilePath(url_without_query)), | |
| 46 shell_file_root_(shell_file_root) { | |
| 47 TRACE_EVENT1("mojo_shell", "LocalFetcher::LocalFetcher", "url", url.spec()); | |
| 48 const std::string ext(base::FilePath(path_.Extension()).AsUTF8Unsafe()); | |
| 49 if (network_service && !base::EqualsCaseInsensitiveASCII(ext, ".mojo")) { | |
| 50 network_service->GetMimeTypeFromFile( | |
| 51 path_.AsUTF8Unsafe(), | |
| 52 base::Bind(&LocalFetcher::GetMimeTypeFromFileCallback, | |
| 53 base::Unretained(this))); | |
| 54 } else { | |
| 55 loader_callback_.Run(make_scoped_ptr(this)); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 LocalFetcher::~LocalFetcher() {} | |
| 60 | |
| 61 void LocalFetcher::GetMimeTypeFromFileCallback(const mojo::String& mime_type) { | |
| 62 mime_type_ = mime_type.To<std::string>(); | |
| 63 loader_callback_.Run(make_scoped_ptr(this)); | |
| 64 } | |
| 65 | |
| 66 const GURL& LocalFetcher::GetURL() const { | |
| 67 return url_; | |
| 68 } | |
| 69 | |
| 70 GURL LocalFetcher::GetRedirectURL() const { | |
| 71 // Use Mandoline's Google Storage bucket if the Mojo component does not exist. | |
| 72 // TODO(msw): Integrate with Mandoline's component updater? crbug.com/479169 | |
| 73 // TODO(msw): Integrate with planned Omaha fetcher and updater work. | |
| 74 // TODO(msw): Support fetching apps with resource files. | |
| 75 // TODO(msw): Handle broken URLs. (unavailable component files, etc.) | |
| 76 // TODO(msw): Avoid kPredictableAppFilenames switch? (need executable bit set) | |
| 77 if (!base::PathExists(path_) && shell_file_root_.IsParent(path_) && | |
| 78 path_.MatchesExtension(FILE_PATH_LITERAL(".mojo")) && | |
| 79 base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 80 switches::kPredictableAppFilenames)) { | |
| 81 GURL url("https://storage.googleapis.com/mandoline/"); | |
| 82 const std::string version("latest/"); | |
| 83 #if defined(OS_WIN) | |
| 84 const std::string platform("win/"); | |
| 85 #elif defined(OS_LINUX) | |
| 86 const std::string platform("linux/"); | |
| 87 #else | |
| 88 const std::string platform("unknown/"); | |
| 89 #endif | |
| 90 // Get the app path relative to the shell (and Google Storage) file root. | |
| 91 base::FilePath app_path; | |
| 92 bool result = shell_file_root_.AppendRelativePath(path_, &app_path); | |
| 93 DCHECK(result); | |
| 94 url = url.Resolve(version).Resolve(platform).Resolve(app_path.value()); | |
| 95 return url; | |
| 96 } | |
| 97 return GURL::EmptyGURL(); | |
| 98 } | |
| 99 | |
| 100 GURL LocalFetcher::GetRedirectReferer() const { | |
| 101 return GURL::EmptyGURL(); | |
| 102 } | |
| 103 | |
| 104 URLResponsePtr LocalFetcher::AsURLResponse(base::TaskRunner* task_runner, | |
| 105 uint32_t skip) { | |
| 106 URLResponsePtr response(URLResponse::New()); | |
| 107 response->url = String::From(url_); | |
| 108 DataPipe data_pipe; | |
| 109 response->body = std::move(data_pipe.consumer_handle); | |
| 110 int64_t file_size; | |
| 111 if (base::GetFileSize(path_, &file_size)) { | |
| 112 response->headers = Array<HttpHeaderPtr>(1); | |
| 113 HttpHeaderPtr header = HttpHeader::New(); | |
| 114 header->name = "Content-Length"; | |
| 115 header->value = base::StringPrintf("%" PRId64, file_size); | |
| 116 response->headers[0] = std::move(header); | |
| 117 } | |
| 118 response->mime_type = String::From(MimeType()); | |
| 119 common::CopyFromFile(path_, std::move(data_pipe.producer_handle), skip, | |
| 120 task_runner, base::Bind(&IgnoreResult)); | |
| 121 return response; | |
| 122 } | |
| 123 | |
| 124 void LocalFetcher::AsPath( | |
| 125 base::TaskRunner* task_runner, | |
| 126 base::Callback<void(const base::FilePath&, bool)> callback) { | |
| 127 // Async for consistency with network case. | |
| 128 base::MessageLoop::current()->PostTask( | |
| 129 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_))); | |
| 130 } | |
| 131 | |
| 132 std::string LocalFetcher::MimeType() { | |
| 133 return mime_type_; | |
| 134 } | |
| 135 | |
| 136 bool LocalFetcher::HasMojoMagic() { | |
| 137 std::string magic; | |
| 138 ReadFileToString(path_, &magic, strlen(kMojoMagic)); | |
| 139 return magic == kMojoMagic; | |
| 140 } | |
| 141 | |
| 142 bool LocalFetcher::PeekFirstLine(std::string* line) { | |
| 143 std::string start_of_file; | |
| 144 ReadFileToString(path_, &start_of_file, kMaxShebangLength); | |
| 145 size_t return_position = start_of_file.find('\n'); | |
| 146 if (return_position == std::string::npos) | |
| 147 return false; | |
| 148 *line = start_of_file.substr(0, return_position + 1); | |
| 149 return true; | |
| 150 } | |
| 151 | |
| 152 } // namespace fetcher | |
| 153 } // namespace mojo | |
| OLD | NEW |