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 ) { | |
35 DCHECK(initiator_tab); | |
36 | |
37 // |initiator_tab| is preview tab. | |
38 if (IsPreviewTab(initiator_tab)) | |
39 return initiator_tab; | |
40 | |
41 // Get the preview tab for |initiator_tab|. | |
42 TabContents* preview_tab = GetPreviewTab(initiator_tab); | |
43 if (preview_tab) { | |
44 // Show current preview tab. | |
45 preview_tab->Activate(); | |
46 return preview_tab; | |
47 } | |
48 return CreatePrintPreviewTab(initiator_tab, browser_window_id); | |
49 } | |
50 | |
51 bool PrintPreviewTabController::IsPreviewTab(TabContents* tab) { | |
52 const GURL& url = tab->GetURL(); | |
53 if (url.SchemeIs(chrome::kChromeUIScheme) && | |
Lei Zhang
2010/11/16 00:47:39
nit: drop the if statement and just return (foo &&
kmadhusu
2010/11/16 01:24:22
Done.
| |
54 url.host() == chrome::kChromeUIPrintHost) { | |
55 return true; | |
56 } | |
57 return false; | |
58 } | |
59 | |
60 TabContents* PrintPreviewTabController::GetInitiatorTab( | |
61 TabContents* preview_tab) { | |
62 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); | |
63 if (it != preview_tab_map_.end()) | |
64 return preview_tab_map_[preview_tab]; | |
65 return NULL; | |
66 } | |
67 | |
68 TabContents* PrintPreviewTabController::GetPreviewTab( | |
69 TabContents* current_tab) { | |
70 for (PrintPreviewTabMap::iterator it = preview_tab_map_.begin(); | |
71 it != preview_tab_map_.end(); ++it) { | |
72 if ((it->second == current_tab) || (it->first == current_tab)) | |
73 return it->first; | |
74 } | |
75 return NULL; | |
76 } | |
77 | |
78 void PrintPreviewTabController::AddObservers(TabContents* tab) { | |
79 registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED, | |
80 Source<TabContents>(tab)); | |
81 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, | |
82 Source<NavigationController>(&tab->controller())); | |
83 } | |
84 | |
85 void PrintPreviewTabController::RemoveObservers(TabContents* tab) { | |
86 registrar_.Remove(this, NotificationType::TAB_CONTENTS_DESTROYED, | |
87 Source<TabContents>(tab)); | |
88 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED, | |
89 Source<NavigationController>(&tab->controller())); | |
90 } | |
91 | |
92 TabContents* PrintPreviewTabController::CreatePrintPreviewTab( | |
93 TabContents* initiator_tab, int browser_window_id) { | |
94 Browser* current_browser = BrowserList::FindBrowserWithID(browser_window_id); | |
95 // Add a new tab next to initiator tab. | |
96 browser::NavigateParams params(current_browser, | |
97 GURL(chrome::kChromeUIPrintURL), | |
98 PageTransition::LINK); | |
99 params.disposition = NEW_FOREGROUND_TAB; | |
100 params.tabstrip_index = current_browser->tabstrip_model()-> | |
101 GetIndexOfTabContents(initiator_tab) + 1; | |
102 browser::Navigate(¶ms); | |
103 TabContents* preview_tab = params.target_contents; | |
104 preview_tab->Activate(); | |
105 | |
106 // Add an entry to the map. | |
107 preview_tab_map_[preview_tab] = initiator_tab; | |
108 waiting_for_new_preview_page_ = true; | |
109 | |
110 AddObservers(initiator_tab); | |
111 AddObservers(preview_tab); | |
112 | |
113 return preview_tab; | |
114 } | |
115 | |
116 void PrintPreviewTabController::Observe(NotificationType type, | |
117 const NotificationSource& source, | |
118 const NotificationDetails& details) { | |
119 TabContents* initiator_tab = NULL; | |
120 TabContents* preview_tab = NULL; | |
121 TabContents* source_tab = NULL; | |
122 NavigationController::LoadCommittedDetails* detail_info = NULL; | |
123 | |
124 switch (type.value) { | |
125 case NotificationType::TAB_CONTENTS_DESTROYED: { | |
126 source_tab = Source<TabContents>(source).ptr(); | |
127 break; | |
128 } | |
129 case NotificationType::NAV_ENTRY_COMMITTED: { | |
130 NavigationController* controller = | |
131 Source<NavigationController>(source).ptr(); | |
132 source_tab = controller->tab_contents(); | |
133 detail_info = | |
134 Details<NavigationController::LoadCommittedDetails>(details).ptr(); | |
135 break; | |
136 } | |
137 default: { | |
138 NOTREACHED(); | |
139 return; | |
140 } | |
141 } | |
142 DCHECK(source_tab); | |
143 preview_tab = GetPreviewTab(source_tab); | |
144 | |
145 // |source_tab| is preview tab. | |
146 if (preview_tab == source_tab) | |
147 initiator_tab = GetInitiatorTab(source_tab); | |
148 else | |
149 initiator_tab = source_tab; | |
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 if (transition_type == PageTransition::RELOAD || | |
158 nav_type == NavigationType::SAME_PAGE) { | |
159 return; | |
160 } | |
161 | |
162 // New |preview_tab| is created. Don't update/erase map entry. | |
163 if (waiting_for_new_preview_page_ && | |
164 transition_type == PageTransition::LINK && | |
165 nav_type == NavigationType::NEW_PAGE && | |
166 source_tab == preview_tab) { | |
167 waiting_for_new_preview_page_ = false; | |
168 return; | |
169 } | |
170 | |
171 // User navigated to a preview tab using forward/back button. | |
172 if (IsPreviewTab(source_tab) && | |
173 transition_type == PageTransition::FORWARD_BACK && | |
174 nav_type == NavigationType::EXISTING_PAGE) { | |
175 return; | |
176 } | |
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 |