Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / mojo / fetcher / about_fetcher.cc
blobc09f09d133d5b47715a73e04b07c948936fcca6a
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/about_fetcher.h"
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
13 namespace mojo {
14 namespace fetcher {
15 namespace {
17 void RunFetcherCallback(const shell::Fetcher::FetchCallback& callback,
18 scoped_ptr<shell::Fetcher> fetcher,
19 bool success) {
20 callback.Run(success ? fetcher.Pass() : nullptr);
23 } // namespace
25 const char AboutFetcher::kAboutScheme[] = "about";
26 const char AboutFetcher::kAboutBlankURL[] = "about:blank";
28 // static
29 void AboutFetcher::Start(const GURL& url,
30 const FetchCallback& loader_callback) {
31 // The object manages its own lifespan.
32 new AboutFetcher(url, loader_callback);
35 AboutFetcher::AboutFetcher(const GURL& url,
36 const FetchCallback& loader_callback)
37 : Fetcher(loader_callback), url_(url) {
38 BuildResponse();
41 AboutFetcher::~AboutFetcher() {}
43 void AboutFetcher::BuildResponse() {
44 response_ = URLResponse::New();
45 response_->url = url_.spec();
47 // about: URLs other than about:blank are not supported yet.
49 // TODO(yzshen): crbug.com/516494 Eventually we need a general solution to
50 // generate error page for network errors/unrecognized app format/etc.
51 response_->status_code = (url_ == GURL(kAboutBlankURL)) ? 200 : 404;
53 response_->mime_type = "text/html";
54 PostToRunCallback(true);
57 void AboutFetcher::PostToRunCallback(bool success) {
58 // Also pass |this| on failure, so that we won't destroy the object while the
59 // constructor is still on the call stack.
60 base::MessageLoop::current()->PostTask(
61 FROM_HERE,
62 base::Bind(RunFetcherCallback, loader_callback_,
63 base::Passed(make_scoped_ptr<Fetcher>(this)), success));
66 const GURL& AboutFetcher::GetURL() const {
67 return url_;
70 GURL AboutFetcher::GetRedirectURL() const {
71 return GURL::EmptyGURL();
74 GURL AboutFetcher::GetRedirectReferer() const {
75 return GURL::EmptyGURL();
78 URLResponsePtr AboutFetcher::AsURLResponse(base::TaskRunner* task_runner,
79 uint32_t skip) {
80 DCHECK(response_);
82 // Ignore |skip| because the only URL handled currently is "about:blank" which
83 // doesn't have a body.
84 DCHECK(!response_->body.is_valid());
86 return response_.Pass();
89 void AboutFetcher::AsPath(
90 base::TaskRunner* task_runner,
91 base::Callback<void(const base::FilePath&, bool)> callback) {
92 NOTIMPLEMENTED();
93 base::MessageLoop::current()->PostTask(
94 FROM_HERE, base::Bind(callback, base::FilePath(), false));
97 std::string AboutFetcher::MimeType() {
98 DCHECK(response_);
99 return response_->mime_type;
102 bool AboutFetcher::HasMojoMagic() {
103 return false;
106 bool AboutFetcher::PeekFirstLine(std::string* line) {
107 // The only URL handled currently is "about:blank" which doesn't have a body.
108 return false;
111 } // namespace fetcher
112 } // namespace mojo