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 3 // found in the LICENSE file. 4 5 #include "ppapi/cpp/dev/printing_dev.h" 6 7 #include "ppapi/c/dev/ppb_printing_dev.h" 8 #include "ppapi/cpp/instance.h" 9 #include "ppapi/cpp/instance_handle.h" 10 #include "ppapi/cpp/module.h" 11 #include "ppapi/cpp/module_impl.h" 12 13 namespace pp { 14 15 namespace { 16 17 static const char kPPPPrintingInterface[] = PPP_PRINTING_DEV_INTERFACE; 18 19 template <> const char* interface_name<PPB_Printing_Dev_0_7>() { 20 return PPB_PRINTING_DEV_INTERFACE_0_7; 21 } 22 23 uint32_t QuerySupportedFormats(PP_Instance instance) { 24 void* object = 25 Instance::GetPerInstanceObject(instance, kPPPPrintingInterface); 26 if (!object) 27 return 0; 28 return static_cast<Printing_Dev*>(object)->QuerySupportedPrintOutputFormats(); 29 } 30 31 int32_t Begin(PP_Instance instance, 32 const struct PP_PrintSettings_Dev* print_settings) { 33 void* object = 34 Instance::GetPerInstanceObject(instance, kPPPPrintingInterface); 35 if (!object) 36 return 0; 37 return static_cast<Printing_Dev*>(object)->PrintBegin(*print_settings); 38 } 39 40 PP_Resource PrintPages(PP_Instance instance, 41 const struct PP_PrintPageNumberRange_Dev* page_ranges, 42 uint32_t page_range_count) { 43 void* object = 44 pp::Instance::GetPerInstanceObject(instance, kPPPPrintingInterface); 45 if (!object) 46 return 0; 47 return static_cast<Printing_Dev*>(object)->PrintPages( 48 page_ranges, page_range_count).detach(); 49 } 50 51 void End(PP_Instance instance) { 52 void* object = 53 pp::Instance::GetPerInstanceObject(instance, kPPPPrintingInterface); 54 if (object) 55 static_cast<Printing_Dev*>(object)->PrintEnd(); 56 } 57 58 PP_Bool IsScalingDisabled(PP_Instance instance) { 59 void* object = 60 pp::Instance::GetPerInstanceObject(instance, kPPPPrintingInterface); 61 if (!object) 62 return PP_FALSE; 63 bool return_value = 64 static_cast<Printing_Dev*>(object)->IsPrintScalingDisabled(); 65 return PP_FromBool(return_value); 66 } 67 68 const PPP_Printing_Dev ppp_printing = { 69 &QuerySupportedFormats, 70 &Begin, 71 &PrintPages, 72 &End, 73 &IsScalingDisabled 74 }; 75 76 } // namespace 77 78 Printing_Dev::Printing_Dev(Instance* instance) 79 : associated_instance_(instance) { 80 Module::Get()->AddPluginInterface(kPPPPrintingInterface, &ppp_printing); 81 instance->AddPerInstanceObject( 82 kPPPPrintingInterface, this); 83 if (has_interface<PPB_Printing_Dev_0_7>()) { 84 PassRefFromConstructor(get_interface<PPB_Printing_Dev_0_7>()->Create( 85 associated_instance_.pp_instance())); 86 } 87 } 88 89 Printing_Dev::~Printing_Dev() { 90 Instance::RemovePerInstanceObject(associated_instance_, 91 kPPPPrintingInterface, this); 92 } 93 94 // static 95 bool Printing_Dev::IsAvailable() { 96 return has_interface<PPB_Printing_Dev_0_7>(); 97 } 98 99 int32_t Printing_Dev::GetDefaultPrintSettings( 100 const CompletionCallbackWithOutput<PP_PrintSettings_Dev>& callback) const { 101 if (has_interface<PPB_Printing_Dev_0_7>()) { 102 return get_interface<PPB_Printing_Dev_0_7>()->GetDefaultPrintSettings( 103 pp_resource(), callback.output(), callback.pp_completion_callback()); 104 } 105 return callback.MayForce(PP_ERROR_NOINTERFACE); 106 } 107 108 } // namespace pp 109