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

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

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

Powered by Google App Engine
This is Rietveld 408576698