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* initiator_tab, int browser_window_id ) { | |
Lei Zhang
2010/11/13 05:04:56
The current implementation of IsPrintPreviewTab()
kmadhusu
2010/11/15 21:22:27
Done.
| |
35 // Initiator tab is a preview tab. | |
36 if (IsPrintPreviewTab(initiator_tab)) | |
37 return initiator_tab; | |
38 | |
39 // Get the preview tab for initiator tab. | |
40 TabContents* preview_tab = GetPreviewTab(initiator_tab); | |
41 if (preview_tab) { | |
42 // Show current preview tab. | |
43 preview_tab->Activate(); | |
44 return preview_tab; | |
45 } | |
46 return CreatePrintPreviewTab(initiator_tab, browser_window_id); | |
47 } | |
48 | |
49 bool PrintPreviewTabController::IsPrintPreviewTab(TabContents* tab) { | |
50 const GURL& url = tab->GetURL(); | |
Lei Zhang
2010/11/13 05:04:56
If you already have a mapping of print preview tab
kmadhusu
2010/11/15 21:22:27
I need this check because we can navigate to a pre
| |
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); | |
Lei Zhang
2010/11/13 05:04:56
nit: you don't need the outer most parenthesis and
kmadhusu
2010/11/15 21:22:27
Previous check is sufficient to identify a preview
| |
57 } | |
58 | |
59 TabContents* PrintPreviewTabController::GetInitiatorTab( | |
60 TabContents* preview_tab) { | |
61 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); | |
62 if (it != preview_tab_map_.end()) | |
63 return preview_tab_map_[preview_tab]; | |
64 return NULL; | |
65 } | |
66 | |
67 TabContents* PrintPreviewTabController::GetPreviewTab( | |
68 TabContents* current_tab) { | |
69 for (PrintPreviewTabMap::iterator it = preview_tab_map_.begin(); | |
70 it != preview_tab_map_.end(); ++it) { | |
71 if ((it->second == current_tab) || (it->first == current_tab)) | |
Lei Zhang
2010/11/13 05:04:56
if you first do preview_tab_map_.find(current_tab)
kmadhusu
2010/11/15 21:22:27
To my understanding, "find(key)" function is used
Lei Zhang
2010/11/16 00:47:39
If preview_tab_map_.find(current_tab) returns prev
| |
72 return it->first; | |
73 } | |
74 return NULL; | |
75 } | |
76 | |
77 void PrintPreviewTabController::AddObservers(TabContents* tab) { | |
78 registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED, | |
79 Source<TabContents>(tab)); | |
80 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, | |
81 Source<NavigationController>(&tab->controller())); | |
82 } | |
83 | |
84 void PrintPreviewTabController::RemoveObservers(TabContents* tab) { | |
85 registrar_.Remove(this, NotificationType::TAB_CONTENTS_DESTROYED, | |
86 Source<TabContents>(tab)); | |
87 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED, | |
88 Source<NavigationController>(&tab->controller())); | |
89 } | |
90 | |
91 TabContents* PrintPreviewTabController::CreatePrintPreviewTab( | |
92 TabContents* initiator_tab, int browser_window_id) { | |
93 Browser* current_browser = BrowserList::FindBrowserWithID(browser_window_id); | |
94 // Add a new tab next to initiator tab. | |
95 browser::NavigateParams params(current_browser, | |
96 GURL(chrome::kChromeUIPrintURL), | |
97 PageTransition::LINK); | |
98 params.disposition = NEW_FOREGROUND_TAB; | |
99 params.tabstrip_index = current_browser->tabstrip_model()-> | |
100 GetIndexOfTabContents(initiator_tab) + 1; | |
101 browser::Navigate(¶ms); | |
102 TabContents* preview_tab = params.target_contents; | |
103 preview_tab->Activate(); | |
104 | |
105 // Add an entry to the map. | |
106 preview_tab_map_[preview_tab] = initiator_tab; | |
107 waiting_for_new_preview_page_ = true; | |
108 | |
109 AddObservers(initiator_tab); | |
110 AddObservers(preview_tab); | |
111 | |
112 return preview_tab; | |
113 } | |
114 | |
115 void PrintPreviewTabController::Observe(NotificationType type, | |
116 const NotificationSource& source, | |
117 const NotificationDetails& details) { | |
118 TabContents* initiator_tab = NULL; | |
119 TabContents* preview_tab = NULL; | |
120 TabContents* source_tab = NULL; | |
121 NavigationController::LoadCommittedDetails* detail_info = NULL; | |
122 | |
123 switch (type.value) { | |
124 case NotificationType::TAB_CONTENTS_DESTROYED: { | |
125 source_tab = Source<TabContents>(source).ptr(); | |
126 break; | |
127 } | |
128 case NotificationType::NAV_ENTRY_COMMITTED: { | |
129 NavigationController* controller = | |
130 Source<NavigationController>(source).ptr(); | |
131 source_tab = controller->tab_contents(); | |
132 detail_info = | |
133 Details<NavigationController::LoadCommittedDetails>(details).ptr(); | |
134 break; | |
135 } | |
136 default: { | |
137 NOTREACHED(); | |
138 return; | |
139 } | |
140 } | |
141 | |
142 if (IsPrintPreviewTab(source_tab)) { | |
143 // |source_tab| is preview tab. | |
144 initiator_tab = GetInitiatorTab(source_tab); | |
145 preview_tab = source_tab; | |
146 } else { | |
147 initiator_tab = source_tab; | |
148 preview_tab = GetPreviewTab(source_tab); | |
149 } | |
150 | |
151 if (detail_info) { | |
152 PageTransition::Type transition_type = | |
153 detail_info->entry->transition_type(); | |
154 NavigationType::Type nav_type = detail_info->type; | |
155 | |
156 // Don't update/erase the map entry if the page has not changed. | |
157 bool reload = (transition_type == PageTransition::RELOAD || | |
Lei Zhang
2010/11/13 05:04:56
nit: no need for |reload| and other variables belo
kmadhusu
2010/11/15 21:22:27
Done.
| |
158 nav_type == NavigationType::SAME_PAGE); | |
159 if (reload) | |
160 return; | |
161 | |
162 // New |preview_tab| is created. Don't update/erase map entry. | |
163 bool is_preview_tab_loaded = (transition_type == PageTransition::LINK && | |
164 nav_type == NavigationType::NEW_PAGE && | |
165 source_tab == preview_tab); | |
166 if (waiting_for_new_preview_page_ && is_preview_tab_loaded) { | |
167 waiting_for_new_preview_page_ = false; | |
168 return; | |
169 } | |
170 | |
171 // User navigated to existing |preview_tab| using forward/back button. | |
172 bool nav_to_prev_tab = (IsPrintPreviewTab(source_tab) && | |
173 transition_type == PageTransition::FORWARD_BACK && | |
174 nav_type == NavigationType::EXISTING_PAGE); | |
175 if (nav_to_prev_tab) | |
176 return; | |
177 } | |
178 | |
179 // If |source_tab| is |initiator_tab|, update the map entry. | |
180 if (source_tab == initiator_tab) { | |
181 preview_tab_map_[preview_tab] = NULL; | |
182 } | |
183 | |
184 // If |source_tab| is |preview_tab|, erase the map entry. | |
185 if (source_tab == preview_tab) { | |
186 preview_tab_map_.erase(preview_tab); | |
187 RemoveObservers(preview_tab); | |
188 } | |
189 | |
190 if (initiator_tab) | |
191 RemoveObservers(initiator_tab); | |
192 } | |
193 | |
194 } // namespace printing | |
OLD | NEW |