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/fetcher/update_fetcher.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"
20 void IgnoreResult(bool result
) {
24 UpdateFetcher::UpdateFetcher(const GURL
& url
,
25 updater::Updater
* updater
,
26 const FetchCallback
& loader_callback
)
27 : Fetcher(loader_callback
), url_(url
), weak_ptr_factory_(this) {
28 DVLOG(1) << "updating: " << url_
;
29 updater
->GetPathForApp(
31 base::Bind(&UpdateFetcher::OnGetAppPath
, weak_ptr_factory_
.GetWeakPtr()));
34 UpdateFetcher::~UpdateFetcher() {
37 const GURL
& UpdateFetcher::GetURL() const {
41 GURL
UpdateFetcher::GetRedirectURL() const {
42 return GURL::EmptyGURL();
45 GURL
UpdateFetcher::GetRedirectReferer() const {
46 return GURL::EmptyGURL();
48 URLResponsePtr
UpdateFetcher::AsURLResponse(base::TaskRunner
* task_runner
,
50 URLResponsePtr
response(URLResponse::New());
51 response
->url
= String::From(url_
);
53 response
->body
= data_pipe
.consumer_handle
.Pass();
55 if (base::GetFileSize(path_
, &file_size
)) {
56 response
->headers
= Array
<HttpHeaderPtr
>(1);
57 HttpHeaderPtr header
= HttpHeader::New();
58 header
->name
= "Content-Length";
59 header
->value
= base::StringPrintf("%" PRId64
, file_size
);
60 response
->headers
[0] = header
.Pass();
62 common::CopyFromFile(path_
, data_pipe
.producer_handle
.Pass(), skip
,
63 task_runner
, base::Bind(&IgnoreResult
));
64 return response
.Pass();
67 void UpdateFetcher::AsPath(
68 base::TaskRunner
* task_runner
,
69 base::Callback
<void(const base::FilePath
&, bool)> callback
) {
70 base::MessageLoop::current()->PostTask(
71 FROM_HERE
, base::Bind(callback
, path_
, base::PathExists(path_
)));
74 std::string
UpdateFetcher::MimeType() {
78 bool UpdateFetcher::HasMojoMagic() {
80 ReadFileToString(path_
, &magic
, strlen(kMojoMagic
));
81 return magic
== kMojoMagic
;
84 bool UpdateFetcher::PeekFirstLine(std::string
* line
) {
85 std::string start_of_file
;
86 ReadFileToString(path_
, &start_of_file
, kMaxShebangLength
);
87 size_t return_position
= start_of_file
.find('\n');
88 if (return_position
== std::string::npos
)
90 *line
= start_of_file
.substr(0, return_position
+ 1);
94 void UpdateFetcher::OnGetAppPath(const mojo::String
& path
) {
95 path_
= base::FilePath::FromUTF8Unsafe(path
);
96 loader_callback_
.Run(make_scoped_ptr(this));
99 } // namespace fetcher