Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/webui/i18n_source_stream.h" | 5 #include "content/browser/webui/i18n_source_stream.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 DCHECK(replacements); | 22 DCHECK(replacements); |
| 23 std::unique_ptr<I18nSourceStream> source( | 23 std::unique_ptr<I18nSourceStream> source( |
| 24 new I18nSourceStream(std::move(upstream), type, replacements)); | 24 new I18nSourceStream(std::move(upstream), type, replacements)); |
| 25 return source; | 25 return source; |
| 26 } | 26 } |
| 27 | 27 |
| 28 I18nSourceStream::I18nSourceStream(std::unique_ptr<SourceStream> upstream, | 28 I18nSourceStream::I18nSourceStream(std::unique_ptr<SourceStream> upstream, |
| 29 SourceStream::SourceType type, | 29 SourceStream::SourceType type, |
| 30 const ui::TemplateReplacements* replacements) | 30 const ui::TemplateReplacements* replacements) |
| 31 : FilterSourceStream(type, std::move(upstream)), | 31 : FilterSourceStream(type, std::move(upstream)), |
| 32 drain_offset_(0), | |
| 33 replacements_(replacements) {} | 32 replacements_(replacements) {} |
| 34 | 33 |
| 35 std::string I18nSourceStream::GetTypeAsString() const { | 34 std::string I18nSourceStream::GetTypeAsString() const { |
| 36 return "i18n"; | 35 return "i18n"; |
| 37 } | 36 } |
| 38 | 37 |
| 39 int I18nSourceStream::FilterData(net::IOBuffer* output_buffer, | 38 int I18nSourceStream::FilterData(net::IOBuffer* output_buffer, |
| 40 int output_buffer_size, | 39 int output_buffer_size, |
| 41 net::IOBuffer* input_buffer, | 40 net::IOBuffer* input_buffer, |
| 42 int input_buffer_size, | 41 int input_buffer_size, |
| 43 int* consumed_bytes, | 42 int* consumed_bytes, |
| 44 bool upstream_end_reached) { | 43 bool upstream_end_reached) { |
| 45 DCHECK(output_.empty() || (upstream_end_reached && input_buffer_size == 0)); | 44 // |input_| is often empty (or it may have something from the prior call). |
| 45 input_.append(input_buffer->data(), input_buffer_size); | |
| 46 *consumed_bytes = input_buffer_size; | 46 *consumed_bytes = input_buffer_size; |
| 47 // TODO(dschuyler): Perform replacements without accumulation. | 47 |
| 48 // Accumulate. | 48 size_t pos = input_.find_last_of("$}"); |
|
mmenke
2017/01/03 16:47:16
optional: Maybe toss in "\r\n"? Not sure how com
dschuyler
2017/01/03 20:37:34
Done.
| |
| 49 input_.append(input_buffer->data(), input_buffer_size); | 49 std::string to_process; |
| 50 if (upstream_end_reached && !drain_offset_ && output_.empty()) { | 50 if (!upstream_end_reached && pos != std::string::npos && input_[pos] == '$') { |
|
mmenke
2017/01/03 16:47:16
A string ending in "$" now has special case code.
dschuyler
2017/01/03 20:37:34
Done.
| |
| 51 // Process. | 51 // If there is a trailing '$' then split the |input_| at that point. Process |
| 52 output_ = ui::ReplaceTemplateExpressions(input_, *replacements_); | 52 // the first part; save the second part for the next call to FilterData(). |
| 53 to_process.assign(input_, 0, pos); | |
| 54 input_.assign(input_, pos, std::string::npos); | |
|
mmenke
2017/01/03 16:47:16
input_.erase(0, pos);? Should do the same thing,
dschuyler
2017/01/03 20:37:34
Done.
| |
| 55 } else { | |
| 56 // There is no risk of a split key, process the whole input. | |
| 57 to_process = input_; | |
| 58 input_.clear(); | |
|
mmenke
2017/01/03 16:47:16
to_process.swap(input_); accomplishes what both t
dschuyler
2017/01/03 20:37:34
Done.
| |
| 53 } | 59 } |
| 54 | 60 |
| 55 if (drain_offset_ == output_.size()) | 61 output_.append(ui::ReplaceTemplateExpressions(to_process, *replacements_)); |
| 56 return 0; | 62 int bytes_out = std::min(output_.size(), |
| 57 | |
| 58 // Drain. | |
| 59 int bytes_out = std::min(output_.size() - drain_offset_, | |
| 60 static_cast<size_t>(output_buffer_size)); | 63 static_cast<size_t>(output_buffer_size)); |
| 61 output_.copy(output_buffer->data(), bytes_out, drain_offset_); | 64 output_.copy(output_buffer->data(), bytes_out); |
| 62 drain_offset_ += bytes_out; | 65 output_.assign(output_, bytes_out, std::string::npos); |
|
mmenke
2017/01/03 16:47:16
ouput_.erase(0, bytes_out);
dschuyler
2017/01/03 20:37:34
Done.
| |
| 63 return bytes_out; | 66 return bytes_out; |
| 64 } | 67 } |
| 65 | 68 |
| 66 } // namespace content | 69 } // namespace content |
| OLD | NEW |