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/update_fetcher.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <stdint.h> | |
9 | |
10 #include <utility> | |
11 | |
12 #include "base/bind.h" | |
13 #include "base/files/file_util.h" | |
14 #include "base/format_macros.h" | |
15 #include "base/message_loop/message_loop.h" | |
16 #include "base/strings/stringprintf.h" | |
17 #include "mojo/common/common_type_converters.h" | |
18 #include "mojo/common/data_pipe_utils.h" | |
19 #include "mojo/common/url_type_converters.h" | |
20 | |
21 namespace mojo { | |
22 namespace fetcher { | |
23 | |
24 namespace { | |
25 void IgnoreResult(bool result) { | |
26 } | |
27 | |
28 } // namespace | |
29 UpdateFetcher::UpdateFetcher(const GURL& url, | |
30 updater::Updater* updater, | |
31 const FetchCallback& loader_callback) | |
32 : Fetcher(loader_callback), url_(url), weak_ptr_factory_(this) { | |
33 DVLOG(1) << "updating: " << url_; | |
34 updater->GetPathForApp( | |
35 url.spec(), | |
36 base::Bind(&UpdateFetcher::OnGetAppPath, weak_ptr_factory_.GetWeakPtr())); | |
37 } | |
38 | |
39 UpdateFetcher::~UpdateFetcher() { | |
40 } | |
41 | |
42 const GURL& UpdateFetcher::GetURL() const { | |
43 return url_; | |
44 } | |
45 | |
46 GURL UpdateFetcher::GetRedirectURL() const { | |
47 return GURL::EmptyGURL(); | |
48 } | |
49 | |
50 GURL UpdateFetcher::GetRedirectReferer() const { | |
51 return GURL::EmptyGURL(); | |
52 } | |
53 URLResponsePtr UpdateFetcher::AsURLResponse(base::TaskRunner* task_runner, | |
54 uint32_t skip) { | |
55 URLResponsePtr response(URLResponse::New()); | |
56 response->url = String::From(url_); | |
57 DataPipe data_pipe; | |
58 response->body = std::move(data_pipe.consumer_handle); | |
59 int64_t file_size; | |
60 if (base::GetFileSize(path_, &file_size)) { | |
61 response->headers = Array<HttpHeaderPtr>(1); | |
62 HttpHeaderPtr header = HttpHeader::New(); | |
63 header->name = "Content-Length"; | |
64 header->value = base::StringPrintf("%" PRId64, file_size); | |
65 response->headers[0] = std::move(header); | |
66 } | |
67 common::CopyFromFile(path_, std::move(data_pipe.producer_handle), skip, | |
68 task_runner, base::Bind(&IgnoreResult)); | |
69 return response; | |
70 } | |
71 | |
72 void UpdateFetcher::AsPath( | |
73 base::TaskRunner* task_runner, | |
74 base::Callback<void(const base::FilePath&, bool)> callback) { | |
75 base::MessageLoop::current()->PostTask( | |
76 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_))); | |
77 } | |
78 | |
79 std::string UpdateFetcher::MimeType() { | |
80 return ""; | |
81 } | |
82 | |
83 bool UpdateFetcher::HasMojoMagic() { | |
84 std::string magic; | |
85 ReadFileToString(path_, &magic, strlen(kMojoMagic)); | |
86 return magic == kMojoMagic; | |
87 } | |
88 | |
89 bool UpdateFetcher::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; | |
97 } | |
98 | |
99 void UpdateFetcher::OnGetAppPath(const mojo::String& path) { | |
100 path_ = base::FilePath::FromUTF8Unsafe(path); | |
101 loader_callback_.Run(make_scoped_ptr(this)); | |
102 } | |
103 | |
104 } // namespace fetcher | |
105 } // namespace mojo | |
OLD | NEW |