diff options
author | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-11 20:20:20 +0000 |
---|---|---|
committer | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-11 20:20:20 +0000 |
commit | dec1f2938f87048121900ff178913e922ea0732b (patch) | |
tree | 847b55e9a98358ed6843d56ecff4349df908dfa5 /chrome | |
parent | 9fe966f91160c4349f48a19f8d0807ea511eed05 (diff) | |
download | chromium_src-dec1f2938f87048121900ff178913e922ea0732b.zip chromium_src-dec1f2938f87048121900ff178913e922ea0732b.tar.gz chromium_src-dec1f2938f87048121900ff178913e922ea0732b.tar.bz2 |
Move printing code out of RenderView.
Review URL: http://codereview.chromium.org/6487017
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@74656 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/renderer/print_web_view_helper.cc | 98 | ||||
-rw-r--r-- | chrome/renderer/print_web_view_helper.h | 45 | ||||
-rw-r--r-- | chrome/renderer/render_view.cc | 65 | ||||
-rw-r--r-- | chrome/renderer/render_view.h | 25 | ||||
-rw-r--r-- | chrome/renderer/render_view_browsertest.cc | 6 |
5 files changed, 105 insertions, 134 deletions
diff --git a/chrome/renderer/print_web_view_helper.cc b/chrome/renderer/print_web_view_helper.cc index a178e22..c46a383 100644 --- a/chrome/renderer/print_web_view_helper.cc +++ b/chrome/renderer/print_web_view_helper.cc @@ -90,7 +90,7 @@ PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { PrintWebViewHelper::PrintWebViewHelper(RenderView* render_view) - : render_view_(render_view), + : RenderViewObserver(render_view), print_web_view_(NULL), user_cancelled_scripted_print_count_(0), is_preview_(false) {} @@ -109,6 +109,58 @@ void PrintWebViewHelper::PrintNode(WebNode* node, Print(node->document().frame(), node, script_initiated, is_preview); } +bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) { + bool handled = true; + IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message) + IPC_MESSAGE_HANDLER(ViewMsg_PrintPages, OnPrintPages) + IPC_MESSAGE_HANDLER(ViewMsg_PrintingDone, OnPrintingDone) + IPC_MESSAGE_HANDLER(ViewMsg_PrintPreview, OnPrintPreview) + IPC_MESSAGE_HANDLER(ViewMsg_PrintNodeUnderContextMenu, + OnPrintNodeUnderContextMenu) + IPC_MESSAGE_UNHANDLED(handled = false) + IPC_END_MESSAGE_MAP() + return handled; +} + +void PrintWebViewHelper::OnPrint(bool is_preview) { + DCHECK(render_view()->webview()); + if (!render_view()->webview()) + return; + + // If the user has selected text in the currently focused frame we print + // only that frame (this makes print selection work for multiple frames). + if (render_view()->webview()->focusedFrame()->hasSelection()) + PrintFrame(render_view()->webview()->focusedFrame(), false, is_preview); + else + PrintFrame(render_view()->webview()->mainFrame(), false, is_preview); +} + +void PrintWebViewHelper::OnPrintPages() { + OnPrint(false); +} + +void PrintWebViewHelper::OnPrintingDone(int document_cookie, bool success) { + // Ignoring document cookie here since only one print job can be outstanding + // per renderer and document_cookie is 0 when printing is successful. + DidFinishPrinting(success); +} + +void PrintWebViewHelper::OnPrintPreview() { + OnPrint(true); +} + +void PrintWebViewHelper::OnPrintNodeUnderContextMenu() { + if (render_view()->context_menu_node().isNull()) { + NOTREACHED(); + return; + } + + // Make a copy of the node, since we will do a sync call to the browser and + // during that time OnContextMenuClosed might reset context_menu_node_. + WebNode context_menu_node(render_view()->context_menu_node()); + PrintNode(&context_menu_node, false, false); +} + void PrintWebViewHelper::Print(WebKit::WebFrame* frame, WebNode* node, bool script_initiated, @@ -117,7 +169,7 @@ void PrintWebViewHelper::Print(WebKit::WebFrame* frame, const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 2 * 60; // 2 Minutes. // If still not finished with earlier print request simply ignore. - if (IsPrinting()) + if (print_web_view_) return; // Check if there is script repeatedly trying to print and ignore it if too @@ -203,9 +255,9 @@ void PrintWebViewHelper::DidFinishPrinting(bool success) { if (!success) { WebView* web_view = print_web_view_; if (!web_view) - web_view = render_view_->webview(); + web_view = render_view()->webview(); - render_view_->runModalAlertDialog( + render_view()->runModalAlertDialog( web_view->mainFrame(), l10n_util::GetStringUTF16(IDS_PRINT_SPOOL_FAILED_ERROR_TEXT)); } @@ -221,7 +273,7 @@ bool PrintWebViewHelper::CopyAndPrint(WebFrame* web_frame) { // Create a new WebView with the same settings as the current display one. // Except that we disable javascript (don't want any active content running // on the page). - WebPreferences prefs = render_view_->webkit_preferences(); + WebPreferences prefs = render_view()->webkit_preferences(); prefs.javascript_enabled = false; prefs.java_enabled = false; @@ -256,9 +308,8 @@ void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, frame->view()); int page_count = prep_frame_view.GetExpectedPageCount(); - Send(new ViewHostMsg_DidGetPrintedPagesCount(routing_id(), - printParams.document_cookie, - page_count)); + render_view()->Send(new ViewHostMsg_DidGetPrintedPagesCount( + render_view()->routing_id(), printParams.document_cookie, page_count)); if (!page_count) return; @@ -281,14 +332,6 @@ void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params, } #endif // OS_MACOSX || OS_WIN -bool PrintWebViewHelper::Send(IPC::Message* msg) { - return render_view_->Send(msg); -} - -int32 PrintWebViewHelper::routing_id() { - return render_view_->routing_id(); -} - void PrintWebViewHelper::didStopLoading() { DCHECK(print_pages_params_.get() != NULL); PrintPages(*print_pages_params_.get(), print_web_view_->mainFrame(), NULL); @@ -435,9 +478,8 @@ bool PrintWebViewHelper::GetDefaultPrintSettings( WebFrame* frame, WebNode* node, ViewMsg_Print_Params* params) { - IPC::SyncMessage* msg = - new ViewHostMsg_GetDefaultPrintSettings(routing_id(), params); - if (!Send(msg)) { + if (!render_view()->Send(new ViewHostMsg_GetDefaultPrintSettings( + render_view()->routing_id(), params))) { NOTREACHED(); return false; } @@ -445,7 +487,7 @@ bool PrintWebViewHelper::GetDefaultPrintSettings( // can safely assume there are no printer drivers configured. So we safely // terminate. if (params->IsEmpty()) { - render_view_->runModalAlertDialog( + render_view()->runModalAlertDialog( frame, l10n_util::GetStringUTF16(IDS_DEFAULT_PRINTER_NOT_FOUND_WARNING)); return false; @@ -468,21 +510,21 @@ bool PrintWebViewHelper::GetPrintSettingsFromUser(WebFrame* frame, // The routing id is sent across as it is needed to look up the // corresponding RenderViewHost instance to signal and reset the // pump messages event. - params.routing_id = routing_id(); + params.routing_id = render_view()->routing_id(); // host_window_ may be NULL at this point if the current window is a // popup and the print() command has been issued from the parent. The // receiver of this message has to deal with this. - params.host_window_id = render_view_->host_window(); + params.host_window_id = render_view()->host_window(); params.cookie = (*print_pages_params_).params.document_cookie; params.has_selection = frame->hasSelection(); params.expected_pages_count = expected_pages_count; params.use_overlays = use_browser_overlays; print_pages_params_.reset(); - IPC::SyncMessage* msg = - new ViewHostMsg_ScriptedPrint(routing_id(), params, &print_settings); + IPC::SyncMessage* msg = new ViewHostMsg_ScriptedPrint( + render_view()->routing_id(), params, &print_settings); msg->EnableMessagePumping(); - if (Send(msg)) { + if (render_view()->Send(msg)) { print_pages_params_.reset(new ViewMsg_PrintPages_Params(print_settings)); } else { // Send() failed. @@ -515,7 +557,8 @@ void PrintWebViewHelper::CreatePreviewDocument( ViewHostMsg_DidPreviewDocument_Params preview_params; // TODO(kmadhusu): Implement this function for linux. preview_params.document_cookie = params.params.document_cookie; - Send(new ViewHostMsg_PagesReadyForPreview(routing_id(), preview_params)); + render_view()->Send(new ViewHostMsg_PagesReadyForPreview( + render_view()->routing_id(), preview_params)); } #endif @@ -525,7 +568,8 @@ bool PrintWebViewHelper::CopyMetafileDataToSharedMem( base::SharedMemoryHandle* shared_mem_handle) { uint32 buf_size = metafile->GetDataSize(); base::SharedMemoryHandle mem_handle; - if (Send(new ViewHostMsg_AllocateSharedMemoryBuffer(buf_size, &mem_handle))) { + if (render_view()->Send( + new ViewHostMsg_AllocateSharedMemoryBuffer(buf_size, &mem_handle))) { if (base::SharedMemory::IsHandleValid(mem_handle)) { base::SharedMemory shared_buf(mem_handle, false); if (shared_buf.Map(buf_size)) { diff --git a/chrome/renderer/print_web_view_helper.h b/chrome/renderer/print_web_view_helper.h index 356ad1c..3f7e1b5 100644 --- a/chrome/renderer/print_web_view_helper.h +++ b/chrome/renderer/print_web_view_helper.h @@ -10,6 +10,7 @@ #include "base/scoped_ptr.h" #include "base/time.h" +#include "chrome/renderer/render_view_observer.h" #include "printing/native_metafile.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrameClient.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebViewClient.h" @@ -23,17 +24,12 @@ namespace gfx { class Size; } -namespace IPC { -class Message; -} - #if defined(USE_X11) namespace skia { class VectorCanvas; } #endif -class RenderView; struct ViewMsg_Print_Params; struct ViewMsg_PrintPage_Params; struct ViewMsg_PrintPages_Params; @@ -80,27 +76,23 @@ class PrepareFrameAndViewForPrint { // PrintWebViewHelper handles most of the printing grunt work for RenderView. // We plan on making print asynchronous and that will require copying the DOM // of the document and creating a new WebView with the contents. -class PrintWebViewHelper : public WebKit::WebViewClient, +class PrintWebViewHelper : public RenderViewObserver , + public WebKit::WebViewClient, public WebKit::WebFrameClient { public: explicit PrintWebViewHelper(RenderView* render_view); virtual ~PrintWebViewHelper(); + // Prints |frame|. void PrintFrame(WebKit::WebFrame* frame, bool script_initiated, bool is_preview); - void PrintNode(WebKit::WebNode* node, - bool script_initiated, - bool is_preview); - - // Is there a background print in progress? - bool IsPrinting() { - return print_web_view_ != NULL; - } - - // Notification when printing is done - signal teardown - void DidFinishPrinting(bool success); + // Message handlers. Public for testing. + void OnPrintingDone(int document_cookie, bool success); + void OnPrintPages(); + void OnPrintPreview(); + void OnPrintNodeUnderContextMenu(); protected: bool CopyAndPrint(WebKit::WebFrame* web_frame); @@ -124,11 +116,6 @@ class PrintWebViewHelper : public WebKit::WebViewClient, WebKit::WebFrame* frame, WebKit::WebNode* node); - // IPC::Message::Sender - bool Send(IPC::Message* msg); - - int32 routing_id(); - // WebKit::WebViewClient override: virtual void didStopLoading(); @@ -144,6 +131,19 @@ class PrintWebViewHelper : public WebKit::WebViewClient, double* margin_bottom_in_points, double* margin_left_in_points); + // RenderViewObserver implementation. + virtual bool OnMessageReceived(const IPC::Message& message); + + // Common method for OnPrintPages() and OnPrintPreview(). + void OnPrint(bool is_preview); + + void PrintNode(WebKit::WebNode* node, + bool script_initiated, + bool is_preview); + + // Notification when printing is done - signal teardown + void DidFinishPrinting(bool success); + void Print(WebKit::WebFrame* frame, WebKit::WebNode* node, bool script_initiated, @@ -195,7 +195,6 @@ class PrintWebViewHelper : public WebKit::WebViewClient, base::SharedMemoryHandle* shared_mem_handle); #endif - RenderView* render_view_; WebKit::WebView* print_web_view_; scoped_ptr<ViewMsg_PrintPages_Params> print_pages_params_; base::Time last_cancelled_script_print_; diff --git a/chrome/renderer/render_view.cc b/chrome/renderer/render_view.cc index e0ab75b..54673fd 100644 --- a/chrome/renderer/render_view.cc +++ b/chrome/renderer/render_view.cc @@ -577,6 +577,7 @@ RenderView::RenderView(RenderThreadBase* render_thread, geolocation_dispatcher_(NULL), speech_input_dispatcher_(NULL), device_orientation_dispatcher_(NULL), + print_helper_(NULL), accessibility_ack_pending_(false), pending_app_icon_requests_(0), session_storage_namespace_id_(session_storage_namespace_id) { @@ -649,6 +650,7 @@ RenderView::RenderView(RenderThreadBase* render_thread, page_click_tracker->AddListener(password_autofill_manager); page_click_tracker->AddListener(autofill_agent); new TranslateHelper(this); + print_helper_ = new PrintWebViewHelper(this); if (CommandLine::ForCurrentProcess()->HasSwitch( switches::kEnableClientSidePhishingDetection)) { @@ -972,11 +974,6 @@ bool RenderView::OnMessageReceived(const IPC::Message& message) { IPC_BEGIN_MESSAGE_MAP(RenderView, message) IPC_MESSAGE_HANDLER(ViewMsg_CaptureThumbnail, OnCaptureThumbnail) IPC_MESSAGE_HANDLER(ViewMsg_CaptureSnapshot, OnCaptureSnapshot) - IPC_MESSAGE_HANDLER(ViewMsg_PrintPages, OnPrintPages) - IPC_MESSAGE_HANDLER(ViewMsg_PrintingDone, OnPrintingDone) - IPC_MESSAGE_HANDLER(ViewMsg_PrintPreview, OnPrintPreview) - IPC_MESSAGE_HANDLER(ViewMsg_PrintNodeUnderContextMenu, - OnPrintNodeUnderContextMenu) IPC_MESSAGE_HANDLER(ViewMsg_Navigate, OnNavigate) IPC_MESSAGE_HANDLER(ViewMsg_Stop, OnStop) IPC_MESSAGE_HANDLER(ViewMsg_ReloadFrame, OnReloadFrame) @@ -1152,35 +1149,6 @@ void RenderView::OnCaptureSnapshot() { Send(new ViewHostMsg_Snapshot(routing_id_, snapshot)); } -void RenderView::OnPrintPages() { - OnPrint(false); -} - -void RenderView::OnPrintingDone(int document_cookie, bool success) { - // Ignoring document cookie here since only one print job can be outstanding - // per renderer and document_cookie is 0 when printing is successful. - DCHECK(print_helper_.get()); - if (print_helper_.get() != NULL) { - print_helper_->DidFinishPrinting(success); - } -} - -void RenderView::OnPrintPreview() { - OnPrint(true); -} - -void RenderView::OnPrintNodeUnderContextMenu() { - if (context_menu_node_.isNull()) { - NOTREACHED(); - return; - } - - // Make a copy of the node, since we will do a sync call to the browser and - // during that time OnContextMenuClosed might reset context_menu_node_. - WebNode context_menu_node(context_menu_node_); - GetPrintWebViewHelper()->PrintNode(&context_menu_node, false, false); -} - void RenderView::CapturePageInfo(int load_id, bool preliminary_capture) { if (load_id != page_id_) return; // this capture call is no longer relevant due to navigation @@ -2150,9 +2118,9 @@ void RenderView::printPage(WebFrame* frame) { DCHECK(frame); if (CommandLine::ForCurrentProcess()->HasSwitch( switches::kEnablePrintPreview)) { - Print(frame, true, true); + print_helper_->PrintFrame(frame, true, true); } else { - Print(frame, true, false); + print_helper_->PrintFrame(frame, true, false); } } @@ -5365,31 +5333,6 @@ void RenderView::postAccessibilityNotification( } } -void RenderView::OnPrint(bool is_preview) { - DCHECK(webview()); - if (webview()) { - // If the user has selected text in the currently focused frame we print - // only that frame (this makes print selection work for multiple frames). - if (webview()->focusedFrame()->hasSelection()) - Print(webview()->focusedFrame(), false, is_preview); - else - Print(webview()->mainFrame(), false, is_preview); - } -} - -void RenderView::Print(WebFrame* frame, - bool script_initiated, - bool is_preview) { - DCHECK(frame); - GetPrintWebViewHelper()->PrintFrame(frame, script_initiated, is_preview); -} - -PrintWebViewHelper* RenderView::GetPrintWebViewHelper() { - if (print_helper_.get() == NULL) - print_helper_.reset(new PrintWebViewHelper(this)); - return print_helper_.get(); -} - void RenderView::OnSetEditCommandsForNextKeyEvent( const EditCommands& edit_commands) { edit_commands_ = edit_commands; diff --git a/chrome/renderer/render_view.h b/chrome/renderer/render_view.h index d4cf067..56962c26 100644 --- a/chrome/renderer/render_view.h +++ b/chrome/renderer/render_view.h @@ -218,10 +218,6 @@ class RenderView : public RenderWidget, return view_type_; } - PrintWebViewHelper* print_helper() const { - return print_helper_.get(); - } - int page_id() const { return page_id_; } @@ -250,6 +246,8 @@ class RenderView : public RenderWidget, return search_box_; } + const WebKit::WebNode& context_menu_node() { return context_menu_node_; } + // Functions to add and remove observers for this object. void AddObserver(RenderViewObserver* observer); void RemoveObserver(RenderViewObserver* observer); @@ -893,10 +891,6 @@ class RenderView : public RenderWidget, #if defined(OS_MACOSX) void OnPluginImeCompositionCompleted(const string16& text, int plugin_id); #endif - void OnPrintingDone(int document_cookie, bool success); - void OnPrintPages(); - void OnPrintPreview(); - void OnPrintNodeUnderContextMenu(); void OnRedo(); void OnReloadFrame(); void OnReplace(const string16& text); @@ -1079,15 +1073,6 @@ class RenderView : public RenderWidget, const WebKit::WebURLError& error, bool replace); - // Common method for OnPrintPages() and OnPrintPreview(). - void OnPrint(bool is_preview); - - // Prints |frame|. - void Print(WebKit::WebFrame* frame, bool script_initiated, bool is_preview); - - // Returns the PrintWebViewHelper for this class, creating if necessary. - PrintWebViewHelper* GetPrintWebViewHelper(); - // Starts nav_state_sync_timer_ if it isn't already running. void StartNavStateSyncTimerIfNecessary(); @@ -1353,9 +1338,9 @@ class RenderView : public RenderWidget, // Device orientation dispatcher attached to this view; lazily initialized. DeviceOrientationDispatcher* device_orientation_dispatcher_; - // PrintWebViewHelper handles printing. Note that this object is constructed - // when printing for the first time but only destroyed with the RenderView. - scoped_ptr<PrintWebViewHelper> print_helper_; + // PrintWebViewHelper handles printing. Weak pointer since it implements + // RenderViewObserver interface. + PrintWebViewHelper* print_helper_; scoped_refptr<AudioMessageFilter> audio_message_filter_; diff --git a/chrome/renderer/render_view_browsertest.cc b/chrome/renderer/render_view_browsertest.cc index 6ab356f0..0b81bec 100644 --- a/chrome/renderer/render_view_browsertest.cc +++ b/chrome/renderer/render_view_browsertest.cc @@ -401,7 +401,7 @@ TEST_F(RenderViewTest, OnSetTextDirection) { TEST_F(RenderViewTest, OnPrintPages) { // Lets simulate a print pages with Hello world. LoadHTML("<body><p>Hello World!</p></body>"); - view_->OnPrintPages(); + view_->print_helper_->OnPrintPages(); VerifyPageCount(1); VerifyPagesPrinted(); @@ -443,7 +443,7 @@ TEST_F(RenderViewTest, PrintWithIframe) { view_->webview()->mainFrame()); // Initiate printing. - view_->OnPrintPages(); + view_->print_helper_->OnPrintPages(); // Verify output through MockPrinter. const MockPrinter* printer(render_thread_.printer()); @@ -514,7 +514,7 @@ TEST_F(RenderViewTest, MAYBE_PrintLayoutTest) { for (size_t i = 0; i < arraysize(kTestPages); ++i) { // Load an HTML page and print it. LoadHTML(kTestPages[i].page); - view_->OnPrintPages(); + view_->print_helper_->OnPrintPages(); // MockRenderThread::Send() just calls MockRenderThread::OnMsgReceived(). // So, all IPC messages sent in the above RenderView::OnPrintPages() call |