diff options
author | raymes@chromium.org <raymes@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-27 23:30:41 +0000 |
---|---|---|
committer | raymes@chromium.org <raymes@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-27 23:30:41 +0000 |
commit | 234c139a2b9e49ebc4fa568de69c1a5c8248538b (patch) | |
tree | 7f2bbff8a839ac6f7e82c849d146928ea9cd6303 | |
parent | 8a6702b4c40143058d6750e198641a7e172db611 (diff) | |
download | chromium_src-234c139a2b9e49ebc4fa568de69c1a5c8248538b.zip chromium_src-234c139a2b9e49ebc4fa568de69c1a5c8248538b.tar.gz chromium_src-234c139a2b9e49ebc4fa568de69c1a5c8248538b.tar.bz2 |
Implement browser side of PPB_Printing resource.
This adds the browser side implementation of PPB_Printing as well as a unittest for PepperPrintingHost and a PPAPI browser_test for PPB_Printing::GetDefaultPrintSettings.
BUG=138333
TEST=content_unittests/browser_tests
Review URL: https://chromiumcodereview.appspot.com/10826072
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@159150 0039d316-1c4b-4281-b951-d872f2087c98
21 files changed, 541 insertions, 20 deletions
diff --git a/chrome/test/ppapi/ppapi_browsertest.cc b/chrome/test/ppapi/ppapi_browsertest.cc index 2c9753f..5e215f5 100644 --- a/chrome/test/ppapi/ppapi_browsertest.cc +++ b/chrome/test/ppapi/ppapi_browsertest.cc @@ -899,6 +899,9 @@ TEST_PPAPI_IN_PROCESS(MouseCursor) TEST_PPAPI_OUT_OF_PROCESS(MouseCursor) TEST_PPAPI_NACL_VIA_HTTP(MouseCursor) +// PPB_Printing only implemented for out of process. +TEST_PPAPI_OUT_OF_PROCESS(Printing) + // PPB_MessageLoop is only supported out-of-process. // TODO(dmichael): Enable for NaCl with the IPC proxy. crbug.com/116317 TEST_PPAPI_OUT_OF_PROCESS(MessageLoop_Basics) diff --git a/content/browser/renderer_host/pepper/browser_ppapi_host_test.cc b/content/browser/renderer_host/pepper/browser_ppapi_host_test.cc index 078224a..42fc6c0 100644 --- a/content/browser/renderer_host/pepper/browser_ppapi_host_test.cc +++ b/content/browser/renderer_host/pepper/browser_ppapi_host_test.cc @@ -20,7 +20,7 @@ BrowserPpapiHostTest::BrowserPpapiHostTest() BrowserPpapiHostTest::~BrowserPpapiHostTest() { } -BrowserPpapiHost* BrowserPpapiHostTest::GetPpapiHost() { +BrowserPpapiHost* BrowserPpapiHostTest::GetBrowserPpapiHost() { return ppapi_host_; } diff --git a/content/browser/renderer_host/pepper/browser_ppapi_host_test.h b/content/browser/renderer_host/pepper/browser_ppapi_host_test.h index eb7971a..d9e73a1 100644 --- a/content/browser/renderer_host/pepper/browser_ppapi_host_test.h +++ b/content/browser/renderer_host/pepper/browser_ppapi_host_test.h @@ -22,7 +22,7 @@ class BrowserPpapiHostTest { virtual ~BrowserPpapiHostTest(); ppapi::proxy::ResourceMessageTestSink& sink() { return sink_; } - BrowserPpapiHost* GetPpapiHost(); + BrowserPpapiHost* GetBrowserPpapiHost(); private: ppapi::proxy::ResourceMessageTestSink sink_; diff --git a/content/browser/renderer_host/pepper/content_browser_pepper_host_factory.cc b/content/browser/renderer_host/pepper/content_browser_pepper_host_factory.cc index 34ba0d3..eb325fd 100644 --- a/content/browser/renderer_host/pepper/content_browser_pepper_host_factory.cc +++ b/content/browser/renderer_host/pepper/content_browser_pepper_host_factory.cc @@ -6,6 +6,8 @@ #include "content/browser/renderer_host/pepper/browser_ppapi_host_impl.h" #include "content/browser/renderer_host/pepper/pepper_gamepad_host.h" +#include "content/browser/renderer_host/pepper/pepper_print_settings_manager.h" +#include "content/browser/renderer_host/pepper/pepper_printing_host.h" #include "ppapi/host/resource_host.h" #include "ppapi/proxy/ppapi_messages.h" @@ -37,6 +39,13 @@ scoped_ptr<ResourceHost> ContentBrowserPepperHostFactory::CreateResourceHost( case PpapiHostMsg_Gamepad_Create::ID: return scoped_ptr<ResourceHost>(new PepperGamepadHost( host_, instance, params.pp_resource())); + case PpapiHostMsg_Printing_Create::ID: { + scoped_ptr<PepperPrintSettingsManager> manager( + new PepperPrintSettingsManagerImpl()); + return scoped_ptr<ResourceHost>(new PepperPrintingHost( + host_->GetPpapiHost(), instance, + params.pp_resource(), manager.Pass())); + } } return scoped_ptr<ResourceHost>(); } diff --git a/content/browser/renderer_host/pepper/pepper_gamepad_host_unittest.cc b/content/browser/renderer_host/pepper/pepper_gamepad_host_unittest.cc index c5a09f7..231110a 100644 --- a/content/browser/renderer_host/pepper/pepper_gamepad_host_unittest.cc +++ b/content/browser/renderer_host/pepper/pepper_gamepad_host_unittest.cc @@ -135,7 +135,7 @@ TEST_F(PepperGamepadHostTest, WaitForReply) { PP_Instance pp_instance = 12345; PP_Resource pp_resource = 67890; - PepperGamepadHost gamepad_host(gamepad_service(), GetPpapiHost(), + PepperGamepadHost gamepad_host(gamepad_service(), GetBrowserPpapiHost(), pp_instance, pp_resource); // Synthesize a request for gamepad data. diff --git a/content/browser/renderer_host/pepper/pepper_print_settings_manager.cc b/content/browser/renderer_host/pepper/pepper_print_settings_manager.cc new file mode 100644 index 0000000..aa15c08 --- /dev/null +++ b/content/browser/renderer_host/pepper/pepper_print_settings_manager.cc @@ -0,0 +1,97 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "content/browser/renderer_host/pepper/pepper_print_settings_manager.h" + +#include "content/public/browser/browser_thread.h" +#include "ppapi/c/pp_errors.h" +#include "printing/printing_context.h" +#include "printing/units.h" + +namespace content { + +namespace { + +#if defined(ENABLE_PRINTING) +// Print units conversion functions. +int32_t DeviceUnitsInPoints(int32_t device_units, + int32_t device_units_per_inch) { + return printing::ConvertUnit(device_units, printing::kPointsPerInch, + device_units_per_inch); +} + +PP_Size PrintSizeToPPPrintSize(const gfx::Size& print_size, + int32_t device_units_per_inch) { + PP_Size result; + result.width = DeviceUnitsInPoints(print_size.width(), device_units_per_inch); + result.height = DeviceUnitsInPoints(print_size.height(), + device_units_per_inch); + return result; +} + +PP_Rect PrintAreaToPPPrintArea(const gfx::Rect& print_area, + int32_t device_units_per_inch) { + PP_Rect result; + result.point.x = DeviceUnitsInPoints(print_area.origin().x(), + device_units_per_inch); + result.point.y = DeviceUnitsInPoints(print_area.origin().y(), + device_units_per_inch); + result.size = PrintSizeToPPPrintSize(print_area.size(), + device_units_per_inch); + return result; +} + +PepperPrintSettingsManager::Result ComputeDefaultPrintSettings() { + // This function should run on the UI thread because |PrintingContext| methods + // call into platform APIs. + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + scoped_ptr<printing::PrintingContext> context( + printing::PrintingContext::Create(std::string())); + if (!context.get()) { + return PepperPrintSettingsManager::Result(PP_PrintSettings_Dev(), + PP_ERROR_FAILED); + } + context->UseDefaultSettings(); + const printing::PrintSettings& print_settings = context->settings(); + const printing::PageSetup& page_setup = + print_settings.page_setup_device_units(); + int device_units_per_inch = print_settings.device_units_per_inch(); + PP_PrintSettings_Dev settings; + settings.printable_area = PrintAreaToPPPrintArea( + page_setup.printable_area(), device_units_per_inch); + settings.content_area = PrintAreaToPPPrintArea( + page_setup.content_area(), device_units_per_inch); + settings.paper_size = PrintSizeToPPPrintSize( + page_setup.physical_size(), device_units_per_inch); + settings.dpi = print_settings.dpi(); + + // The remainder of the attributes are hard-coded to the defaults as set + // elsewhere. + settings.orientation = PP_PRINTORIENTATION_NORMAL; + settings.grayscale = PP_FALSE; + settings.print_scaling_option = PP_PRINTSCALINGOPTION_SOURCE_SIZE; + + // TODO(raymes): Should be computed in the same way as + // |PluginInstance::GetPreferredPrintOutputFormat|. + // |PP_PRINTOUTPUTFORMAT_PDF| is currently the only supported format though, + // so just make it the default. + settings.format = PP_PRINTOUTPUTFORMAT_PDF; + return PepperPrintSettingsManager::Result(settings, PP_OK); +} +#else +PepperPrintSettingsManager::Result ComputeDefaultPrintSettings() { + return PepperPrintSettingsManager::Result(PP_PrintSettings_Dev(), + PP_ERROR_NOTSUPPORTED); +} +#endif + +} // namespace + +void PepperPrintSettingsManagerImpl::GetDefaultPrintSettings( + PepperPrintSettingsManager::Callback callback) { + BrowserThread::PostTaskAndReplyWithResult(BrowserThread::UI, FROM_HERE, + base::Bind(ComputeDefaultPrintSettings), callback); +} + +} // namespace content diff --git a/content/browser/renderer_host/pepper/pepper_print_settings_manager.h b/content/browser/renderer_host/pepper/pepper_print_settings_manager.h new file mode 100644 index 0000000..48a8e9f --- /dev/null +++ b/content/browser/renderer_host/pepper/pepper_print_settings_manager.h @@ -0,0 +1,47 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_PRINT_SETTINGS_MANAGER_H_ +#define CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_PRINT_SETTINGS_MANAGER_H_ + +#include "base/bind.h" +#include "base/compiler_specific.h" +#include "content/common/content_export.h" +#include "ppapi/c/dev/pp_print_settings_dev.h" + +namespace content { + +// A class for getting the default print settings for the default printer. +class CONTENT_EXPORT PepperPrintSettingsManager { + public: + typedef std::pair<PP_PrintSettings_Dev, int32_t> Result; + typedef base::Callback<void(Result)> Callback; + + // The default print settings are obtained asynchronously and |callback| + // is called with the the print settings when they are available. |callback| + // will always be called on the same thread from which + // |GetDefaultPrintSettings| was issued. + virtual void GetDefaultPrintSettings(Callback callback) = 0; + + virtual ~PepperPrintSettingsManager() {} +}; + +// Real implementation for getting the default print settings. +class CONTENT_EXPORT PepperPrintSettingsManagerImpl + : public PepperPrintSettingsManager { + public: + PepperPrintSettingsManagerImpl() {} + virtual ~PepperPrintSettingsManagerImpl() {} + + // PepperPrintSettingsManager implementation. + virtual void GetDefaultPrintSettings( + PepperPrintSettingsManager::Callback callback) OVERRIDE; + + private: + DISALLOW_COPY_AND_ASSIGN(PepperPrintSettingsManagerImpl); +}; + +} // namespace content + +#endif // CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_PRINT_SETTINGS_MANAGER_H_ diff --git a/content/browser/renderer_host/pepper/pepper_printing_host.cc b/content/browser/renderer_host/pepper/pepper_printing_host.cc new file mode 100644 index 0000000..7d14f85 --- /dev/null +++ b/content/browser/renderer_host/pepper/pepper_printing_host.cc @@ -0,0 +1,57 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "content/browser/renderer_host/pepper/pepper_printing_host.h" + +#include "ppapi/c/dev/pp_print_settings_dev.h" +#include "ppapi/c/pp_errors.h" +#include "ppapi/host/dispatch_host_message.h" +#include "ppapi/host/host_message_context.h" +#include "ppapi/host/ppapi_host.h" +#include "ppapi/proxy/ppapi_messages.h" + +namespace content { + +PepperPrintingHost::PepperPrintingHost( + ppapi::host::PpapiHost* host, + PP_Instance instance, + PP_Resource resource, + scoped_ptr<PepperPrintSettingsManager> print_settings_manager) + : ResourceHost(host, instance, resource), + print_settings_manager_(print_settings_manager.Pass()), + weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { +} + +PepperPrintingHost::~PepperPrintingHost() { +} + +int32_t PepperPrintingHost::OnResourceMessageReceived( + const IPC::Message& msg, + ppapi::host::HostMessageContext* context) { + IPC_BEGIN_MESSAGE_MAP(PepperPrintingHost, msg) + PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( + PpapiHostMsg_Printing_GetDefaultPrintSettings, + OnMsgGetDefaultPrintSettings) + IPC_END_MESSAGE_MAP() + return PP_ERROR_FAILED; +} + +int32_t PepperPrintingHost::OnMsgGetDefaultPrintSettings( + ppapi::host::HostMessageContext* context) { + print_settings_manager_->GetDefaultPrintSettings( + base::Bind(&PepperPrintingHost::PrintSettingsCallback, + weak_factory_.GetWeakPtr(), context->MakeReplyParams())); + return PP_OK_COMPLETIONPENDING; +} + +void PepperPrintingHost::PrintSettingsCallback( + ppapi::proxy::ResourceMessageReplyParams reply_params, + PepperPrintSettingsManager::Result result) { + reply_params.set_result(result.second); + host()->SendReply(reply_params, + PpapiPluginMsg_Printing_GetDefaultPrintSettingsReply(result.first)); +} + + +} // namespace content diff --git a/content/browser/renderer_host/pepper/pepper_printing_host.h b/content/browser/renderer_host/pepper/pepper_printing_host.h new file mode 100644 index 0000000..5b86716 --- /dev/null +++ b/content/browser/renderer_host/pepper/pepper_printing_host.h @@ -0,0 +1,49 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_PRINTING_HOST_H_ +#define CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_PRINTING_HOST_H_ + +#include "base/compiler_specific.h" +#include "base/memory/scoped_ptr.h" +#include "base/memory/weak_ptr.h" +#include "content/browser/renderer_host/pepper/pepper_print_settings_manager.h" +#include "content/common/content_export.h" +#include "ppapi/host/resource_host.h" +#include "ppapi/proxy/resource_message_params.h" + +namespace content { + +class CONTENT_EXPORT PepperPrintingHost : public ppapi::host::ResourceHost { + public: + PepperPrintingHost( + ppapi::host::PpapiHost* host, + PP_Instance instance, + PP_Resource resource, + scoped_ptr<PepperPrintSettingsManager> print_settings_manager); + virtual ~PepperPrintingHost(); + + // ppapi::host::ResourceHost implementation. + virtual int32_t OnResourceMessageReceived( + const IPC::Message& msg, + ppapi::host::HostMessageContext* context) OVERRIDE; + + private: + int32_t OnMsgGetDefaultPrintSettings( + ppapi::host::HostMessageContext* context); + + void PrintSettingsCallback( + ppapi::proxy::ResourceMessageReplyParams reply_params, + PepperPrintSettingsManager::Result result); + + scoped_ptr<PepperPrintSettingsManager> print_settings_manager_; + + base::WeakPtrFactory<PepperPrintingHost> weak_factory_; + + DISALLOW_COPY_AND_ASSIGN(PepperPrintingHost); +}; + +} // namespace content + +#endif // CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_PRINTING_HOST_H_ diff --git a/content/browser/renderer_host/pepper/pepper_printing_host_unittest.cc b/content/browser/renderer_host/pepper/pepper_printing_host_unittest.cc new file mode 100644 index 0000000..2237d48 --- /dev/null +++ b/content/browser/renderer_host/pepper/pepper_printing_host_unittest.cc @@ -0,0 +1,128 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "content/browser/renderer_host/pepper/browser_ppapi_host_test.h" +#include "content/browser/renderer_host/pepper/pepper_print_settings_manager.h" +#include "content/browser/renderer_host/pepper/pepper_printing_host.h" +#include "ppapi/c/pp_errors.h" +#include "ppapi/host/host_message_context.h" +#include "ppapi/host/ppapi_host.h" +#include "ppapi/proxy/ppapi_messages.h" +#include "ppapi/proxy/resource_message_params.h" +#include "ppapi/proxy/resource_message_test_sink.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace content { + +namespace { + +// Mock implementation of |PepperPrintSettingsManager| for test purposes. +class MockPepperPrintSettingsManager + : public PepperPrintSettingsManager { + public: + MockPepperPrintSettingsManager(const PP_PrintSettings_Dev& settings); + virtual ~MockPepperPrintSettingsManager() {} + + // PepperPrintSettingsManager implementation. + virtual void GetDefaultPrintSettings( + PepperPrintSettingsManager::Callback callback) OVERRIDE; + private: + PP_PrintSettings_Dev settings_; + + DISALLOW_COPY_AND_ASSIGN(MockPepperPrintSettingsManager); +}; + +MockPepperPrintSettingsManager::MockPepperPrintSettingsManager( + const PP_PrintSettings_Dev& settings) + : settings_(settings) { +} + +void MockPepperPrintSettingsManager::GetDefaultPrintSettings( + PepperPrintSettingsManager::Callback callback) { + callback.Run(PepperPrintSettingsManager::Result(settings_, PP_OK)); +} + +class PepperPrintingHostTest + : public testing::Test, + public BrowserPpapiHostTest { + public: + PepperPrintingHostTest() { + } + + ~PepperPrintingHostTest() { + } + + DISALLOW_COPY_AND_ASSIGN(PepperPrintingHostTest); +}; + +bool PP_SizeEqual(const PP_Size& lhs, const PP_Size& rhs) { + return lhs.width == rhs.width && lhs.height == rhs.height; +} + +bool PP_RectEqual(const PP_Rect& lhs, const PP_Rect& rhs) { + return lhs.point.x == rhs.point.x && + lhs.point.y == rhs.point.y && + PP_SizeEqual(lhs.size, rhs.size); +} + +} // namespace + +TEST_F(PepperPrintingHostTest, GetDefaultPrintSettings) { + PP_Instance pp_instance = 12345; + PP_Resource pp_resource = 67890; + PP_PrintSettings_Dev expected_settings = { + { { 0, 0 }, { 500, 515 } }, + { { 25, 35 }, { 300, 720 } }, + { 600, 700 }, + 200, + PP_PRINTORIENTATION_NORMAL, + PP_PRINTSCALINGOPTION_NONE, + PP_FALSE, + PP_PRINTOUTPUTFORMAT_PDF + }; + + // Construct the resource host. + scoped_ptr<PepperPrintSettingsManager> manager( + new MockPepperPrintSettingsManager(expected_settings)); + PepperPrintingHost printing(GetBrowserPpapiHost()->GetPpapiHost(), + pp_instance, pp_resource, manager.Pass()); + + // Simulate a message being received. + ppapi::proxy::ResourceMessageCallParams call_params(pp_resource, 1); + ppapi::host::HostMessageContext context(call_params); + int32 result = printing.OnResourceMessageReceived( + PpapiHostMsg_Printing_GetDefaultPrintSettings(), &context); + EXPECT_EQ(PP_OK_COMPLETIONPENDING, result); + + // This should have sent the Pepper reply to our test sink. + ppapi::proxy::ResourceMessageReplyParams reply_params; + IPC::Message reply_msg; + ASSERT_TRUE(sink().GetFirstResourceReplyMatching( + PpapiPluginMsg_Printing_GetDefaultPrintSettingsReply::ID, &reply_params, + &reply_msg)); + + // Validation of reply. + EXPECT_EQ(call_params.sequence(), reply_params.sequence()); + EXPECT_EQ(PP_OK, reply_params.result()); + PpapiPluginMsg_Printing_GetDefaultPrintSettingsReply::Schema::Param + reply_msg_param; + ASSERT_TRUE(PpapiPluginMsg_Printing_GetDefaultPrintSettingsReply::Read( + &reply_msg, &reply_msg_param)); + PP_PrintSettings_Dev actual_settings = reply_msg_param.a; + + EXPECT_TRUE(PP_RectEqual(expected_settings.printable_area, + actual_settings.printable_area)); + EXPECT_TRUE(PP_RectEqual(expected_settings.content_area, + actual_settings.content_area)); + EXPECT_TRUE(PP_SizeEqual(expected_settings.paper_size, + actual_settings.paper_size)); + EXPECT_EQ(expected_settings.dpi, actual_settings.dpi); + EXPECT_EQ(expected_settings.orientation, actual_settings.orientation); + EXPECT_EQ(expected_settings.print_scaling_option, + actual_settings.print_scaling_option); + EXPECT_EQ(expected_settings.grayscale, actual_settings.grayscale); + EXPECT_EQ(expected_settings.format, actual_settings.format); +} + +} // namespace content diff --git a/content/content_browser.gypi b/content/content_browser.gypi index b829492..47a493f 100644 --- a/content/content_browser.gypi +++ b/content/content_browser.gypi @@ -8,6 +8,7 @@ '../base/base.gyp:base_static', '../crypto/crypto.gyp:crypto', '../google_apis/google_apis.gyp:google_apis', + '../printing/printing.gyp:printing', '../net/net.gyp:net', '../skia/skia.gyp:skia', '../third_party/zlib/zlib.gyp:zlib', @@ -636,6 +637,10 @@ 'browser/renderer_host/pepper/pepper_lookup_request.h', 'browser/renderer_host/pepper/pepper_message_filter.cc', 'browser/renderer_host/pepper/pepper_message_filter.h', + 'browser/renderer_host/pepper/pepper_print_settings_manager.cc', + 'browser/renderer_host/pepper/pepper_print_settings_manager.h', + 'browser/renderer_host/pepper/pepper_printing_host.cc', + 'browser/renderer_host/pepper/pepper_printing_host.h', 'browser/renderer_host/pepper/pepper_tcp_server_socket.cc', 'browser/renderer_host/pepper/pepper_tcp_server_socket.h', 'browser/renderer_host/pepper/pepper_tcp_socket.cc', diff --git a/content/content_tests.gypi b/content/content_tests.gypi index dd5f0088..edceca5 100644 --- a/content/content_tests.gypi +++ b/content/content_tests.gypi @@ -300,6 +300,7 @@ 'browser/renderer_host/pepper/browser_ppapi_host_test.cc', 'browser/renderer_host/pepper/browser_ppapi_host_test.h', 'browser/renderer_host/pepper/pepper_gamepad_host_unittest.cc', + 'browser/renderer_host/pepper/pepper_printing_host_unittest.cc', 'browser/renderer_host/render_view_host_unittest.cc', 'browser/renderer_host/render_widget_host_unittest.cc', 'browser/renderer_host/render_widget_host_view_aura_unittest.cc', diff --git a/ppapi/cpp/dev/printing_dev.cc b/ppapi/cpp/dev/printing_dev.cc index 8c30725..00bb15b 100644 --- a/ppapi/cpp/dev/printing_dev.cc +++ b/ppapi/cpp/dev/printing_dev.cc @@ -16,6 +16,10 @@ namespace { static const char kPPPPrintingInterface[] = PPP_PRINTING_DEV_INTERFACE; +template <> const char* interface_name<PPB_Printing_Dev_0_7>() { + return PPB_PRINTING_DEV_INTERFACE_0_7; +} + template <> const char* interface_name<PPB_Printing_Dev_0_6>() { return PPB_PRINTING_DEV_INTERFACE_0_6; } @@ -76,9 +80,14 @@ const PPP_Printing_Dev ppp_printing = { } // namespace Printing_Dev::Printing_Dev(Instance* instance) - : associated_instance_(instance) { + : associated_instance_(instance) { Module::Get()->AddPluginInterface(kPPPPrintingInterface, &ppp_printing); - instance->AddPerInstanceObject(kPPPPrintingInterface, this); + instance->AddPerInstanceObject( + kPPPPrintingInterface, this); + if (has_interface<PPB_Printing_Dev_0_7>()) { + PassRefFromConstructor(get_interface<PPB_Printing_Dev_0_7>()->Create( + associated_instance_.pp_instance())); + } } Printing_Dev::~Printing_Dev() { @@ -88,16 +97,23 @@ Printing_Dev::~Printing_Dev() { // static bool Printing_Dev::IsAvailable() { - return has_interface<PPB_Printing_Dev_0_6>(); + return has_interface<PPB_Printing_Dev_0_7>() || + has_interface<PPB_Printing_Dev_0_6>(); + } -bool Printing_Dev::GetDefaultPrintSettings( - PP_PrintSettings_Dev* print_settings) const { - if (!has_interface<PPB_Printing_Dev_0_6>()) - return false; - return PP_ToBool( - get_interface<PPB_Printing_Dev_0_6>()->GetDefaultPrintSettings( - associated_instance_.pp_instance(), print_settings)); +void Printing_Dev::GetDefaultPrintSettings( + const CompletionCallbackWithOutput<PP_PrintSettings_Dev>& callback) const { + if (has_interface<PPB_Printing_Dev_0_7>()) { + get_interface<PPB_Printing_Dev_0_7>()->GetDefaultPrintSettings( + pp_resource(), callback.output(), callback.pp_completion_callback()); + } else if (has_interface<PPB_Printing_Dev_0_6>()) { + bool success = PP_ToBool(get_interface<PPB_Printing_Dev_0_6>()-> + GetDefaultPrintSettings(associated_instance_.pp_instance(), + callback.output())); + Module::Get()->core()->CallOnMainThread(0, callback, + success ? PP_OK : PP_ERROR_FAILED); + } } } // namespace pp diff --git a/ppapi/cpp/dev/printing_dev.h b/ppapi/cpp/dev/printing_dev.h index eb20e56..9d5a7db 100644 --- a/ppapi/cpp/dev/printing_dev.h +++ b/ppapi/cpp/dev/printing_dev.h @@ -6,6 +6,7 @@ #define PPAPI_CPP_DEV_PRINTING_DEV_H_ #include "ppapi/c/dev/ppp_printing_dev.h" +#include "ppapi/cpp/completion_callback.h" #include "ppapi/cpp/instance_handle.h" #include "ppapi/cpp/resource.h" @@ -15,7 +16,7 @@ class Instance; // You would typically use this either via inheritance on your instance or // by composition: see find_dev.h for an example. -class Printing_Dev { +class Printing_Dev : public Resource { public: // The instance parameter must outlive this class. explicit Printing_Dev(Instance* instance); @@ -35,9 +36,11 @@ class Printing_Dev { // interface. static bool IsAvailable(); - // Outputs the default print settings for the default printer into - // |print_settings|. Returns false on error. - bool GetDefaultPrintSettings(PP_PrintSettings_Dev* print_settings) const; + // Get the default print settings and store them in the output of |callback|. + // This method always runs asynchronously and the callback will always be + // triggered. + void GetDefaultPrintSettings( + const CompletionCallbackWithOutput<PP_PrintSettings_Dev>& callback) const; private: InstanceHandle associated_instance_; diff --git a/ppapi/host/dispatch_host_message.h b/ppapi/host/dispatch_host_message.h index b01a0ed..f891851 100644 --- a/ppapi/host/dispatch_host_message.h +++ b/ppapi/host/dispatch_host_message.h @@ -63,7 +63,7 @@ inline int32_t DispatchResourceCall(ObjT* obj, Method method, // Note that this only works for message with 1 or more parameters. For // 0-parameter messages you need to use the _0 version below (since there are -// no Params in the message). +// no params in the message). #define PPAPI_DISPATCH_HOST_RESOURCE_CALL(msg_class, member_func) \ case msg_class::ID: { \ TRACK_RUN_IN_IPC_HANDLER(member_func); \ diff --git a/ppapi/ppapi_sources.gypi b/ppapi/ppapi_sources.gypi index e939b79..79ca558 100644 --- a/ppapi/ppapi_sources.gypi +++ b/ppapi/ppapi_sources.gypi @@ -379,6 +379,8 @@ 'tests/test_paint_aggregator.h', 'tests/test_post_message.cc', 'tests/test_post_message.h', + 'tests/test_printing.cc', + 'tests/test_printing.h', 'tests/test_scrollbar.cc', 'tests/test_scrollbar.h', 'tests/test_tcp_server_socket_private.cc', diff --git a/ppapi/proxy/plugin_dispatcher.cc b/ppapi/proxy/plugin_dispatcher.cc index d48dbfb..8dbca8d 100644 --- a/ppapi/proxy/plugin_dispatcher.cc +++ b/ppapi/proxy/plugin_dispatcher.cc @@ -102,6 +102,10 @@ PluginDispatcher* PluginDispatcher::GetForResource(const Resource* resource) { // static const void* PluginDispatcher::GetBrowserInterface(const char* interface_name) { + DCHECK(interface_name) << "|interface_name| is null. Did you forget to add " + "the |interface_name()| template function to the interface's C++ " + "wrapper?"; + return InterfaceList::GetInstance()->GetInterfaceForPPB(interface_name); } diff --git a/ppapi/proxy/printing_resource.cc b/ppapi/proxy/printing_resource.cc index 4d55db0..9fd8fde 100644 --- a/ppapi/proxy/printing_resource.cc +++ b/ppapi/proxy/printing_resource.cc @@ -34,7 +34,7 @@ int32_t PrintingResource::GetDefaultPrintSettings( if (TrackedCallback::IsPending(callback_)) return PP_ERROR_INPROGRESS; - if (!sent_create_to_renderer()) + if (!sent_create_to_browser()) SendCreateToBrowser(PpapiHostMsg_Printing_Create()); DCHECK(!print_settings_); diff --git a/ppapi/tests/test_printing.cc b/ppapi/tests/test_printing.cc new file mode 100644 index 0000000..6a82ebb --- /dev/null +++ b/ppapi/tests/test_printing.cc @@ -0,0 +1,64 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ppapi/tests/test_printing.h" + +#include "ppapi/cpp/dev/printing_dev.h" +#include "ppapi/cpp/instance.h" +#include "ppapi/tests/testing_instance.h" + +namespace { + bool g_callback_triggered; + int32_t g_callback_result; +} // namespace + +REGISTER_TEST_CASE(Printing); + +class TestPrinting_Dev : public pp::Printing_Dev { + public: + explicit TestPrinting_Dev(pp::Instance* instance) : + pp::Printing_Dev(instance) {} + virtual ~TestPrinting_Dev() {} + virtual uint32_t QuerySupportedPrintOutputFormats() { return 0; } + virtual int32_t PrintBegin( + const PP_PrintSettings_Dev& print_settings) { return 0; } + virtual pp::Resource PrintPages( + const PP_PrintPageNumberRange_Dev* page_ranges, + uint32_t page_range_count) { + return pp::Resource(); + } + virtual void PrintEnd() {} + virtual bool IsPrintScalingDisabled() { return false; } +}; + +TestPrinting::TestPrinting(TestingInstance* instance) + : TestCase(instance), + nested_event_(instance->pp_instance()) { + callback_factory_.Initialize(this); +} + +void TestPrinting::RunTests(const std::string& filter) { + RUN_TEST(GetDefaultPrintSettings, filter); +} + +std::string TestPrinting::TestGetDefaultPrintSettings() { + g_callback_triggered = false; + TestPrinting_Dev test_printing(instance_); + pp::CompletionCallbackWithOutput<PP_PrintSettings_Dev> cb = + callback_factory_.NewCallbackWithOutput(&TestPrinting::Callback); + test_printing.GetDefaultPrintSettings(cb); + nested_event_.Wait(); + + ASSERT_EQ(PP_OK, g_callback_result); + ASSERT_TRUE(g_callback_triggered); + + PASS(); +} + +void TestPrinting::Callback(int32_t result, + PP_PrintSettings_Dev& /* unused */) { + g_callback_triggered = true; + g_callback_result = result; + nested_event_.Signal(); +} diff --git a/ppapi/tests/test_printing.h b/ppapi/tests/test_printing.h new file mode 100644 index 0000000..ceb0983 --- /dev/null +++ b/ppapi/tests/test_printing.h @@ -0,0 +1,35 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PPAPI_TESTS_TEST_PRINTING_H_ +#define PPAPI_TESTS_TEST_PRINTING_H_ + +#include <string> + +#include "ppapi/tests/test_case.h" +#include "ppapi/tests/test_utils.h" +#include "ppapi/utility/completion_callback_factory.h" + +struct PP_PrintSettings_Dev; + +class TestPrinting : public TestCase { + public: + explicit TestPrinting(TestingInstance* instance); + + // TestCase implementation. + virtual void RunTests(const std::string& filter); + + private: + // Tests. + std::string TestGetDefaultPrintSettings(); + + void Callback(int32_t result, + PP_PrintSettings_Dev&); + + NestedEvent nested_event_; + + pp::CompletionCallbackFactory<TestPrinting> callback_factory_; +}; + +#endif // PPAPI_TESTS_TEST_PRINTING_H_ diff --git a/ppapi/thunk/enter.cc b/ppapi/thunk/enter.cc index d5d822b..c89c969 100644 --- a/ppapi/thunk/enter.cc +++ b/ppapi/thunk/enter.cc @@ -62,7 +62,8 @@ EnterBase::~EnterBase() { // callback_ is cleared any time it is run, scheduled to be run, or once we // know it will be completed asynchronously. So by this point it should be // NULL. - DCHECK(!callback_); + DCHECK(!callback_) << "|callback_| is not NULL. Did you forget to call " + "|EnterBase::SetResult| in the interface's thunk?"; } int32_t EnterBase::SetResult(int32_t result) { |