OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_ | 5 #ifndef PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_ |
6 #define PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_ | 6 #define PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 | 9 |
10 namespace chrome_pdf { | 10 namespace chrome_pdf { |
(...skipping 30 matching lines...) Expand all Loading... |
41 private: | 41 private: |
42 StringType* const str_; | 42 StringType* const str_; |
43 void* const data_; | 43 void* const data_; |
44 const size_t expected_size_; | 44 const size_t expected_size_; |
45 const bool check_expected_size_; | 45 const bool check_expected_size_; |
46 bool is_closed_; | 46 bool is_closed_; |
47 | 47 |
48 DISALLOW_COPY_AND_ASSIGN(PDFiumAPIStringBufferAdapter); | 48 DISALLOW_COPY_AND_ASSIGN(PDFiumAPIStringBufferAdapter); |
49 }; | 49 }; |
50 | 50 |
| 51 // Helper to deal with the fact that many PDFium APIs write the null-terminator |
| 52 // into string buffers that are passed to them, but the PDF plugin likes to pass |
| 53 // in std::strings / base::string16s, where one should not count on the internal |
| 54 // string buffers to be null-terminated. This version is suitable for APIs that |
| 55 // work in terms of number of bytes instead of the number of characters. |
| 56 template <class StringType> |
| 57 class PDFiumAPIStringBufferSizeInBytesAdapter { |
| 58 public: |
| 59 // |str| is the string to write into. |
| 60 // |expected_size| is the number of bytes the PDFium API will write, |
| 61 // including the null-terminator. It should be at least the size of a |
| 62 // character in bytes. |
| 63 // |check_expected_size| whether to check the actual number of bytes |
| 64 // written into |str| against |expected_size| when calling Close(). |
| 65 PDFiumAPIStringBufferSizeInBytesAdapter(StringType* str, |
| 66 size_t expected_size, |
| 67 bool check_expected_size); |
| 68 ~PDFiumAPIStringBufferSizeInBytesAdapter(); |
| 69 |
| 70 // Returns a pointer to |str_|'s buffer. The buffer's size is large enough to |
| 71 // hold |expected_size_| + sizeof(StringType::value_type) bytes, so the PDFium |
| 72 // API that uses the pointer has space to write a null-terminator. |
| 73 void* GetData(); |
| 74 |
| 75 // Resizes |str_| to |actual_size| - sizeof(StringType::value_type) bytes, |
| 76 // thereby removing the extra null-terminator. This must be called prior to |
| 77 // the adapter's destruction. The pointer returned by GetData() should be |
| 78 // considered invalid. |
| 79 void Close(int actual_size); |
| 80 void Close(size_t actual_size); |
| 81 |
| 82 private: |
| 83 PDFiumAPIStringBufferAdapter<StringType> adapter_; |
| 84 }; |
| 85 |
51 } // namespace chrome_pdf | 86 } // namespace chrome_pdf |
52 | 87 |
53 #endif // PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_ | 88 #endif // PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_ |
OLD | NEW |