Revert "Move the code of getting printing settings to another class."

This reverts commit 57d06c5241.

Conflicts:
	atom/browser/printing/printing_config_service.cc
This commit is contained in:
Cheng Zhao 2014-08-22 15:03:39 +08:00
parent e43b3309af
commit d20ec6952a
6 changed files with 134 additions and 225 deletions

View file

@ -9,8 +9,8 @@
#include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/atom_resource_dispatcher_host_delegate.h"
#include "atom/browser/native_window.h"
#include "atom/browser/printing/printing_message_filter.h"
#include "atom/browser/window_list.h"
#include "chrome/browser/printing/printing_message_filter.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/resource_dispatcher_host.h"

View file

@ -1,119 +0,0 @@
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/printing/printing_config_service.h"
#include "base/memory/singleton.h"
#include "chrome/browser/printing/printer_query.h"
#include "chrome/browser/printing/printing_ui_web_contents_observer.h"
#include "chrome/common/print_messages.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
using content::BrowserThread;
namespace atom {
namespace {
void RenderParamsFromPrintSettings(const printing::PrintSettings& settings,
PrintMsg_Print_Params* params) {
params->page_size = settings.page_setup_device_units().physical_size();
params->content_size.SetSize(
settings.page_setup_device_units().content_area().width(),
settings.page_setup_device_units().content_area().height());
params->printable_area.SetRect(
settings.page_setup_device_units().printable_area().x(),
settings.page_setup_device_units().printable_area().y(),
settings.page_setup_device_units().printable_area().width(),
settings.page_setup_device_units().printable_area().height());
params->margin_top = settings.page_setup_device_units().content_area().y();
params->margin_left = settings.page_setup_device_units().content_area().x();
params->dpi = settings.dpi();
// Currently hardcoded at 1.25. See PrintSettings' constructor.
params->min_shrink = settings.min_shrink();
// Currently hardcoded at 2.0. See PrintSettings' constructor.
params->max_shrink = settings.max_shrink();
// Currently hardcoded at 72dpi. See PrintSettings' constructor.
params->desired_dpi = settings.desired_dpi();
// Always use an invalid cookie.
params->document_cookie = 0;
params->selection_only = settings.selection_only();
params->supports_alpha_blend = settings.supports_alpha_blend();
params->should_print_backgrounds = settings.should_print_backgrounds();
params->title = settings.title();
params->url = settings.url();
}
} // namespace
// static
PrintingConfigService* PrintingConfigService::GetInstance() {
return Singleton<PrintingConfigService>::get();
}
PrintingConfigService::PrintingConfigService()
: weak_factory_(this) {
}
PrintingConfigService::~PrintingConfigService() {
}
void PrintingConfigService::GetPrintSettings(
content::WebContents* wc,
scoped_refptr<printing::PrinterQuery> printer_query,
bool ask_user_for_settings,
const PrintHostMsg_ScriptedPrint_Params& params,
PrintSettingsCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (wc) {
scoped_ptr<PrintingUIWebContentsObserver> wc_observer(
new PrintingUIWebContentsObserver(wc));
printing::PrinterQuery::GetSettingsAskParam ask_param =
ask_user_for_settings ? printing::PrinterQuery::ASK_USER :
printing::PrinterQuery::DEFAULTS;
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&printing::PrinterQuery::GetSettings, printer_query,
ask_param, base::Passed(&wc_observer),
params.expected_pages_count, params.has_selection,
params.margin_type,
base::Bind(&PrintingConfigService::OnGetSettings,
weak_factory_.GetWeakPtr(), printer_query,
callback)));
} else {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&PrintingConfigService::OnGetSettingsFailed,
weak_factory_.GetWeakPtr(), printer_query, callback));
}
}
void PrintingConfigService::OnGetSettings(
scoped_refptr<printing::PrinterQuery> printer_query,
PrintSettingsCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
PrintMsg_PrintPages_Params params;
if (printer_query->last_status() != printing::PrintingContext::OK ||
!printer_query->settings().dpi()) {
params.Reset();
} else {
RenderParamsFromPrintSettings(printer_query->settings(), &params.params);
params.params.document_cookie = printer_query->cookie();
params.pages =
printing::PageRange::GetPages(printer_query->settings().ranges());
}
callback.Run(params);
}
void PrintingConfigService::OnGetSettingsFailed(
scoped_refptr<printing::PrinterQuery> printer_query,
PrintSettingsCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
printer_query->GetSettingsDone(printing::PrintSettings(),
printing::PrintingContext::FAILED);
callback.Run(PrintMsg_PrintPages_Params());
}
} // namespace atom

View file

@ -1,60 +0,0 @@
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_PRINTING_PRINTING_CONFIG_SERVICE_H_
#define ATOM_BROWSER_PRINTING_PRINTING_CONFIG_SERVICE_H_
#include "base/memory/weak_ptr.h"
template <typename T> struct DefaultSingletonTraits;
struct PrintHostMsg_ScriptedPrint_Params;
struct PrintMsg_PrintPages_Params;
namespace content {
class WebContents;
}
namespace printing {
class PrinterQuery;
}
namespace atom {
// This interface manages the config of printing.
class PrintingConfigService {
public:
static PrintingConfigService* GetInstance();
typedef base::Callback<void(const PrintMsg_PrintPages_Params&)>
PrintSettingsCallback;
// Gets printing settings for query on UI thread, and then call the |callback|
// on the IO thread with the result.
void GetPrintSettings(content::WebContents* wc,
scoped_refptr<printing::PrinterQuery> printer_query,
bool ask_user_for_settings,
const PrintHostMsg_ScriptedPrint_Params& params,
PrintSettingsCallback callback);
private:
PrintingConfigService();
virtual ~PrintingConfigService();
// Called by content::PrinterQuery::GetSettings in GetPrintSettings.
void OnGetSettings(scoped_refptr<printing::PrinterQuery> printer_query,
PrintSettingsCallback callback);
void OnGetSettingsFailed(scoped_refptr<printing::PrinterQuery> printer_query,
PrintSettingsCallback callback);
private:
friend struct DefaultSingletonTraits<PrintingConfigService>;
base::WeakPtrFactory<PrintingConfigService> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(PrintingConfigService);
};
} // namespace atom
#endif // ATOM_BROWSER_PRINTING_PRINTING_CONFIG_SERVICE_H_

View file

@ -1,154 +0,0 @@
// 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 "atom/browser/printing/printing_message_filter.h"
#include "atom/browser/printing/printing_config_service.h"
#include "base/bind.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/printing/print_job_manager.h"
#include "chrome/browser/printing/printer_query.h"
#include "chrome/common/print_messages.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
using content::BrowserThread;
namespace atom {
PrintingMessageFilter::PrintingMessageFilter(int render_process_id)
: BrowserMessageFilter(PrintMsgStart),
render_process_id_(render_process_id),
queue_(g_browser_process->print_job_manager()->queue()) {
DCHECK(queue_);
}
PrintingMessageFilter::~PrintingMessageFilter() {
}
bool PrintingMessageFilter::OnMessageReceived(const IPC::Message& message,
bool* message_was_ok) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP_EX(PrintingMessageFilter, message, *message_was_ok)
#if defined(OS_WIN)
IPC_MESSAGE_HANDLER(PrintHostMsg_DuplicateSection, OnDuplicateSection)
#endif
IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_GetDefaultPrintSettings,
OnGetDefaultPrintSettings)
IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_ScriptedPrint, OnScriptedPrint)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
#if defined(OS_WIN)
void PrintingMessageFilter::OnDuplicateSection(
base::SharedMemoryHandle renderer_handle,
base::SharedMemoryHandle* browser_handle) {
// Duplicate the handle in this process right now so the memory is kept alive
// (even if it is not mapped)
base::SharedMemory shared_buf(renderer_handle, true, PeerHandle());
shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), browser_handle);
}
#endif
content::WebContents* PrintingMessageFilter::GetWebContentsForRenderView(
int render_view_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
content::RenderViewHost* view = content::RenderViewHost::FromID(
render_process_id_, render_view_id);
return view ? content::WebContents::FromRenderViewHost(view) : NULL;
}
void PrintingMessageFilter::GetPrintSettingsForRenderView(
int render_view_id,
bool ask_user_for_settings,
PrintHostMsg_ScriptedPrint_Params params,
const base::Callback<void(const PrintMsg_PrintPages_Params&)>callback,
scoped_refptr<printing::PrinterQuery> printer_query) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
content::WebContents* wc = GetWebContentsForRenderView(render_view_id);
PrintingConfigService::GetInstance()->GetPrintSettings(
wc, printer_query, ask_user_for_settings, params, callback);
}
void PrintingMessageFilter::OnGetDefaultPrintSettings(IPC::Message* reply_msg) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
scoped_refptr<printing::PrinterQuery> printer_query;
if (false) {
// Reply with NULL query.
OnGetDefaultPrintSettingsReply(printer_query, reply_msg,
PrintMsg_PrintPages_Params());
return;
}
printer_query = queue_->PopPrinterQuery(0);
if (!printer_query)
printer_query = queue_->CreatePrinterQuery();
// Loads default settings. This is asynchronous, only the IPC message sender
// will hang until the settings are retrieved.
PrintHostMsg_ScriptedPrint_Params params;
params.expected_pages_count = 0;
params.has_selection = false;
params.margin_type = printing::DEFAULT_MARGINS;
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&PrintingMessageFilter::GetPrintSettingsForRenderView, this,
reply_msg->routing_id(), false, params,
base::Bind(&PrintingMessageFilter::OnGetDefaultPrintSettingsReply,
this, printer_query, reply_msg),
printer_query));
}
void PrintingMessageFilter::OnGetDefaultPrintSettingsReply(
scoped_refptr<printing::PrinterQuery> printer_query,
IPC::Message* reply_msg,
const PrintMsg_PrintPages_Params& params) {
PrintHostMsg_GetDefaultPrintSettings::WriteReplyParams(
reply_msg, params.params);
Send(reply_msg);
// If printing was enabled.
if (printer_query.get()) {
// If user hasn't cancelled.
if (printer_query->cookie() && printer_query->settings().dpi()) {
queue_->QueuePrinterQuery(printer_query.get());
} else {
printer_query->StopWorker();
}
}
}
void PrintingMessageFilter::OnScriptedPrint(
const PrintHostMsg_ScriptedPrint_Params& params,
IPC::Message* reply_msg) {
scoped_refptr<printing::PrinterQuery> printer_query =
queue_->PopPrinterQuery(params.cookie);
if (!printer_query)
printer_query = queue_->CreatePrinterQuery();
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&PrintingMessageFilter::GetPrintSettingsForRenderView, this,
reply_msg->routing_id(), true, params,
base::Bind(&PrintingMessageFilter::OnScriptedPrintReply, this,
printer_query, reply_msg),
printer_query));
}
void PrintingMessageFilter::OnScriptedPrintReply(
scoped_refptr<printing::PrinterQuery> printer_query,
IPC::Message* reply_msg,
const PrintMsg_PrintPages_Params& params) {
PrintHostMsg_ScriptedPrint::WriteReplyParams(reply_msg, params);
Send(reply_msg);
if (params.params.dpi && params.params.document_cookie) {
queue_->QueuePrinterQuery(printer_query.get());
} else {
printer_query->StopWorker();
}
}
} // namespace atom

View file

@ -1,87 +0,0 @@
// 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 ATOM_BROWSER_PRINTING_PRINTING_MESSAGE_FILTER_H_
#define ATOM_BROWSER_PRINTING_PRINTING_MESSAGE_FILTER_H_
#include "base/compiler_specific.h"
#include "content/public/browser/browser_message_filter.h"
#if defined(OS_WIN)
#include "base/memory/shared_memory.h"
#endif
struct PrintMsg_PrintPages_Params;
struct PrintHostMsg_ScriptedPrint_Params;
namespace content {
class WebContents;
}
namespace printing {
class PrinterQuery;
class PrintQueriesQueue;
}
namespace atom {
// This class filters out incoming printing related IPC messages for the
// renderer process on the IPC thread.
class PrintingMessageFilter : public content::BrowserMessageFilter {
public:
explicit PrintingMessageFilter(int render_process_id);
// content::BrowserMessageFilter methods.
virtual bool OnMessageReceived(const IPC::Message& message,
bool* message_was_ok) OVERRIDE;
private:
virtual ~PrintingMessageFilter();
#if defined(OS_WIN)
// Used to pass resulting EMF from renderer to browser in printing.
void OnDuplicateSection(base::SharedMemoryHandle renderer_handle,
base::SharedMemoryHandle* browser_handle);
#endif
// Given a render_view_id get the corresponding WebContents.
// Must be called on the UI thread.
content::WebContents* GetWebContentsForRenderView(int render_view_id);
// Retrieve print settings. Uses |render_view_id| to get a parent
// for any UI created if needed.
void GetPrintSettingsForRenderView(
int render_view_id,
bool ask_user_for_settings,
PrintHostMsg_ScriptedPrint_Params params,
const base::Callback<void(const PrintMsg_PrintPages_Params&)>callback,
scoped_refptr<printing::PrinterQuery> printer_query);
// Get the default print setting.
void OnGetDefaultPrintSettings(IPC::Message* reply_msg);
void OnGetDefaultPrintSettingsReply(
scoped_refptr<printing::PrinterQuery> printer_query,
IPC::Message* reply_msg,
const PrintMsg_PrintPages_Params& params);
// The renderer host have to show to the user the print dialog and returns
// the selected print settings. The task is handled by the print worker
// thread and the UI thread. The reply occurs on the IO thread.
void OnScriptedPrint(const PrintHostMsg_ScriptedPrint_Params& params,
IPC::Message* reply_msg);
void OnScriptedPrintReply(
scoped_refptr<printing::PrinterQuery> printer_query,
IPC::Message* reply_msg,
const PrintMsg_PrintPages_Params& params);
const int render_process_id_;
scoped_refptr<printing::PrintQueriesQueue> queue_;
DISALLOW_COPY_AND_ASSIGN(PrintingMessageFilter);
};
} // namespace atom
#endif // ATOM_BROWSER_PRINTING_PRINTING_MESSAGE_FILTER_H_