OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "chrome/renderer/pepper/ppb_pdf_impl.h" | 5 #include "chrome/renderer/pepper/ppb_pdf_impl.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "ppapi/shared_impl/resource.h" | 22 #include "ppapi/shared_impl/resource.h" |
23 #include "ppapi/shared_impl/resource_tracker.h" | 23 #include "ppapi/shared_impl/resource_tracker.h" |
24 #include "ppapi/shared_impl/var.h" | 24 #include "ppapi/shared_impl/var.h" |
25 #include "skia/ext/platform_canvas.h" | 25 #include "skia/ext/platform_canvas.h" |
26 #include "third_party/skia/include/core/SkBitmap.h" | 26 #include "third_party/skia/include/core/SkBitmap.h" |
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" | 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" |
31 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
| 32 #include "ui/base/layout.h" |
32 #include "ui/base/resource/resource_bundle.h" | 33 #include "ui/base/resource/resource_bundle.h" |
| 34 #include "ui/gfx/image/image_skia.h" |
| 35 #include "ui/gfx/image/image_skia_rep.h" |
33 #include "unicode/usearch.h" | 36 #include "unicode/usearch.h" |
34 #include "webkit/plugins/ppapi/host_globals.h" | 37 #include "webkit/plugins/ppapi/host_globals.h" |
35 #include "webkit/plugins/ppapi/plugin_delegate.h" | 38 #include "webkit/plugins/ppapi/plugin_delegate.h" |
36 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 39 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
37 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" | 40 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" |
38 | 41 |
39 using ppapi::PpapiGlobals; | 42 using ppapi::PpapiGlobals; |
40 using webkit::ppapi::HostGlobals; | 43 using webkit::ppapi::HostGlobals; |
41 using webkit::ppapi::PluginInstance; | 44 using webkit::ppapi::PluginInstance; |
42 using WebKit::WebElement; | 45 using WebKit::WebElement; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 rv = UTF16ToUTF8(l10n_util::GetStringUTF16(IDS_PDF_PAGE_LOAD_FAILED)); | 149 rv = UTF16ToUTF8(l10n_util::GetStringUTF16(IDS_PDF_PAGE_LOAD_FAILED)); |
147 } else if (string_id == PP_RESOURCESTRING_PDFPROGRESSLOADING) { | 150 } else if (string_id == PP_RESOURCESTRING_PDFPROGRESSLOADING) { |
148 rv = UTF16ToUTF8(l10n_util::GetStringUTF16(IDS_PDF_PROGRESS_LOADING)); | 151 rv = UTF16ToUTF8(l10n_util::GetStringUTF16(IDS_PDF_PROGRESS_LOADING)); |
149 } else { | 152 } else { |
150 NOTREACHED(); | 153 NOTREACHED(); |
151 } | 154 } |
152 | 155 |
153 return ppapi::StringVar::StringToPPVar(rv); | 156 return ppapi::StringVar::StringToPPVar(rv); |
154 } | 157 } |
155 | 158 |
156 PP_Resource GetResourceImage(PP_Instance instance_id, | |
157 PP_ResourceImage image_id) { | |
158 int res_id = 0; | |
159 for (size_t i = 0; i < arraysize(kResourceImageMap); ++i) { | |
160 if (kResourceImageMap[i].pp_id == image_id) { | |
161 res_id = kResourceImageMap[i].res_id; | |
162 break; | |
163 } | |
164 } | |
165 if (res_id == 0) | |
166 return 0; | |
167 | |
168 SkBitmap* res_bitmap = | |
169 ResourceBundle::GetSharedInstance().GetBitmapNamed(res_id); | |
170 | |
171 // Validate the instance. | |
172 if (!content::GetHostGlobals()->GetInstance(instance_id)) | |
173 return 0; | |
174 scoped_refptr<webkit::ppapi::PPB_ImageData_Impl> image_data( | |
175 new webkit::ppapi::PPB_ImageData_Impl( | |
176 instance_id, | |
177 webkit::ppapi::PPB_ImageData_Impl::PLATFORM)); | |
178 if (!image_data->Init( | |
179 webkit::ppapi::PPB_ImageData_Impl::GetNativeImageDataFormat(), | |
180 res_bitmap->width(), res_bitmap->height(), false)) { | |
181 return 0; | |
182 } | |
183 | |
184 webkit::ppapi::ImageDataAutoMapper mapper(image_data); | |
185 if (!mapper.is_valid()) | |
186 return 0; | |
187 | |
188 skia::PlatformCanvas* canvas = image_data->GetPlatformCanvas(); | |
189 // Note: Do not skBitmap::copyTo the canvas bitmap directly because it will | |
190 // ignore the allocated pixels in shared memory and re-allocate a new buffer. | |
191 canvas->writePixels(*res_bitmap, 0, 0); | |
192 | |
193 return image_data->GetReference(); | |
194 } | |
195 | |
196 PP_Resource GetFontFileWithFallback( | 159 PP_Resource GetFontFileWithFallback( |
197 PP_Instance instance_id, | 160 PP_Instance instance_id, |
198 const PP_FontDescription_Dev* description, | 161 const PP_FontDescription_Dev* description, |
199 PP_PrivateFontCharset charset) { | 162 PP_PrivateFontCharset charset) { |
200 #if defined(OS_LINUX) || defined(OS_OPENBSD) | 163 #if defined(OS_LINUX) || defined(OS_OPENBSD) |
201 // Validate the instance before using it below. | 164 // Validate the instance before using it below. |
202 if (!content::GetHostGlobals()->GetInstance(instance_id)) | 165 if (!content::GetHostGlobals()->GetInstance(instance_id)) |
203 return 0; | 166 return 0; |
204 | 167 |
205 scoped_refptr<ppapi::StringVar> face_name(ppapi::StringVar::FromPPVar( | 168 scoped_refptr<ppapi::StringVar> face_name(ppapi::StringVar::FromPPVar( |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 switch (feature) { | 319 switch (feature) { |
357 case PP_PDFFEATURE_HIDPI: | 320 case PP_PDFFEATURE_HIDPI: |
358 if (CommandLine::ForCurrentProcess()->HasSwitch( | 321 if (CommandLine::ForCurrentProcess()->HasSwitch( |
359 switches::kEnableHighDPIPDFPlugin)) | 322 switches::kEnableHighDPIPDFPlugin)) |
360 result = PP_TRUE; | 323 result = PP_TRUE; |
361 break; | 324 break; |
362 } | 325 } |
363 return result; | 326 return result; |
364 } | 327 } |
365 | 328 |
| 329 PP_Resource GetResourceImageForScale(PP_Instance instance_id, |
| 330 PP_ResourceImage image_id, |
| 331 float scale) { |
| 332 int res_id = 0; |
| 333 for (size_t i = 0; i < arraysize(kResourceImageMap); ++i) { |
| 334 if (kResourceImageMap[i].pp_id == image_id) { |
| 335 res_id = kResourceImageMap[i].res_id; |
| 336 break; |
| 337 } |
| 338 } |
| 339 if (res_id == 0) |
| 340 return 0; |
| 341 |
| 342 ui::ScaleFactor scale_factor = ui::GetScaleFactorFromScale(scale); |
| 343 |
| 344 gfx::ImageSkia* res_image_skia = |
| 345 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(res_id); |
| 346 |
| 347 if (!res_image_skia) |
| 348 return 0; |
| 349 |
| 350 // Validate the instance. |
| 351 if (!content::GetHostGlobals()->GetInstance(instance_id)) |
| 352 return 0; |
| 353 |
| 354 if (!res_image_skia->HasRepresentation(scale_factor)) |
| 355 return 0; |
| 356 |
| 357 gfx::ImageSkiaRep image_skia_rep = res_image_skia->GetRepresentation( |
| 358 scale_factor); |
| 359 |
| 360 scoped_refptr<webkit::ppapi::PPB_ImageData_Impl> image_data( |
| 361 new webkit::ppapi::PPB_ImageData_Impl( |
| 362 instance_id, |
| 363 webkit::ppapi::PPB_ImageData_Impl::PLATFORM)); |
| 364 if (!image_data->Init( |
| 365 webkit::ppapi::PPB_ImageData_Impl::GetNativeImageDataFormat(), |
| 366 image_skia_rep.pixel_width(), |
| 367 image_skia_rep.pixel_height(), |
| 368 false)) { |
| 369 return 0; |
| 370 } |
| 371 |
| 372 webkit::ppapi::ImageDataAutoMapper mapper(image_data); |
| 373 if (!mapper.is_valid()) |
| 374 return 0; |
| 375 |
| 376 skia::PlatformCanvas* canvas = image_data->GetPlatformCanvas(); |
| 377 // Note: Do not skBitmap::copyTo the canvas bitmap directly because it will |
| 378 // ignore the allocated pixels in shared memory and re-allocate a new buffer. |
| 379 canvas->writePixels(image_skia_rep.sk_bitmap(), 0, 0); |
| 380 |
| 381 return image_data->GetReference(); |
| 382 } |
| 383 |
| 384 PP_Resource GetResourceImage(PP_Instance instance_id, |
| 385 PP_ResourceImage image_id) { |
| 386 return GetResourceImageForScale(instance_id, image_id, 1.0f); |
| 387 } |
| 388 |
366 const PPB_PDF ppb_pdf = { | 389 const PPB_PDF ppb_pdf = { |
367 &GetLocalizedString, | 390 &GetLocalizedString, |
368 &GetResourceImage, | 391 &GetResourceImage, |
369 &GetFontFileWithFallback, | 392 &GetFontFileWithFallback, |
370 &GetFontTableForPrivateFontFile, | 393 &GetFontTableForPrivateFontFile, |
371 &SearchString, | 394 &SearchString, |
372 &DidStartLoading, | 395 &DidStartLoading, |
373 &DidStopLoading, | 396 &DidStopLoading, |
374 &SetContentRestriction, | 397 &SetContentRestriction, |
375 &HistogramPDFPageCount, | 398 &HistogramPDFPageCount, |
376 &UserMetricsRecordAction, | 399 &UserMetricsRecordAction, |
377 &HasUnsupportedFeature, | 400 &HasUnsupportedFeature, |
378 &SaveAs, | 401 &SaveAs, |
379 &PPB_PDF_Impl::InvokePrintingForInstance, | 402 &PPB_PDF_Impl::InvokePrintingForInstance, |
380 &IsFeatureEnabled | 403 &IsFeatureEnabled, |
| 404 &GetResourceImageForScale |
381 }; | 405 }; |
382 | 406 |
383 } // namespace | 407 } // namespace |
384 | 408 |
385 // static | 409 // static |
386 const PPB_PDF* PPB_PDF_Impl::GetInterface() { | 410 const PPB_PDF* PPB_PDF_Impl::GetInterface() { |
387 return &ppb_pdf; | 411 return &ppb_pdf; |
388 } | 412 } |
389 | 413 |
390 // static | 414 // static |
391 void PPB_PDF_Impl::InvokePrintingForInstance(PP_Instance instance_id) { | 415 void PPB_PDF_Impl::InvokePrintingForInstance(PP_Instance instance_id) { |
392 #if defined(ENABLE_PRINTING) | 416 #if defined(ENABLE_PRINTING) |
393 PluginInstance* instance = HostGlobals::Get()->GetInstance(instance_id); | 417 PluginInstance* instance = HostGlobals::Get()->GetInstance(instance_id); |
394 if (!instance) | 418 if (!instance) |
395 return; | 419 return; |
396 | 420 |
397 WebKit::WebElement element = instance->container()->element(); | 421 WebKit::WebElement element = instance->container()->element(); |
398 WebKit::WebView* view = element.document().frame()->view(); | 422 WebKit::WebView* view = element.document().frame()->view(); |
399 content::RenderView* render_view = content::RenderView::FromWebView(view); | 423 content::RenderView* render_view = content::RenderView::FromWebView(view); |
400 | 424 |
401 PrintWebViewHelper* print_view_helper = PrintWebViewHelper::Get(render_view); | 425 PrintWebViewHelper* print_view_helper = PrintWebViewHelper::Get(render_view); |
402 if (print_view_helper) | 426 if (print_view_helper) |
403 print_view_helper->PrintNode(element); | 427 print_view_helper->PrintNode(element); |
404 #endif | 428 #endif |
405 } | 429 } |
OLD | NEW |