Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / pdf / pdfium / pdfium_api_string_buffer_adapter.h
blob0005e97b77bbc0f6cf6a4bb2618ab615c877f5a5
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 #ifndef PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_
6 #define PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_
8 #include "base/basictypes.h"
9 #include "base/numerics/safe_math.h"
11 namespace chrome_pdf {
13 // Helper to deal with the fact that many PDFium APIs write the null-terminator
14 // into string buffers that are passed to them, but the PDF plugin likes to pass
15 // in std::strings / base::string16s, where one should not count on the internal
16 // string buffers to be null-terminated.
18 template <class StringType>
19 class PDFiumAPIStringBufferAdapter {
20 public:
21 // |str| is the string to write into.
22 // |expected_size| is the number of characters the PDFium API will write,
23 // including the null-terminator. It should be at least 1.
24 // |check_expected_size| whether to check the actual number of characters
25 // written into |str| against |expected_size| when calling Close().
26 PDFiumAPIStringBufferAdapter(StringType* str,
27 size_t expected_size,
28 bool check_expected_size);
29 ~PDFiumAPIStringBufferAdapter();
31 // Returns a pointer to |str_|'s buffer. The buffer's size is large enough to
32 // hold |expected_size_| + 1 characters, so the PDFium API that uses the
33 // pointer has space to write a null-terminator.
34 void* GetData();
36 // Resizes |str_| to |actual_size| - 1 characters, thereby removing the extra
37 // null-terminator. This must be called prior to the adapter's destruction.
38 // The pointer returned by GetData() should be considered invalid.
39 void Close(size_t actual_size);
41 template <typename IntType>
42 void Close(IntType actual_size) {
43 base::CheckedNumeric<size_t> unsigned_size = actual_size;
44 Close(unsigned_size.ValueOrDie());
47 private:
48 StringType* const str_;
49 void* const data_;
50 const size_t expected_size_;
51 const bool check_expected_size_;
52 bool is_closed_;
54 DISALLOW_COPY_AND_ASSIGN(PDFiumAPIStringBufferAdapter);
57 // Helper to deal with the fact that many PDFium APIs write the null-terminator
58 // into string buffers that are passed to them, but the PDF plugin likes to pass
59 // in std::strings / base::string16s, where one should not count on the internal
60 // string buffers to be null-terminated. This version is suitable for APIs that
61 // work in terms of number of bytes instead of the number of characters.
62 template <class StringType>
63 class PDFiumAPIStringBufferSizeInBytesAdapter {
64 public:
65 // |str| is the string to write into.
66 // |expected_size| is the number of bytes the PDFium API will write,
67 // including the null-terminator. It should be at least the size of a
68 // character in bytes.
69 // |check_expected_size| whether to check the actual number of bytes
70 // written into |str| against |expected_size| when calling Close().
71 PDFiumAPIStringBufferSizeInBytesAdapter(StringType* str,
72 size_t expected_size,
73 bool check_expected_size);
74 ~PDFiumAPIStringBufferSizeInBytesAdapter();
76 // Returns a pointer to |str_|'s buffer. The buffer's size is large enough to
77 // hold |expected_size_| + sizeof(StringType::value_type) bytes, so the PDFium
78 // API that uses the pointer has space to write a null-terminator.
79 void* GetData();
81 // Resizes |str_| to |actual_size| - sizeof(StringType::value_type) bytes,
82 // thereby removing the extra null-terminator. This must be called prior to
83 // the adapter's destruction. The pointer returned by GetData() should be
84 // considered invalid.
85 void Close(size_t actual_size);
87 template <typename IntType>
88 void Close(IntType actual_size) {
89 base::CheckedNumeric<size_t> unsigned_size = actual_size;
90 Close(unsigned_size.ValueOrDie());
93 private:
94 PDFiumAPIStringBufferAdapter<StringType> adapter_;
97 } // namespace chrome_pdf
99 #endif // PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_