Roll src/third_party/WebKit 6b63e20:35e1984 (svn 201060:201061)
[chromium-blink-merge.git] / pdf / pdfium / pdfium_api_string_buffer_adapter.cc
blobc5be3021d5b367ca3c5f122b1d97be58b24a9db9
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.
5 #include "pdf/pdfium/pdfium_api_string_buffer_adapter.h"
7 #include <string>
9 #include "base/logging.h"
10 #include "base/numerics/safe_math.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/string_util.h"
14 namespace chrome_pdf {
16 template <class StringType>
17 PDFiumAPIStringBufferAdapter<StringType>::PDFiumAPIStringBufferAdapter(
18 StringType* str,
19 size_t expected_size,
20 bool check_expected_size)
21 : str_(str),
22 data_(base::WriteInto(str, expected_size + 1)),
23 expected_size_(expected_size),
24 check_expected_size_(check_expected_size),
25 is_closed_(false) {
28 template <class StringType>
29 PDFiumAPIStringBufferAdapter<StringType>::~PDFiumAPIStringBufferAdapter() {
30 DCHECK(is_closed_);
33 template <class StringType>
34 void* PDFiumAPIStringBufferAdapter<StringType>::GetData() {
35 DCHECK(!is_closed_);
36 return data_;
39 template <class StringType>
40 void PDFiumAPIStringBufferAdapter<StringType>::Close(int actual_size) {
41 base::CheckedNumeric<size_t> unsigned_size = actual_size;
42 Close(unsigned_size.ValueOrDie());
45 template <class StringType>
46 void PDFiumAPIStringBufferAdapter<StringType>::Close(size_t actual_size) {
47 DCHECK(!is_closed_);
48 is_closed_ = true;
50 if (check_expected_size_)
51 DCHECK_EQ(expected_size_, actual_size);
53 if (actual_size > 0) {
54 DCHECK((*str_)[actual_size - 1] == 0);
55 str_->resize(actual_size - 1);
56 } else {
57 str_->clear();
61 // explicit instantiations
62 template class PDFiumAPIStringBufferAdapter<std::string>;
63 template class PDFiumAPIStringBufferAdapter<base::string16>;
65 } // namespace chrome_pdf