Commit 1bc6c54d authored by vandebo@chromium.org's avatar vandebo@chromium.org

Fix smelly scoped_ptr code in print_web_view_helper_win.

A series of changes resulted in the need to store an owned pointer in a different scoped_ptr temporarily.  This change changes the ownership model of the called method to remove the hack.

BUG=NONE
TEST=NONE


Review URL: http://codereview.chromium.org/7491074

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@95954 0039d316-1c4b-4281-b951-d872f2087c98
parent dcd1e886
...@@ -228,9 +228,14 @@ class PrintWebViewHelper : public RenderViewObserver, ...@@ -228,9 +228,14 @@ class PrintWebViewHelper : public RenderViewObserver,
// Platform specific helper function for rendering page(s) to |metafile|. // Platform specific helper function for rendering page(s) to |metafile|.
#if defined(OS_WIN) #if defined(OS_WIN)
void RenderPage(const PrintMsg_Print_Params& params, float* scale_factor, // Because of mixed support for alpha channels on printers, this method may
int page_number, bool is_preview, WebKit::WebFrame* frame, // need to create a new metafile. The result may be either the passed
scoped_ptr<printing::Metafile>* metafile); // |metafile| or a new one. In either case, the caller owns both |metafile|
// and the result.
printing::Metafile* RenderPage(const PrintMsg_Print_Params& params,
float* scale_factor, int page_number,
bool is_preview, WebKit::WebFrame* frame,
printing::Metafile* metafile);
#elif defined(OS_MACOSX) #elif defined(OS_MACOSX)
void RenderPage(const gfx::Size& page_size, const gfx::Rect& content_area, void RenderPage(const gfx::Size& page_size, const gfx::Rect& content_area,
const float& scale_factor, int page_number, const float& scale_factor, int page_number,
......
...@@ -90,8 +90,8 @@ void PrintWebViewHelper::PrintPageInternal( ...@@ -90,8 +90,8 @@ void PrintWebViewHelper::PrintPageInternal(
params.params.dpi); params.params.dpi);
// Render page for printing. // Render page for printing.
RenderPage(params.params, &scale_factor, page_number, false, frame, metafile.reset(RenderPage(params.params, &scale_factor, page_number, false,
&metafile); frame, metafile.get()));
// Close the device context to retrieve the compiled metafile. // Close the device context to retrieve the compiled metafile.
if (!metafile->FinishDocument()) if (!metafile->FinishDocument())
...@@ -127,19 +127,17 @@ void PrintWebViewHelper::RenderPreviewPage(int page_number) { ...@@ -127,19 +127,17 @@ void PrintWebViewHelper::RenderPreviewPage(int page_number) {
float scale_factor = static_cast<float>(print_params.desired_dpi / float scale_factor = static_cast<float>(print_params.desired_dpi /
print_params.dpi); print_params.dpi);
// |metafile| is needed for RenderPage() below. |metafile| will not take the
// ownership of |print_preview_context_| metafile.
scoped_ptr<Metafile> metafile(print_preview_context_.metafile());
base::TimeTicks begin_time = base::TimeTicks::Now(); base::TimeTicks begin_time = base::TimeTicks::Now();
RenderPage(print_params, &scale_factor, page_number, true, printing::Metafile* render_page_result =
print_preview_context_.frame(), &metafile); RenderPage(print_params, &scale_factor, page_number, true,
print_preview_context_.frame(),
print_preview_context_.metafile());
// In the preview flow, RenderPage will never return a new metafile.
DCHECK_EQ(render_page_result, print_preview_context_.metafile());
print_preview_context_.RenderedPreviewPage( print_preview_context_.RenderedPreviewPage(
base::TimeTicks::Now() - begin_time); base::TimeTicks::Now() - begin_time);
// Release since |print_preview_context_| is the real owner.
metafile.release();
scoped_ptr<printing::Metafile> page_metafile; scoped_ptr<printing::Metafile> page_metafile;
if (print_preview_context_.IsModifiable()) { if (print_preview_context_.IsModifiable()) {
page_metafile.reset( page_metafile.reset(
...@@ -148,9 +146,9 @@ void PrintWebViewHelper::RenderPreviewPage(int page_number) { ...@@ -148,9 +146,9 @@ void PrintWebViewHelper::RenderPreviewPage(int page_number) {
PreviewPageRendered(page_number, page_metafile.get()); PreviewPageRendered(page_number, page_metafile.get());
} }
void PrintWebViewHelper::RenderPage( Metafile* PrintWebViewHelper::RenderPage(
const PrintMsg_Print_Params& params, float* scale_factor, int page_number, const PrintMsg_Print_Params& params, float* scale_factor, int page_number,
bool is_preview, WebFrame* frame, scoped_ptr<Metafile>* metafile) { bool is_preview, WebFrame* frame, Metafile* metafile) {
PageSizeMargins page_layout_in_points; PageSizeMargins page_layout_in_points;
GetPageSizeAndMarginsInPoints(frame, page_number, params, GetPageSizeAndMarginsInPoints(frame, page_number, params,
&page_layout_in_points); &page_layout_in_points);
...@@ -178,17 +176,15 @@ void PrintWebViewHelper::RenderPage( ...@@ -178,17 +176,15 @@ void PrintWebViewHelper::RenderPage(
static_cast<int>(page_layout_in_points.margin_top), static_cast<int>(page_layout_in_points.margin_top),
static_cast<int>(page_layout_in_points.content_width), static_cast<int>(page_layout_in_points.content_width),
static_cast<int>(page_layout_in_points.content_height)); static_cast<int>(page_layout_in_points.content_height));
SkDevice* device = (*metafile)->StartPageForVectorCanvas( SkDevice* device = metafile->StartPageForVectorCanvas(
page_size, content_area, frame->getPrintPageShrink(page_number)); page_size, content_area, frame->getPrintPageShrink(page_number));
DCHECK(device); DCHECK(device);
// The printPage method may take a reference to the canvas we pass down, so it // The printPage method may take a reference to the canvas we pass down, so it
// can't be a stack object. // can't be a stack object.
SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device);
canvas->unref(); // SkRefPtr and new both took a reference. canvas->unref(); // SkRefPtr and new both took a reference.
if (is_preview) { if (is_preview)
printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas.get(), printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas.get(), metafile);
metafile->get());
}
float webkit_scale_factor = frame->printPage(page_number, canvas.get()); float webkit_scale_factor = frame->printPage(page_number, canvas.get());
if (*scale_factor <= 0 || webkit_scale_factor <= 0) { if (*scale_factor <= 0 || webkit_scale_factor <= 0) {
...@@ -199,7 +195,7 @@ void PrintWebViewHelper::RenderPage( ...@@ -199,7 +195,7 @@ void PrintWebViewHelper::RenderPage(
*scale_factor /= webkit_scale_factor; *scale_factor /= webkit_scale_factor;
} }
bool result = (*metafile)->FinishPage(); bool result = metafile->FinishPage();
DCHECK(result); DCHECK(result);
if (!params.supports_alpha_blend) { if (!params.supports_alpha_blend) {
...@@ -210,10 +206,10 @@ void PrintWebViewHelper::RenderPage( ...@@ -210,10 +206,10 @@ void PrintWebViewHelper::RenderPage(
if (platform_device && platform_device->AlphaBlendUsed()) { if (platform_device && platform_device->AlphaBlendUsed()) {
// Currently, we handle alpha blend transparency for a single page. // Currently, we handle alpha blend transparency for a single page.
// Therefore, expecting a metafile with page count 1. // Therefore, expecting a metafile with page count 1.
DCHECK_EQ(1U, (*metafile)->GetPageCount()); DCHECK_EQ(1U, metafile->GetPageCount());
// Close the device context to retrieve the compiled metafile. // Close the device context to retrieve the compiled metafile.
if (!(*metafile)->FinishDocument()) if (!metafile->FinishDocument())
NOTREACHED(); NOTREACHED();
// Page used alpha blend, but printer doesn't support it. Rewrite the // Page used alpha blend, but printer doesn't support it. Rewrite the
...@@ -235,26 +231,27 @@ void PrintWebViewHelper::RenderPage( ...@@ -235,26 +231,27 @@ void PrintWebViewHelper::RenderPage(
HBRUSH whiteBrush = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)); HBRUSH whiteBrush = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
FillRect(bitmap_dc, &rect, whiteBrush); FillRect(bitmap_dc, &rect, whiteBrush);
scoped_ptr<Metafile> metafile2(new printing::NativeMetafile); Metafile* metafile2(new printing::NativeMetafile);
metafile2->Init(); metafile2->Init();
HDC hdc = metafile2->context(); HDC hdc = metafile2->context();
DCHECK(hdc); DCHECK(hdc);
skia::InitializeDC(hdc); skia::InitializeDC(hdc);
RECT metafile_bounds = (*metafile)->GetPageBounds(1).ToRECT(); RECT metafile_bounds = metafile->GetPageBounds(1).ToRECT();
// Process the old metafile, placing all non-AlphaBlend calls into the // Process the old metafile, placing all non-AlphaBlend calls into the
// new metafile, and copying the results of all the AlphaBlend calls // new metafile, and copying the results of all the AlphaBlend calls
// from the bitmap DC. // from the bitmap DC.
EnumEnhMetaFile(hdc, EnumEnhMetaFile(hdc,
(*metafile)->emf(), metafile->emf(),
EnhMetaFileProc, EnhMetaFileProc,
&bitmap_dc, &bitmap_dc,
&metafile_bounds); &metafile_bounds);
SelectObject(bitmap_dc, old_bitmap); SelectObject(bitmap_dc, old_bitmap);
metafile->reset(metafile2.release()); return metafile2;
} }
} }
return metafile;
} }
bool PrintWebViewHelper::CopyMetafileDataToSharedMem( bool PrintWebViewHelper::CopyMetafileDataToSharedMem(
......
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