Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: mojo/fetcher/data_fetcher.cc

Issue 1356623002: mojo: Add DataURLFetcher for processing data: urls. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: lolgyp Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/fetcher/data_fetcher.h ('k') | mojo/fetcher/data_fetcher_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/fetcher/data_fetcher.h"
6
7 #include "base/bind.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"
15
16 namespace mojo {
17 namespace fetcher {
18
19 ScopedDataPipeConsumerHandle CreateConsumerHandleForString(
20 const std::string& data) {
21 uint32_t num_bytes = static_cast<uint32_t>(data.size());
sky 2015/09/17 23:21:51 Might it be possible for the string to be bigger t
sadrul 2015/09/18 05:08:58 I suppose it could be. Perhaps we should check if
sky 2015/09/18 16:02:38 Fine by me.
22 MojoCreateDataPipeOptions options;
23 options.struct_size = sizeof(MojoCreateDataPipeOptions);
24 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE;
25 options.element_num_bytes = 1;
26 options.capacity_num_bytes = num_bytes;
27 mojo::DataPipe data_pipe(options);
28 MojoResult result =
29 WriteDataRaw(data_pipe.producer_handle.get(), data.data(), &num_bytes,
30 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE);
31 CHECK_EQ(MOJO_RESULT_OK, result);
32 return data_pipe.consumer_handle.Pass();
33 }
34
35 // static
36 void DataFetcher::Start(const GURL& url, const FetchCallback& loader_callback) {
37 // The object manages its own lifespan.
38 new DataFetcher(url, loader_callback);
39 }
40
41 DataFetcher::DataFetcher(const GURL& url, const FetchCallback& loader_callback)
42 : Fetcher(loader_callback), url_(url) {
43 BuildAndDispatchResponse();
44 }
45
46 DataFetcher::~DataFetcher() {}
47
48 void DataFetcher::BuildAndDispatchResponse() {
49 response_ = URLResponse::New();
50 response_->url = url_.spec();
51
52 response_->status_code = 400; // Bad request
53 if (url_.SchemeIs(url::kDataScheme)) {
54 std::string mime_type, charset, data;
55 if (net::DataURL::Parse(url_, &mime_type, &charset, &data)) {
56 response_->status_code = 200;
57 response_->mime_type = mime_type;
58 response_->charset = charset;
59 if (!data.empty())
60 response_->body = CreateConsumerHandleForString(data);
61 }
62 }
63
64 base::ThreadTaskRunnerHandle::Get()->PostTask(
65 FROM_HERE, base::Bind(loader_callback_,
66 base::Passed(make_scoped_ptr<Fetcher>(this))));
67 }
68
69 const GURL& DataFetcher::GetURL() const {
70 return url_;
71 }
72
73 GURL DataFetcher::GetRedirectURL() const {
74 return GURL::EmptyGURL();
75 }
76
77 GURL DataFetcher::GetRedirectReferer() const {
78 return GURL::EmptyGURL();
79 }
80
81 URLResponsePtr DataFetcher::AsURLResponse(base::TaskRunner* task_runner,
82 uint32_t skip) {
83 DCHECK(response_);
84 return response_.Pass();
85 }
86
87 void DataFetcher::AsPath(
88 base::TaskRunner* task_runner,
89 base::Callback<void(const base::FilePath&, bool)> callback) {
90 NOTIMPLEMENTED();
91 base::ThreadTaskRunnerHandle::Get()->PostTask(
92 FROM_HERE, base::Bind(callback, base::FilePath(), false));
93 }
94
95 std::string DataFetcher::MimeType() {
96 DCHECK(response_);
97 return response_->mime_type;
98 }
99
100 bool DataFetcher::HasMojoMagic() {
101 return false;
102 }
103
104 bool DataFetcher::PeekFirstLine(std::string* line) {
105 // This is only called for 'mojo magic' (i.e. detecting shebang'ed
106 // content-handler. Since HasMojoMagic() returns false above, this should
107 // never be reached.
108 NOTREACHED();
109 return false;
110 }
111
112 } // namespace fetcher
113 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/fetcher/data_fetcher.h ('k') | mojo/fetcher/data_fetcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698