| 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/public/browser/notification_details.h" | 33 #include "content/public/browser/notification_details.h" |
| 29 #include "content/public/browser/notification_source.h" | 34 #include "content/public/browser/notification_source.h" |
| 30 #include "content/public/browser/notification_types.h" | 35 #include "content/public/browser/notification_types.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 { |
| 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*>* handlers) const OVERRIDE; |
| 59 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE; |
| 60 virtual std::string GetDialogArgs() const OVERRIDE; |
| 61 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE; |
| 62 virtual void OnCloseContents(TabContents* source, |
| 63 bool* out_close_dialog) 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 kMinDialogSize(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(), kMinDialogSize.width()) - kBorder); |
| 79 size_.set_height(std::max(rect.height(), kMinDialogSize.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*>* /* handlers */) const { |
| 102 // PrintPreviewUI adds its own message handlers. |
| 103 } |
| 104 |
| 105 void PrintPreviewTabDelegate::GetDialogSize(gfx::Size* size) const { |
| 106 *size = size_; |
| 107 } |
| 108 |
| 109 std::string PrintPreviewTabDelegate::GetDialogArgs() const { |
| 110 return std::string(); |
| 111 } |
| 112 |
| 113 void PrintPreviewTabDelegate::OnDialogClosed( |
| 114 const std::string& /* json_retval */) { |
| 115 delete this; |
| 116 } |
| 117 |
| 118 void PrintPreviewTabDelegate::OnCloseContents(TabContents* /* source */, |
| 119 bool* /* out_close_dialog */) { |
| 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(); |
| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 } | 217 } |
| 135 default: { | 218 default: { |
| 136 NOTREACHED(); | 219 NOTREACHED(); |
| 137 break; | 220 break; |
| 138 } | 221 } |
| 139 } | 222 } |
| 140 } | 223 } |
| 141 | 224 |
| 142 void PrintPreviewTabController::OnRendererProcessClosed( | 225 void PrintPreviewTabController::OnRendererProcessClosed( |
| 143 RenderProcessHost* rph) { | 226 RenderProcessHost* rph) { |
| 227 // Store tabs in a vector and deal with them after iterating through |
| 228 // |preview_tab_map_| because RemoveFooTab() can change |preview_tab_map_|. |
| 229 std::vector<TabContentsWrapper*> closed_initiator_tabs; |
| 230 std::vector<TabContentsWrapper*> closed_preview_tabs; |
| 144 for (PrintPreviewTabMap::iterator iter = preview_tab_map_.begin(); | 231 for (PrintPreviewTabMap::iterator iter = preview_tab_map_.begin(); |
| 145 iter != preview_tab_map_.end(); ++iter) { | 232 iter != preview_tab_map_.end(); ++iter) { |
| 233 TabContentsWrapper* preview_tab = iter->first; |
| 146 TabContentsWrapper* initiator_tab = iter->second; | 234 TabContentsWrapper* initiator_tab = iter->second; |
| 147 if (initiator_tab && | 235 if (preview_tab->render_view_host()->process() == rph) { |
| 148 initiator_tab->render_view_host()->process() == rph) { | 236 closed_preview_tabs.push_back(preview_tab); |
| 149 TabContentsWrapper* preview_tab = iter->first; | 237 } else if (initiator_tab && |
| 150 PrintPreviewUI* print_preview_ui = | 238 initiator_tab->render_view_host()->process() == rph) { |
| 151 static_cast<PrintPreviewUI*>(preview_tab->web_ui()); | 239 closed_initiator_tabs.push_back(initiator_tab); |
| 152 print_preview_ui->OnInitiatorTabCrashed(); | |
| 153 } | 240 } |
| 154 } | 241 } |
| 242 |
| 243 for (size_t i = 0; i < closed_preview_tabs.size(); ++i) { |
| 244 RemovePreviewTab(closed_preview_tabs[i]); |
| 245 PrintPreviewUI* print_preview_ui = |
| 246 static_cast<PrintPreviewUI*>(closed_preview_tabs[i]->web_ui()); |
| 247 if (print_preview_ui) |
| 248 print_preview_ui->OnPrintPreviewTabClosed(); |
| 249 } |
| 250 |
| 251 for (size_t i = 0; i < closed_initiator_tabs.size(); ++i) |
| 252 RemoveInitiatorTab(closed_initiator_tabs[i]); |
| 155 } | 253 } |
| 156 | 254 |
| 157 void PrintPreviewTabController::OnTabContentsDestroyed( | 255 void PrintPreviewTabController::OnTabContentsDestroyed( |
| 158 TabContentsWrapper* tab) { | 256 TabContentsWrapper* tab) { |
| 159 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(tab); | 257 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(tab); |
| 160 if (!preview_tab) | 258 if (!preview_tab) { |
| 259 NOTREACHED(); |
| 161 return; | 260 return; |
| 162 | |
| 163 if (tab == preview_tab) { | |
| 164 // Remove the initiator tab's observers before erasing the mapping. | |
| 165 TabContentsWrapper* initiator_tab = GetInitiatorTab(tab); | |
| 166 if (initiator_tab) | |
| 167 RemoveObservers(initiator_tab); | |
| 168 | |
| 169 // Print preview tab contents are destroyed. Notify |PrintPreviewUI| to | |
| 170 // abort the initiator tab preview request. | |
| 171 if (IsPrintPreviewTab(tab) && tab->web_ui()) { | |
| 172 PrintPreviewUI* print_preview_ui = | |
| 173 static_cast<PrintPreviewUI*>(tab->web_ui()); | |
| 174 print_preview_ui->OnTabDestroyed(); | |
| 175 } | |
| 176 | |
| 177 // Erase the map entry. | |
| 178 preview_tab_map_.erase(tab); | |
| 179 } else { | |
| 180 // Initiator tab is closed. Disable the controls in preview tab. | |
| 181 if (preview_tab->web_ui()) { | |
| 182 PrintPreviewUI* print_preview_ui = | |
| 183 static_cast<PrintPreviewUI*>(preview_tab->web_ui()); | |
| 184 print_preview_ui->OnInitiatorTabClosed(); | |
| 185 } | |
| 186 | |
| 187 // |tab| is an initiator tab, update the map entry and remove observers. | |
| 188 preview_tab_map_[preview_tab] = NULL; | |
| 189 } | 261 } |
| 190 | 262 |
| 191 ResetPreviewTabOverrideTitle(preview_tab); | 263 if (tab == preview_tab) |
| 192 RemoveObservers(tab); | 264 RemovePreviewTab(tab); |
| 265 else |
| 266 RemoveInitiatorTab(tab); |
| 193 } | 267 } |
| 194 | 268 |
| 195 void PrintPreviewTabController::OnNavEntryCommitted( | 269 void PrintPreviewTabController::OnNavEntryCommitted( |
| 196 TabContentsWrapper* tab, content::LoadCommittedDetails* details) { | 270 TabContentsWrapper* tab, content::LoadCommittedDetails* details) { |
| 197 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(tab); | 271 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(tab); |
| 272 if (!preview_tab) { |
| 273 NOTREACHED(); |
| 274 return; |
| 275 } |
| 198 bool source_tab_is_preview_tab = (tab == preview_tab); | 276 bool source_tab_is_preview_tab = (tab == preview_tab); |
| 199 if (details) { | |
| 200 content::PageTransition transition_type = details->entry->transition_type(); | |
| 201 content::NavigationType nav_type = details->type; | |
| 202 | 277 |
| 203 // Don't update/erase the map entry if the page has not changed. | 278 if (source_tab_is_preview_tab) { |
| 204 if (transition_type == content::PAGE_TRANSITION_RELOAD || | 279 // Preview tab navigated. |
| 205 nav_type == content::NAVIGATION_TYPE_SAME_PAGE) { | 280 if (details) { |
| 206 if (source_tab_is_preview_tab) | 281 content::PageTransition transition_type = |
| 282 details->entry->transition_type(); |
| 283 content::NavigationType nav_type = details->type; |
| 284 |
| 285 // New |preview_tab| is created. Don't update/erase map entry. |
| 286 if (waiting_for_new_preview_page_ && |
| 287 transition_type == content::PAGE_TRANSITION_START_PAGE && |
| 288 nav_type == content::NAVIGATION_TYPE_NEW_PAGE) { |
| 289 waiting_for_new_preview_page_ = false; |
| 207 SetInitiatorTabURLAndTitle(preview_tab); | 290 SetInitiatorTabURLAndTitle(preview_tab); |
| 208 return; | 291 |
| 292 // Disabling the delegate will prevent all future navigation. |
| 293 tab->tab_contents()->set_delegate(NULL); |
| 294 return; |
| 295 } |
| 296 |
| 297 // Cloud print sign-in causes a reload. |
| 298 if (!waiting_for_new_preview_page_ && |
| 299 transition_type == content::PAGE_TRANSITION_RELOAD && |
| 300 nav_type == content::NAVIGATION_TYPE_EXISTING_PAGE && |
| 301 IsPrintPreviewURL(details->previous_url)) { |
| 302 return; |
| 303 } |
| 209 } | 304 } |
| 210 | 305 NOTREACHED(); |
| 211 // New |preview_tab| is created. Don't update/erase map entry. | 306 return; |
| 212 if (waiting_for_new_preview_page_ && | |
| 213 transition_type == content::PAGE_TRANSITION_LINK && | |
| 214 nav_type == content::NAVIGATION_TYPE_NEW_PAGE && | |
| 215 source_tab_is_preview_tab) { | |
| 216 waiting_for_new_preview_page_ = false; | |
| 217 SetInitiatorTabURLAndTitle(preview_tab); | |
| 218 return; | |
| 219 } | |
| 220 | |
| 221 // User navigated to a preview tab using forward/back button. | |
| 222 if (source_tab_is_preview_tab && | |
| 223 transition_type == content::PAGE_TRANSITION_FORWARD_BACK && | |
| 224 nav_type == content::NAVIGATION_TYPE_EXISTING_PAGE) { | |
| 225 return; | |
| 226 } | |
| 227 } | 307 } |
| 228 | 308 |
| 229 RemoveObservers(tab); | 309 // Initiator tab navigated. |
| 230 ResetPreviewTabOverrideTitle(preview_tab); | 310 RemoveInitiatorTab(tab); |
| 231 if (source_tab_is_preview_tab) { | |
| 232 // Remove the initiator tab's observers before erasing the mapping. | |
| 233 TabContentsWrapper* initiator_tab = GetInitiatorTab(tab); | |
| 234 if (initiator_tab) | |
| 235 RemoveObservers(initiator_tab); | |
| 236 preview_tab_map_.erase(tab); | |
| 237 } else { | |
| 238 preview_tab_map_[preview_tab] = NULL; | |
| 239 | |
| 240 // Initiator tab is closed. Disable the controls in preview tab. | |
| 241 if (preview_tab->web_ui()) { | |
| 242 PrintPreviewUI* print_preview_ui = | |
| 243 static_cast<PrintPreviewUI*>(preview_tab->web_ui()); | |
| 244 print_preview_ui->OnInitiatorTabClosed(); | |
| 245 } | |
| 246 } | |
| 247 } | 311 } |
| 248 | 312 |
| 249 // static | 313 // static |
| 250 bool PrintPreviewTabController::IsPrintPreviewTab(TabContentsWrapper* tab) { | 314 bool PrintPreviewTabController::IsPrintPreviewTab(TabContentsWrapper* tab) { |
| 251 return IsPrintPreviewURL(tab->tab_contents()->GetURL()); | 315 return IsPrintPreviewURL(tab->tab_contents()->GetURL()); |
| 252 } | 316 } |
| 253 | 317 |
| 254 // static | 318 // static |
| 255 bool PrintPreviewTabController::IsPrintPreviewURL(const GURL& url) { | 319 bool PrintPreviewTabController::IsPrintPreviewURL(const GURL& url) { |
| 256 return (url.SchemeIs(chrome::kChromeUIScheme) && | 320 return (url.SchemeIs(chrome::kChromeUIScheme) && |
| 257 url.host() == chrome::kChromeUIPrintHost); | 321 url.host() == chrome::kChromeUIPrintHost); |
| 258 } | 322 } |
| 259 | 323 |
| 260 void PrintPreviewTabController::EraseInitiatorTabInfo( | 324 void PrintPreviewTabController::EraseInitiatorTabInfo( |
| 261 TabContentsWrapper* preview_tab) { | 325 TabContentsWrapper* preview_tab) { |
| 262 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); | 326 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); |
| 263 if (it == preview_tab_map_.end()) | 327 if (it == preview_tab_map_.end()) |
| 264 return; | 328 return; |
| 265 | 329 |
| 266 RemoveObservers(it->second); | 330 RemoveObservers(it->second); |
| 267 preview_tab_map_[preview_tab] = NULL; | 331 preview_tab_map_[preview_tab] = NULL; |
| 268 ResetPreviewTabOverrideTitle(preview_tab); | |
| 269 } | 332 } |
| 270 | 333 |
| 271 TabContentsWrapper* PrintPreviewTabController::GetInitiatorTab( | 334 TabContentsWrapper* PrintPreviewTabController::GetInitiatorTab( |
| 272 TabContentsWrapper* preview_tab) { | 335 TabContentsWrapper* preview_tab) { |
| 273 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); | 336 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); |
| 274 if (it != preview_tab_map_.end()) | 337 if (it != preview_tab_map_.end()) |
| 275 return preview_tab_map_[preview_tab]; | 338 return preview_tab_map_[preview_tab]; |
| 276 return NULL; | 339 return NULL; |
| 277 } | 340 } |
| 278 | 341 |
| 279 TabContentsWrapper* PrintPreviewTabController::CreatePrintPreviewTab( | 342 TabContentsWrapper* PrintPreviewTabController::CreatePrintPreviewTab( |
| 280 TabContentsWrapper* initiator_tab) { | 343 TabContentsWrapper* initiator_tab) { |
| 281 Browser* current_browser = BrowserList::FindBrowserWithID( | 344 Browser* current_browser = BrowserList::FindBrowserWithID( |
| 282 initiator_tab->restore_tab_helper()->window_id().id()); | 345 initiator_tab->restore_tab_helper()->window_id().id()); |
| 283 if (!current_browser) { | 346 if (!current_browser) { |
| 284 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) { | 347 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) { |
| 285 Profile* profile = Profile::FromBrowserContext( | 348 Profile* profile = Profile::FromBrowserContext( |
| 286 initiator_tab->tab_contents()->browser_context()); | 349 initiator_tab->tab_contents()->browser_context()); |
| 287 current_browser = Browser::CreateForType(Browser::TYPE_POPUP, profile); | 350 current_browser = Browser::CreateForType(Browser::TYPE_POPUP, profile); |
| 288 if (!current_browser) { | 351 if (!current_browser) { |
| 289 NOTREACHED() << "Failed to create popup browser window"; | 352 NOTREACHED() << "Failed to create popup browser window"; |
| 290 return NULL; | 353 return NULL; |
| 291 } | 354 } |
| 292 } else { | 355 } else { |
| 293 return NULL; | 356 return NULL; |
| 294 } | 357 } |
| 295 } | 358 } |
| 296 | 359 |
| 297 // Add a new tab next to initiator tab. | 360 HtmlDialogUIDelegate* delegate = new PrintPreviewTabDelegate(initiator_tab); |
| 298 browser::NavigateParams params(current_browser, | 361 ConstrainedHtmlUIDelegate* html_delegate = |
| 299 GURL(chrome::kChromeUIPrintURL), | 362 ConstrainedHtmlUI::CreateConstrainedHtmlDialog(current_browser->profile(), |
| 300 content::PAGE_TRANSITION_LINK); | 363 delegate, |
| 301 params.disposition = NEW_FOREGROUND_TAB; | 364 initiator_tab); |
| 302 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) | 365 TabContentsWrapper* preview_tab = html_delegate->tab(); |
| 303 params.disposition = NEW_POPUP; | |
| 304 | |
| 305 // For normal tabs, set the position as immediately to the right, | |
| 306 // otherwise let the tab strip decide. | |
| 307 if (current_browser->is_type_tabbed()) { | |
| 308 params.tabstrip_index = current_browser->tabstrip_model()-> | |
| 309 GetIndexOfTabContents(initiator_tab) + 1; | |
| 310 } | |
| 311 | |
| 312 browser::Navigate(¶ms); | |
| 313 TabContentsWrapper* preview_tab = params.target_contents; | |
| 314 EnableInternalPDFPluginForTab(preview_tab); | 366 EnableInternalPDFPluginForTab(preview_tab); |
| 315 static_cast<RenderViewHostDelegate*>(preview_tab->tab_contents())->Activate(); | |
| 316 | 367 |
| 317 // Add an entry to the map. | 368 // Add an entry to the map. |
| 318 preview_tab_map_[preview_tab] = initiator_tab; | 369 preview_tab_map_[preview_tab] = initiator_tab; |
| 319 waiting_for_new_preview_page_ = true; | 370 waiting_for_new_preview_page_ = true; |
| 320 | 371 |
| 321 AddObservers(initiator_tab); | 372 AddObservers(initiator_tab); |
| 322 AddObservers(preview_tab); | 373 AddObservers(preview_tab); |
| 323 | 374 |
| 324 return preview_tab; | 375 return preview_tab; |
| 325 } | 376 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 // notification has already been added. | 418 // notification has already been added. |
| 368 RenderProcessHost* rph = tab->render_view_host()->process(); | 419 RenderProcessHost* rph = tab->render_view_host()->process(); |
| 369 if (registrar_.IsRegistered(this, | 420 if (registrar_.IsRegistered(this, |
| 370 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 421 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 371 content::Source<RenderProcessHost>(rph))) { | 422 content::Source<RenderProcessHost>(rph))) { |
| 372 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 423 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 373 content::Source<RenderProcessHost>(rph)); | 424 content::Source<RenderProcessHost>(rph)); |
| 374 } | 425 } |
| 375 } | 426 } |
| 376 | 427 |
| 428 void PrintPreviewTabController::RemoveInitiatorTab( |
| 429 TabContentsWrapper* initiator_tab) { |
| 430 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(initiator_tab); |
| 431 DCHECK(preview_tab); |
| 432 // Update the map entry first, so when the print preview tab gets destroyed |
| 433 // and reaches RemovePreviewTab(), it does not attempt to also remove the |
| 434 // initiator tab's observers. |
| 435 preview_tab_map_[preview_tab] = NULL; |
| 436 RemoveObservers(initiator_tab); |
| 437 |
| 438 // Initiator tab is closed. Close the print preview tab too. |
| 439 PrintPreviewUI* print_preview_ui = |
| 440 static_cast<PrintPreviewUI*>(preview_tab->web_ui()); |
| 441 if (print_preview_ui) |
| 442 print_preview_ui->OnInitiatorTabClosed(); |
| 443 } |
| 444 |
| 445 void PrintPreviewTabController::RemovePreviewTab( |
| 446 TabContentsWrapper* preview_tab) { |
| 447 // Remove the initiator tab's observers before erasing the mapping. |
| 448 TabContentsWrapper* initiator_tab = GetInitiatorTab(preview_tab); |
| 449 if (initiator_tab) |
| 450 RemoveObservers(initiator_tab); |
| 451 |
| 452 // Print preview TabContents is destroyed. Notify |PrintPreviewUI| to abort |
| 453 // the initiator tab preview request. |
| 454 PrintPreviewUI* print_preview_ui = |
| 455 static_cast<PrintPreviewUI*>(preview_tab->web_ui()); |
| 456 if (print_preview_ui) |
| 457 print_preview_ui->OnTabDestroyed(); |
| 458 |
| 459 preview_tab_map_.erase(preview_tab); |
| 460 RemoveObservers(preview_tab); |
| 461 } |
| 462 |
| 377 } // namespace printing | 463 } // namespace printing |
| OLD | NEW |