Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: content/browser/webui/i18n_source_stream.cc

Issue 2601313002: [MD settings] stream i18n replacement without large accumulation buffer (Closed)
Patch Set: review changes Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 // The replacement tag starts with '$' and ends with '}'. The white-space
49 input_.append(input_buffer->data(), input_buffer_size); 49 // characters are an optimization that looks for characters that are invalid
50 if (upstream_end_reached && !drain_offset_ && output_.empty()) { 50 // within $i18n{} tags.
51 // Process. 51 size_t pos = input_.find_last_of("$} \t\f\v\r\n");
mmenke 2017/01/03 20:47:32 Are spaces and tabs not allowed in string identifi
dschuyler 2017/01/03 21:51:40 Ah, I wondered if omitting them would beg the ques
52 output_ = ui::ReplaceTemplateExpressions(input_, *replacements_); 52 std::string to_process;
53 if (!upstream_end_reached && pos != std::string::npos && input_[pos] == '$') {
54 // If there is a trailing '$' then split the |input_| at that point. Process
55 // the first part; save the second part for the next call to FilterData().
56 to_process.assign(input_, 0, pos);
57 input_.erase(0, pos);
58 } else {
59 // There is no risk of a split key, process the whole input.
60 to_process.swap(input_);
53 } 61 }
54 62
55 if (drain_offset_ == output_.size()) 63 output_.append(ui::ReplaceTemplateExpressions(to_process, *replacements_));
56 return 0; 64 int bytes_out =
57 65 std::min(output_.size(), static_cast<size_t>(output_buffer_size));
58 // Drain. 66 output_.copy(output_buffer->data(), bytes_out);
59 int bytes_out = std::min(output_.size() - drain_offset_, 67 output_.erase(0, bytes_out);
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; 68 return bytes_out;
64 } 69 }
65 70
66 } // namespace content 71 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698