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) { |
| 45 DCHECK(output_.empty() || (upstream_end_reached && input_buffer_size == 0)); |
| 46 *consumed_bytes = input_buffer_size; |
| 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()) |
| 56 return 0; |
| 57 |
| 58 // Drain. |
| 59 int bytes_out = std::min(output_.size() - drain_offset_, |
| 60 static_cast<size_t>(output_buffer_size)); |
| 61 output_.copy(output_buffer->data(), bytes_out, drain_offset_); |
| 62 drain_offset_ += bytes_out; |
| 63 return bytes_out; |
| 64 } |
| 65 |
| 66 } // namespace content |
OLD | NEW |