OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "content/browser/webui/i18n_source_stream.h" | |
6 | |
7 #include <algorithm> | |
8 #include <utility> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/strings/string_piece.h" | |
12 #include "net/base/io_buffer.h" | |
13 | |
14 namespace content { | |
15 | |
16 I18nSourceStream::~I18nSourceStream() {} | |
17 | |
18 std::unique_ptr<I18nSourceStream> I18nSourceStream::Create( | |
19 std::unique_ptr<SourceStream> upstream, | |
20 SourceStream::SourceType type, | |
21 const ui::TemplateReplacements* replacements) { | |
22 DCHECK(replacements); | |
23 std::unique_ptr<I18nSourceStream> source( | |
24 new I18nSourceStream(std::move(upstream), type, replacements)); | |
25 return source; | |
26 } | |
27 | |
28 I18nSourceStream::I18nSourceStream(std::unique_ptr<SourceStream> upstream, | |
29 SourceStream::SourceType type, | |
30 const ui::TemplateReplacements* replacements) | |
31 : FilterSourceStream(type, std::move(upstream)), | |
32 drain_offset_(0), | |
33 replacements_(replacements) {} | |
34 | |
35 std::string I18nSourceStream::GetTypeAsString() const { | |
36 return "i18n"; | |
37 } | |
38 | |
39 int I18nSourceStream::FilterData(net::IOBuffer* output_buffer, | |
40 int output_buffer_size, | |
41 net::IOBuffer* input_buffer, | |
42 int input_buffer_size, | |
43 int* consumed_bytes, | |
44 bool upstream_end_reached) { | |
mmenke
2016/12/14 16:10:45
optional: Suggest DCHECK(output_.empty() || (upst
dschuyler
2016/12/15 00:17:08
Done.
| |
45 *consumed_bytes = input_buffer_size; | |
46 int bytes_out = 0; | |
mmenke
2016/12/14 16:10:45
If you decide not to go with my suggestion below,
dschuyler
2016/12/15 00:17:09
Done.
| |
47 // TODO(dschuyler): Perform replacements without accumulation. | |
48 // Accumulate. | |
49 input_.append(input_buffer->data(), input_buffer_size); | |
50 if (upstream_end_reached && !drain_offset_ && output_.empty()) { | |
51 // Process. | |
52 output_ = ui::ReplaceTemplateExpressions(input_, *replacements_); | |
53 } | |
54 | |
55 if (drain_offset_ < output_.size()) { | |
mmenke
2016/12/14 16:10:45
optional nit: When there's nothing else to do, ea
dschuyler
2016/12/15 00:17:09
Done.
| |
56 // Drain. | |
57 bytes_out = std::min(output_.size() - drain_offset_, | |
58 static_cast<size_t>(output_buffer_size)); | |
59 output_.copy(output_buffer->data(), bytes_out, drain_offset_); | |
60 drain_offset_ += bytes_out; | |
61 } | |
62 | |
63 return bytes_out; | |
64 } | |
65 | |
66 } // namespace content | |
OLD | NEW |