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