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