| 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/shell/fetcher/data_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/location.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/thread_task_runner_handle.h" | |
| 17 #include "mojo/public/cpp/system/data_pipe.h" | |
| 18 #include "net/base/data_url.h" | |
| 19 | |
| 20 namespace mojo { | |
| 21 namespace shell { | |
| 22 | |
| 23 ScopedDataPipeConsumerHandle CreateConsumerHandleForString( | |
| 24 const std::string& data) { | |
| 25 if (data.size() > std::numeric_limits<uint32_t>::max()) | |
| 26 return ScopedDataPipeConsumerHandle(); | |
| 27 uint32_t num_bytes = static_cast<uint32_t>(data.size()); | |
| 28 MojoCreateDataPipeOptions options; | |
| 29 options.struct_size = sizeof(MojoCreateDataPipeOptions); | |
| 30 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; | |
| 31 options.element_num_bytes = 1; | |
| 32 options.capacity_num_bytes = num_bytes; | |
| 33 mojo::DataPipe data_pipe(options); | |
| 34 MojoResult result = | |
| 35 WriteDataRaw(data_pipe.producer_handle.get(), data.data(), &num_bytes, | |
| 36 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE); | |
| 37 CHECK_EQ(MOJO_RESULT_OK, result); | |
| 38 return std::move(data_pipe.consumer_handle); | |
| 39 } | |
| 40 | |
| 41 // static | |
| 42 void DataFetcher::Start(const GURL& url, const FetchCallback& loader_callback) { | |
| 43 // The object manages its own lifespan. | |
| 44 new DataFetcher(url, loader_callback); | |
| 45 } | |
| 46 | |
| 47 DataFetcher::DataFetcher(const GURL& url, const FetchCallback& loader_callback) | |
| 48 : Fetcher(loader_callback), url_(url) { | |
| 49 BuildAndDispatchResponse(); | |
| 50 } | |
| 51 | |
| 52 DataFetcher::~DataFetcher() {} | |
| 53 | |
| 54 void DataFetcher::BuildAndDispatchResponse() { | |
| 55 response_ = URLResponse::New(); | |
| 56 response_->url = url_.spec(); | |
| 57 | |
| 58 response_->status_code = 400; // Bad request | |
| 59 if (url_.SchemeIs(url::kDataScheme)) { | |
| 60 std::string mime_type, charset, data; | |
| 61 if (net::DataURL::Parse(url_, &mime_type, &charset, &data)) { | |
| 62 response_->status_code = 200; | |
| 63 response_->mime_type = mime_type; | |
| 64 response_->charset = charset; | |
| 65 if (!data.empty()) | |
| 66 response_->body = CreateConsumerHandleForString(data); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 71 FROM_HERE, base::Bind(loader_callback_, | |
| 72 base::Passed(make_scoped_ptr<Fetcher>(this)))); | |
| 73 } | |
| 74 | |
| 75 const GURL& DataFetcher::GetURL() const { | |
| 76 return url_; | |
| 77 } | |
| 78 | |
| 79 GURL DataFetcher::GetRedirectURL() const { | |
| 80 return GURL::EmptyGURL(); | |
| 81 } | |
| 82 | |
| 83 GURL DataFetcher::GetRedirectReferer() const { | |
| 84 return GURL::EmptyGURL(); | |
| 85 } | |
| 86 | |
| 87 URLResponsePtr DataFetcher::AsURLResponse(base::TaskRunner* task_runner, | |
| 88 uint32_t skip) { | |
| 89 DCHECK(response_); | |
| 90 return std::move(response_); | |
| 91 } | |
| 92 | |
| 93 void DataFetcher::AsPath( | |
| 94 base::TaskRunner* task_runner, | |
| 95 base::Callback<void(const base::FilePath&, bool)> callback) { | |
| 96 NOTIMPLEMENTED(); | |
| 97 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 98 FROM_HERE, base::Bind(callback, base::FilePath(), false)); | |
| 99 } | |
| 100 | |
| 101 std::string DataFetcher::MimeType() { | |
| 102 DCHECK(response_); | |
| 103 return response_->mime_type; | |
| 104 } | |
| 105 | |
| 106 bool DataFetcher::HasMojoMagic() { | |
| 107 return false; | |
| 108 } | |
| 109 | |
| 110 bool DataFetcher::PeekFirstLine(std::string* line) { | |
| 111 // This is only called for 'mojo magic' (i.e. detecting shebang'ed | |
| 112 // content-handler. Since HasMojoMagic() returns false above, this should | |
| 113 // never be reached. | |
| 114 NOTREACHED(); | |
| 115 return false; | |
| 116 } | |
| 117 | |
| 118 } // namespace shell | |
| 119 } // namespace mojo | |
| OLD | NEW |