Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / mojo / shell / local_fetcher.cc
blob8da8ca9b5d4cef31a5fdfd03949a4d331ba64ffb
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.
5 #include "mojo/shell/local_fetcher.h"
7 #include "base/bind.h"
8 #include "base/files/file_util.h"
9 #include "base/format_macros.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/trace_event/trace_event.h"
14 #include "mojo/common/common_type_converters.h"
15 #include "mojo/common/data_pipe_utils.h"
16 #include "mojo/common/url_type_converters.h"
17 #include "mojo/services/network/public/interfaces/network_service.mojom.h"
18 #include "mojo/util/filename_util.h"
19 #include "url/url_util.h"
21 namespace mojo {
22 namespace shell {
24 namespace {
26 void IgnoreResult(bool result) {
29 } // namespace
31 // A loader for local files.
32 LocalFetcher::LocalFetcher(NetworkService* network_service,
33 const GURL& url,
34 const GURL& url_without_query,
35 const FetchCallback& loader_callback)
36 : Fetcher(loader_callback),
37 url_(url),
38 path_(util::UrlToFilePath(url_without_query)) {
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));
51 void LocalFetcher::GetMimeTypeFromFileCallback(const mojo::String& mime_type) {
52 mime_type_ = mime_type.To<std::string>();
53 loader_callback_.Run(make_scoped_ptr(this));
56 const GURL& LocalFetcher::GetURL() const {
57 return url_;
60 GURL LocalFetcher::GetRedirectURL() const {
61 return GURL::EmptyGURL();
64 GURL LocalFetcher::GetRedirectReferer() const {
65 return GURL::EmptyGURL();
68 URLResponsePtr LocalFetcher::AsURLResponse(base::TaskRunner* task_runner,
69 uint32_t skip) {
70 URLResponsePtr response(URLResponse::New());
71 response->url = String::From(url_);
72 DataPipe data_pipe;
73 response->body = data_pipe.consumer_handle.Pass();
74 int64 file_size;
75 if (base::GetFileSize(path_, &file_size)) {
76 response->headers = Array<HttpHeaderPtr>(1);
77 HttpHeaderPtr header = HttpHeader::New();
78 header->name = "Content-Length";
79 header->value = base::StringPrintf("%" PRId64, file_size);
80 response->headers[0] = header.Pass();
82 response->mime_type = String::From(MimeType());
83 common::CopyFromFile(path_, data_pipe.producer_handle.Pass(), skip,
84 task_runner, base::Bind(&IgnoreResult));
85 return response.Pass();
88 void LocalFetcher::AsPath(
89 base::TaskRunner* task_runner,
90 base::Callback<void(const base::FilePath&, bool)> callback) {
91 // Async for consistency with network case.
92 base::MessageLoop::current()->PostTask(
93 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_)));
96 std::string LocalFetcher::MimeType() {
97 return mime_type_;
100 bool LocalFetcher::HasMojoMagic() {
101 std::string magic;
102 ReadFileToString(path_, &magic, strlen(kMojoMagic));
103 return magic == kMojoMagic;
106 bool LocalFetcher::PeekFirstLine(std::string* line) {
107 std::string start_of_file;
108 ReadFileToString(path_, &start_of_file, kMaxShebangLength);
109 size_t return_position = start_of_file.find('\n');
110 if (return_position == std::string::npos)
111 return false;
112 *line = start_of_file.substr(0, return_position + 1);
113 return true;
116 } // namespace shell
117 } // namespace mojo