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_TAB_CONTROLLER_H_ | 5 #ifndef CHROME_BROWSER_PRINTING_PRINT_PREVIEW_TAB_CONTROLLER_H_ |
6 #define CHROME_BROWSER_PRINTING_PRINT_PREVIEW_TAB_CONTROLLER_H_ | 6 #define CHROME_BROWSER_PRINTING_PRINT_PREVIEW_TAB_CONTROLLER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 | 10 |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "chrome/browser/sessions/session_id.h" | 12 #include "chrome/browser/sessions/session_id.h" |
13 #include "content/public/browser/notification_observer.h" | 13 #include "content/public/browser/notification_observer.h" |
14 #include "content/public/browser/notification_registrar.h" | 14 #include "content/public/browser/notification_registrar.h" |
15 | 15 |
16 class GURL; | 16 class GURL; |
17 class TabContents; | 17 class TabContents; |
18 typedef TabContents TabContentsWrapper; | |
19 | 18 |
20 namespace content { | 19 namespace content { |
21 struct LoadCommittedDetails; | 20 struct LoadCommittedDetails; |
22 class RenderProcessHost; | 21 class RenderProcessHost; |
23 } | 22 } |
24 | 23 |
25 namespace printing { | 24 namespace printing { |
26 | 25 |
27 // For print preview, a print preview (PP) tab is linked with the initiator tab | 26 // For print preview, a print preview (PP) tab is linked with the initiator tab |
28 // that initiated the printing operation. If the tab initiates a second | 27 // that initiated the printing operation. If the tab initiates a second |
29 // printing operation while the first print preview tab is still open, that PP | 28 // printing operation while the first print preview tab is still open, that PP |
30 // tab is focused/activated. There may be more than one PP tab open. There is a | 29 // tab is focused/activated. There may be more than one PP tab open. There is a |
31 // 1:1 relationship between PP tabs and initiating tabs. This class manages PP | 30 // 1:1 relationship between PP tabs and initiating tabs. This class manages PP |
32 // tabs and initiator tabs. | 31 // tabs and initiator tabs. |
33 class PrintPreviewTabController | 32 class PrintPreviewTabController |
34 : public base::RefCounted<PrintPreviewTabController>, | 33 : public base::RefCounted<PrintPreviewTabController>, |
35 public content::NotificationObserver { | 34 public content::NotificationObserver { |
36 public: | 35 public: |
37 PrintPreviewTabController(); | 36 PrintPreviewTabController(); |
38 | 37 |
39 static PrintPreviewTabController* GetInstance(); | 38 static PrintPreviewTabController* GetInstance(); |
40 | 39 |
41 // Initiate print preview for |initiator_tab|. | 40 // Initiate print preview for |initiator_tab|. |
42 // Call this instead of GetOrCreatePreviewTab(). | 41 // Call this instead of GetOrCreatePreviewTab(). |
43 static void PrintPreview(TabContentsWrapper* initiator_tab); | 42 static void PrintPreview(TabContents* initiator_tab); |
44 | 43 |
45 // Get/Create the print preview tab for |initiator_tab|. | 44 // Get/Create the print preview tab for |initiator_tab|. |
46 // Exposed for unit tests. | 45 // Exposed for unit tests. |
47 TabContentsWrapper* GetOrCreatePreviewTab(TabContentsWrapper* initiator_tab); | 46 TabContents* GetOrCreatePreviewTab(TabContents* initiator_tab); |
48 | 47 |
49 // Returns preview tab for |tab|. | 48 // Returns preview tab for |tab|. |
50 // Returns |tab| if |tab| is a preview tab. | 49 // Returns |tab| if |tab| is a preview tab. |
51 // Returns NULL if no preview tab exists for |tab|. | 50 // Returns NULL if no preview tab exists for |tab|. |
52 TabContentsWrapper* GetPrintPreviewForTab(TabContentsWrapper* tab) const; | 51 TabContents* GetPrintPreviewForTab(TabContents* tab) const; |
53 | 52 |
54 // Returns initiator tab for |preview_tab|. | 53 // Returns initiator tab for |preview_tab|. |
55 // Returns NULL if no initiator tab exists for |preview_tab|. | 54 // Returns NULL if no initiator tab exists for |preview_tab|. |
56 TabContentsWrapper* GetInitiatorTab(TabContentsWrapper* preview_tab); | 55 TabContents* GetInitiatorTab(TabContents* preview_tab); |
57 | 56 |
58 // content::NotificationObserver implementation. | 57 // content::NotificationObserver implementation. |
59 virtual void Observe(int type, | 58 virtual void Observe(int type, |
60 const content::NotificationSource& source, | 59 const content::NotificationSource& source, |
61 const content::NotificationDetails& details) OVERRIDE; | 60 const content::NotificationDetails& details) OVERRIDE; |
62 | 61 |
63 // Returns true if |tab| is a print preview tab. | 62 // Returns true if |tab| is a print preview tab. |
64 static bool IsPrintPreviewTab(TabContentsWrapper* tab); | 63 static bool IsPrintPreviewTab(TabContents* tab); |
65 | 64 |
66 // Returns true if |url| is a print preview url. | 65 // Returns true if |url| is a print preview url. |
67 static bool IsPrintPreviewURL(const GURL& url); | 66 static bool IsPrintPreviewURL(const GURL& url); |
68 | 67 |
69 // Erase the initiator tab info associated with |preview_tab|. | 68 // Erase the initiator tab info associated with |preview_tab|. |
70 void EraseInitiatorTabInfo(TabContentsWrapper* preview_tab); | 69 void EraseInitiatorTabInfo(TabContents* preview_tab); |
71 | 70 |
72 bool is_creating_print_preview_tab() const; | 71 bool is_creating_print_preview_tab() const; |
73 | 72 |
74 private: | 73 private: |
75 friend class base::RefCounted<PrintPreviewTabController>; | 74 friend class base::RefCounted<PrintPreviewTabController>; |
76 | 75 |
77 // 1:1 relationship between initiator tab and print preview tab. | 76 // 1:1 relationship between initiator tab and print preview tab. |
78 // Key: Preview tab. | 77 // Key: Preview tab. |
79 // Value: Initiator tab. | 78 // Value: Initiator tab. |
80 typedef std::map<TabContentsWrapper*, TabContentsWrapper*> PrintPreviewTabMap; | 79 typedef std::map<TabContents*, TabContents*> PrintPreviewTabMap; |
81 | 80 |
82 virtual ~PrintPreviewTabController(); | 81 virtual ~PrintPreviewTabController(); |
83 | 82 |
84 // Handler for the RENDERER_PROCESS_CLOSED notification. This is observed when | 83 // Handler for the RENDERER_PROCESS_CLOSED notification. This is observed when |
85 // the initiator renderer crashed. | 84 // the initiator renderer crashed. |
86 void OnRendererProcessClosed(content::RenderProcessHost* rph); | 85 void OnRendererProcessClosed(content::RenderProcessHost* rph); |
87 | 86 |
88 // Handler for the TAB_CONTENTS_DESTROYED notification. This is observed when | 87 // Handler for the TAB_CONTENTS_DESTROYED notification. This is observed when |
89 // either tab is closed. | 88 // either tab is closed. |
90 void OnTabContentsDestroyed(TabContentsWrapper* tab); | 89 void OnTabContentsDestroyed(TabContents* tab); |
91 | 90 |
92 // Handler for the NAV_ENTRY_COMMITTED notification. This is observed when the | 91 // Handler for the NAV_ENTRY_COMMITTED notification. This is observed when the |
93 // renderer is navigated to a different page. | 92 // renderer is navigated to a different page. |
94 void OnNavEntryCommitted(TabContentsWrapper* tab, | 93 void OnNavEntryCommitted(TabContents* tab, |
95 content::LoadCommittedDetails* details); | 94 content::LoadCommittedDetails* details); |
96 | 95 |
97 // Creates a new print preview tab. | 96 // Creates a new print preview tab. |
98 TabContentsWrapper* CreatePrintPreviewTab(TabContentsWrapper* initiator_tab); | 97 TabContents* CreatePrintPreviewTab(TabContents* initiator_tab); |
99 | 98 |
100 // Helper function to store the initiator tab(title and url) information | 99 // Helper function to store the initiator tab(title and url) information |
101 // in PrintPreviewUI. | 100 // in PrintPreviewUI. |
102 void SetInitiatorTabURLAndTitle(TabContentsWrapper* preview_tab); | 101 void SetInitiatorTabURLAndTitle(TabContents* preview_tab); |
103 | 102 |
104 // Adds/Removes observers for notifications from |tab|. | 103 // Adds/Removes observers for notifications from |tab|. |
105 void AddObservers(TabContentsWrapper* tab); | 104 void AddObservers(TabContents* tab); |
106 void RemoveObservers(TabContentsWrapper* tab); | 105 void RemoveObservers(TabContents* tab); |
107 | 106 |
108 // Removes tabs when they close/crash/navigate. | 107 // Removes tabs when they close/crash/navigate. |
109 void RemoveInitiatorTab(TabContentsWrapper* initiator_tab, | 108 void RemoveInitiatorTab(TabContents* initiator_tab, bool is_navigation); |
110 bool is_navigation); | 109 void RemovePreviewTab(TabContents* preview_tab); |
111 void RemovePreviewTab(TabContentsWrapper* preview_tab); | |
112 | 110 |
113 // Mapping between print preview tab and the corresponding initiator tab. | 111 // Mapping between print preview tab and the corresponding initiator tab. |
114 PrintPreviewTabMap preview_tab_map_; | 112 PrintPreviewTabMap preview_tab_map_; |
115 | 113 |
116 // A registrar for listening notifications. | 114 // A registrar for listening notifications. |
117 content::NotificationRegistrar registrar_; | 115 content::NotificationRegistrar registrar_; |
118 | 116 |
119 // True if the controller is waiting for a new preview tab via | 117 // True if the controller is waiting for a new preview tab via |
120 // content::NAVIGATION_TYPE_NEW_PAGE. | 118 // content::NAVIGATION_TYPE_NEW_PAGE. |
121 bool waiting_for_new_preview_page_; | 119 bool waiting_for_new_preview_page_; |
122 | 120 |
123 // Whether the PrintPreviewTabController is in the middle of creating a | 121 // Whether the PrintPreviewTabController is in the middle of creating a |
124 // print preview tab. | 122 // print preview tab. |
125 bool is_creating_print_preview_tab_; | 123 bool is_creating_print_preview_tab_; |
126 | 124 |
127 DISALLOW_COPY_AND_ASSIGN(PrintPreviewTabController); | 125 DISALLOW_COPY_AND_ASSIGN(PrintPreviewTabController); |
128 }; | 126 }; |
129 | 127 |
130 } // namespace printing | 128 } // namespace printing |
131 | 129 |
132 #endif // CHROME_BROWSER_PRINTING_PRINT_PREVIEW_TAB_CONTROLLER_H_ | 130 #endif // CHROME_BROWSER_PRINTING_PRINT_PREVIEW_TAB_CONTROLLER_H_ |
OLD | NEW |