OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2010 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 #include "chrome/browser/printing/print_preview_tab_controller.h" | |
6 | |
7 #include "chrome/browser/browser_process.h" | |
8 #include "chrome/browser/tab_contents/tab_contents.h" | |
9 #include "chrome/browser/tabs/tab_strip_model.h" | |
10 #include "chrome/browser/ui/browser.h" | |
11 #include "chrome/browser/ui/browser_list.h" | |
12 #include "chrome/browser/ui/browser_navigator.h" | |
13 #include "chrome/common/notification_service.h" | |
14 #include "chrome/common/url_constants.h" | |
15 | |
16 namespace printing { | |
17 | |
18 // static | |
19 PrintPreviewTabController* PrintPreviewTabController::GetInstance() { | |
20 if (!g_browser_process) | |
21 return NULL; | |
22 return g_browser_process->print_preview_tab_controller(); | |
23 } | |
24 | |
25 PrintPreviewTabController::PrintPreviewTabController() | |
26 : waiting_for_new_preview_page_(false) { | |
27 } | |
28 | |
29 PrintPreviewTabController::~PrintPreviewTabController() { | |
30 preview_tab_map_.clear(); | |
31 } | |
32 | |
33 TabContents* PrintPreviewTabController::GetOrCreatePreviewTab( | |
34 TabContents* current_tab, int browser_window_id ) { | |
Lei Zhang
2010/11/12 23:17:18
nit: |current_tab| should be named "initiator_tab"
kmadhusu
2010/11/13 00:05:59
Done.
| |
35 // Initiator tab is a preview tab. | |
36 if (IsPrintPreviewTab(current_tab)) | |
37 return current_tab; | |
38 | |
39 // Get the preview tab for initiator tab. | |
40 TabContents* preview_tab = GetPreviewTab(current_tab); | |
41 if (preview_tab) { | |
42 // Show current preview tab. | |
43 preview_tab->Activate(); | |
44 return preview_tab; | |
45 } | |
46 return CreatePrintPreviewTab(current_tab, browser_window_id); | |
47 } | |
48 | |
49 bool PrintPreviewTabController::IsPrintPreviewTab(TabContents* tab) { | |
50 const GURL& url = tab->GetURL(); | |
51 if (url.SchemeIs(chrome::kChromeUIScheme) && | |
52 url.host() == chrome::kChromeUIPrintHost) | |
53 return true; | |
54 | |
55 TabContents* preview_tab = GetPreviewTab(tab); | |
56 return ((preview_tab)? (preview_tab == tab) : false); | |
57 } | |
58 | |
59 Browser* PrintPreviewTabController::GetBrowserWindow(int browser_window_id) { | |
60 return BrowserList::FindBrowserWithID(browser_window_id); | |
61 } | |
62 | |
63 TabContents* PrintPreviewTabController::GetInitiatorTab( | |
64 TabContents* preview_tab) { | |
65 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); | |
66 if (it != preview_tab_map_.end()) | |
67 return preview_tab_map_[preview_tab]; | |
68 return NULL; | |
69 } | |
70 | |
71 TabContents* PrintPreviewTabController::GetPreviewTab( | |
72 TabContents* current_tab) { | |
73 for (PrintPreviewTabMap::iterator it = preview_tab_map_.begin(); | |
74 it != preview_tab_map_.end(); ++it) { | |
75 if ((it->second == current_tab) || (it->first == current_tab)) | |
76 return it->first; | |
77 } | |
78 return NULL; | |
79 } | |
80 | |
81 void PrintPreviewTabController::AddObservers(TabContents* tab) { | |
82 // Add TAB_CONTENTS_DESTROYED notification. | |
Lei Zhang
2010/11/12 23:48:40
nit: the comments in {Add,Remove}Observers are not
kmadhusu
2010/11/13 00:05:59
Done.
| |
83 registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED, | |
84 Source<TabContents>(tab)); | |
85 | |
86 // Add NAV_ENTRY_COMMITTED notification. | |
87 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, | |
88 Source<NavigationController>(&tab->controller())); | |
89 } | |
90 | |
91 void PrintPreviewTabController::RemoveObservers(TabContents* tab) { | |
92 // Remove TAB_CONTENTS_DESTROYED notification. | |
93 registrar_.Remove(this, NotificationType::TAB_CONTENTS_DESTROYED, | |
94 Source<TabContents>(tab)); | |
95 | |
96 // Remove NAV_ENTRY_COMMITTED notification. | |
97 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED, | |
98 Source<NavigationController>(&tab->controller())); | |
99 } | |
100 | |
101 TabContents* PrintPreviewTabController::CreatePrintPreviewTab( | |
102 TabContents* initiator_tab, int browser_window_id) { | |
103 Browser* current_browser = GetBrowserWindow(browser_window_id); | |
104 // Add a new tab next to initiator tab. | |
105 browser::NavigateParams params(current_browser, | |
106 GURL(chrome::kChromeUIPrintURL), | |
107 PageTransition::LINK); | |
108 params.disposition = NEW_FOREGROUND_TAB; | |
109 params.tabstrip_index = current_browser->tabstrip_model()-> | |
110 GetIndexOfTabContents(initiator_tab)+1; | |
Lei Zhang
2010/11/12 23:17:18
nit: foo + 1;
kmadhusu
2010/11/13 00:05:59
Done.
| |
111 browser::Navigate(¶ms); | |
112 TabContents* preview_tab = params.target_contents; | |
113 preview_tab->Activate(); | |
114 | |
115 // Add an entry to the map. | |
116 preview_tab_map_[preview_tab] = initiator_tab; | |
117 waiting_for_new_preview_page_ = true; | |
118 | |
119 AddObservers(initiator_tab); | |
120 AddObservers(preview_tab); | |
121 | |
122 return preview_tab; | |
123 } | |
124 | |
125 void PrintPreviewTabController::Observe(NotificationType type, | |
126 const NotificationSource& source, | |
127 const NotificationDetails& details) { | |
128 TabContents* initiator_tab = NULL; | |
129 TabContents* preview_tab = NULL; | |
130 TabContents* source_tab = NULL; | |
131 NavigationController::LoadCommittedDetails* detail_info = NULL; | |
132 | |
133 switch (type.value) { | |
134 case NotificationType::TAB_CONTENTS_DESTROYED: { | |
135 source_tab = Source<TabContents>(source).ptr(); | |
136 break; | |
137 } | |
138 case NotificationType::NAV_ENTRY_COMMITTED: { | |
139 NavigationController* controller = | |
140 Source<NavigationController>(source).ptr(); | |
141 source_tab = controller->tab_contents(); | |
142 detail_info = | |
143 Details<NavigationController::LoadCommittedDetails>(details).ptr(); | |
144 break; | |
145 } | |
146 default: { | |
147 NOTREACHED(); | |
148 return; | |
149 } | |
150 } | |
151 | |
152 if (IsPrintPreviewTab(source_tab)) { | |
153 // |source_tab| is preview tab. | |
154 initiator_tab = GetInitiatorTab(source_tab); | |
155 preview_tab = source_tab; | |
156 } else { | |
157 initiator_tab = source_tab; | |
158 preview_tab = GetPreviewTab(source_tab); | |
159 } | |
160 | |
161 if (detail_info) { | |
162 PageTransition::Type transition_type = | |
163 detail_info->entry->transition_type(); | |
164 NavigationType::Type nav_type = detail_info->type; | |
165 | |
166 // Don't update/erase the map entry if the page has not changed. | |
167 bool reload = (transition_type == PageTransition::RELOAD || | |
168 nav_type == NavigationType::SAME_PAGE); | |
169 if (reload) | |
170 return; | |
171 | |
172 // New |preview_tab| is created. Don't update/erase map entry. | |
173 bool is_preview_tab_loaded = (transition_type == PageTransition::LINK && | |
174 nav_type == NavigationType::NEW_PAGE && | |
175 source_tab == preview_tab); | |
176 if (waiting_for_new_preview_page_ && is_preview_tab_loaded) { | |
177 waiting_for_new_preview_page_ = false; | |
178 return; | |
179 } | |
180 | |
181 // User navigated to existing |preview_tab| using forward/back button. | |
182 bool nav_to_prev_tab = (IsPrintPreviewTab(source_tab) && | |
183 transition_type == PageTransition::FORWARD_BACK && | |
184 nav_type == NavigationType::EXISTING_PAGE); | |
185 if (nav_to_prev_tab) | |
186 return; | |
187 } | |
188 | |
189 // If |source_tab| is |initiator_tab|, update the map entry. | |
190 if (source_tab == initiator_tab) { | |
191 preview_tab_map_[preview_tab] = NULL; | |
192 } | |
193 | |
194 // If |source_tab| is |preview_tab|, erase the map entry. | |
195 if (source_tab == preview_tab) { | |
196 EraseMapEntry(preview_tab); | |
197 RemoveObservers(preview_tab); | |
198 } | |
199 | |
200 if (initiator_tab) | |
201 RemoveObservers(initiator_tab); | |
202 } | |
203 | |
204 void PrintPreviewTabController::EraseMapEntry(TabContents* initiator_tab) { | |
205 preview_tab_map_.erase(initiator_tab); | |
206 } | |
207 | |
208 } // namespace printing | |
OLD | NEW |