Index: content/browser/webui/i18n_source_stream.cc |
diff --git a/content/browser/webui/i18n_source_stream.cc b/content/browser/webui/i18n_source_stream.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2cbf7a1cf5e70833f7d4a07dbb397441d2ec0437 |
--- /dev/null |
+++ b/content/browser/webui/i18n_source_stream.cc |
@@ -0,0 +1,67 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/webui/i18n_source_stream.h" |
+ |
+#include <algorithm> |
+#include <utility> |
+ |
+#include "base/logging.h" |
+#include "net/base/io_buffer.h" |
+ |
+namespace content { |
+ |
+I18nSourceStream::~I18nSourceStream() {} |
+ |
+std::unique_ptr<I18nSourceStream> I18nSourceStream::Create( |
+ std::unique_ptr<SourceStream> upstream, |
+ SourceStream::SourceType type, |
+ const ui::TemplateReplacements* replacements) { |
+ DCHECK(replacements); |
+ std::unique_ptr<I18nSourceStream> source( |
+ new I18nSourceStream(std::move(upstream), type, replacements)); |
+ return source; |
+} |
+ |
+I18nSourceStream::I18nSourceStream(std::unique_ptr<SourceStream> upstream, |
+ SourceStream::SourceType type, |
+ const ui::TemplateReplacements* replacements) |
+ : FilterSourceStream(type, std::move(upstream)), |
+ replacements_(replacements) {} |
+ |
+std::string I18nSourceStream::GetTypeAsString() const { |
+ return "i18n"; |
+} |
+ |
+int I18nSourceStream::FilterData(net::IOBuffer* output_buffer, |
+ int output_buffer_size, |
+ net::IOBuffer* input_buffer, |
+ int input_buffer_size, |
+ int* consumed_bytes, |
+ bool upstream_end_reached) { |
+ *consumed_bytes = 0; |
+ int bytes_out = 0; |
+ // TODO(dschuyler): Perform replacements without accumulation. |
+ if (!upstream_end_reached) { |
+ // Accumulate. |
+ input_.append(input_buffer->data(), input_buffer_size); |
mmenke
2016/12/09 18:40:17
I assume there's no way for non-chrome files to re
dschuyler
2016/12/09 22:31:17
Same here, but:
Is there something that should ch
mmenke
2016/12/09 22:54:08
You're buffering the entire response in a single s
mmenke
2016/12/09 22:55:21
Also, just from a more general memory standpoint,
dschuyler
2016/12/10 00:05:55
Acknowledged.
dschuyler
2016/12/10 00:05:55
I'd like to make this more streaming, that's what
|
+ *consumed_bytes = input_buffer_size; |
mmenke
2016/12/09 18:40:17
I think both of these lines can be done unconditio
dschuyler
2016/12/09 22:31:17
I agree that they could be moved outside of the if
mmenke
2016/12/09 22:54:08
I'm not sure we're on the same page here. The API
dschuyler
2016/12/10 00:05:55
I didn't know that. Thanks, that helps me understa
|
+ } else if (!input_.empty() && output_.empty()) { |
+ // Process. |
+ output_ = ui::ReplaceTemplateExpressions(base::StringPiece(input_), |
mmenke
2016/12/09 18:40:17
base::StringPiece not needed - it should be implic
mmenke
2016/12/09 18:40:17
Include string_piece.h (Even without the explicit
dschuyler
2016/12/09 22:31:16
Done.
dschuyler
2016/12/09 22:31:17
Done.
|
+ *replacements_); |
+ input_.clear(); |
mmenke
2016/12/09 18:40:17
Shouldn't modify input_ here. I don't think it br
dschuyler
2016/12/09 22:31:16
done.
|
+ } |
+ |
+ if (!output_.empty()) { |
+ // Drain. |
+ bytes_out = std::min(static_cast<int>(output_.size()), output_buffer_size); |
mmenke
2016/12/09 18:40:17
Doesn't really matter, but from a "this works even
dschuyler
2016/12/09 22:31:16
I can see that being good practice in general (con
mmenke
2016/12/09 22:54:08
There's nothing preventing output_buffer_size_ fro
dschuyler
2016/12/10 00:05:55
I wasn't seeing the signed vs unsigned issue. Than
|
+ output_.copy(output_buffer->data(), bytes_out); |
+ output_.erase(0, bytes_out); |
mmenke
2016/12/09 18:40:17
Is it worth the erase? The memory allocated by th
dschuyler
2016/12/09 22:31:16
Depending on how erase() is implemented, this may
|
+ } |
+ |
+ return bytes_out; |
+} |
+ |
+} // namespace content |