This commit is contained in:
Haojian Wu 2015-06-01 18:00:00 +08:00
parent 10da361db1
commit 2597ded985
5 changed files with 13 additions and 72 deletions

View file

@ -421,7 +421,7 @@ void Window::CapturePage(mate::Arguments* args) {
} }
void Window::Print(mate::Arguments* args) { void Window::Print(mate::Arguments* args) {
PrintSettings settings = { false, false };; PrintSettings settings = { false, false };
if (args->Length() == 1 && !args->GetNext(&settings)) { if (args->Length() == 1 && !args->GetNext(&settings)) {
args->ThrowError(); args->ThrowError();
return; return;

View file

@ -268,7 +268,7 @@ void NativeWindow::Print(bool silent, bool print_background) {
void NativeWindow::PrintToPDF(const mate::Dictionary& options, void NativeWindow::PrintToPDF(const mate::Dictionary& options,
const PrintToPDFCallback& callback) { const PrintToPDFCallback& callback) {
printing::PrintPreviewMessageHandler::FromWebContents(GetWebContents())-> printing::PrintPreviewMessageHandler::FromWebContents(GetWebContents())->
HandleGetPreview(options, callback); PrintToPDF(options, callback);
} }
void NativeWindow::ShowDefinitionForSelection() { void NativeWindow::ShowDefinitionForSelection() {

View file

@ -79,7 +79,8 @@ namespace printing {
PrintPreviewMessageHandler::PrintPreviewMessageHandler( PrintPreviewMessageHandler::PrintPreviewMessageHandler(
WebContents* web_contents) WebContents* web_contents)
: content::WebContentsObserver(web_contents) { : request_id_(0),
content::WebContentsObserver(web_contents) {
DCHECK(web_contents); DCHECK(web_contents);
} }
@ -92,8 +93,6 @@ void PrintPreviewMessageHandler::OnDidGetPreviewPageCount(
NOTREACHED(); NOTREACHED();
return; return;
} }
LOG(ERROR) << "OnDidGetPreviewPageCount: " << params.page_count;
} }
void PrintPreviewMessageHandler::OnDidPreviewPage( void PrintPreviewMessageHandler::OnDidPreviewPage(
@ -101,7 +100,6 @@ void PrintPreviewMessageHandler::OnDidPreviewPage(
int page_number = params.page_number; int page_number = params.page_number;
if (page_number < FIRST_PAGE_INDEX || !params.data_size) if (page_number < FIRST_PAGE_INDEX || !params.data_size)
return; return;
LOG(ERROR) << "OnDidPreviewPage: " << params.data_size;
} }
void PrintPreviewMessageHandler::OnMetafileReadyForPrinting( void PrintPreviewMessageHandler::OnMetafileReadyForPrinting(
@ -122,7 +120,6 @@ void PrintPreviewMessageHandler::OnMetafileReadyForPrinting(
if (!data || !data->size()) if (!data || !data->size())
return; return;
LOG(ERROR) << params.preview_request_id;
atom::NativeWindow* window = atom::NativeWindow::FromWebContents( atom::NativeWindow* window = atom::NativeWindow::FromWebContents(
web_contents()); web_contents());
base::FilePath save_path; base::FilePath save_path;
@ -147,39 +144,6 @@ void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie,
RunPrintToPDFCallback(request_id, FAIL_PREVIEW); RunPrintToPDFCallback(request_id, FAIL_PREVIEW);
} }
//void PrintPreviewMessageHandler::OnDidGetDefaultPageLayout(
//const PageSizeMargins& page_layout_in_points,
//const gfx::Rect& printable_area_in_points,
//bool has_custom_page_size_style) {
////PrintPreviewUI* print_preview_ui = GetPrintPreviewUI();
////if (!print_preview_ui)
////return;
////print_preview_ui->OnDidGetDefaultPageLayout(page_layout_in_points,
////printable_area_in_points,
////has_custom_page_size_style);
//}
//void PrintPreviewMessageHandler::OnPrintPreviewCancelled(int document_cookie) {
//// Always need to stop the worker.
//StopWorker(document_cookie);
//}
//void PrintPreviewMessageHandler::OnInvalidPrinterSettings(int document_cookie) {
//StopWorker(document_cookie);
////PrintPreviewUI* print_preview_ui = GetPrintPreviewUI();
////if (!print_preview_ui)
////return;
////print_preview_ui->OnInvalidPrinterSettings();
//}
//void PrintPreviewMessageHandler::OnSetOptionsFromDocument(
//const PrintHostMsg_SetOptionsFromDocument_Params& params) {
////PrintPreviewUI* print_preview_ui = GetPrintPreviewUI();
////if (!print_preview_ui)
////return;
////print_preview_ui->OnSetOptionsFromDocument(params);
//}
bool PrintPreviewMessageHandler::OnMessageReceived( bool PrintPreviewMessageHandler::OnMessageReceived(
const IPC::Message& message) { const IPC::Message& message) {
bool handled = true; bool handled = true;
@ -192,24 +156,14 @@ bool PrintPreviewMessageHandler::OnMessageReceived(
OnMetafileReadyForPrinting) OnMetafileReadyForPrinting)
IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewFailed, IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewFailed,
OnPrintPreviewFailed) OnPrintPreviewFailed)
//IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetDefaultPageLayout,
//OnDidGetDefaultPageLayout)
//IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewCancelled,
//OnPrintPreviewCancelled)
//IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewInvalidPrinterSettings,
//OnInvalidPrinterSettings)
//IPC_MESSAGE_HANDLER(PrintHostMsg_SetOptionsFromDocument,
//OnSetOptionsFromDocument)
IPC_MESSAGE_UNHANDLED(handled = false) IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP() IPC_END_MESSAGE_MAP()
return handled; return handled;
} }
void PrintPreviewMessageHandler::HandleGetPreview( void PrintPreviewMessageHandler::PrintToPDF(
const mate::Dictionary& options, const mate::Dictionary& options,
const atom::NativeWindow::PrintToPDFCallback& callback) { const atom::NativeWindow::PrintToPDFCallback& callback) {
static int request_id = 0;
request_id++;
// A simulated Chromium print preivew setting. // A simulated Chromium print preivew setting.
const std::string setting_json_str = "{ \ const std::string setting_json_str = "{ \
\"pageRage\":[], \ \"pageRage\":[], \
@ -244,8 +198,9 @@ void PrintPreviewMessageHandler::HandleGetPreview(
scoped_ptr<base::DictionaryValue> settings( scoped_ptr<base::DictionaryValue> settings(
static_cast<base::DictionaryValue*>( static_cast<base::DictionaryValue*>(
base::JSONReader::Read(setting_json_str))); base::JSONReader::Read(setting_json_str)));
settings->SetInteger(printing::kPreviewRequestID, request_id); settings->SetInteger(printing::kPreviewRequestID, request_id_);
print_to_pdf_callback_map_[request_id] = callback; print_to_pdf_callback_map_[request_id_] = callback;
++request_id_;
// Default Print PDF settings: // Default Print PDF settings:
@ -267,7 +222,6 @@ void PrintPreviewMessageHandler::HandleGetPreview(
settings->SetBoolean(printing::kSettingShouldPrintSelectionOnly, settings->SetBoolean(printing::kSettingShouldPrintSelectionOnly,
print_selection_only); print_selection_only);
settings->SetBoolean(printing::kSettingLandscape, is_landscape); settings->SetBoolean(printing::kSettingLandscape, is_landscape);
LOG(ERROR) << "Print preview request start";
content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); content::RenderViewHost* rvh = web_contents()->GetRenderViewHost();
rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), *settings)); rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), *settings));
} }

View file

@ -49,10 +49,8 @@ class PrintPreviewMessageHandler
// content::WebContentsObserver implementation. // content::WebContentsObserver implementation.
bool OnMessageReceived(const IPC::Message& message) override; bool OnMessageReceived(const IPC::Message& message) override;
// Asks the initiator renderer to generate a preview. First element of |args| void PrintToPDF(const mate::Dictionary& options,
// is a job settings JSON string. const atom::NativeWindow::PrintToPDFCallback& callback);
void HandleGetPreview(const mate::Dictionary& options,
const atom::NativeWindow::PrintToPDFCallback& callback);
private: private:
typedef std::map<int, atom::NativeWindow::PrintToPDFCallback> PrintToPDFCallbackMap; typedef std::map<int, atom::NativeWindow::PrintToPDFCallback> PrintToPDFCallbackMap;
@ -60,27 +58,19 @@ class PrintPreviewMessageHandler
explicit PrintPreviewMessageHandler(content::WebContents* web_contents); explicit PrintPreviewMessageHandler(content::WebContents* web_contents);
friend class content::WebContentsUserData<PrintPreviewMessageHandler>; friend class content::WebContentsUserData<PrintPreviewMessageHandler>;
// Message handlers. // Message handlers.
//void OnRequestPrintPreview(
//const PrintHostMsg_RequestPrintPreview_Params& params);
//void OnDidGetDefaultPageLayout(
//const printing::PageSizeMargins& page_layout_in_points,
//const gfx::Rect& printable_area_in_points,
//bool has_custom_page_size_style);
void OnDidGetPreviewPageCount( void OnDidGetPreviewPageCount(
const PrintHostMsg_DidGetPreviewPageCount_Params& params); const PrintHostMsg_DidGetPreviewPageCount_Params& params);
void OnDidPreviewPage(const PrintHostMsg_DidPreviewPage_Params& params); void OnDidPreviewPage(const PrintHostMsg_DidPreviewPage_Params& params);
void OnMetafileReadyForPrinting( void OnMetafileReadyForPrinting(
const PrintHostMsg_DidPreviewDocument_Params& params); const PrintHostMsg_DidPreviewDocument_Params& params);
void OnPrintPreviewFailed(int document_cookie, int request_id); void OnPrintPreviewFailed(int document_cookie, int request_id);
//void OnPrintPreviewCancelled(int document_cookie);
//void OnInvalidPrinterSettings(int document_cookie);
//void OnSetOptionsFromDocument(
//const PrintHostMsg_SetOptionsFromDocument_Params& params);
void RunPrintToPDFCallback(int request_id, PrintPDFResult result); void RunPrintToPDFCallback(int request_id, PrintPDFResult result);
// PrintToPDF request id counter.
int request_id_;
PrintToPDFCallbackMap print_to_pdf_callback_map_; PrintToPDFCallbackMap print_to_pdf_callback_map_;
DISALLOW_COPY_AND_ASSIGN(PrintPreviewMessageHandler); DISALLOW_COPY_AND_ASSIGN(PrintPreviewMessageHandler);

View file

@ -717,19 +717,16 @@ void PrintWebViewHelper::OnPrintPreview(const base::DictionaryValue& settings) {
blink::WebLocalFrame* frame; blink::WebLocalFrame* frame;
if (GetPrintFrame(&frame)) { if (GetPrintFrame(&frame)) {
print_preview_context_.InitWithFrame(frame); print_preview_context_.InitWithFrame(frame);
LOG(ERROR) << "OnPrintPreview1";
if (!print_preview_context_.source_frame()) { if (!print_preview_context_.source_frame()) {
DidFinishPrinting(FAIL_PREVIEW); DidFinishPrinting(FAIL_PREVIEW);
return; return;
} }
LOG(ERROR) << "OnPrintPreview2";
if (!UpdatePrintSettings(print_preview_context_.source_frame(), if (!UpdatePrintSettings(print_preview_context_.source_frame(),
print_preview_context_.source_node(), settings)) { print_preview_context_.source_node(), settings)) {
DidFinishPrinting(FAIL_PREVIEW); DidFinishPrinting(FAIL_PREVIEW);
return; return;
} }
LOG(ERROR) << "OnPrintPreview3";
is_print_ready_metafile_sent_ = false; is_print_ready_metafile_sent_ = false;
PrepareFrameForPreviewDocument(); PrepareFrameForPreviewDocument();
} }