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"
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"
17 void RunFetcherCallback(const shell::Fetcher::FetchCallback
& callback
,
18 scoped_ptr
<shell::Fetcher
> fetcher
,
20 callback
.Run(success
? fetcher
.Pass() : nullptr);
25 const char AboutFetcher::kAboutScheme
[] = "about";
26 const char AboutFetcher::kAboutBlankURL
[] = "about:blank";
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
) {
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(
62 base::Bind(RunFetcherCallback
, loader_callback_
,
63 base::Passed(make_scoped_ptr
<Fetcher
>(this)), success
));
66 const GURL
& AboutFetcher::GetURL() const {
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
,
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
) {
93 base::MessageLoop::current()->PostTask(
94 FROM_HERE
, base::Bind(callback
, base::FilePath(), false));
97 std::string
AboutFetcher::MimeType() {
99 return response_
->mime_type
;
102 bool AboutFetcher::HasMojoMagic() {
106 bool AboutFetcher::PeekFirstLine(std::string
* line
) {
107 // The only URL handled currently is "about:blank" which doesn't have a body.
111 } // namespace fetcher