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