summaryrefslogtreecommitdiffstats
path: root/content
diff options
context:
space:
mode:
authorraymes@chromium.org <raymes@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-20 23:30:00 +0000
committerraymes@chromium.org <raymes@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-20 23:30:00 +0000
commit49727344dc47bc593929c22312754c8e05861bde (patch)
tree234eb58b474c1fa68457d68826110b2e92d5f9f2 /content
parente39c71e424779b812ca783c2e1f99cc67c8c702f (diff)
downloadchromium_src-49727344dc47bc593929c22312754c8e05861bde.zip
chromium_src-49727344dc47bc593929c22312754c8e05861bde.tar.gz
chromium_src-49727344dc47bc593929c22312754c8e05861bde.tar.bz2
Provides a real implementation of GetDefaultPrintSettings.
This accesses the Chrome printing code to determine the default print settings for the default printer. Ideally we would query these settings every time they are requested, however this cannot be done (see the description in pepper_print_settings_initializer.h). Instead the settings are grabbed once at startup and cached. BUG= TEST=Added ppapi test. Review URL: https://chromiumcodereview.appspot.com/10539171 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@143294 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content')
-rw-r--r--content/browser/renderer_host/pepper_message_filter.cc19
-rw-r--r--content/browser/renderer_host/pepper_message_filter.h10
-rw-r--r--content/browser/renderer_host/pepper_print_settings_initializer.cc124
-rw-r--r--content/browser/renderer_host/pepper_print_settings_initializer.h46
-rw-r--r--content/content_browser.gypi3
5 files changed, 200 insertions, 2 deletions
diff --git a/content/browser/renderer_host/pepper_message_filter.cc b/content/browser/renderer_host/pepper_message_filter.cc
index d6336aa..f1f50d5 100644
--- a/content/browser/renderer_host/pepper_message_filter.cc
+++ b/content/browser/renderer_host/pepper_message_filter.cc
@@ -52,6 +52,7 @@
#endif
using content::BrowserThread;
+using content::PepperPrintSettingsInitializer;
using content::RenderViewHostImpl;
using ppapi::NetAddressPrivateImpl;
@@ -77,13 +78,15 @@ PepperMessageFilter::PepperMessageFilter(
process_id_(process_id),
resource_context_(browser_context->GetResourceContext()),
host_resolver_(NULL),
- next_socket_id_(1) {
+ next_socket_id_(1),
+ print_settings_initializer_(new PepperPrintSettingsInitializer) {
DCHECK(type == RENDERER);
DCHECK(browser_context);
// Keep BrowserContext data in FILE-thread friendly storage.
browser_path_ = browser_context->GetPath();
incognito_ = browser_context->IsOffTheRecord();
DCHECK(resource_context_);
+ print_settings_initializer_->Initialize();
}
PepperMessageFilter::PepperMessageFilter(ProcessType type,
@@ -93,9 +96,11 @@ PepperMessageFilter::PepperMessageFilter(ProcessType type,
resource_context_(NULL),
host_resolver_(host_resolver),
next_socket_id_(1),
- incognito_(false) {
+ incognito_(false),
+ print_settings_initializer_(new PepperPrintSettingsInitializer) {
DCHECK(type == PLUGIN);
DCHECK(host_resolver);
+ print_settings_initializer_->Initialize();
}
void PepperMessageFilter::OverrideThreadForMessage(
@@ -166,6 +171,10 @@ bool PepperMessageFilter::OnMessageReceived(const IPC::Message& msg,
IPC_MESSAGE_HANDLER(PepperMsg_GetDeviceID, OnGetDeviceID)
IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlashDeviceID_Get, OnGetDeviceIDAsync)
+ // PPBInstance messages.
+ IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBPInstance_GetDefaultPrintSettings,
+ OnGetDefaultPrintSettings);
+
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP_EX()
return handled;
@@ -697,6 +706,12 @@ void PepperMessageFilter::OnGetDeviceIDAsync(int32_t routing_id,
result));
}
+void PepperMessageFilter::OnGetDefaultPrintSettings(
+ PP_PrintSettings_Dev* settings,
+ bool* result) {
+ *result = print_settings_initializer_->GetDefaultPrintSettings(settings);
+}
+
void PepperMessageFilter::GetFontFamiliesComplete(
IPC::Message* reply_msg,
scoped_ptr<base::ListValue> result) {
diff --git a/content/browser/renderer_host/pepper_message_filter.h b/content/browser/renderer_host/pepper_message_filter.h
index 9a0a453..fab8f50 100644
--- a/content/browser/renderer_host/pepper_message_filter.h
+++ b/content/browser/renderer_host/pepper_message_filter.h
@@ -16,6 +16,7 @@
#include "base/memory/scoped_ptr.h"
#include "base/process.h"
#include "base/time.h"
+#include "content/browser/renderer_host/pepper_print_settings_initializer.h"
#include "content/public/browser/browser_message_filter.h"
#include "net/base/network_change_notifier.h"
#include "net/base/net_util.h"
@@ -29,6 +30,7 @@ class PepperTCPSocket;
class PepperUDPSocket;
struct PP_HostResolver_Private_Hint;
struct PP_NetAddress_Private;
+struct PP_PrintSettings_Dev;
namespace base {
class ListValue;
@@ -216,6 +218,11 @@ class PepperMessageFilter
void OnGetDeviceID(std::string* id);
void OnGetDeviceIDAsync(int32_t routing_id, PP_Resource resource);
+ // Outputs the default print settings in |settings|. Sets |result| to true
+ // on success.
+ void OnGetDefaultPrintSettings(PP_PrintSettings_Dev* settings,
+ bool* result);
+
// Callback when the font list has been retrieved on a background thread.
void GetFontFamiliesComplete(IPC::Message* reply_msg,
scoped_ptr<base::ListValue> result);
@@ -258,6 +265,9 @@ class PepperMessageFilter
FilePath browser_path_;
bool incognito_;
+ scoped_refptr<content::PepperPrintSettingsInitializer>
+ print_settings_initializer_;
+
DISALLOW_COPY_AND_ASSIGN(PepperMessageFilter);
};
diff --git a/content/browser/renderer_host/pepper_print_settings_initializer.cc b/content/browser/renderer_host/pepper_print_settings_initializer.cc
new file mode 100644
index 0000000..59cb5dd
--- /dev/null
+++ b/content/browser/renderer_host/pepper_print_settings_initializer.cc
@@ -0,0 +1,124 @@
+// 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_print_settings_initializer.h"
+
+#include "content/browser/browser_thread_impl.h"
+#include "printing/printing_context.h"
+#include "printing/units.h"
+
+namespace content {
+
+namespace {
+
+// |kAccessorThread| is the thread which will call |GetDefaultPrintSettings|.
+const BrowserThread::ID kAccessorThread = BrowserThread::IO;
+
+// Print units conversion functions.
+int32_t DeviceUnitsInPoints(int32_t device_units,
+ int32_t device_units_per_inch) {
+#if defined(ENABLE_PRINTING)
+ return printing::ConvertUnit(device_units, printing::kPointsPerInch,
+ device_units_per_inch);
+#else
+ return 0;
+#endif
+}
+
+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;
+}
+
+} // namespace
+
+PepperPrintSettingsInitializer::PepperPrintSettingsInitializer()
+ : initialized_(false) {
+}
+
+void PepperPrintSettingsInitializer::Initialize() {
+ // Multiple initializations are OK, it would just refresh the settings.
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
+ base::Bind(&PepperPrintSettingsInitializer::DoInitialize, this));
+}
+
+bool PepperPrintSettingsInitializer::GetDefaultPrintSettings(
+ PP_PrintSettings_Dev* settings) const {
+ DCHECK(BrowserThread::CurrentlyOn(kAccessorThread));
+ if (!initialized_)
+ return false;
+ *settings = default_print_settings_;
+ return true;
+}
+
+PepperPrintSettingsInitializer::~PepperPrintSettingsInitializer() {
+}
+
+void PepperPrintSettingsInitializer::DoInitialize() {
+// If printing isn't enabled, we don't do any initialization.
+#if defined(ENABLE_PRINTING)
+ // This function should run on the UI thread because |PrintingContext| methods
+ // call into platform APIs.
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ PP_PrintSettings_Dev default_settings;
+ scoped_ptr<printing::PrintingContext> context(
+ printing::PrintingContext::Create(""));
+ if (!context.get())
+ return;
+ 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();
+ default_settings.printable_area =
+ PrintAreaToPPPrintArea(page_setup.printable_area(), device_units_per_inch);
+ default_settings.content_area =
+ PrintAreaToPPPrintArea(page_setup.content_area(), device_units_per_inch);
+ default_settings.paper_size =
+ PrintSizeToPPPrintSize(page_setup.physical_size(), device_units_per_inch);
+ default_settings.dpi = print_settings.dpi();
+
+ // The remainder of the attributes are hard-coded to the defaults as set
+ // elsewhere.
+ default_settings.orientation = PP_PRINTORIENTATION_NORMAL;
+ default_settings.grayscale = PP_FALSE;
+ default_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.
+ default_settings.format = PP_PRINTOUTPUTFORMAT_PDF;
+
+ BrowserThread::PostTask(kAccessorThread, FROM_HERE,
+ base::Bind(&PepperPrintSettingsInitializer::SetDefaultPrintSettings,
+ this, default_settings));
+#endif
+}
+
+void PepperPrintSettingsInitializer::SetDefaultPrintSettings(
+ const PP_PrintSettings_Dev& settings) {
+ DCHECK(BrowserThread::CurrentlyOn(kAccessorThread));
+ initialized_ = true;
+ default_print_settings_ = settings;
+}
+
+} // namespace content
diff --git a/content/browser/renderer_host/pepper_print_settings_initializer.h b/content/browser/renderer_host/pepper_print_settings_initializer.h
new file mode 100644
index 0000000..f4beec4
--- /dev/null
+++ b/content/browser/renderer_host/pepper_print_settings_initializer.h
@@ -0,0 +1,46 @@
+// 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_PRINT_SETTINGS_INITIALIZER_H_
+#define CONTENT_BROWSER_RENDERER_HOST_PEPPER_PRINT_SETTINGS_INITIALIZER_H_
+#pragma once
+
+#include "base/memory/ref_counted.h"
+#include "ppapi/c/dev/pp_print_settings_dev.h"
+
+namespace content {
+
+// This is a helper class for |PepperMessageFilter| which caches the default
+// print settings which are obtained from the system upon startup.
+// TODO(raymes): Ideally we would grab the default print settings every time
+// they are requested (because there is a chance they could change) however we
+// need to get the print settings on the UI thread and sync messages can't be
+// processed on the UI thread on Windows. We want this message to be synchronous
+// because it is called by Flash. There might be a better solution than this.
+class PepperPrintSettingsInitializer
+ : public base::RefCountedThreadSafe<PepperPrintSettingsInitializer> {
+ public:
+ PepperPrintSettingsInitializer();
+ void Initialize();
+ // Returns true if default settings are available and places them in
+ // |settings|.
+ bool GetDefaultPrintSettings(PP_PrintSettings_Dev* settings) const;
+
+ private:
+ friend class base::RefCountedThreadSafe<PepperPrintSettingsInitializer>;
+
+ virtual ~PepperPrintSettingsInitializer();
+
+ void DoInitialize();
+ void SetDefaultPrintSettings(const PP_PrintSettings_Dev& settings);
+
+ bool initialized_;
+ PP_PrintSettings_Dev default_print_settings_;
+
+ DISALLOW_COPY_AND_ASSIGN(PepperPrintSettingsInitializer);
+};
+
+} // namespace content
+
+#endif // CONTENT_BROWSER_RENDERER_HOST_PEPPER_PRINT_SETTINGS_INITIALIZER_H_
diff --git a/content/content_browser.gypi b/content/content_browser.gypi
index e5489bd..60617c6 100644
--- a/content/content_browser.gypi
+++ b/content/content_browser.gypi
@@ -11,6 +11,7 @@
'../net/net.gyp:http_server',
'../net/net.gyp:net',
'../ppapi/ppapi_internal.gyp:ppapi_proxy',
+ '../printing/printing.gyp:printing',
'../skia/skia.gyp:skia',
'../third_party/flac/flac.gyp:libflac',
'../third_party/speex/speex.gyp:libspeex',
@@ -567,6 +568,8 @@
'browser/renderer_host/pepper_lookup_request.h',
'browser/renderer_host/pepper_message_filter.cc',
'browser/renderer_host/pepper_message_filter.h',
+ 'browser/renderer_host/pepper_print_settings_initializer.cc',
+ 'browser/renderer_host/pepper_print_settings_initializer.h',
'browser/renderer_host/pepper_tcp_server_socket.cc',
'browser/renderer_host/pepper_tcp_server_socket.h',
'browser/renderer_host/pepper_tcp_socket.cc',