ChildAccountService: get service flags from AccountTrackerService instead of fetching...
[chromium-blink-merge.git] / mojo / shell / local_fetcher.cc
blobbdb862edd2c508947947a5e995b42c2b5bb1c352
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/stringprintf.h"
12 #include "base/trace_event/trace_event.h"
13 #include "mojo/common/common_type_converters.h"
14 #include "mojo/common/data_pipe_utils.h"
15 #include "mojo/common/url_type_converters.h"
16 #include "mojo/util/filename_util.h"
17 #include "url/url_util.h"
19 namespace mojo {
20 namespace shell {
22 namespace {
24 void IgnoreResult(bool result) {
27 } // namespace
29 // A loader for local files.
30 LocalFetcher::LocalFetcher(const GURL& url,
31 const GURL& url_without_query,
32 const FetchCallback& loader_callback)
33 : Fetcher(loader_callback),
34 url_(url),
35 path_(util::UrlToFilePath(url_without_query)) {
36 TRACE_EVENT1("mojo_shell", "LocalFetcher::LocalFetcher", "url", url.spec());
37 loader_callback_.Run(make_scoped_ptr(this));
40 const GURL& LocalFetcher::GetURL() const {
41 return url_;
44 GURL LocalFetcher::GetRedirectURL() const {
45 return GURL::EmptyGURL();
48 GURL LocalFetcher::GetRedirectReferer() const {
49 return GURL::EmptyGURL();
52 URLResponsePtr LocalFetcher::AsURLResponse(base::TaskRunner* task_runner,
53 uint32_t skip) {
54 URLResponsePtr response(URLResponse::New());
55 response->url = String::From(url_);
56 DataPipe data_pipe;
57 response->body = data_pipe.consumer_handle.Pass();
58 int64 file_size;
59 if (base::GetFileSize(path_, &file_size)) {
60 response->headers = Array<HttpHeaderPtr>(1);
61 HttpHeaderPtr header = HttpHeader::New();
62 header->name = "Content-Length";
63 header->value = base::StringPrintf("%" PRId64, file_size);
64 response->headers[0] = header.Pass();
66 common::CopyFromFile(path_, data_pipe.producer_handle.Pass(), skip,
67 task_runner, base::Bind(&IgnoreResult));
68 return response.Pass();
71 void LocalFetcher::AsPath(
72 base::TaskRunner* task_runner,
73 base::Callback<void(const base::FilePath&, bool)> callback) {
74 // Async for consistency with network case.
75 base::MessageLoop::current()->PostTask(
76 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_)));
79 std::string LocalFetcher::MimeType() {
80 return "";
83 bool LocalFetcher::HasMojoMagic() {
84 std::string magic;
85 ReadFileToString(path_, &magic, strlen(kMojoMagic));
86 return magic == kMojoMagic;
89 bool LocalFetcher::PeekFirstLine(std::string* line) {
90 std::string start_of_file;
91 ReadFileToString(path_, &start_of_file, kMaxShebangLength);
92 size_t return_position = start_of_file.find('\n');
93 if (return_position == std::string::npos)
94 return false;
95 *line = start_of_file.substr(0, return_position + 1);
96 return true;
99 } // namespace shell
100 } // namespace mojo