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