Commit 9aa2f87d authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Add ScopedSdkInitializer in pdf/pdf.cc.

Help make sure ShutdownSDK() gets called when needed.

Change-Id: I9be9e521892c3dd9f49e4141e93a4dbe46c7f5ce
Reviewed-on: https://chromium-review.googlesource.com/1135914Reviewed-by: default avatarHenrique Nakashima <hnakashima@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#575013}
parent 063d1eb0
...@@ -11,11 +11,43 @@ ...@@ -11,11 +11,43 @@
#endif #endif
#include "base/logging.h" #include "base/logging.h"
#include "base/macros.h"
#include "pdf/out_of_process_instance.h" #include "pdf/out_of_process_instance.h"
#include "pdf/pdf_ppapi.h" #include "pdf/pdf_ppapi.h"
namespace chrome_pdf { namespace chrome_pdf {
namespace {
class ScopedSdkInitializer {
public:
ScopedSdkInitializer() {}
~ScopedSdkInitializer() {
#if DCHECK_IS_ON()
DCHECK(initialized_);
#endif
if (!IsSDKInitializedViaPepper())
ShutdownSDK();
}
// Must be called.
bool Init() {
#if DCHECK_IS_ON()
initialized_ = true;
#endif
return IsSDKInitializedViaPepper() || InitializeSDK();
}
private:
#if DCHECK_IS_ON()
bool initialized_ = false;
#endif
DISALLOW_COPY_AND_ASSIGN(ScopedSdkInitializer);
};
} // namespace
#if defined(OS_WIN) #if defined(OS_WIN)
bool RenderPDFPageToDC(const void* pdf_buffer, bool RenderPDFPageToDC(const void* pdf_buffer,
int buffer_size, int buffer_size,
...@@ -33,23 +65,18 @@ bool RenderPDFPageToDC(const void* pdf_buffer, ...@@ -33,23 +65,18 @@ bool RenderPDFPageToDC(const void* pdf_buffer,
bool center_in_bounds, bool center_in_bounds,
bool autorotate, bool autorotate,
bool use_color) { bool use_color) {
if (!IsSDKInitializedViaPepper()) { ScopedSdkInitializer scoped_sdk_initializer;
if (!InitializeSDK()) { if (!scoped_sdk_initializer.Init())
return false; return false;
}
}
PDFEngineExports* engine_exports = PDFEngineExports::Get(); PDFEngineExports* engine_exports = PDFEngineExports::Get();
PDFEngineExports::RenderingSettings settings( PDFEngineExports::RenderingSettings settings(
dpi_x, dpi_y, dpi_x, dpi_y,
pp::Rect(bounds_origin_x, bounds_origin_y, bounds_width, bounds_height), pp::Rect(bounds_origin_x, bounds_origin_y, bounds_width, bounds_height),
fit_to_bounds, stretch_to_bounds, keep_aspect_ratio, center_in_bounds, fit_to_bounds, stretch_to_bounds, keep_aspect_ratio, center_in_bounds,
autorotate, use_color); autorotate, use_color);
bool ret = engine_exports->RenderPDFPageToDC(pdf_buffer, buffer_size, return engine_exports->RenderPDFPageToDC(pdf_buffer, buffer_size, page_number,
page_number, settings, dc); settings, dc);
if (!IsSDKInitializedViaPepper())
ShutdownSDK();
return ret;
} }
void SetPDFEnsureTypefaceCharactersAccessible( void SetPDFEnsureTypefaceCharactersAccessible(
...@@ -70,17 +97,13 @@ bool GetPDFDocInfo(const void* pdf_buffer, ...@@ -70,17 +97,13 @@ bool GetPDFDocInfo(const void* pdf_buffer,
int buffer_size, int buffer_size,
int* page_count, int* page_count,
double* max_page_width) { double* max_page_width) {
if (!IsSDKInitializedViaPepper()) { ScopedSdkInitializer scoped_sdk_initializer;
if (!InitializeSDK()) if (!scoped_sdk_initializer.Init())
return false; return false;
}
PDFEngineExports* engine_exports = PDFEngineExports::Get();
bool ret = engine_exports->GetPDFDocInfo(pdf_buffer, buffer_size, page_count,
max_page_width);
if (!IsSDKInitializedViaPepper())
ShutdownSDK();
return ret; PDFEngineExports* engine_exports = PDFEngineExports::Get();
return engine_exports->GetPDFDocInfo(pdf_buffer, buffer_size, page_count,
max_page_width);
} }
bool GetPDFPageSizeByIndex(const void* pdf_buffer, bool GetPDFPageSizeByIndex(const void* pdf_buffer,
...@@ -88,17 +111,14 @@ bool GetPDFPageSizeByIndex(const void* pdf_buffer, ...@@ -88,17 +111,14 @@ bool GetPDFPageSizeByIndex(const void* pdf_buffer,
int page_number, int page_number,
double* width, double* width,
double* height) { double* height) {
if (!IsSDKInitializedViaPepper()) { ScopedSdkInitializer scoped_sdk_initializer;
if (!chrome_pdf::InitializeSDK()) if (!scoped_sdk_initializer.Init())
return false; return false;
}
chrome_pdf::PDFEngineExports* engine_exports = chrome_pdf::PDFEngineExports* engine_exports =
chrome_pdf::PDFEngineExports::Get(); chrome_pdf::PDFEngineExports::Get();
bool ret = engine_exports->GetPDFPageSizeByIndex(pdf_buffer, pdf_buffer_size, return engine_exports->GetPDFPageSizeByIndex(pdf_buffer, pdf_buffer_size,
page_number, width, height); page_number, width, height);
if (!IsSDKInitializedViaPepper())
chrome_pdf::ShutdownSDK();
return ret;
} }
bool RenderPDFPageToBitmap(const void* pdf_buffer, bool RenderPDFPageToBitmap(const void* pdf_buffer,
...@@ -111,20 +131,16 @@ bool RenderPDFPageToBitmap(const void* pdf_buffer, ...@@ -111,20 +131,16 @@ bool RenderPDFPageToBitmap(const void* pdf_buffer,
int dpi_y, int dpi_y,
bool autorotate, bool autorotate,
bool use_color) { bool use_color) {
if (!IsSDKInitializedViaPepper()) { ScopedSdkInitializer scoped_sdk_initializer;
if (!InitializeSDK()) if (!scoped_sdk_initializer.Init())
return false; return false;
}
PDFEngineExports* engine_exports = PDFEngineExports::Get(); PDFEngineExports* engine_exports = PDFEngineExports::Get();
PDFEngineExports::RenderingSettings settings( PDFEngineExports::RenderingSettings settings(
dpi_x, dpi_y, pp::Rect(bitmap_width, bitmap_height), true, false, true, dpi_x, dpi_y, pp::Rect(bitmap_width, bitmap_height), true, false, true,
true, autorotate, use_color); true, autorotate, use_color);
bool ret = engine_exports->RenderPDFPageToBitmap( return engine_exports->RenderPDFPageToBitmap(
pdf_buffer, pdf_buffer_size, page_number, settings, bitmap_buffer); pdf_buffer, pdf_buffer_size, page_number, settings, bitmap_buffer);
if (!IsSDKInitializedViaPepper())
ShutdownSDK();
return ret;
} }
} // namespace chrome_pdf } // namespace chrome_pdf
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment