| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/fetcher/data_fetcher.h" | 5 #include "mojo/fetcher/data_fetcher.h" |
| 6 | 6 |
| 7 #include <utility> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 9 #include "base/location.h" | 11 #include "base/location.h" |
| 10 #include "base/logging.h" | 12 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
| 13 #include "mojo/public/cpp/system/data_pipe.h" | 15 #include "mojo/public/cpp/system/data_pipe.h" |
| 14 #include "net/base/data_url.h" | 16 #include "net/base/data_url.h" |
| 15 | 17 |
| 16 namespace mojo { | 18 namespace mojo { |
| 17 namespace fetcher { | 19 namespace fetcher { |
| 18 | 20 |
| 19 ScopedDataPipeConsumerHandle CreateConsumerHandleForString( | 21 ScopedDataPipeConsumerHandle CreateConsumerHandleForString( |
| 20 const std::string& data) { | 22 const std::string& data) { |
| 21 if (data.size() > std::numeric_limits<uint32_t>::max()) | 23 if (data.size() > std::numeric_limits<uint32_t>::max()) |
| 22 return ScopedDataPipeConsumerHandle(); | 24 return ScopedDataPipeConsumerHandle(); |
| 23 uint32_t num_bytes = static_cast<uint32_t>(data.size()); | 25 uint32_t num_bytes = static_cast<uint32_t>(data.size()); |
| 24 MojoCreateDataPipeOptions options; | 26 MojoCreateDataPipeOptions options; |
| 25 options.struct_size = sizeof(MojoCreateDataPipeOptions); | 27 options.struct_size = sizeof(MojoCreateDataPipeOptions); |
| 26 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; | 28 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; |
| 27 options.element_num_bytes = 1; | 29 options.element_num_bytes = 1; |
| 28 options.capacity_num_bytes = num_bytes; | 30 options.capacity_num_bytes = num_bytes; |
| 29 mojo::DataPipe data_pipe(options); | 31 mojo::DataPipe data_pipe(options); |
| 30 MojoResult result = | 32 MojoResult result = |
| 31 WriteDataRaw(data_pipe.producer_handle.get(), data.data(), &num_bytes, | 33 WriteDataRaw(data_pipe.producer_handle.get(), data.data(), &num_bytes, |
| 32 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE); | 34 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE); |
| 33 CHECK_EQ(MOJO_RESULT_OK, result); | 35 CHECK_EQ(MOJO_RESULT_OK, result); |
| 34 return data_pipe.consumer_handle.Pass(); | 36 return std::move(data_pipe.consumer_handle); |
| 35 } | 37 } |
| 36 | 38 |
| 37 // static | 39 // static |
| 38 void DataFetcher::Start(const GURL& url, const FetchCallback& loader_callback) { | 40 void DataFetcher::Start(const GURL& url, const FetchCallback& loader_callback) { |
| 39 // The object manages its own lifespan. | 41 // The object manages its own lifespan. |
| 40 new DataFetcher(url, loader_callback); | 42 new DataFetcher(url, loader_callback); |
| 41 } | 43 } |
| 42 | 44 |
| 43 DataFetcher::DataFetcher(const GURL& url, const FetchCallback& loader_callback) | 45 DataFetcher::DataFetcher(const GURL& url, const FetchCallback& loader_callback) |
| 44 : Fetcher(loader_callback), url_(url) { | 46 : Fetcher(loader_callback), url_(url) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 return GURL::EmptyGURL(); | 78 return GURL::EmptyGURL(); |
| 77 } | 79 } |
| 78 | 80 |
| 79 GURL DataFetcher::GetRedirectReferer() const { | 81 GURL DataFetcher::GetRedirectReferer() const { |
| 80 return GURL::EmptyGURL(); | 82 return GURL::EmptyGURL(); |
| 81 } | 83 } |
| 82 | 84 |
| 83 URLResponsePtr DataFetcher::AsURLResponse(base::TaskRunner* task_runner, | 85 URLResponsePtr DataFetcher::AsURLResponse(base::TaskRunner* task_runner, |
| 84 uint32_t skip) { | 86 uint32_t skip) { |
| 85 DCHECK(response_); | 87 DCHECK(response_); |
| 86 return response_.Pass(); | 88 return std::move(response_); |
| 87 } | 89 } |
| 88 | 90 |
| 89 void DataFetcher::AsPath( | 91 void DataFetcher::AsPath( |
| 90 base::TaskRunner* task_runner, | 92 base::TaskRunner* task_runner, |
| 91 base::Callback<void(const base::FilePath&, bool)> callback) { | 93 base::Callback<void(const base::FilePath&, bool)> callback) { |
| 92 NOTIMPLEMENTED(); | 94 NOTIMPLEMENTED(); |
| 93 base::ThreadTaskRunnerHandle::Get()->PostTask( | 95 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 94 FROM_HERE, base::Bind(callback, base::FilePath(), false)); | 96 FROM_HERE, base::Bind(callback, base::FilePath(), false)); |
| 95 } | 97 } |
| 96 | 98 |
| 97 std::string DataFetcher::MimeType() { | 99 std::string DataFetcher::MimeType() { |
| 98 DCHECK(response_); | 100 DCHECK(response_); |
| 99 return response_->mime_type; | 101 return response_->mime_type; |
| 100 } | 102 } |
| 101 | 103 |
| 102 bool DataFetcher::HasMojoMagic() { | 104 bool DataFetcher::HasMojoMagic() { |
| 103 return false; | 105 return false; |
| 104 } | 106 } |
| 105 | 107 |
| 106 bool DataFetcher::PeekFirstLine(std::string* line) { | 108 bool DataFetcher::PeekFirstLine(std::string* line) { |
| 107 // This is only called for 'mojo magic' (i.e. detecting shebang'ed | 109 // This is only called for 'mojo magic' (i.e. detecting shebang'ed |
| 108 // content-handler. Since HasMojoMagic() returns false above, this should | 110 // content-handler. Since HasMojoMagic() returns false above, this should |
| 109 // never be reached. | 111 // never be reached. |
| 110 NOTREACHED(); | 112 NOTREACHED(); |
| 111 return false; | 113 return false; |
| 112 } | 114 } |
| 113 | 115 |
| 114 } // namespace fetcher | 116 } // namespace fetcher |
| 115 } // namespace mojo | 117 } // namespace mojo |
| OLD | NEW |