Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "chrome/browser/printing/print_preview_tab_controller.h" | 5 #include "chrome/browser/printing/print_preview_tab_controller.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 #include <memory> | |
| 9 #include <string> | |
| 7 #include <vector> | 10 #include <vector> |
| 8 | 11 |
| 9 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 10 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/chrome_plugin_service_filter.h" | 15 #include "chrome/browser/chrome_plugin_service_filter.h" |
| 13 #include "chrome/browser/printing/print_view_manager.h" | 16 #include "chrome/browser/printing/print_view_manager.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/sessions/restore_tab_helper.h" | 18 #include "chrome/browser/sessions/restore_tab_helper.h" |
| 16 #include "chrome/browser/tabs/tab_strip_model.h" | 19 #include "chrome/browser/tabs/tab_strip_model.h" |
| 17 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
| 18 #include "chrome/browser/ui/browser_list.h" | 21 #include "chrome/browser/ui/browser_list.h" |
| 19 #include "chrome/browser/ui/browser_navigator.h" | 22 #include "chrome/browser/ui/browser_navigator.h" |
| 20 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 23 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 24 #include "chrome/browser/ui/webui/constrained_html_ui.h" | |
| 25 #include "chrome/browser/ui/webui/html_dialog_ui.h" | |
| 21 #include "chrome/browser/ui/webui/print_preview_ui.h" | 26 #include "chrome/browser/ui/webui/print_preview_ui.h" |
| 22 #include "chrome/common/chrome_content_client.h" | 27 #include "chrome/common/chrome_content_client.h" |
| 23 #include "chrome/common/chrome_switches.h" | 28 #include "chrome/common/chrome_switches.h" |
| 24 #include "chrome/common/url_constants.h" | 29 #include "chrome/common/url_constants.h" |
| 25 #include "content/browser/renderer_host/render_view_host.h" | 30 #include "content/browser/renderer_host/render_view_host.h" |
| 26 #include "content/browser/tab_contents/navigation_details.h" | 31 #include "content/browser/tab_contents/navigation_details.h" |
| 27 #include "content/browser/tab_contents/tab_contents.h" | 32 #include "content/browser/tab_contents/tab_contents.h" |
| 28 #include "content/common/content_notification_types.h" | 33 #include "content/common/content_notification_types.h" |
| 29 #include "content/common/notification_details.h" | 34 #include "content/common/notification_details.h" |
| 30 #include "content/common/notification_source.h" | 35 #include "content/common/notification_source.h" |
| 31 #include "webkit/plugins/webplugininfo.h" | 36 #include "webkit/plugins/webplugininfo.h" |
| 32 | 37 |
| 33 namespace { | 38 namespace { |
| 34 | 39 |
| 35 void EnableInternalPDFPluginForTab(TabContentsWrapper* preview_tab) { | 40 void EnableInternalPDFPluginForTab(TabContentsWrapper* preview_tab) { |
| 36 // Always enable the internal PDF plugin for the print preview page. | 41 // Always enable the internal PDF plugin for the print preview page. |
| 37 ChromePluginServiceFilter::GetInstance()->OverridePluginForTab( | 42 ChromePluginServiceFilter::GetInstance()->OverridePluginForTab( |
| 38 preview_tab->render_view_host()->process()->id(), | 43 preview_tab->render_view_host()->process()->id(), |
| 39 preview_tab->render_view_host()->routing_id(), | 44 preview_tab->render_view_host()->routing_id(), |
| 40 GURL(), | 45 GURL(), |
| 41 ASCIIToUTF16(chrome::ChromeContentClient::kPDFPluginName)); | 46 ASCIIToUTF16(chrome::ChromeContentClient::kPDFPluginName)); |
| 42 } | 47 } |
| 43 | 48 |
| 44 void ResetPreviewTabOverrideTitle(TabContentsWrapper* preview_tab) { | 49 class PrintPreviewTabDelegate : public HtmlDialogUIDelegate { |
|
kmadhusu
2011/10/13 18:52:18
Maybe this class should get moved out of pptc?
Lei Zhang
2011/10/13 21:12:12
No, I don't think this delegate makes sense for an
| |
| 45 preview_tab->print_view_manager()->ResetTitleOverride(); | 50 public: |
| 51 explicit PrintPreviewTabDelegate(TabContentsWrapper* initiator_tab); | |
| 52 virtual ~PrintPreviewTabDelegate(); | |
| 53 | |
| 54 virtual bool IsDialogModal() const OVERRIDE; | |
| 55 virtual string16 GetDialogTitle() const OVERRIDE; | |
| 56 virtual GURL GetDialogContentURL() const OVERRIDE; | |
| 57 virtual void GetWebUIMessageHandlers( | |
| 58 std::vector<WebUIMessageHandler*, | |
| 59 std::allocator<WebUIMessageHandler*> >*) const OVERRIDE; | |
| 60 virtual void GetDialogSize(gfx::Size*) const OVERRIDE; | |
| 61 virtual std::string GetDialogArgs() const OVERRIDE; | |
| 62 virtual void OnDialogClosed(const std::string&) OVERRIDE; | |
| 63 virtual void OnCloseContents(TabContents*, bool*) OVERRIDE; | |
| 64 virtual bool ShouldShowDialogTitle() const OVERRIDE; | |
| 65 | |
| 66 private: | |
| 67 gfx::Size size_; | |
| 68 | |
| 69 DISALLOW_COPY_AND_ASSIGN(PrintPreviewTabDelegate); | |
| 70 }; | |
| 71 | |
| 72 PrintPreviewTabDelegate::PrintPreviewTabDelegate( | |
| 73 TabContentsWrapper* initiator_tab) { | |
| 74 const gfx::Size min_size(800, 480); | |
| 75 const int kBorder = 50; | |
| 76 gfx::Rect rect; | |
| 77 initiator_tab->tab_contents()->GetContainerBounds(&rect); | |
| 78 size_.set_width(std::max(rect.width(), min_size.width()) - kBorder); | |
| 79 size_.set_height(std::max(rect.height(), min_size.height()) - kBorder); | |
| 80 } | |
| 81 | |
| 82 PrintPreviewTabDelegate::~PrintPreviewTabDelegate() { | |
| 83 } | |
| 84 | |
| 85 bool PrintPreviewTabDelegate::IsDialogModal() const { | |
| 86 // Not used, returning dummy value. | |
| 87 NOTREACHED(); | |
| 88 return true; | |
| 89 } | |
| 90 | |
| 91 string16 PrintPreviewTabDelegate::GetDialogTitle() const { | |
| 92 // Only used on Windows? UI folks prefer no title. | |
| 93 return string16(); | |
| 94 } | |
| 95 | |
| 96 GURL PrintPreviewTabDelegate::GetDialogContentURL() const { | |
| 97 return GURL(chrome::kChromeUIPrintURL); | |
| 98 } | |
| 99 | |
| 100 void PrintPreviewTabDelegate::GetWebUIMessageHandlers( | |
| 101 std::vector<WebUIMessageHandler*, | |
| 102 std::allocator<WebUIMessageHandler*> >* handlers) const { | |
| 103 // PrintPreviewUI adds its own message handlers. | |
| 104 } | |
| 105 | |
| 106 void PrintPreviewTabDelegate::GetDialogSize(gfx::Size* size) const { | |
| 107 *size = size_; | |
| 108 } | |
| 109 | |
| 110 std::string PrintPreviewTabDelegate::GetDialogArgs() const { | |
| 111 return std::string(); | |
| 112 } | |
| 113 | |
| 114 void PrintPreviewTabDelegate::OnDialogClosed(const std::string& /* unused */) { | |
| 115 delete this; | |
| 116 } | |
| 117 | |
| 118 void PrintPreviewTabDelegate::OnCloseContents(TabContents* /* unused */, | |
| 119 bool* /* unused */) { | |
| 120 // Not used, returning dummy value. | |
| 121 NOTREACHED(); | |
| 122 } | |
| 123 | |
| 124 bool PrintPreviewTabDelegate::ShouldShowDialogTitle() const { | |
| 125 // Not used, returning dummy value. | |
| 126 NOTREACHED(); | |
| 127 return false; | |
| 46 } | 128 } |
| 47 | 129 |
| 48 } // namespace | 130 } // namespace |
| 49 | 131 |
| 50 namespace printing { | 132 namespace printing { |
| 51 | 133 |
| 52 PrintPreviewTabController::PrintPreviewTabController() | 134 PrintPreviewTabController::PrintPreviewTabController() |
| 53 : waiting_for_new_preview_page_(false) { | 135 : waiting_for_new_preview_page_(false) { |
| 54 } | 136 } |
| 55 | 137 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 75 | 157 |
| 76 TabContentsWrapper* PrintPreviewTabController::GetOrCreatePreviewTab( | 158 TabContentsWrapper* PrintPreviewTabController::GetOrCreatePreviewTab( |
| 77 TabContentsWrapper* initiator_tab) { | 159 TabContentsWrapper* initiator_tab) { |
| 78 DCHECK(initiator_tab); | 160 DCHECK(initiator_tab); |
| 79 | 161 |
| 80 // Get the print preview tab for |initiator_tab|. | 162 // Get the print preview tab for |initiator_tab|. |
| 81 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(initiator_tab); | 163 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(initiator_tab); |
| 82 if (!preview_tab) | 164 if (!preview_tab) |
| 83 return CreatePrintPreviewTab(initiator_tab); | 165 return CreatePrintPreviewTab(initiator_tab); |
| 84 | 166 |
| 85 // Show current preview tab. | 167 // Show the initiator tab holding the existing preview tab. |
| 86 static_cast<RenderViewHostDelegate*>(preview_tab->tab_contents())->Activate(); | 168 static_cast<RenderViewHostDelegate*>( |
| 169 initiator_tab->tab_contents())->Activate(); | |
|
kmadhusu
2011/10/13 18:52:18
Is this required? We are already in initiator tab.
Lei Zhang
2011/10/13 21:12:12
One possible reason is tab A is loading, and the u
| |
| 87 return preview_tab; | 170 return preview_tab; |
| 88 } | 171 } |
| 89 | 172 |
| 90 TabContentsWrapper* PrintPreviewTabController::GetPrintPreviewForTab( | 173 TabContentsWrapper* PrintPreviewTabController::GetPrintPreviewForTab( |
| 91 TabContentsWrapper* tab) const { | 174 TabContentsWrapper* tab) const { |
| 92 // |preview_tab_map_| is keyed by the preview tab, so if find() succeeds, then | 175 // |preview_tab_map_| is keyed by the preview tab, so if find() succeeds, then |
| 93 // |tab| is the preview tab. | 176 // |tab| is the preview tab. |
| 94 PrintPreviewTabMap::const_iterator it = preview_tab_map_.find(tab); | 177 PrintPreviewTabMap::const_iterator it = preview_tab_map_.find(tab); |
| 95 if (it != preview_tab_map_.end()) | 178 if (it != preview_tab_map_.end()) |
| 96 return tab; | 179 return tab; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 } | 216 } |
| 134 default: { | 217 default: { |
| 135 NOTREACHED(); | 218 NOTREACHED(); |
| 136 break; | 219 break; |
| 137 } | 220 } |
| 138 } | 221 } |
| 139 } | 222 } |
| 140 | 223 |
| 141 void PrintPreviewTabController::OnRendererProcessClosed( | 224 void PrintPreviewTabController::OnRendererProcessClosed( |
| 142 RenderProcessHost* rph) { | 225 RenderProcessHost* rph) { |
| 226 // Store tabs in a vector and deal with them after iterating through | |
| 227 // |preview_tab_map_| becase RemoveFooTab() can change |preview_tab_map_|. | |
|
kmadhusu
2011/10/13 18:52:18
typo "because"
Lei Zhang
2011/10/13 21:12:12
Done.
| |
| 228 std::vector<TabContentsWrapper*> vec; | |
| 229 for (PrintPreviewTabMap::iterator iter = preview_tab_map_.begin(); | |
|
kmadhusu
2011/10/13 18:52:18
Rather than reusing the "vec" I would like to use
Lei Zhang
2011/10/13 21:12:12
Done.
| |
| 230 iter != preview_tab_map_.end(); ++iter) { | |
| 231 TabContentsWrapper* preview_tab = iter->first; | |
| 232 if (preview_tab->render_view_host()->process() == rph) { | |
| 233 vec.push_back(preview_tab); | |
| 234 } | |
| 235 } | |
| 236 for (size_t i = 0; i < vec.size(); ++i) { | |
|
kmadhusu
2011/10/13 18:52:18
size_t vector_len = vec.size();
for (size_t i = 0;
Lei Zhang
2011/10/13 21:12:12
I'll leave the optimization up to the compiler. We
| |
| 237 RemovePreviewTab(vec[i]); | |
| 238 PrintPreviewUI* print_preview_ui = | |
| 239 static_cast<PrintPreviewUI*>(vec[i]->web_ui()); | |
| 240 if (print_preview_ui) | |
| 241 print_preview_ui->OnPrintPreviewTabCrashed(); | |
| 242 } | |
| 243 | |
| 244 vec.clear(); | |
| 143 for (PrintPreviewTabMap::iterator iter = preview_tab_map_.begin(); | 245 for (PrintPreviewTabMap::iterator iter = preview_tab_map_.begin(); |
| 144 iter != preview_tab_map_.end(); ++iter) { | 246 iter != preview_tab_map_.end(); ++iter) { |
| 145 TabContentsWrapper* initiator_tab = iter->second; | 247 TabContentsWrapper* initiator_tab = iter->second; |
| 146 if (initiator_tab && | 248 if (initiator_tab && initiator_tab->render_view_host()->process() == rph) { |
| 147 initiator_tab->render_view_host()->process() == rph) { | 249 vec.push_back(initiator_tab); |
| 148 TabContentsWrapper* preview_tab = iter->first; | |
| 149 PrintPreviewUI* print_preview_ui = | |
| 150 static_cast<PrintPreviewUI*>(preview_tab->web_ui()); | |
| 151 print_preview_ui->OnInitiatorTabCrashed(); | |
| 152 } | 250 } |
| 153 } | 251 } |
| 252 for (size_t i = 0; i < vec.size(); ++i) | |
| 253 RemoveInitiatorTab(vec[i]); | |
| 154 } | 254 } |
| 155 | 255 |
| 156 void PrintPreviewTabController::OnTabContentsDestroyed( | 256 void PrintPreviewTabController::OnTabContentsDestroyed( |
| 157 TabContentsWrapper* tab) { | 257 TabContentsWrapper* tab) { |
| 158 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(tab); | 258 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(tab); |
| 159 if (!preview_tab) | 259 if (!preview_tab) { |
| 260 NOTREACHED(); | |
| 160 return; | 261 return; |
| 161 | |
| 162 if (tab == preview_tab) { | |
| 163 // Remove the initiator tab's observers before erasing the mapping. | |
| 164 TabContentsWrapper* initiator_tab = GetInitiatorTab(tab); | |
| 165 if (initiator_tab) | |
| 166 RemoveObservers(initiator_tab); | |
| 167 | |
| 168 // Print preview tab contents are destroyed. Notify |PrintPreviewUI| to | |
| 169 // abort the initiator tab preview request. | |
| 170 if (IsPrintPreviewTab(tab) && tab->web_ui()) { | |
| 171 PrintPreviewUI* print_preview_ui = | |
| 172 static_cast<PrintPreviewUI*>(tab->web_ui()); | |
| 173 print_preview_ui->OnTabDestroyed(); | |
| 174 } | |
| 175 | |
| 176 // Erase the map entry. | |
| 177 preview_tab_map_.erase(tab); | |
| 178 } else { | |
| 179 // Initiator tab is closed. Disable the controls in preview tab. | |
| 180 if (preview_tab->web_ui()) { | |
| 181 PrintPreviewUI* print_preview_ui = | |
| 182 static_cast<PrintPreviewUI*>(preview_tab->web_ui()); | |
| 183 print_preview_ui->OnInitiatorTabClosed(); | |
| 184 } | |
| 185 | |
| 186 // |tab| is an initiator tab, update the map entry and remove observers. | |
| 187 preview_tab_map_[preview_tab] = NULL; | |
| 188 } | 262 } |
| 189 | 263 |
| 190 ResetPreviewTabOverrideTitle(preview_tab); | 264 if (tab == preview_tab) |
| 191 RemoveObservers(tab); | 265 RemovePreviewTab(tab); |
| 266 else | |
| 267 RemoveInitiatorTab(tab); | |
| 192 } | 268 } |
| 193 | 269 |
| 194 void PrintPreviewTabController::OnNavEntryCommitted( | 270 void PrintPreviewTabController::OnNavEntryCommitted( |
| 195 TabContentsWrapper* tab, content::LoadCommittedDetails* details) { | 271 TabContentsWrapper* tab, content::LoadCommittedDetails* details) { |
| 196 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(tab); | 272 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(tab); |
| 273 if (!preview_tab) { | |
| 274 NOTREACHED(); | |
| 275 return; | |
| 276 } | |
| 197 bool source_tab_is_preview_tab = (tab == preview_tab); | 277 bool source_tab_is_preview_tab = (tab == preview_tab); |
| 198 if (details) { | |
| 199 content::PageTransition transition_type = details->entry->transition_type(); | |
| 200 NavigationType::Type nav_type = details->type; | |
| 201 | 278 |
| 202 // Don't update/erase the map entry if the page has not changed. | 279 if (source_tab_is_preview_tab) { |
| 203 if (transition_type == content::PAGE_TRANSITION_RELOAD || | 280 // Preview tab navigated. |
| 204 nav_type == NavigationType::SAME_PAGE) { | 281 if (details) { |
| 205 if (source_tab_is_preview_tab) | 282 content::PageTransition transition_type = |
| 283 details->entry->transition_type(); | |
| 284 NavigationType::Type nav_type = details->type; | |
| 285 | |
| 286 // New |preview_tab| is created. Don't update/erase map entry. | |
| 287 if (waiting_for_new_preview_page_ && | |
| 288 transition_type == content::PAGE_TRANSITION_START_PAGE && | |
| 289 nav_type == NavigationType::NEW_PAGE && | |
| 290 source_tab_is_preview_tab) { | |
|
kmadhusu
2011/10/13 18:52:18
"source_tab_is_preview_tab" check is not required
Lei Zhang
2011/10/13 21:12:12
Done.
| |
| 291 waiting_for_new_preview_page_ = false; | |
| 206 SetInitiatorTabURLAndTitle(preview_tab); | 292 SetInitiatorTabURLAndTitle(preview_tab); |
| 207 return; | 293 return; |
| 294 } | |
| 208 } | 295 } |
| 209 | 296 NOTREACHED(); |
| 210 // New |preview_tab| is created. Don't update/erase map entry. | 297 return; |
| 211 if (waiting_for_new_preview_page_ && | |
| 212 transition_type == content::PAGE_TRANSITION_LINK && | |
| 213 nav_type == NavigationType::NEW_PAGE && | |
| 214 source_tab_is_preview_tab) { | |
| 215 waiting_for_new_preview_page_ = false; | |
| 216 SetInitiatorTabURLAndTitle(preview_tab); | |
| 217 return; | |
| 218 } | |
| 219 | |
| 220 // User navigated to a preview tab using forward/back button. | |
| 221 if (source_tab_is_preview_tab && | |
| 222 transition_type == content::PAGE_TRANSITION_FORWARD_BACK && | |
| 223 nav_type == NavigationType::EXISTING_PAGE) { | |
| 224 return; | |
| 225 } | |
| 226 } | 298 } |
| 227 | 299 |
| 228 RemoveObservers(tab); | 300 // Initiator tab navigated. |
| 229 ResetPreviewTabOverrideTitle(preview_tab); | 301 RemoveInitiatorTab(tab); |
| 230 if (source_tab_is_preview_tab) { | |
| 231 // Remove the initiator tab's observers before erasing the mapping. | |
| 232 TabContentsWrapper* initiator_tab = GetInitiatorTab(tab); | |
| 233 if (initiator_tab) | |
| 234 RemoveObservers(initiator_tab); | |
| 235 preview_tab_map_.erase(tab); | |
| 236 } else { | |
| 237 preview_tab_map_[preview_tab] = NULL; | |
| 238 | |
| 239 // Initiator tab is closed. Disable the controls in preview tab. | |
| 240 if (preview_tab->web_ui()) { | |
| 241 PrintPreviewUI* print_preview_ui = | |
| 242 static_cast<PrintPreviewUI*>(preview_tab->web_ui()); | |
| 243 print_preview_ui->OnInitiatorTabClosed(); | |
| 244 } | |
| 245 } | |
| 246 } | 302 } |
| 247 | 303 |
| 248 // static | 304 // static |
| 249 bool PrintPreviewTabController::IsPrintPreviewTab(TabContentsWrapper* tab) { | 305 bool PrintPreviewTabController::IsPrintPreviewTab(TabContentsWrapper* tab) { |
| 250 return IsPrintPreviewURL(tab->tab_contents()->GetURL()); | 306 return IsPrintPreviewURL(tab->tab_contents()->GetURL()); |
| 251 } | 307 } |
| 252 | 308 |
| 253 // static | 309 // static |
| 254 bool PrintPreviewTabController::IsPrintPreviewURL(const GURL& url) { | 310 bool PrintPreviewTabController::IsPrintPreviewURL(const GURL& url) { |
| 255 return (url.SchemeIs(chrome::kChromeUIScheme) && | 311 return (url.SchemeIs(chrome::kChromeUIScheme) && |
| 256 url.host() == chrome::kChromeUIPrintHost); | 312 url.host() == chrome::kChromeUIPrintHost); |
| 257 } | 313 } |
| 258 | 314 |
| 259 void PrintPreviewTabController::EraseInitiatorTabInfo( | 315 void PrintPreviewTabController::EraseInitiatorTabInfo( |
| 260 TabContentsWrapper* preview_tab) { | 316 TabContentsWrapper* preview_tab) { |
| 261 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); | 317 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); |
| 262 if (it == preview_tab_map_.end()) | 318 if (it == preview_tab_map_.end()) |
| 263 return; | 319 return; |
| 264 | 320 |
| 265 RemoveObservers(it->second); | 321 RemoveObservers(it->second); |
| 266 preview_tab_map_[preview_tab] = NULL; | 322 preview_tab_map_[preview_tab] = NULL; |
| 267 ResetPreviewTabOverrideTitle(preview_tab); | |
| 268 } | 323 } |
| 269 | 324 |
| 270 TabContentsWrapper* PrintPreviewTabController::GetInitiatorTab( | 325 TabContentsWrapper* PrintPreviewTabController::GetInitiatorTab( |
| 271 TabContentsWrapper* preview_tab) { | 326 TabContentsWrapper* preview_tab) { |
| 272 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); | 327 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); |
| 273 if (it != preview_tab_map_.end()) | 328 if (it != preview_tab_map_.end()) |
| 274 return preview_tab_map_[preview_tab]; | 329 return preview_tab_map_[preview_tab]; |
| 275 return NULL; | 330 return NULL; |
| 276 } | 331 } |
| 277 | 332 |
| 278 TabContentsWrapper* PrintPreviewTabController::CreatePrintPreviewTab( | 333 TabContentsWrapper* PrintPreviewTabController::CreatePrintPreviewTab( |
| 279 TabContentsWrapper* initiator_tab) { | 334 TabContentsWrapper* initiator_tab) { |
| 280 Browser* current_browser = BrowserList::FindBrowserWithID( | 335 Browser* current_browser = BrowserList::FindBrowserWithID( |
| 281 initiator_tab->restore_tab_helper()->window_id().id()); | 336 initiator_tab->restore_tab_helper()->window_id().id()); |
| 282 if (!current_browser) { | 337 if (!current_browser) { |
| 283 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) { | 338 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) { |
| 284 Profile* profile = Profile::FromBrowserContext( | 339 Profile* profile = Profile::FromBrowserContext( |
| 285 initiator_tab->tab_contents()->browser_context()); | 340 initiator_tab->tab_contents()->browser_context()); |
| 286 current_browser = Browser::CreateForType(Browser::TYPE_POPUP, profile); | 341 current_browser = Browser::CreateForType(Browser::TYPE_POPUP, profile); |
| 287 if (!current_browser) { | 342 if (!current_browser) { |
| 288 NOTREACHED() << "Failed to create popup browser window"; | 343 NOTREACHED() << "Failed to create popup browser window"; |
| 289 return NULL; | 344 return NULL; |
| 290 } | 345 } |
| 291 } else { | 346 } else { |
| 292 return NULL; | 347 return NULL; |
| 293 } | 348 } |
| 294 } | 349 } |
| 295 | 350 |
| 296 // Add a new tab next to initiator tab. | 351 HtmlDialogUIDelegate* delegate = new PrintPreviewTabDelegate(initiator_tab); |
| 297 browser::NavigateParams params(current_browser, | 352 ConstrainedHtmlUIDelegate* html_delegate = |
| 298 GURL(chrome::kChromeUIPrintURL), | 353 ConstrainedHtmlUI::CreateConstrainedHtmlDialog(current_browser->profile(), |
| 299 content::PAGE_TRANSITION_LINK); | 354 delegate, |
| 300 params.disposition = NEW_FOREGROUND_TAB; | 355 initiator_tab); |
| 301 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) | 356 TabContentsWrapper* preview_tab = html_delegate->tab(); |
| 302 params.disposition = NEW_POPUP; | |
| 303 | |
| 304 // For normal tabs, set the position as immediately to the right, | |
| 305 // otherwise let the tab strip decide. | |
| 306 if (current_browser->is_type_tabbed()) { | |
| 307 params.tabstrip_index = current_browser->tabstrip_model()-> | |
| 308 GetIndexOfTabContents(initiator_tab) + 1; | |
| 309 } | |
| 310 | |
| 311 browser::Navigate(¶ms); | |
| 312 TabContentsWrapper* preview_tab = params.target_contents; | |
| 313 EnableInternalPDFPluginForTab(preview_tab); | 357 EnableInternalPDFPluginForTab(preview_tab); |
| 314 static_cast<RenderViewHostDelegate*>(preview_tab->tab_contents())->Activate(); | |
| 315 | 358 |
| 316 // Add an entry to the map. | 359 // Add an entry to the map. |
| 317 preview_tab_map_[preview_tab] = initiator_tab; | 360 preview_tab_map_[preview_tab] = initiator_tab; |
| 318 waiting_for_new_preview_page_ = true; | 361 waiting_for_new_preview_page_ = true; |
| 319 | 362 |
| 320 AddObservers(initiator_tab); | 363 AddObservers(initiator_tab); |
| 321 AddObservers(preview_tab); | 364 AddObservers(preview_tab); |
| 322 | 365 |
| 323 return preview_tab; | 366 return preview_tab; |
| 324 } | 367 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 364 // notification has already been added. | 407 // notification has already been added. |
| 365 RenderProcessHost* rph = tab->render_view_host()->process(); | 408 RenderProcessHost* rph = tab->render_view_host()->process(); |
| 366 if (registrar_.IsRegistered(this, | 409 if (registrar_.IsRegistered(this, |
| 367 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 410 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 368 Source<RenderProcessHost>(rph))) { | 411 Source<RenderProcessHost>(rph))) { |
| 369 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 412 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 370 Source<RenderProcessHost>(rph)); | 413 Source<RenderProcessHost>(rph)); |
| 371 } | 414 } |
| 372 } | 415 } |
| 373 | 416 |
| 417 void PrintPreviewTabController::RemoveInitiatorTab( | |
| 418 TabContentsWrapper* initiator_tab) { | |
| 419 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(initiator_tab); | |
| 420 // Update the map entry first, so when the print preview tab gets destroyed | |
| 421 // and reaches RemovePreviewTab(), it does not attempt to also remove the | |
| 422 // initiator tab's observers. | |
| 423 preview_tab_map_[preview_tab] = NULL; | |
| 424 RemoveObservers(initiator_tab); | |
| 425 | |
| 426 initiator_tab->print_view_manager()->PrintPreviewDone(); | |
| 427 | |
| 428 // Initiator tab is closed. Close the print preview tab too. | |
| 429 PrintPreviewUI* print_preview_ui = | |
| 430 static_cast<PrintPreviewUI*>(preview_tab->web_ui()); | |
| 431 if (print_preview_ui) | |
| 432 print_preview_ui->OnInitiatorTabClosed(); | |
| 433 } | |
| 434 | |
| 435 void PrintPreviewTabController::RemovePreviewTab( | |
| 436 TabContentsWrapper* preview_tab) { | |
| 437 // Remove the initiator tab's observers before erasing the mapping. | |
| 438 TabContentsWrapper* initiator_tab = GetInitiatorTab(preview_tab); | |
| 439 if (initiator_tab) { | |
| 440 initiator_tab->print_view_manager()->PrintPreviewDone(); | |
| 441 RemoveObservers(initiator_tab); | |
| 442 } | |
| 443 | |
| 444 // Print preview TabContents is destroyed. Notify |PrintPreviewUI| to abort | |
| 445 // the initiator tab preview request. | |
| 446 if (IsPrintPreviewTab(preview_tab) && preview_tab->web_ui()) { | |
|
kmadhusu
2011/10/13 18:52:18
We already know that this is a print preview tab.
Lei Zhang
2011/10/13 21:12:12
This was originally added for the case where the p
| |
| 447 PrintPreviewUI* print_preview_ui = | |
| 448 static_cast<PrintPreviewUI*>(preview_tab->web_ui()); | |
| 449 print_preview_ui->OnTabDestroyed(); | |
| 450 } | |
| 451 | |
| 452 preview_tab_map_.erase(preview_tab); | |
| 453 RemoveObservers(preview_tab); | |
| 454 } | |
| 455 | |
| 374 } // namespace printing | 456 } // namespace printing |
| OLD | NEW |