cc: Convert LTHCommon tests to LayerImpl
[chromium-blink-merge.git] / mojo / shell / update_fetcher.cc
blob5e1d434f261885b53418db926dddbe37d0315bd5
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/update_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 "mojo/common/common_type_converters.h"
13 #include "mojo/common/data_pipe_utils.h"
14 #include "mojo/common/url_type_converters.h"
16 namespace mojo {
17 namespace shell {
19 namespace {
21 void IgnoreResult(bool result) {
24 } // namespace
25 UpdateFetcher::UpdateFetcher(const GURL& url,
26 updater::Updater* updater,
27 const FetchCallback& loader_callback)
28 : Fetcher(loader_callback), url_(url), weak_ptr_factory_(this) {
29 DVLOG(1) << "updating: " << url_;
30 updater->GetPathForApp(
31 url.spec(),
32 base::Bind(&UpdateFetcher::OnGetAppPath, weak_ptr_factory_.GetWeakPtr()));
35 UpdateFetcher::~UpdateFetcher() {
38 const GURL& UpdateFetcher::GetURL() const {
39 return url_;
42 GURL UpdateFetcher::GetRedirectURL() const {
43 return GURL::EmptyGURL();
46 GURL UpdateFetcher::GetRedirectReferer() const {
47 return GURL::EmptyGURL();
49 URLResponsePtr UpdateFetcher::AsURLResponse(base::TaskRunner* task_runner,
50 uint32_t skip) {
51 URLResponsePtr response(URLResponse::New());
52 response->url = String::From(url_);
53 DataPipe data_pipe;
54 response->body = data_pipe.consumer_handle.Pass();
55 int64 file_size;
56 if (base::GetFileSize(path_, &file_size)) {
57 response->headers = Array<HttpHeaderPtr>(1);
58 HttpHeaderPtr header = HttpHeader::New();
59 header->name = "Content-Length";
60 header->value = base::StringPrintf("%" PRId64, file_size);
61 response->headers[0] = header.Pass();
63 common::CopyFromFile(path_, data_pipe.producer_handle.Pass(), skip,
64 task_runner, base::Bind(&IgnoreResult));
65 return response.Pass();
68 void UpdateFetcher::AsPath(
69 base::TaskRunner* task_runner,
70 base::Callback<void(const base::FilePath&, bool)> callback) {
71 base::MessageLoop::current()->PostTask(
72 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_)));
75 std::string UpdateFetcher::MimeType() {
76 return "";
79 bool UpdateFetcher::HasMojoMagic() {
80 std::string magic;
81 ReadFileToString(path_, &magic, strlen(kMojoMagic));
82 return magic == kMojoMagic;
85 bool UpdateFetcher::PeekFirstLine(std::string* line) {
86 std::string start_of_file;
87 ReadFileToString(path_, &start_of_file, kMaxShebangLength);
88 size_t return_position = start_of_file.find('\n');
89 if (return_position == std::string::npos)
90 return false;
91 *line = start_of_file.substr(0, return_position + 1);
92 return true;
95 void UpdateFetcher::OnGetAppPath(const mojo::String& path) {
96 path_ = base::FilePath::FromUTF8Unsafe(path);
97 loader_callback_.Run(make_scoped_ptr(this));
100 } // namespace shell
101 } // namespace mojo