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.
5 #include "ppapi/cpp/dev/printing_dev.h"
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"
17 static const char kPPPPrintingInterface
[] = PPP_PRINTING_DEV_INTERFACE
;
19 template <> const char* interface_name
<PPB_Printing_Dev_0_7
>() {
20 return PPB_PRINTING_DEV_INTERFACE_0_7
;
23 template <> const char* interface_name
<PPB_Printing_Dev_0_6
>() {
24 return PPB_PRINTING_DEV_INTERFACE_0_6
;
27 uint32_t QuerySupportedFormats(PP_Instance instance
) {
29 Instance::GetPerInstanceObject(instance
, kPPPPrintingInterface
);
32 return static_cast<Printing_Dev
*>(object
)->QuerySupportedPrintOutputFormats();
35 int32_t Begin(PP_Instance instance
,
36 const struct PP_PrintSettings_Dev
* print_settings
) {
38 Instance::GetPerInstanceObject(instance
, kPPPPrintingInterface
);
41 return static_cast<Printing_Dev
*>(object
)->PrintBegin(*print_settings
);
44 PP_Resource
PrintPages(PP_Instance instance
,
45 const struct PP_PrintPageNumberRange_Dev
* page_ranges
,
46 uint32_t page_range_count
) {
48 pp::Instance::GetPerInstanceObject(instance
, kPPPPrintingInterface
);
51 return static_cast<Printing_Dev
*>(object
)->PrintPages(
52 page_ranges
, page_range_count
).detach();
55 void End(PP_Instance instance
) {
57 pp::Instance::GetPerInstanceObject(instance
, kPPPPrintingInterface
);
59 static_cast<Printing_Dev
*>(object
)->PrintEnd();
62 PP_Bool
IsScalingDisabled(PP_Instance instance
) {
64 pp::Instance::GetPerInstanceObject(instance
, kPPPPrintingInterface
);
68 static_cast<Printing_Dev
*>(object
)->IsPrintScalingDisabled();
69 return PP_FromBool(return_value
);
72 const PPP_Printing_Dev ppp_printing
= {
73 &QuerySupportedFormats
,
82 Printing_Dev::Printing_Dev(Instance
* instance
)
83 : associated_instance_(instance
) {
84 Module::Get()->AddPluginInterface(kPPPPrintingInterface
, &ppp_printing
);
85 instance
->AddPerInstanceObject(
86 kPPPPrintingInterface
, this);
87 if (has_interface
<PPB_Printing_Dev_0_7
>()) {
88 PassRefFromConstructor(get_interface
<PPB_Printing_Dev_0_7
>()->Create(
89 associated_instance_
.pp_instance()));
93 Printing_Dev::~Printing_Dev() {
94 Instance::RemovePerInstanceObject(associated_instance_
,
95 kPPPPrintingInterface
, this);
99 bool Printing_Dev::IsAvailable() {
100 return has_interface
<PPB_Printing_Dev_0_7
>() ||
101 has_interface
<PPB_Printing_Dev_0_6
>();
105 int32_t Printing_Dev::GetDefaultPrintSettings(
106 const CompletionCallbackWithOutput
<PP_PrintSettings_Dev
>& callback
) const {
107 if (has_interface
<PPB_Printing_Dev_0_7
>()) {
108 return get_interface
<PPB_Printing_Dev_0_7
>()->GetDefaultPrintSettings(
109 pp_resource(), callback
.output(), callback
.pp_completion_callback());
110 } else if (has_interface
<PPB_Printing_Dev_0_6
>()) {
111 bool success
= PP_ToBool(get_interface
<PPB_Printing_Dev_0_6
>()->
112 GetDefaultPrintSettings(associated_instance_
.pp_instance(),
114 return callback
.MayForce(success
? PP_OK
: PP_ERROR_FAILED
);
116 return callback
.MayForce(PP_ERROR_NOINTERFACE
);