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 namespace content { |
| 11 |
| 12 I18nSourceStream::~I18nSourceStream() {} |
| 13 |
| 14 std::unique_ptr<I18nSourceStream> I18nSourceStream::Create( |
| 15 std::unique_ptr<SourceStream> upstream, |
| 16 SourceStream::SourceType type) { |
| 17 std::unique_ptr<I18nSourceStream> source( |
| 18 new I18nSourceStream(std::move(upstream), type)); |
| 19 return source; |
| 20 } |
| 21 |
| 22 I18nSourceStream::I18nSourceStream(std::unique_ptr<SourceStream> upstream, |
| 23 SourceStream::SourceType type) |
| 24 : FilterSourceStream(type, std::move(upstream)) {} |
| 25 |
| 26 std::string I18nSourceStream::GetTypeAsString() const { |
| 27 return "i18n"; |
| 28 } |
| 29 |
| 30 int I18nSourceStream::FilterData(net::IOBuffer* output_buffer, |
| 31 int output_buffer_size, |
| 32 net::IOBuffer* input_buffer, |
| 33 int input_buffer_size, |
| 34 int* consumed_bytes, |
| 35 bool upstream_end_reached) { |
| 36 *consumed_bytes = 0; |
| 37 int bytes_out = 0; |
| 38 // TODO(dschuyler): Perform replacements without accumulation. |
| 39 if (!upstream_end_reached) { |
| 40 // Accumulate. |
| 41 input_.append(input_buffer->data(), input_buffer_size); |
| 42 *consumed_bytes = input_buffer_size; |
| 43 } else if (!input_.empty() && output_.empty()) { |
| 44 // Process. |
| 45 DCHECK(replacements_); |
| 46 output_ = ui::ReplaceTemplateExpressions(base::StringPiece(input_), |
| 47 *replacements_); |
| 48 input_.clear(); |
| 49 } |
| 50 |
| 51 if (!output_.empty()) { |
| 52 // Drain. |
| 53 bytes_out = std::min(static_cast<int>(output_.size()), output_buffer_size); |
| 54 output_.copy(output_buffer->data(), bytes_out); |
| 55 output_.erase(0, bytes_out); |
| 56 } |
| 57 |
| 58 return bytes_out; |
| 59 } |
| 60 |
| 61 void I18nSourceStream::SetReplacements( |
| 62 const ui::TemplateReplacements* replacements) { |
| 63 replacements_ = replacements; |
| 64 } |
| 65 |
| 66 } // namespace content |
OLD | NEW |