Chromium Code Reviews| 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/runner/about_fetcher.h" | |
| 6 | |
| 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" | |
| 12 #include "mojo/shell/data_pipe_peek.h" | |
| 13 | |
| 14 namespace mojo { | |
| 15 namespace runner { | |
| 16 namespace { | |
| 17 | |
| 18 void RunFetcherCallback(const shell::Fetcher::FetchCallback& callback, | |
| 19 scoped_ptr<shell::Fetcher> fetcher, | |
| 20 bool success) { | |
| 21 callback.Run(success ? fetcher.Pass() : nullptr); | |
| 22 } | |
| 23 | |
| 24 } // namespace | |
| 25 | |
| 26 const char AboutFetcher::kAboutScheme[] = "about"; | |
| 27 const char AboutFetcher::kAboutBlankURL[] = "about:blank"; | |
| 28 | |
| 29 AboutFetcher::AboutFetcher(const GURL& url, | |
| 30 const FetchCallback& loader_callback) | |
| 31 : Fetcher(loader_callback), url_(url) { | |
| 32 BuildResponse(); | |
| 33 } | |
| 34 | |
| 35 AboutFetcher::~AboutFetcher() {} | |
| 36 | |
| 37 void AboutFetcher::BuildResponse() { | |
| 38 if (url_ != GURL(kAboutBlankURL)) { | |
| 39 PostToRunCallback(false); | |
| 40 return; | |
| 41 } | |
| 42 | |
| 43 response_ = URLResponse::New(); | |
| 44 response_->url = kAboutBlankURL; | |
| 45 response_->status_code = 200; | |
| 46 response_->mime_type = "text/html"; | |
| 47 PostToRunCallback(true); | |
| 48 } | |
| 49 | |
| 50 void AboutFetcher::PostToRunCallback(bool success) { | |
| 51 // Also pass |this| on failure, so that we won't destroy the object while the | |
| 52 // constructor is still on the call stack. | |
| 53 base::MessageLoop::current()->PostTask( | |
| 54 FROM_HERE, | |
| 55 base::Bind(RunFetcherCallback, loader_callback_, | |
| 56 base::Passed(make_scoped_ptr<Fetcher>(this)), success)); | |
| 57 } | |
| 58 | |
| 59 const GURL& AboutFetcher::GetURL() const { | |
| 60 return url_; | |
| 61 } | |
| 62 | |
| 63 GURL AboutFetcher::GetRedirectURL() const { | |
| 64 return GURL::EmptyGURL(); | |
| 65 } | |
| 66 | |
| 67 GURL AboutFetcher::GetRedirectReferer() const { | |
| 68 return GURL::EmptyGURL(); | |
| 69 } | |
| 70 | |
| 71 URLResponsePtr AboutFetcher::AsURLResponse(base::TaskRunner* task_runner, | |
| 72 uint32_t skip) { | |
| 73 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.
| |
| 74 MojoResult result = ReadDataRaw( | |
| 75 response_->body.get(), nullptr, &skip, | |
| 76 MOJO_READ_DATA_FLAG_ALL_OR_NONE | MOJO_READ_DATA_FLAG_DISCARD); | |
| 77 DCHECK_EQ(result, MOJO_RESULT_OK); | |
| 78 } | |
| 79 return response_.Pass(); | |
| 80 } | |
| 81 | |
| 82 void AboutFetcher::AsPath( | |
| 83 base::TaskRunner* task_runner, | |
| 84 base::Callback<void(const base::FilePath&, bool)> callback) { | |
| 85 NOTIMPLEMENTED(); | |
| 86 base::MessageLoop::current()->PostTask( | |
| 87 FROM_HERE, base::Bind(callback, base::FilePath(), false)); | |
| 88 } | |
| 89 | |
| 90 std::string AboutFetcher::MimeType() { | |
| 91 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.
| |
| 92 } | |
| 93 | |
| 94 bool AboutFetcher::HasMojoMagic() { | |
| 95 return false; | |
| 96 } | |
| 97 | |
| 98 bool AboutFetcher::PeekFirstLine(std::string* line) { | |
| 99 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.
| |
| 100 MOJO_DEADLINE_INDEFINITE); | |
| 101 } | |
| 102 | |
| 103 } // namespace runner | |
| 104 } // namespace mojo | |
| OLD | NEW |