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 #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); | |
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
| |
49 *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
| |
50 } else if (!input_.empty() && output_.empty()) { | |
51 // Process. | |
52 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.
| |
53 *replacements_); | |
54 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.
| |
55 } | |
56 | |
57 if (!output_.empty()) { | |
58 // Drain. | |
59 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
| |
60 output_.copy(output_buffer->data(), bytes_out); | |
61 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
| |
62 } | |
63 | |
64 return bytes_out; | |
65 } | |
66 | |
67 } // namespace content | |
OLD | NEW |