Linux Sandbox: make sure Credentials test pass on TSAN
[chromium-blink-merge.git] / pdf / pdfium / pdfium_api_string_buffer_adapter.h
bloba36879f112bc745dc264b997192fdd8cf9c54e88
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"
10 namespace chrome_pdf {
12 // Helper to deal with the fact that many PDFium APIs write the null-terminator
13 // into string buffers that are passed to them, but the PDF plugin likes to pass
14 // in std::strings / base::string16s, where one should not count on the internal
15 // string buffers to be null-terminated.
17 template <class StringType>
18 class PDFiumAPIStringBufferAdapter {
19 public:
20 // |str| is the string to write into.
21 // |expected_size| is the number of characters the PDFium API will write,
22 // including the null-terminator. It should be at least 1.
23 // |check_expected_size| whether to check the actual number of characters
24 // written into |str| against |expected_size| when calling Close().
25 PDFiumAPIStringBufferAdapter(StringType* str,
26 size_t expected_size,
27 bool check_expected_size);
28 ~PDFiumAPIStringBufferAdapter();
30 // Returns a pointer to |str_|'s buffer. The buffer's size is large enough to
31 // hold |expected_size_| + 1 characters, so the PDFium API that uses the
32 // pointer has space to write a null-terminator.
33 void* GetData();
35 // Resizes |str_| to |actual_size| - 1 characters, thereby removing the extra
36 // null-terminator. This must be called prior to the adapter's destruction.
37 // The pointer returned by GetData() should be considered invalid.
38 void Close(int actual_size);
39 void Close(size_t actual_size);
41 private:
42 StringType* const str_;
43 void* const data_;
44 const size_t expected_size_;
45 const bool check_expected_size_;
46 bool is_closed_;
48 DISALLOW_COPY_AND_ASSIGN(PDFiumAPIStringBufferAdapter);
51 } // namespace chrome_pdf
53 #endif // PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_