| 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> | |
| 10 #include <vector> | 7 #include <vector> |
| 11 | 8 |
| 12 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 13 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/chrome_plugin_service_filter.h" | 12 #include "chrome/browser/chrome_plugin_service_filter.h" |
| 16 #include "chrome/browser/printing/print_view_manager.h" | 13 #include "chrome/browser/printing/print_view_manager.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/sessions/restore_tab_helper.h" | 15 #include "chrome/browser/sessions/restore_tab_helper.h" |
| 19 #include "chrome/browser/tabs/tab_strip_model.h" | 16 #include "chrome/browser/tabs/tab_strip_model.h" |
| 20 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
| 21 #include "chrome/browser/ui/browser_list.h" | 18 #include "chrome/browser/ui/browser_list.h" |
| 22 #include "chrome/browser/ui/browser_navigator.h" | 19 #include "chrome/browser/ui/browser_navigator.h" |
| 23 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 20 #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" | |
| 26 #include "chrome/browser/ui/webui/print_preview_ui.h" | 21 #include "chrome/browser/ui/webui/print_preview_ui.h" |
| 27 #include "chrome/common/chrome_content_client.h" | 22 #include "chrome/common/chrome_content_client.h" |
| 28 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
| 29 #include "chrome/common/url_constants.h" | 24 #include "chrome/common/url_constants.h" |
| 30 #include "content/browser/renderer_host/render_view_host.h" | 25 #include "content/browser/renderer_host/render_view_host.h" |
| 31 #include "content/browser/tab_contents/navigation_details.h" | 26 #include "content/browser/tab_contents/navigation_details.h" |
| 32 #include "content/browser/tab_contents/tab_contents.h" | 27 #include "content/browser/tab_contents/tab_contents.h" |
| 33 #include "content/public/browser/notification_details.h" | 28 #include "content/public/browser/notification_details.h" |
| 34 #include "content/public/browser/notification_source.h" | 29 #include "content/public/browser/notification_source.h" |
| 35 #include "content/public/browser/notification_types.h" | 30 #include "content/public/browser/notification_types.h" |
| 36 #include "webkit/plugins/webplugininfo.h" | 31 #include "webkit/plugins/webplugininfo.h" |
| 37 | 32 |
| 38 namespace { | 33 namespace { |
| 39 | 34 |
| 40 void EnableInternalPDFPluginForTab(TabContentsWrapper* preview_tab) { | 35 void EnableInternalPDFPluginForTab(TabContentsWrapper* preview_tab) { |
| 41 // Always enable the internal PDF plugin for the print preview page. | 36 // Always enable the internal PDF plugin for the print preview page. |
| 42 ChromePluginServiceFilter::GetInstance()->OverridePluginForTab( | 37 ChromePluginServiceFilter::GetInstance()->OverridePluginForTab( |
| 43 preview_tab->render_view_host()->process()->id(), | 38 preview_tab->render_view_host()->process()->id(), |
| 44 preview_tab->render_view_host()->routing_id(), | 39 preview_tab->render_view_host()->routing_id(), |
| 45 GURL(), | 40 GURL(), |
| 46 ASCIIToUTF16(chrome::ChromeContentClient::kPDFPluginName)); | 41 ASCIIToUTF16(chrome::ChromeContentClient::kPDFPluginName)); |
| 47 } | 42 } |
| 48 | 43 |
| 49 class PrintPreviewTabDelegate : public HtmlDialogUIDelegate { | 44 void ResetPreviewTabOverrideTitle(TabContentsWrapper* preview_tab) { |
| 50 public: | 45 preview_tab->print_view_manager()->ResetTitleOverride(); |
| 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; | |
| 128 } | 46 } |
| 129 | 47 |
| 130 } // namespace | 48 } // namespace |
| 131 | 49 |
| 132 namespace printing { | 50 namespace printing { |
| 133 | 51 |
| 134 PrintPreviewTabController::PrintPreviewTabController() | 52 PrintPreviewTabController::PrintPreviewTabController() |
| 135 : waiting_for_new_preview_page_(false) { | 53 : waiting_for_new_preview_page_(false) { |
| 136 } | 54 } |
| 137 | 55 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 157 | 75 |
| 158 TabContentsWrapper* PrintPreviewTabController::GetOrCreatePreviewTab( | 76 TabContentsWrapper* PrintPreviewTabController::GetOrCreatePreviewTab( |
| 159 TabContentsWrapper* initiator_tab) { | 77 TabContentsWrapper* initiator_tab) { |
| 160 DCHECK(initiator_tab); | 78 DCHECK(initiator_tab); |
| 161 | 79 |
| 162 // Get the print preview tab for |initiator_tab|. | 80 // Get the print preview tab for |initiator_tab|. |
| 163 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(initiator_tab); | 81 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(initiator_tab); |
| 164 if (!preview_tab) | 82 if (!preview_tab) |
| 165 return CreatePrintPreviewTab(initiator_tab); | 83 return CreatePrintPreviewTab(initiator_tab); |
| 166 | 84 |
| 167 // Show the initiator tab holding the existing preview tab. | 85 // Show current preview tab. |
| 168 static_cast<RenderViewHostDelegate*>( | 86 static_cast<RenderViewHostDelegate*>(preview_tab->tab_contents())->Activate(); |
| 169 initiator_tab->tab_contents())->Activate(); | |
| 170 return preview_tab; | 87 return preview_tab; |
| 171 } | 88 } |
| 172 | 89 |
| 173 TabContentsWrapper* PrintPreviewTabController::GetPrintPreviewForTab( | 90 TabContentsWrapper* PrintPreviewTabController::GetPrintPreviewForTab( |
| 174 TabContentsWrapper* tab) const { | 91 TabContentsWrapper* tab) const { |
| 175 // |preview_tab_map_| is keyed by the preview tab, so if find() succeeds, then | 92 // |preview_tab_map_| is keyed by the preview tab, so if find() succeeds, then |
| 176 // |tab| is the preview tab. | 93 // |tab| is the preview tab. |
| 177 PrintPreviewTabMap::const_iterator it = preview_tab_map_.find(tab); | 94 PrintPreviewTabMap::const_iterator it = preview_tab_map_.find(tab); |
| 178 if (it != preview_tab_map_.end()) | 95 if (it != preview_tab_map_.end()) |
| 179 return tab; | 96 return tab; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 } | 134 } |
| 218 default: { | 135 default: { |
| 219 NOTREACHED(); | 136 NOTREACHED(); |
| 220 break; | 137 break; |
| 221 } | 138 } |
| 222 } | 139 } |
| 223 } | 140 } |
| 224 | 141 |
| 225 void PrintPreviewTabController::OnRendererProcessClosed( | 142 void PrintPreviewTabController::OnRendererProcessClosed( |
| 226 RenderProcessHost* rph) { | 143 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; | |
| 231 for (PrintPreviewTabMap::iterator iter = preview_tab_map_.begin(); | 144 for (PrintPreviewTabMap::iterator iter = preview_tab_map_.begin(); |
| 232 iter != preview_tab_map_.end(); ++iter) { | 145 iter != preview_tab_map_.end(); ++iter) { |
| 233 TabContentsWrapper* preview_tab = iter->first; | |
| 234 TabContentsWrapper* initiator_tab = iter->second; | 146 TabContentsWrapper* initiator_tab = iter->second; |
| 235 if (preview_tab->render_view_host()->process() == rph) { | 147 if (initiator_tab && |
| 236 closed_preview_tabs.push_back(preview_tab); | 148 initiator_tab->render_view_host()->process() == rph) { |
| 237 } else if (initiator_tab && | 149 TabContentsWrapper* preview_tab = iter->first; |
| 238 initiator_tab->render_view_host()->process() == rph) { | 150 PrintPreviewUI* print_preview_ui = |
| 239 closed_initiator_tabs.push_back(initiator_tab); | 151 static_cast<PrintPreviewUI*>(preview_tab->web_ui()); |
| 152 print_preview_ui->OnInitiatorTabCrashed(); |
| 240 } | 153 } |
| 241 } | 154 } |
| 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]); | |
| 253 } | 155 } |
| 254 | 156 |
| 255 void PrintPreviewTabController::OnTabContentsDestroyed( | 157 void PrintPreviewTabController::OnTabContentsDestroyed( |
| 256 TabContentsWrapper* tab) { | 158 TabContentsWrapper* tab) { |
| 257 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(tab); | 159 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(tab); |
| 258 if (!preview_tab) { | 160 if (!preview_tab) |
| 259 NOTREACHED(); | |
| 260 return; | 161 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; |
| 261 } | 189 } |
| 262 | 190 |
| 263 if (tab == preview_tab) | 191 ResetPreviewTabOverrideTitle(preview_tab); |
| 264 RemovePreviewTab(tab); | 192 RemoveObservers(tab); |
| 265 else | |
| 266 RemoveInitiatorTab(tab); | |
| 267 } | 193 } |
| 268 | 194 |
| 269 void PrintPreviewTabController::OnNavEntryCommitted( | 195 void PrintPreviewTabController::OnNavEntryCommitted( |
| 270 TabContentsWrapper* tab, content::LoadCommittedDetails* details) { | 196 TabContentsWrapper* tab, content::LoadCommittedDetails* details) { |
| 271 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(tab); | 197 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(tab); |
| 272 if (!preview_tab) { | |
| 273 NOTREACHED(); | |
| 274 return; | |
| 275 } | |
| 276 bool source_tab_is_preview_tab = (tab == preview_tab); | 198 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; |
| 277 | 202 |
| 278 if (source_tab_is_preview_tab) { | 203 // Don't update/erase the map entry if the page has not changed. |
| 279 // Preview tab navigated. | 204 if (transition_type == content::PAGE_TRANSITION_RELOAD || |
| 280 if (details) { | 205 nav_type == content::NAVIGATION_TYPE_SAME_PAGE) { |
| 281 content::PageTransition transition_type = | 206 if (source_tab_is_preview_tab) |
| 282 details->entry->transition_type(); | 207 SetInitiatorTabURLAndTitle(preview_tab); |
| 283 content::NavigationType nav_type = details->type; | 208 return; |
| 209 } |
| 284 | 210 |
| 285 // New |preview_tab| is created. Don't update/erase map entry. | 211 // New |preview_tab| is created. Don't update/erase map entry. |
| 286 if (waiting_for_new_preview_page_ && | 212 if (waiting_for_new_preview_page_ && |
| 287 transition_type == content::PAGE_TRANSITION_START_PAGE && | 213 transition_type == content::PAGE_TRANSITION_LINK && |
| 288 nav_type == content::NAVIGATION_TYPE_NEW_PAGE) { | 214 nav_type == content::NAVIGATION_TYPE_NEW_PAGE && |
| 289 waiting_for_new_preview_page_ = false; | 215 source_tab_is_preview_tab) { |
| 290 SetInitiatorTabURLAndTitle(preview_tab); | 216 waiting_for_new_preview_page_ = false; |
| 217 SetInitiatorTabURLAndTitle(preview_tab); |
| 218 return; |
| 219 } |
| 291 | 220 |
| 292 // Disabling the delegate will prevent all future navigation. | 221 // User navigated to a preview tab using forward/back button. |
| 293 tab->tab_contents()->set_delegate(NULL); | 222 if (source_tab_is_preview_tab && |
| 294 return; | 223 transition_type == content::PAGE_TRANSITION_FORWARD_BACK && |
| 295 } | 224 nav_type == content::NAVIGATION_TYPE_EXISTING_PAGE) { |
| 296 | 225 return; |
| 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 } | |
| 304 } | 226 } |
| 305 NOTREACHED(); | |
| 306 return; | |
| 307 } | 227 } |
| 308 | 228 |
| 309 // Initiator tab navigated. | 229 RemoveObservers(tab); |
| 310 RemoveInitiatorTab(tab); | 230 ResetPreviewTabOverrideTitle(preview_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 } |
| 311 } | 247 } |
| 312 | 248 |
| 313 // static | 249 // static |
| 314 bool PrintPreviewTabController::IsPrintPreviewTab(TabContentsWrapper* tab) { | 250 bool PrintPreviewTabController::IsPrintPreviewTab(TabContentsWrapper* tab) { |
| 315 return IsPrintPreviewURL(tab->tab_contents()->GetURL()); | 251 return IsPrintPreviewURL(tab->tab_contents()->GetURL()); |
| 316 } | 252 } |
| 317 | 253 |
| 318 // static | 254 // static |
| 319 bool PrintPreviewTabController::IsPrintPreviewURL(const GURL& url) { | 255 bool PrintPreviewTabController::IsPrintPreviewURL(const GURL& url) { |
| 320 return (url.SchemeIs(chrome::kChromeUIScheme) && | 256 return (url.SchemeIs(chrome::kChromeUIScheme) && |
| 321 url.host() == chrome::kChromeUIPrintHost); | 257 url.host() == chrome::kChromeUIPrintHost); |
| 322 } | 258 } |
| 323 | 259 |
| 324 void PrintPreviewTabController::EraseInitiatorTabInfo( | 260 void PrintPreviewTabController::EraseInitiatorTabInfo( |
| 325 TabContentsWrapper* preview_tab) { | 261 TabContentsWrapper* preview_tab) { |
| 326 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); | 262 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); |
| 327 if (it == preview_tab_map_.end()) | 263 if (it == preview_tab_map_.end()) |
| 328 return; | 264 return; |
| 329 | 265 |
| 330 RemoveObservers(it->second); | 266 RemoveObservers(it->second); |
| 331 preview_tab_map_[preview_tab] = NULL; | 267 preview_tab_map_[preview_tab] = NULL; |
| 268 ResetPreviewTabOverrideTitle(preview_tab); |
| 332 } | 269 } |
| 333 | 270 |
| 334 TabContentsWrapper* PrintPreviewTabController::GetInitiatorTab( | 271 TabContentsWrapper* PrintPreviewTabController::GetInitiatorTab( |
| 335 TabContentsWrapper* preview_tab) { | 272 TabContentsWrapper* preview_tab) { |
| 336 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); | 273 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); |
| 337 if (it != preview_tab_map_.end()) | 274 if (it != preview_tab_map_.end()) |
| 338 return preview_tab_map_[preview_tab]; | 275 return preview_tab_map_[preview_tab]; |
| 339 return NULL; | 276 return NULL; |
| 340 } | 277 } |
| 341 | 278 |
| 342 TabContentsWrapper* PrintPreviewTabController::CreatePrintPreviewTab( | 279 TabContentsWrapper* PrintPreviewTabController::CreatePrintPreviewTab( |
| 343 TabContentsWrapper* initiator_tab) { | 280 TabContentsWrapper* initiator_tab) { |
| 344 Browser* current_browser = BrowserList::FindBrowserWithID( | 281 Browser* current_browser = BrowserList::FindBrowserWithID( |
| 345 initiator_tab->restore_tab_helper()->window_id().id()); | 282 initiator_tab->restore_tab_helper()->window_id().id()); |
| 346 if (!current_browser) { | 283 if (!current_browser) { |
| 347 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) { | 284 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) { |
| 348 Profile* profile = Profile::FromBrowserContext( | 285 Profile* profile = Profile::FromBrowserContext( |
| 349 initiator_tab->tab_contents()->browser_context()); | 286 initiator_tab->tab_contents()->browser_context()); |
| 350 current_browser = Browser::CreateForType(Browser::TYPE_POPUP, profile); | 287 current_browser = Browser::CreateForType(Browser::TYPE_POPUP, profile); |
| 351 if (!current_browser) { | 288 if (!current_browser) { |
| 352 NOTREACHED() << "Failed to create popup browser window"; | 289 NOTREACHED() << "Failed to create popup browser window"; |
| 353 return NULL; | 290 return NULL; |
| 354 } | 291 } |
| 355 } else { | 292 } else { |
| 356 return NULL; | 293 return NULL; |
| 357 } | 294 } |
| 358 } | 295 } |
| 359 | 296 |
| 360 HtmlDialogUIDelegate* delegate = new PrintPreviewTabDelegate(initiator_tab); | 297 // Add a new tab next to initiator tab. |
| 361 ConstrainedHtmlUIDelegate* html_delegate = | 298 browser::NavigateParams params(current_browser, |
| 362 ConstrainedHtmlUI::CreateConstrainedHtmlDialog(current_browser->profile(), | 299 GURL(chrome::kChromeUIPrintURL), |
| 363 delegate, | 300 content::PAGE_TRANSITION_LINK); |
| 364 initiator_tab); | 301 params.disposition = NEW_FOREGROUND_TAB; |
| 365 TabContentsWrapper* preview_tab = html_delegate->tab(); | 302 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) |
| 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; |
| 366 EnableInternalPDFPluginForTab(preview_tab); | 314 EnableInternalPDFPluginForTab(preview_tab); |
| 315 static_cast<RenderViewHostDelegate*>(preview_tab->tab_contents())->Activate(); |
| 367 | 316 |
| 368 // Add an entry to the map. | 317 // Add an entry to the map. |
| 369 preview_tab_map_[preview_tab] = initiator_tab; | 318 preview_tab_map_[preview_tab] = initiator_tab; |
| 370 waiting_for_new_preview_page_ = true; | 319 waiting_for_new_preview_page_ = true; |
| 371 | 320 |
| 372 AddObservers(initiator_tab); | 321 AddObservers(initiator_tab); |
| 373 AddObservers(preview_tab); | 322 AddObservers(preview_tab); |
| 374 | 323 |
| 375 return preview_tab; | 324 return preview_tab; |
| 376 } | 325 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 // notification has already been added. | 367 // notification has already been added. |
| 419 RenderProcessHost* rph = tab->render_view_host()->process(); | 368 RenderProcessHost* rph = tab->render_view_host()->process(); |
| 420 if (registrar_.IsRegistered(this, | 369 if (registrar_.IsRegistered(this, |
| 421 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 370 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 422 content::Source<RenderProcessHost>(rph))) { | 371 content::Source<RenderProcessHost>(rph))) { |
| 423 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 372 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 424 content::Source<RenderProcessHost>(rph)); | 373 content::Source<RenderProcessHost>(rph)); |
| 425 } | 374 } |
| 426 } | 375 } |
| 427 | 376 |
| 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 | |
| 463 } // namespace printing | 377 } // namespace printing |
| OLD | NEW |