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

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

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

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