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

Side by Side Diff: blimp/net/compressed_packet_reader.cc

Issue 2632803002: Remove all blimp network code. (Closed)
Patch Set: merge from origin/master for good measure 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
« no previous file with comments | « blimp/net/compressed_packet_reader.h ('k') | blimp/net/compressed_packet_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 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 "blimp/net/compressed_packet_reader.h"
6
7 #include <algorithm>
8 #include <iostream>
9 #include <utility>
10
11 #include "base/callback_helpers.h"
12 #include "base/logging.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/sys_byteorder.h"
16 #include "blimp/net/common.h"
17 #include "net/base/io_buffer.h"
18 #include "net/base/net_errors.h"
19 #include "net/socket/stream_socket.h"
20
21 namespace blimp {
22 namespace {
23
24 constexpr double kInitialDecompressionBufferSizeFactor = 1.5;
25 constexpr double kDecompressionGrowthFactor = 2.0;
26
27 } // namespace
28
29 CompressedPacketReader::CompressedPacketReader(
30 std::unique_ptr<PacketReader> source)
31 : source_(std::move(source)),
32 compressed_buf_(new net::GrowableIOBuffer),
33 weak_factory_(this) {
34 DCHECK(source_);
35
36 memset(&zlib_stream_, 0, sizeof(z_stream));
37
38 // MAX_WBITS means we are using the maximal window size for decompression;
39 // a negative value means that we are ignoring headers and CRC checks.
40 int init_result = inflateInit2(&zlib_stream_, -MAX_WBITS);
41 DCHECK_EQ(Z_OK, init_result);
42 }
43
44 CompressedPacketReader::~CompressedPacketReader() {
45 inflateEnd(&zlib_stream_);
46 }
47
48 void CompressedPacketReader::ReadPacket(
49 const scoped_refptr<net::GrowableIOBuffer>& decompressed_buf,
50 const net::CompletionCallback& callback) {
51 DCHECK(decompressed_buf);
52 DCHECK(!callback.is_null());
53
54 source_->ReadPacket(
55 compressed_buf_,
56 base::Bind(&CompressedPacketReader::OnCompressedPacketReceived,
57 weak_factory_.GetWeakPtr(), decompressed_buf, callback));
58 }
59
60 void CompressedPacketReader::OnCompressedPacketReceived(
61 const scoped_refptr<net::GrowableIOBuffer> decompressed_buf,
62 const net::CompletionCallback& callback,
63 int result) {
64 if (result <= 0) {
65 callback.Run(result);
66 return;
67 }
68
69 callback.Run(DecompressPacket(decompressed_buf, result));
70 }
71
72 int CompressedPacketReader::DecompressPacket(
73 const scoped_refptr<net::GrowableIOBuffer>& decompressed_buf,
74 int size_compressed) {
75 scoped_refptr<net::DrainableIOBuffer> drainable_input(
76 new net::DrainableIOBuffer(compressed_buf_.get(), size_compressed));
77
78 // Prepare the sink for decompressed data.
79 decompressed_buf->set_offset(0);
80 const int min_size = kInitialDecompressionBufferSizeFactor * size_compressed;
81 if (decompressed_buf->capacity() < min_size) {
82 decompressed_buf->SetCapacity(min_size);
83 }
84
85 // Repeatedly decompress |drainable_input| until it's fully consumed, growing
86 // |decompressed_buf| as necessary to accomodate the decompressed output.
87 do {
88 zlib_stream_.next_in = reinterpret_cast<uint8_t*>(drainable_input->data());
89 zlib_stream_.avail_in = drainable_input->BytesRemaining();
90 zlib_stream_.next_out =
91 reinterpret_cast<uint8_t*>(decompressed_buf->data());
92 zlib_stream_.avail_out = decompressed_buf->RemainingCapacity();
93 int inflate_result = inflate(&zlib_stream_, Z_SYNC_FLUSH);
94 if (inflate_result != Z_OK) {
95 DLOG(ERROR) << "inflate() returned unexpected error code: "
96 << inflate_result;
97 return net::ERR_UNEXPECTED;
98 }
99
100 // Process the inflate() result.
101 const int bytes_in =
102 drainable_input->BytesRemaining() - zlib_stream_.avail_in;
103 const int bytes_out =
104 (decompressed_buf->RemainingCapacity() - zlib_stream_.avail_out);
105 drainable_input->DidConsume(bytes_in);
106 decompressed_buf->set_offset(decompressed_buf->offset() + bytes_out);
107 if (static_cast<size_t>(decompressed_buf->offset()) >
108 kMaxPacketPayloadSizeBytes) {
109 DLOG(ERROR)
110 << "Decompressed buffer size exceeds allowable limits; aborting.";
111 return net::ERR_FILE_TOO_BIG;
112 }
113
114 if (drainable_input->BytesRemaining() > 0) {
115 // Output buffer isn't large enough to fit the compressed input, so
116 // enlarge it.
117 DCHECK_GT(zlib_stream_.avail_in, 0u);
118 DCHECK_EQ(0u, zlib_stream_.avail_out);
119
120 decompressed_buf->SetCapacity(
121 std::min(static_cast<size_t>(kDecompressionGrowthFactor *
122 decompressed_buf->capacity()),
123 kMaxPacketPayloadSizeBytes + 1));
124 VLOG(2) << "Increase buffer size to " << decompressed_buf->capacity()
125 << " bytes.";
126 }
127 } while (zlib_stream_.avail_in > 0);
128
129 int total_decompressed_size = decompressed_buf->offset();
130 decompressed_buf->set_offset(0);
131 return total_decompressed_size;
132 }
133
134 } // namespace blimp
OLDNEW
« no previous file with comments | « blimp/net/compressed_packet_reader.h ('k') | blimp/net/compressed_packet_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698