0

Untemplatize PDFiumAPIStringBufferSizeInBytesAdapter.

This templatized class is only ever used for base::string16. Since that
is the only string type it will be used with in the foreseeable future,
remove the overhead of the template.

Change-Id: I7a62e83ab612c1d7e7b2461a50bc7be7be0768f9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2207737
Reviewed-by: Daniel Hosseinian <dhoss@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#770809}
This commit is contained in:
Lei Zhang
2020-05-20 22:26:56 +00:00
committed by Commit Bot
parent b8e5a41e9a
commit cbc5e46d2c
4 changed files with 30 additions and 39 deletions

@ -9,7 +9,6 @@
#include <string>
#include "base/check_op.h"
#include "base/strings/string16.h"
#include "base/strings/string_util.h"
namespace chrome_pdf {
@ -52,36 +51,28 @@ void PDFiumAPIStringBufferAdapter<StringType>::Close(size_t actual_size) {
}
}
template <class StringType>
PDFiumAPIStringBufferSizeInBytesAdapter<StringType>::
PDFiumAPIStringBufferSizeInBytesAdapter(StringType* str,
PDFiumAPIStringBufferSizeInBytesAdapter::
PDFiumAPIStringBufferSizeInBytesAdapter(base::string16* str,
size_t expected_size,
bool check_expected_size)
: adapter_(str,
expected_size / sizeof(typename StringType::value_type),
check_expected_size) {
DCHECK(expected_size % sizeof(typename StringType::value_type) == 0);
: adapter_(str, expected_size / sizeof(base::char16), check_expected_size) {
DCHECK(expected_size % sizeof(base::char16) == 0);
}
template <class StringType>
PDFiumAPIStringBufferSizeInBytesAdapter<
StringType>::~PDFiumAPIStringBufferSizeInBytesAdapter() = default;
PDFiumAPIStringBufferSizeInBytesAdapter::
~PDFiumAPIStringBufferSizeInBytesAdapter() = default;
template <class StringType>
void* PDFiumAPIStringBufferSizeInBytesAdapter<StringType>::GetData() {
void* PDFiumAPIStringBufferSizeInBytesAdapter::GetData() {
return adapter_.GetData();
}
template <class StringType>
void PDFiumAPIStringBufferSizeInBytesAdapter<StringType>::Close(
size_t actual_size) {
DCHECK(actual_size % sizeof(typename StringType::value_type) == 0);
adapter_.Close(actual_size / sizeof(typename StringType::value_type));
void PDFiumAPIStringBufferSizeInBytesAdapter::Close(size_t actual_size) {
DCHECK(actual_size % sizeof(base::char16) == 0);
adapter_.Close(actual_size / sizeof(base::char16));
}
// explicit instantiations
template class PDFiumAPIStringBufferAdapter<std::string>;
template class PDFiumAPIStringBufferAdapter<base::string16>;
template class PDFiumAPIStringBufferSizeInBytesAdapter<base::string16>;
} // namespace chrome_pdf

@ -9,14 +9,14 @@
#include "base/macros.h"
#include "base/numerics/safe_math.h"
#include "base/strings/string16.h"
namespace chrome_pdf {
// Helper to deal with the fact that many PDFium APIs write the null-terminator
// into string buffers that are passed to them, but the PDF plugin likes to pass
// in std::strings / base::string16s, where one should not count on the internal
// into string buffers that are passed to them, but the PDF code likes to use
// std::strings / base::string16s, where one should not count on the internal
// string buffers to be null-terminated.
template <class StringType>
class PDFiumAPIStringBufferAdapter {
public:
@ -56,11 +56,11 @@ class PDFiumAPIStringBufferAdapter {
};
// Helper to deal with the fact that many PDFium APIs write the null-terminator
// into string buffers that are passed to them, but the PDF plugin likes to pass
// in std::strings / base::string16s, where one should not count on the internal
// into string buffers that are passed to them, but the PDF code likes to use
// std::strings / base::string16s, where one should not count on the internal
// string buffers to be null-terminated. This version is suitable for APIs that
// work in terms of number of bytes instead of the number of characters.
template <class StringType>
// work in terms of number of bytes instead of the number of characters. Though
// for std::strings, PDFiumAPIStringBufferAdapter is equivalent.
class PDFiumAPIStringBufferSizeInBytesAdapter {
public:
// |str| is the string to write into.
@ -69,19 +69,19 @@ class PDFiumAPIStringBufferSizeInBytesAdapter {
// character in bytes.
// |check_expected_size| whether to check the actual number of bytes
// written into |str| against |expected_size| when calling Close().
PDFiumAPIStringBufferSizeInBytesAdapter(StringType* str,
PDFiumAPIStringBufferSizeInBytesAdapter(base::string16* str,
size_t expected_size,
bool check_expected_size);
~PDFiumAPIStringBufferSizeInBytesAdapter();
// Returns a pointer to |str_|'s buffer. The buffer's size is large enough to
// hold |expected_size_| + sizeof(StringType::value_type) bytes, so the PDFium
// API that uses the pointer has space to write a null-terminator.
// hold |expected_size_| + sizeof(base::char16) bytes, so the PDFium API that
// uses the pointer has space to write a null-terminator.
void* GetData();
// Resizes |str_| to |actual_size| - sizeof(StringType::value_type) bytes,
// thereby removing the extra null-terminator. This must be called prior to
// the adapter's destruction. The pointer returned by GetData() should be
// Resizes |str_| to |actual_size| - sizeof(base::char16) bytes, thereby
// removing the extra null-terminator. This must be called prior to the
// adapter's destruction. The pointer returned by GetData() should be
// considered invalid.
void Close(size_t actual_size);
@ -91,7 +91,7 @@ class PDFiumAPIStringBufferSizeInBytesAdapter {
}
private:
PDFiumAPIStringBufferAdapter<StringType> adapter_;
PDFiumAPIStringBufferAdapter<base::string16> adapter_;
};
} // namespace chrome_pdf

@ -1000,7 +1000,7 @@ void PDFiumEngine::SetFormSelectedText(FPDF_FORMHANDLE form_handle,
return;
base::string16 selected_form_text16;
PDFiumAPIStringBufferSizeInBytesAdapter<base::string16> string_adapter(
PDFiumAPIStringBufferSizeInBytesAdapter string_adapter(
&selected_form_text16, form_sel_text_len, false);
string_adapter.Close(FORM_GetSelectedText(
form_handle, page, string_adapter.GetData(), form_sel_text_len));
@ -2147,7 +2147,7 @@ pp::VarDictionary PDFiumEngine::TraverseBookmarks(FPDF_BOOKMARK bookmark,
base::string16 title;
unsigned long buffer_size = FPDFBookmark_GetTitle(bookmark, nullptr, 0);
if (buffer_size > 0) {
PDFiumAPIStringBufferSizeInBytesAdapter<base::string16> api_string_adapter(
PDFiumAPIStringBufferSizeInBytesAdapter api_string_adapter(
&title, buffer_size, true);
api_string_adapter.Close(FPDFBookmark_GetTitle(
bookmark, api_string_adapter.GetData(), buffer_size));
@ -3802,7 +3802,7 @@ std::string PDFiumEngine::GetMetadataByField(FPDF_BYTESTRING field) const {
return std::string();
base::string16 value;
PDFiumAPIStringBufferSizeInBytesAdapter<base::string16> string_adapter(
PDFiumAPIStringBufferSizeInBytesAdapter string_adapter(
&value, size, /*check_expected_size=*/false);
string_adapter.Close(
FPDF_GetMetaText(doc(), field, string_adapter.GetData(), size));

@ -195,7 +195,7 @@ std::string GetFormFieldProperty(GetFormFieldPropertyFunction function) {
base::string16 data;
size_t buffer_size = function.Run(nullptr, 0);
if (buffer_size > 0) {
PDFiumAPIStringBufferSizeInBytesAdapter<base::string16> api_string_adapter(
PDFiumAPIStringBufferSizeInBytesAdapter api_string_adapter(
&data, buffer_size, true);
api_string_adapter.Close(function.Run(
reinterpret_cast<unsigned short*>(api_string_adapter.GetData()),
@ -1157,8 +1157,8 @@ void PDFiumPage::PopulateImageAltTextForStructElement(
FPDF_StructElement_GetAltText(current_element, nullptr, 0);
if (buffer_size > 0) {
base::string16 alt_text;
PDFiumAPIStringBufferSizeInBytesAdapter<base::string16>
api_string_adapter(&alt_text, buffer_size, true);
PDFiumAPIStringBufferSizeInBytesAdapter api_string_adapter(
&alt_text, buffer_size, true);
api_string_adapter.Close(FPDF_StructElement_GetAltText(
current_element, api_string_adapter.GetData(), buffer_size));
images_[it->second].alt_text = base::UTF16ToUTF8(alt_text);