Home | History | Annotate | Download | only in print_preview
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_UI_H_
      6 #define CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_UI_H_
      7 
      8 #include <string>
      9 
     10 #include "base/callback_forward.h"
     11 #include "base/gtest_prod_util.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/time/time.h"
     14 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h"
     15 
     16 class PrintPreviewDataService;
     17 class PrintPreviewHandler;
     18 struct PrintHostMsg_DidGetPreviewPageCount_Params;
     19 struct PrintHostMsg_RequestPrintPreview_Params;
     20 struct PrintHostMsg_SetOptionsFromDocument_Params;
     21 
     22 namespace base {
     23 class FilePath;
     24 class RefCountedBytes;
     25 }
     26 
     27 namespace gfx {
     28 class Rect;
     29 }
     30 
     31 namespace printing {
     32 struct PageSizeMargins;
     33 }
     34 
     35 class PrintPreviewUI : public ConstrainedWebDialogUI {
     36  public:
     37   explicit PrintPreviewUI(content::WebUI* web_ui);
     38   virtual ~PrintPreviewUI();
     39 
     40   // Gets the print preview |data|. |index| is zero-based, and can be
     41   // |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to get the entire preview
     42   // document.
     43   void GetPrintPreviewDataForIndex(int index,
     44                                    scoped_refptr<base::RefCountedBytes>* data);
     45 
     46   // Sets the print preview |data|. |index| is zero-based, and can be
     47   // |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to set the entire preview
     48   // document.
     49   void SetPrintPreviewDataForIndex(int index,
     50                                    const base::RefCountedBytes* data);
     51 
     52   // Clear the existing print preview data.
     53   void ClearAllPreviewData();
     54 
     55   // Returns the available draft page count.
     56   int GetAvailableDraftPageCount();
     57 
     58   // Setters
     59   void SetInitiatorTitle(const base::string16& initiator_title);
     60 
     61   base::string16 initiator_title() { return initiator_title_; }
     62 
     63   bool source_is_modifiable() { return source_is_modifiable_; }
     64 
     65   bool source_has_selection() { return source_has_selection_; }
     66 
     67   bool print_selection_only() { return print_selection_only_; }
     68 
     69   // Set initial settings for PrintPreviewUI.
     70   static void SetInitialParams(
     71       content::WebContents* print_preview_dialog,
     72       const PrintHostMsg_RequestPrintPreview_Params& params);
     73 
     74   // Determines whether to cancel a print preview request based on
     75   // |preview_ui_id| and |request_id|.
     76   // Can be called from any thread.
     77   static void GetCurrentPrintPreviewStatus(int32 preview_ui_id,
     78                                            int request_id,
     79                                            bool* cancel);
     80 
     81   // Returns an id to uniquely identify this PrintPreviewUI.
     82   int32 GetIDForPrintPreviewUI() const;
     83 
     84   // Notifies the Web UI of a print preview request with |request_id|.
     85   void OnPrintPreviewRequest(int request_id);
     86 
     87 #if !defined(DISABLE_BASIC_PRINTING)
     88   // Notifies the Web UI to show the system dialog.
     89   void OnShowSystemDialog();
     90 #endif  // !DISABLE_BASIC_PRINTING
     91 
     92   // Notifies the Web UI about the page count of the request preview.
     93   void OnDidGetPreviewPageCount(
     94       const PrintHostMsg_DidGetPreviewPageCount_Params& params);
     95 
     96   // Notifies the Web UI of the default page layout according to the currently
     97   // selected printer and page size.
     98   void OnDidGetDefaultPageLayout(const printing::PageSizeMargins& page_layout,
     99                                  const gfx::Rect& printable_area,
    100                                  bool has_custom_page_size_style);
    101 
    102   // Notifies the Web UI that the 0-based page |page_number| has been rendered.
    103   // |preview_request_id| indicates wich request resulted in this response.
    104   void OnDidPreviewPage(int page_number, int preview_request_id);
    105 
    106   // Notifies the Web UI renderer that preview data is available.
    107   // |expected_pages_count| specifies the total number of pages.
    108   // |preview_request_id| indicates which request resulted in this response.
    109   void OnPreviewDataIsAvailable(int expected_pages_count,
    110                                 int preview_request_id);
    111 
    112   // Notifies the Web UI that preview dialog has been destroyed. This is the
    113   // last chance to communicate with the initiator before the association is
    114   // erased.
    115   void OnPrintPreviewDialogDestroyed();
    116 
    117   // Notifies the Web UI that the print preview failed to render.
    118   void OnPrintPreviewFailed();
    119 
    120   // Notified the Web UI that this print preview dialog's RenderProcess has been
    121   // closed, which may occur for several reasons, e.g. tab closure or crash.
    122   void OnPrintPreviewDialogClosed();
    123 
    124   // Notifies the Web UI that initiator is closed, so we can disable all the
    125   // controls that need the initiator for generating the preview data.
    126   void OnInitiatorClosed();
    127 
    128   // Notifies the Web UI renderer that file selection has been cancelled.
    129   void OnFileSelectionCancelled();
    130 
    131   // Notifies the Web UI that the printer is unavailable or its settings are
    132   // invalid.
    133   void OnInvalidPrinterSettings();
    134 
    135   // Notifies the Web UI to cancel the pending preview request.
    136   void OnCancelPendingPreviewRequest();
    137 
    138   // Hides the print preview dialog.
    139   void OnHidePreviewDialog();
    140 
    141   // Closes the print preview dialog.
    142   void OnClosePrintPreviewDialog();
    143 
    144   // Reload the printers list.
    145   void OnReloadPrintersList();
    146 
    147   // Notifies the WebUI to set print preset options from source PDF.
    148   void OnSetOptionsFromDocument(
    149       const PrintHostMsg_SetOptionsFromDocument_Params& params);
    150 
    151   // Allows tests to wait until the print preview dialog is loaded. Optionally
    152   // also instructs the dialog to auto-cancel, which is used for testing only.
    153   class TestingDelegate {
    154    public:
    155     virtual bool IsAutoCancelEnabled() = 0;
    156     virtual void DidGetPreviewPageCount(int page_count) = 0;
    157     virtual void DidRenderPreviewPage(content::WebContents* preview_dialog) = 0;
    158   };
    159 
    160   static void SetDelegateForTesting(TestingDelegate* delegate);
    161 
    162   // Allows for tests to set a file path to print a PDF to. This also initiates
    163   // the printing without having to click a button on the print preview dialog.
    164   void SetSelectedFileForTesting(const base::FilePath& path);
    165 
    166   // Passes |closure| to PrintPreviewHandler::SetPdfSavedClosureForTesting().
    167   void SetPdfSavedClosureForTesting(const base::Closure& closure);
    168 
    169  private:
    170   friend class PrintPreviewHandlerTest;
    171   FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest, StickyMarginsCustom);
    172   FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest, StickyMarginsDefault);
    173   FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest,
    174                            StickyMarginsCustomThenDefault);
    175   FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest,
    176                            GetLastUsedMarginSettingsCustom);
    177   FRIEND_TEST_ALL_PREFIXES(PrintPreviewHandlerTest,
    178                            GetLastUsedMarginSettingsDefault);
    179   FRIEND_TEST_ALL_PREFIXES(PrintPreviewDialogControllerUnitTest,
    180                            TitleAfterReload);
    181 
    182   // Returns the Singleton instance of the PrintPreviewDataService.
    183   PrintPreviewDataService* print_preview_data_service();
    184 
    185   base::TimeTicks initial_preview_start_time_;
    186 
    187   // The unique ID for this class instance. Stored here to avoid calling
    188   // GetIDForPrintPreviewUI() everywhere.
    189   const int32 id_;
    190 
    191   // Weak pointer to the WebUI handler.
    192   PrintPreviewHandler* handler_;
    193 
    194   // Indicates whether the source document can be modified.
    195   bool source_is_modifiable_;
    196 
    197   // Indicates whether the source document has selection.
    198   bool source_has_selection_;
    199 
    200   // Indicates whether only the selection should be printed.
    201   bool print_selection_only_;
    202 
    203   // Store the initiator title, used for populating the print preview dialog
    204   // title.
    205   base::string16 initiator_title_;
    206 
    207   // Keeps track of whether OnClosePrintPreviewDialog() has been called or not.
    208   bool dialog_closed_;
    209 
    210   DISALLOW_COPY_AND_ASSIGN(PrintPreviewUI);
    211 };
    212 
    213 #endif  // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_PRINT_PREVIEW_UI_H_
    214