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/data_fetcher.h"
8 #include "base/files/file_path.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "net/base/data_url.h"
14 #include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h"
19 ScopedDataPipeConsumerHandle
CreateConsumerHandleForString(
20 const std::string
& data
) {
21 if (data
.size() > std::numeric_limits
<uint32_t>::max())
22 return ScopedDataPipeConsumerHandle();
23 uint32_t num_bytes
= static_cast<uint32_t>(data
.size());
24 MojoCreateDataPipeOptions options
;
25 options
.struct_size
= sizeof(MojoCreateDataPipeOptions
);
26 options
.flags
= MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE
;
27 options
.element_num_bytes
= 1;
28 options
.capacity_num_bytes
= num_bytes
;
29 mojo::DataPipe
data_pipe(options
);
31 WriteDataRaw(data_pipe
.producer_handle
.get(), data
.data(), &num_bytes
,
32 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE
);
33 CHECK_EQ(MOJO_RESULT_OK
, result
);
34 return data_pipe
.consumer_handle
.Pass();
38 void DataFetcher::Start(const GURL
& url
, const FetchCallback
& loader_callback
) {
39 // The object manages its own lifespan.
40 new DataFetcher(url
, loader_callback
);
43 DataFetcher::DataFetcher(const GURL
& url
, const FetchCallback
& loader_callback
)
44 : Fetcher(loader_callback
), url_(url
) {
45 BuildAndDispatchResponse();
48 DataFetcher::~DataFetcher() {}
50 void DataFetcher::BuildAndDispatchResponse() {
51 response_
= URLResponse::New();
52 response_
->url
= url_
.spec();
54 response_
->status_code
= 400; // Bad request
55 if (url_
.SchemeIs(url::kDataScheme
)) {
56 std::string mime_type
, charset
, data
;
57 if (net::DataURL::Parse(url_
, &mime_type
, &charset
, &data
)) {
58 response_
->status_code
= 200;
59 response_
->mime_type
= mime_type
;
60 response_
->charset
= charset
;
62 response_
->body
= CreateConsumerHandleForString(data
);
66 base::ThreadTaskRunnerHandle::Get()->PostTask(
67 FROM_HERE
, base::Bind(loader_callback_
,
68 base::Passed(make_scoped_ptr
<Fetcher
>(this))));
71 const GURL
& DataFetcher::GetURL() const {
75 GURL
DataFetcher::GetRedirectURL() const {
76 return GURL::EmptyGURL();
79 GURL
DataFetcher::GetRedirectReferer() const {
80 return GURL::EmptyGURL();
83 URLResponsePtr
DataFetcher::AsURLResponse(base::TaskRunner
* task_runner
,
86 return response_
.Pass();
89 void DataFetcher::AsPath(
90 base::TaskRunner
* task_runner
,
91 base::Callback
<void(const base::FilePath
&, bool)> callback
) {
93 base::ThreadTaskRunnerHandle::Get()->PostTask(
94 FROM_HERE
, base::Bind(callback
, base::FilePath(), false));
97 std::string
DataFetcher::MimeType() {
99 return response_
->mime_type
;
102 bool DataFetcher::HasMojoMagic() {
106 bool DataFetcher::PeekFirstLine(std::string
* line
) {
107 // This is only called for 'mojo magic' (i.e. detecting shebang'ed
108 // content-handler. Since HasMojoMagic() returns false above, this should
114 } // namespace fetcher