| 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/about_fetcher.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/files/file_path.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/message_loop/message_loop.h" | |
| 16 | |
| 17 namespace mojo { | |
| 18 namespace fetcher { | |
| 19 namespace { | |
| 20 | |
| 21 void RunFetcherCallback(const shell::Fetcher::FetchCallback& callback, | |
| 22 scoped_ptr<shell::Fetcher> fetcher, | |
| 23 bool success) { | |
| 24 callback.Run(success ? std::move(fetcher) : nullptr); | |
| 25 } | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 const char AboutFetcher::kAboutScheme[] = "about"; | |
| 30 const char AboutFetcher::kAboutBlankURL[] = "about:blank"; | |
| 31 | |
| 32 // static | |
| 33 void AboutFetcher::Start(const GURL& url, | |
| 34 const FetchCallback& loader_callback) { | |
| 35 // The object manages its own lifespan. | |
| 36 new AboutFetcher(url, loader_callback); | |
| 37 } | |
| 38 | |
| 39 AboutFetcher::AboutFetcher(const GURL& url, | |
| 40 const FetchCallback& loader_callback) | |
| 41 : Fetcher(loader_callback), url_(url) { | |
| 42 BuildResponse(); | |
| 43 } | |
| 44 | |
| 45 AboutFetcher::~AboutFetcher() {} | |
| 46 | |
| 47 void AboutFetcher::BuildResponse() { | |
| 48 response_ = URLResponse::New(); | |
| 49 response_->url = url_.spec(); | |
| 50 | |
| 51 // about: URLs other than about:blank are not supported yet. | |
| 52 // | |
| 53 // TODO(yzshen): crbug.com/516494 Eventually we need a general solution to | |
| 54 // generate error page for network errors/unrecognized app format/etc. | |
| 55 response_->status_code = (url_ == GURL(kAboutBlankURL)) ? 200 : 404; | |
| 56 | |
| 57 response_->mime_type = "text/html"; | |
| 58 PostToRunCallback(true); | |
| 59 } | |
| 60 | |
| 61 void AboutFetcher::PostToRunCallback(bool success) { | |
| 62 // Also pass |this| on failure, so that we won't destroy the object while the | |
| 63 // constructor is still on the call stack. | |
| 64 base::MessageLoop::current()->PostTask( | |
| 65 FROM_HERE, | |
| 66 base::Bind(RunFetcherCallback, loader_callback_, | |
| 67 base::Passed(make_scoped_ptr<Fetcher>(this)), success)); | |
| 68 } | |
| 69 | |
| 70 const GURL& AboutFetcher::GetURL() const { | |
| 71 return url_; | |
| 72 } | |
| 73 | |
| 74 GURL AboutFetcher::GetRedirectURL() const { | |
| 75 return GURL::EmptyGURL(); | |
| 76 } | |
| 77 | |
| 78 GURL AboutFetcher::GetRedirectReferer() const { | |
| 79 return GURL::EmptyGURL(); | |
| 80 } | |
| 81 | |
| 82 URLResponsePtr AboutFetcher::AsURLResponse(base::TaskRunner* task_runner, | |
| 83 uint32_t skip) { | |
| 84 DCHECK(response_); | |
| 85 | |
| 86 // Ignore |skip| because the only URL handled currently is "about:blank" which | |
| 87 // doesn't have a body. | |
| 88 DCHECK(!response_->body.is_valid()); | |
| 89 | |
| 90 return std::move(response_); | |
| 91 } | |
| 92 | |
| 93 void AboutFetcher::AsPath( | |
| 94 base::TaskRunner* task_runner, | |
| 95 base::Callback<void(const base::FilePath&, bool)> callback) { | |
| 96 NOTIMPLEMENTED(); | |
| 97 base::MessageLoop::current()->PostTask( | |
| 98 FROM_HERE, base::Bind(callback, base::FilePath(), false)); | |
| 99 } | |
| 100 | |
| 101 std::string AboutFetcher::MimeType() { | |
| 102 DCHECK(response_); | |
| 103 return response_->mime_type; | |
| 104 } | |
| 105 | |
| 106 bool AboutFetcher::HasMojoMagic() { | |
| 107 return false; | |
| 108 } | |
| 109 | |
| 110 bool AboutFetcher::PeekFirstLine(std::string* line) { | |
| 111 // The only URL handled currently is "about:blank" which doesn't have a body. | |
| 112 return false; | |
| 113 } | |
| 114 | |
| 115 } // namespace fetcher | |
| 116 } // namespace mojo | |
| OLD | NEW |