| 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 "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" | |
| 15 | |
| 16 namespace mojo { | |
| 17 namespace fetcher { | |
| 18 | |
| 19 namespace { | |
| 20 void IgnoreResult(bool result) { | |
| 21 } | |
| 22 | |
| 23 } // namespace | |
| 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( | |
| 30 url.spec(), | |
| 31 base::Bind(&UpdateFetcher::OnGetAppPath, weak_ptr_factory_.GetWeakPtr())); | |
| 32 } | |
| 33 | |
| 34 UpdateFetcher::~UpdateFetcher() { | |
| 35 } | |
| 36 | |
| 37 const GURL& UpdateFetcher::GetURL() const { | |
| 38 return url_; | |
| 39 } | |
| 40 | |
| 41 GURL UpdateFetcher::GetRedirectURL() const { | |
| 42 return GURL::EmptyGURL(); | |
| 43 } | |
| 44 | |
| 45 GURL UpdateFetcher::GetRedirectReferer() const { | |
| 46 return GURL::EmptyGURL(); | |
| 47 } | |
| 48 URLResponsePtr UpdateFetcher::AsURLResponse(base::TaskRunner* task_runner, | |
| 49 uint32_t skip) { | |
| 50 URLResponsePtr response(URLResponse::New()); | |
| 51 response->url = String::From(url_); | |
| 52 DataPipe data_pipe; | |
| 53 response->body = data_pipe.consumer_handle.Pass(); | |
| 54 int64 file_size; | |
| 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(); | |
| 61 } | |
| 62 common::CopyFromFile(path_, data_pipe.producer_handle.Pass(), skip, | |
| 63 task_runner, base::Bind(&IgnoreResult)); | |
| 64 return response.Pass(); | |
| 65 } | |
| 66 | |
| 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_))); | |
| 72 } | |
| 73 | |
| 74 std::string UpdateFetcher::MimeType() { | |
| 75 return ""; | |
| 76 } | |
| 77 | |
| 78 bool UpdateFetcher::HasMojoMagic() { | |
| 79 std::string magic; | |
| 80 ReadFileToString(path_, &magic, strlen(kMojoMagic)); | |
| 81 return magic == kMojoMagic; | |
| 82 } | |
| 83 | |
| 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) | |
| 89 return false; | |
| 90 *line = start_of_file.substr(0, return_position + 1); | |
| 91 return true; | |
| 92 } | |
| 93 | |
| 94 void UpdateFetcher::OnGetAppPath(const mojo::String& path) { | |
| 95 path_ = base::FilePath::FromUTF8Unsafe(path); | |
| 96 loader_callback_.Run(make_scoped_ptr(this)); | |
| 97 } | |
| 98 | |
| 99 } // namespace fetcher | |
| 100 } // namespace mojo | |
| OLD | NEW |