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_PRINTING_PRINT_PREVIEW_DIALOG_CONTROLLER_H_ 6 #define CHROME_BROWSER_PRINTING_PRINT_PREVIEW_DIALOG_CONTROLLER_H_ 7 8 #include <map> 9 10 #include "base/memory/ref_counted.h" 11 #include "chrome/browser/sessions/session_id.h" 12 #include "content/public/browser/notification_observer.h" 13 #include "content/public/browser/notification_registrar.h" 14 15 class GURL; 16 17 namespace content { 18 struct LoadCommittedDetails; 19 class RenderProcessHost; 20 class WebContents; 21 } 22 23 namespace printing { 24 25 // For print preview, the WebContents that initiates the printing operation is 26 // the initiator, and the constrained dialog that shows the print preview is the 27 // print preview dialog. 28 // This class manages print preview dialog creation and destruction, and keeps 29 // track of the 1:1 relationship between initiatora tabs and print preview 30 // dialogs. 31 class PrintPreviewDialogController 32 : public base::RefCounted<PrintPreviewDialogController>, 33 public content::NotificationObserver { 34 public: 35 PrintPreviewDialogController(); 36 37 static PrintPreviewDialogController* GetInstance(); 38 39 // Initiate print preview for |initiator|. 40 // Call this instead of GetOrCreatePreviewDialog(). 41 static void PrintPreview(content::WebContents* initiator); 42 43 // Get/Create the print preview dialog for |initiator|. 44 // Exposed for unit tests. 45 content::WebContents* GetOrCreatePreviewDialog( 46 content::WebContents* initiator); 47 48 // Returns the preview dialog for |contents|. 49 // Returns |contents| if |contents| is a preview dialog. 50 // Returns NULL if no preview dialog exists for |contents|. 51 content::WebContents* GetPrintPreviewForContents( 52 content::WebContents* contents) const; 53 54 // Returns the initiator for |preview_dialog|. 55 // Returns NULL if no initiator exists for |preview_dialog|. 56 content::WebContents* GetInitiator(content::WebContents* preview_dialog); 57 58 // content::NotificationObserver implementation. 59 virtual void Observe(int type, 60 const content::NotificationSource& source, 61 const content::NotificationDetails& details) OVERRIDE; 62 63 // Returns true if |contents| is a print preview dialog. 64 static bool IsPrintPreviewDialog(content::WebContents* contents); 65 66 // Returns true if |url| is a print preview url. 67 static bool IsPrintPreviewURL(const GURL& url); 68 69 // Erase the initiator info associated with |preview_dialog|. 70 void EraseInitiatorInfo(content::WebContents* preview_dialog); 71 72 bool is_creating_print_preview_dialog() const { 73 return is_creating_print_preview_dialog_; 74 } 75 76 private: 77 friend class base::RefCounted<PrintPreviewDialogController>; 78 79 // 1:1 relationship between a print preview dialog and its initiator tab. 80 // Key: Print preview dialog. 81 // Value: Initiator. 82 typedef std::map<content::WebContents*, content::WebContents*> 83 PrintPreviewDialogMap; 84 85 virtual ~PrintPreviewDialogController(); 86 87 // Handler for the RENDERER_PROCESS_CLOSED notification. This is observed when 88 // the initiator renderer crashed. 89 void OnRendererProcessClosed(content::RenderProcessHost* rph); 90 91 // Handler for the WEB_CONTENTS_DESTROYED notification. This is observed when 92 // either WebContents is closed. 93 void OnWebContentsDestroyed(content::WebContents* contents); 94 95 // Handler for the NAV_ENTRY_COMMITTED notification. This is observed when the 96 // renderer is navigated to a different page. 97 void OnNavEntryCommitted(content::WebContents* contents, 98 content::LoadCommittedDetails* details); 99 100 // Creates a new print preview dialog. 101 content::WebContents* CreatePrintPreviewDialog( 102 content::WebContents* initiator); 103 104 // Helper function to store the title of the initiator associated with 105 // |preview_dialog| in |preview_dialog|'s PrintPreviewUI. 106 void SaveInitiatorTitle(content::WebContents* preview_dialog); 107 108 // Adds/Removes observers for notifications from |contents|. 109 void AddObservers(content::WebContents* contents); 110 void RemoveObservers(content::WebContents* contents); 111 112 // Removes WebContents when they close/crash/navigate. 113 void RemoveInitiator(content::WebContents* initiator); 114 void RemovePreviewDialog(content::WebContents* preview_dialog); 115 116 // Mapping between print preview dialog and the corresponding initiator. 117 PrintPreviewDialogMap preview_dialog_map_; 118 119 // A registrar for listening notifications. 120 content::NotificationRegistrar registrar_; 121 122 // True if the controller is waiting for a new preview dialog via 123 // content::NAVIGATION_TYPE_NEW_PAGE. 124 bool waiting_for_new_preview_page_; 125 126 // Whether the PrintPreviewDialogController is in the middle of creating a 127 // print preview dialog. 128 bool is_creating_print_preview_dialog_; 129 130 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogController); 131 }; 132 133 } // namespace printing 134 135 #endif // CHROME_BROWSER_PRINTING_PRINT_PREVIEW_DIALOG_CONTROLLER_H_ 136