Chromium Code Reviews| Index: mojo/runner/about_fetcher.cc |
| diff --git a/mojo/runner/about_fetcher.cc b/mojo/runner/about_fetcher.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e760a4c8cb7014a4d75a0d9dafa8d951e28b0a3f |
| --- /dev/null |
| +++ b/mojo/runner/about_fetcher.cc |
| @@ -0,0 +1,104 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "mojo/runner/about_fetcher.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/files/file_path.h" |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "mojo/shell/data_pipe_peek.h" |
| + |
| +namespace mojo { |
| +namespace runner { |
| +namespace { |
| + |
| +void RunFetcherCallback(const shell::Fetcher::FetchCallback& callback, |
| + scoped_ptr<shell::Fetcher> fetcher, |
| + bool success) { |
| + callback.Run(success ? fetcher.Pass() : nullptr); |
| +} |
| + |
| +} // namespace |
| + |
| +const char AboutFetcher::kAboutScheme[] = "about"; |
| +const char AboutFetcher::kAboutBlankURL[] = "about:blank"; |
| + |
| +AboutFetcher::AboutFetcher(const GURL& url, |
| + const FetchCallback& loader_callback) |
| + : Fetcher(loader_callback), url_(url) { |
| + BuildResponse(); |
| +} |
| + |
| +AboutFetcher::~AboutFetcher() {} |
| + |
| +void AboutFetcher::BuildResponse() { |
| + if (url_ != GURL(kAboutBlankURL)) { |
| + PostToRunCallback(false); |
| + return; |
| + } |
| + |
| + response_ = URLResponse::New(); |
| + response_->url = kAboutBlankURL; |
| + response_->status_code = 200; |
| + response_->mime_type = "text/html"; |
| + PostToRunCallback(true); |
| +} |
| + |
| +void AboutFetcher::PostToRunCallback(bool success) { |
| + // Also pass |this| on failure, so that we won't destroy the object while the |
| + // constructor is still on the call stack. |
| + base::MessageLoop::current()->PostTask( |
| + FROM_HERE, |
| + base::Bind(RunFetcherCallback, loader_callback_, |
| + base::Passed(make_scoped_ptr<Fetcher>(this)), success)); |
| +} |
| + |
| +const GURL& AboutFetcher::GetURL() const { |
| + return url_; |
| +} |
| + |
| +GURL AboutFetcher::GetRedirectURL() const { |
| + return GURL::EmptyGURL(); |
| +} |
| + |
| +GURL AboutFetcher::GetRedirectReferer() const { |
| + return GURL::EmptyGURL(); |
| +} |
| + |
| +URLResponsePtr AboutFetcher::AsURLResponse(base::TaskRunner* task_runner, |
| + uint32_t skip) { |
| + if (skip != 0 && response_->body.is_valid()) { |
|
sky
2015/08/04 20:04:55
Don't you need to make sure response_ is valid? AF
yzshen1
2015/08/05 16:11:08
If |response_| is not valid, this object won't be
sky
2015/08/05 17:35:49
How about a DCHECK then.
yzshen1
2015/08/05 18:00:27
Done.
|
| + MojoResult result = ReadDataRaw( |
| + response_->body.get(), nullptr, &skip, |
| + MOJO_READ_DATA_FLAG_ALL_OR_NONE | MOJO_READ_DATA_FLAG_DISCARD); |
| + DCHECK_EQ(result, MOJO_RESULT_OK); |
| + } |
| + return response_.Pass(); |
| +} |
| + |
| +void AboutFetcher::AsPath( |
| + base::TaskRunner* task_runner, |
| + base::Callback<void(const base::FilePath&, bool)> callback) { |
| + NOTIMPLEMENTED(); |
| + base::MessageLoop::current()->PostTask( |
| + FROM_HERE, base::Bind(callback, base::FilePath(), false)); |
| +} |
| + |
| +std::string AboutFetcher::MimeType() { |
| + return response_->mime_type; |
|
sky
2015/08/04 20:04:55
Check response?
yzshen1
2015/08/05 16:11:08
Please see my previous comment.
|
| +} |
| + |
| +bool AboutFetcher::HasMojoMagic() { |
| + return false; |
| +} |
| + |
| +bool AboutFetcher::PeekFirstLine(std::string* line) { |
| + return shell::BlockingPeekLine(response_->body.get(), line, kMaxShebangLength, |
|
sky
2015/08/04 20:04:55
Similar comments to that of AsURLResponse.
yzshen1
2015/08/05 16:11:08
Please see my previous comment.
|
| + MOJO_DEADLINE_INDEFINITE); |
| +} |
| + |
| +} // namespace runner |
| +} // namespace mojo |