Don't add an aura tooltip to bubble close buttons on Windows.
[chromium-blink-merge.git] / net / base / io_buffer.cc
bloba375381bfd9bdc51f018506e0dbe6093647e7df2
1 // Copyright (c) 2011 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.
5 #include "net/base/io_buffer.h"
7 #include "base/logging.h"
9 namespace net {
11 IOBuffer::IOBuffer()
12 : data_(NULL) {
15 IOBuffer::IOBuffer(int buffer_size) {
16 CHECK_GE(buffer_size, 0);
17 data_ = new char[buffer_size];
20 IOBuffer::IOBuffer(char* data)
21 : data_(data) {
24 IOBuffer::~IOBuffer() {
25 delete[] data_;
26 data_ = NULL;
29 IOBufferWithSize::IOBufferWithSize(int size)
30 : IOBuffer(size),
31 size_(size) {
34 IOBufferWithSize::IOBufferWithSize(char* data, int size)
35 : IOBuffer(data),
36 size_(size) {
39 IOBufferWithSize::~IOBufferWithSize() {
42 StringIOBuffer::StringIOBuffer(const std::string& s)
43 : IOBuffer(static_cast<char*>(NULL)),
44 string_data_(s) {
45 CHECK_LT(s.size(), static_cast<size_t>(INT_MAX));
46 data_ = const_cast<char*>(string_data_.data());
49 StringIOBuffer::StringIOBuffer(scoped_ptr<std::string> s)
50 : IOBuffer(static_cast<char*>(NULL)) {
51 CHECK_LT(s->size(), static_cast<size_t>(INT_MAX));
52 string_data_.swap(*s.get());
53 data_ = const_cast<char*>(string_data_.data());
56 StringIOBuffer::~StringIOBuffer() {
57 // We haven't allocated the buffer, so remove it before the base class
58 // destructor tries to delete[] it.
59 data_ = NULL;
62 DrainableIOBuffer::DrainableIOBuffer(IOBuffer* base, int size)
63 : IOBuffer(base->data()),
64 base_(base),
65 size_(size),
66 used_(0) {
69 void DrainableIOBuffer::DidConsume(int bytes) {
70 SetOffset(used_ + bytes);
73 int DrainableIOBuffer::BytesRemaining() const {
74 return size_ - used_;
77 // Returns the number of consumed bytes.
78 int DrainableIOBuffer::BytesConsumed() const {
79 return used_;
82 void DrainableIOBuffer::SetOffset(int bytes) {
83 DCHECK_GE(bytes, 0);
84 DCHECK_LE(bytes, size_);
85 used_ = bytes;
86 data_ = base_->data() + used_;
89 DrainableIOBuffer::~DrainableIOBuffer() {
90 // The buffer is owned by the |base_| instance.
91 data_ = NULL;
94 GrowableIOBuffer::GrowableIOBuffer()
95 : IOBuffer(),
96 capacity_(0),
97 offset_(0) {
100 void GrowableIOBuffer::SetCapacity(int capacity) {
101 DCHECK_GE(capacity, 0);
102 // realloc will crash if it fails.
103 real_data_.reset(static_cast<char*>(realloc(real_data_.release(), capacity)));
104 capacity_ = capacity;
105 if (offset_ > capacity)
106 set_offset(capacity);
107 else
108 set_offset(offset_); // The pointer may have changed.
111 void GrowableIOBuffer::set_offset(int offset) {
112 DCHECK_GE(offset, 0);
113 DCHECK_LE(offset, capacity_);
114 offset_ = offset;
115 data_ = real_data_.get() + offset;
118 int GrowableIOBuffer::RemainingCapacity() {
119 return capacity_ - offset_;
122 char* GrowableIOBuffer::StartOfBuffer() {
123 return real_data_.get();
126 GrowableIOBuffer::~GrowableIOBuffer() {
127 data_ = NULL;
130 PickledIOBuffer::PickledIOBuffer() : IOBuffer() {}
132 void PickledIOBuffer::Done() {
133 data_ = const_cast<char*>(static_cast<const char*>(pickle_.data()));
136 PickledIOBuffer::~PickledIOBuffer() { data_ = NULL; }
138 WrappedIOBuffer::WrappedIOBuffer(const char* data)
139 : IOBuffer(const_cast<char*>(data)) {
142 WrappedIOBuffer::~WrappedIOBuffer() {
143 data_ = NULL;
146 } // namespace net