| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_dialog_controller.h" | 5 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 ChromePluginServiceFilter::GetInstance()->OverridePluginForTab( | 67 ChromePluginServiceFilter::GetInstance()->OverridePluginForTab( |
| 68 preview_dialog->GetRenderProcessHost()->GetID(), | 68 preview_dialog->GetRenderProcessHost()->GetID(), |
| 69 preview_dialog->GetRenderViewHost()->GetRoutingID(), | 69 preview_dialog->GetRenderViewHost()->GetRoutingID(), |
| 70 GURL(), pdf_plugin); | 70 GURL(), pdf_plugin); |
| 71 } | 71 } |
| 72 | 72 |
| 73 // WebDialogDelegate that specifies what the print preview dialog | 73 // WebDialogDelegate that specifies what the print preview dialog |
| 74 // will look like. | 74 // will look like. |
| 75 class PrintPreviewDialogDelegate : public WebDialogDelegate { | 75 class PrintPreviewDialogDelegate : public WebDialogDelegate { |
| 76 public: | 76 public: |
| 77 explicit PrintPreviewDialogDelegate(WebContents* initiator); | 77 explicit PrintPreviewDialogDelegate(WebContents* initiator_tab); |
| 78 virtual ~PrintPreviewDialogDelegate(); | 78 virtual ~PrintPreviewDialogDelegate(); |
| 79 | 79 |
| 80 virtual ui::ModalType GetDialogModalType() const OVERRIDE; | 80 virtual ui::ModalType GetDialogModalType() const OVERRIDE; |
| 81 virtual string16 GetDialogTitle() const OVERRIDE; | 81 virtual string16 GetDialogTitle() const OVERRIDE; |
| 82 virtual GURL GetDialogContentURL() const OVERRIDE; | 82 virtual GURL GetDialogContentURL() const OVERRIDE; |
| 83 virtual void GetWebUIMessageHandlers( | 83 virtual void GetWebUIMessageHandlers( |
| 84 std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE; | 84 std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE; |
| 85 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE; | 85 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE; |
| 86 virtual std::string GetDialogArgs() const OVERRIDE; | 86 virtual std::string GetDialogArgs() const OVERRIDE; |
| 87 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE; | 87 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE; |
| 88 virtual void OnCloseContents(WebContents* source, | 88 virtual void OnCloseContents(WebContents* source, |
| 89 bool* out_close_dialog) OVERRIDE; | 89 bool* out_close_dialog) OVERRIDE; |
| 90 virtual bool ShouldShowDialogTitle() const OVERRIDE; | 90 virtual bool ShouldShowDialogTitle() const OVERRIDE; |
| 91 | 91 |
| 92 private: | 92 private: |
| 93 WebContents* initiator_; | 93 WebContents* initiator_tab_; |
| 94 | 94 |
| 95 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogDelegate); | 95 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogDelegate); |
| 96 }; | 96 }; |
| 97 | 97 |
| 98 PrintPreviewDialogDelegate::PrintPreviewDialogDelegate(WebContents* initiator) | 98 PrintPreviewDialogDelegate::PrintPreviewDialogDelegate( |
| 99 : initiator_(initiator) { | 99 WebContents* initiator_tab) { |
| 100 initiator_tab_ = initiator_tab; |
| 100 } | 101 } |
| 101 | 102 |
| 102 PrintPreviewDialogDelegate::~PrintPreviewDialogDelegate() { | 103 PrintPreviewDialogDelegate::~PrintPreviewDialogDelegate() { |
| 103 } | 104 } |
| 104 | 105 |
| 105 ui::ModalType PrintPreviewDialogDelegate::GetDialogModalType() const { | 106 ui::ModalType PrintPreviewDialogDelegate::GetDialogModalType() const { |
| 106 // Not used, returning dummy value. | 107 // Not used, returning dummy value. |
| 107 NOTREACHED(); | 108 NOTREACHED(); |
| 108 return ui::MODAL_TYPE_WINDOW; | 109 return ui::MODAL_TYPE_WINDOW; |
| 109 } | 110 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 121 std::vector<WebUIMessageHandler*>* /* handlers */) const { | 122 std::vector<WebUIMessageHandler*>* /* handlers */) const { |
| 122 // PrintPreviewUI adds its own message handlers. | 123 // PrintPreviewUI adds its own message handlers. |
| 123 } | 124 } |
| 124 | 125 |
| 125 void PrintPreviewDialogDelegate::GetDialogSize(gfx::Size* size) const { | 126 void PrintPreviewDialogDelegate::GetDialogSize(gfx::Size* size) const { |
| 126 DCHECK(size); | 127 DCHECK(size); |
| 127 const gfx::Size kMinDialogSize(800, 480); | 128 const gfx::Size kMinDialogSize(800, 480); |
| 128 const int kBorder = 25; | 129 const int kBorder = 25; |
| 129 const int kConstrainedWindowOverlap = 3; | 130 const int kConstrainedWindowOverlap = 3; |
| 130 gfx::Rect rect; | 131 gfx::Rect rect; |
| 131 initiator_->GetView()->GetContainerBounds(&rect); | 132 initiator_tab_->GetView()->GetContainerBounds(&rect); |
| 132 size->set_width(std::max(rect.width(), kMinDialogSize.width()) - 2 * kBorder); | 133 size->set_width(std::max(rect.width(), kMinDialogSize.width()) - 2 * kBorder); |
| 133 size->set_height(std::max(rect.height(), kMinDialogSize.height()) - kBorder + | 134 size->set_height(std::max(rect.height(), kMinDialogSize.height()) - kBorder + |
| 134 kConstrainedWindowOverlap); | 135 kConstrainedWindowOverlap); |
| 135 | 136 |
| 136 #if defined(OS_MACOSX) | 137 #if defined(OS_MACOSX) |
| 137 // Limit the maximum size on MacOS X. | 138 // Limit the maximum size on MacOS X. |
| 138 // http://crbug.com/105815 | 139 // http://crbug.com/105815 |
| 139 const gfx::Size kMaxDialogSize(1000, 660); | 140 const gfx::Size kMaxDialogSize(1000, 660); |
| 140 size->set_width(std::min(size->width(), kMaxDialogSize.width())); | 141 size->set_width(std::min(size->width(), kMaxDialogSize.width())); |
| 141 size->set_height(std::min(size->height(), kMaxDialogSize.height())); | 142 size->set_height(std::min(size->height(), kMaxDialogSize.height())); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 157 } | 158 } |
| 158 | 159 |
| 159 bool PrintPreviewDialogDelegate::ShouldShowDialogTitle() const { | 160 bool PrintPreviewDialogDelegate::ShouldShowDialogTitle() const { |
| 160 return false; | 161 return false; |
| 161 } | 162 } |
| 162 | 163 |
| 163 // WebContentsDelegate that forwards shortcut keys in the print preview | 164 // WebContentsDelegate that forwards shortcut keys in the print preview |
| 164 // renderer to the browser. | 165 // renderer to the browser. |
| 165 class PrintPreviewWebContentDelegate : public WebDialogWebContentsDelegate { | 166 class PrintPreviewWebContentDelegate : public WebDialogWebContentsDelegate { |
| 166 public: | 167 public: |
| 167 PrintPreviewWebContentDelegate(Profile* profile, WebContents* initiator); | 168 PrintPreviewWebContentDelegate(Profile* profile, WebContents* initiator_tab); |
| 168 virtual ~PrintPreviewWebContentDelegate(); | 169 virtual ~PrintPreviewWebContentDelegate(); |
| 169 | 170 |
| 170 // Overridden from WebDialogWebContentsDelegate: | 171 // Overridden from WebDialogWebContentsDelegate: |
| 171 virtual void HandleKeyboardEvent( | 172 virtual void HandleKeyboardEvent( |
| 172 WebContents* source, | 173 WebContents* source, |
| 173 const NativeWebKeyboardEvent& event) OVERRIDE; | 174 const NativeWebKeyboardEvent& event) OVERRIDE; |
| 174 | 175 |
| 175 private: | 176 private: |
| 176 WebContents* tab_; | 177 WebContents* tab_; |
| 177 | 178 |
| 178 DISALLOW_COPY_AND_ASSIGN(PrintPreviewWebContentDelegate); | 179 DISALLOW_COPY_AND_ASSIGN(PrintPreviewWebContentDelegate); |
| 179 }; | 180 }; |
| 180 | 181 |
| 181 PrintPreviewWebContentDelegate::PrintPreviewWebContentDelegate( | 182 PrintPreviewWebContentDelegate::PrintPreviewWebContentDelegate( |
| 182 Profile* profile, | 183 Profile* profile, |
| 183 WebContents* initiator) | 184 WebContents* initiator_tab) |
| 184 : WebDialogWebContentsDelegate(profile, new ChromeWebContentsHandler), | 185 : WebDialogWebContentsDelegate(profile, new ChromeWebContentsHandler), |
| 185 tab_(initiator) {} | 186 tab_(initiator_tab) {} |
| 186 | 187 |
| 187 PrintPreviewWebContentDelegate::~PrintPreviewWebContentDelegate() {} | 188 PrintPreviewWebContentDelegate::~PrintPreviewWebContentDelegate() {} |
| 188 | 189 |
| 189 void PrintPreviewWebContentDelegate::HandleKeyboardEvent( | 190 void PrintPreviewWebContentDelegate::HandleKeyboardEvent( |
| 190 WebContents* source, | 191 WebContents* source, |
| 191 const NativeWebKeyboardEvent& event) { | 192 const NativeWebKeyboardEvent& event) { |
| 192 // Disabled on Mac due to http://crbug.com/112173 | 193 // Disabled on Mac due to http://crbug.com/112173 |
| 193 #if !defined(OS_MACOSX) | 194 #if !defined(OS_MACOSX) |
| 194 Browser* current_browser = chrome::FindBrowserWithWebContents(tab_); | 195 Browser* current_browser = chrome::FindBrowserWithWebContents(tab_); |
| 195 if (!current_browser) | 196 if (!current_browser) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 208 } | 209 } |
| 209 | 210 |
| 210 // static | 211 // static |
| 211 PrintPreviewDialogController* PrintPreviewDialogController::GetInstance() { | 212 PrintPreviewDialogController* PrintPreviewDialogController::GetInstance() { |
| 212 if (!g_browser_process) | 213 if (!g_browser_process) |
| 213 return NULL; | 214 return NULL; |
| 214 return g_browser_process->print_preview_dialog_controller(); | 215 return g_browser_process->print_preview_dialog_controller(); |
| 215 } | 216 } |
| 216 | 217 |
| 217 // static | 218 // static |
| 218 void PrintPreviewDialogController::PrintPreview(WebContents* initiator) { | 219 void PrintPreviewDialogController::PrintPreview(WebContents* initiator_tab) { |
| 219 if (initiator->ShowingInterstitialPage()) | 220 if (initiator_tab->ShowingInterstitialPage()) |
| 220 return; | 221 return; |
| 221 | 222 |
| 222 PrintPreviewDialogController* dialog_controller = GetInstance(); | 223 PrintPreviewDialogController* dialog_controller = GetInstance(); |
| 223 if (!dialog_controller) | 224 if (!dialog_controller) |
| 224 return; | 225 return; |
| 225 if (!dialog_controller->GetOrCreatePreviewDialog(initiator)) | 226 if (!dialog_controller->GetOrCreatePreviewDialog(initiator_tab)) |
| 226 PrintViewManager::FromWebContents(initiator)->PrintPreviewDone(); | 227 PrintViewManager::FromWebContents(initiator_tab)->PrintPreviewDone(); |
| 227 } | 228 } |
| 228 | 229 |
| 229 WebContents* PrintPreviewDialogController::GetOrCreatePreviewDialog( | 230 WebContents* PrintPreviewDialogController::GetOrCreatePreviewDialog( |
| 230 WebContents* initiator) { | 231 WebContents* initiator_tab) { |
| 231 DCHECK(initiator); | 232 DCHECK(initiator_tab); |
| 232 | 233 |
| 233 // Get the print preview dialog for |initiator|. | 234 // Get the print preview dialog for |initiator_tab|. |
| 234 WebContents* preview_dialog = GetPrintPreviewForContents(initiator); | 235 WebContents* preview_dialog = GetPrintPreviewForContents(initiator_tab); |
| 235 if (!preview_dialog) | 236 if (!preview_dialog) |
| 236 return CreatePrintPreviewDialog(initiator); | 237 return CreatePrintPreviewDialog(initiator_tab); |
| 237 | 238 |
| 238 // Show the initiator holding the existing preview dialog. | 239 // Show the initiator tab holding the existing preview dialog. |
| 239 initiator->GetDelegate()->ActivateContents(initiator); | 240 initiator_tab->GetDelegate()->ActivateContents(initiator_tab); |
| 240 return preview_dialog; | 241 return preview_dialog; |
| 241 } | 242 } |
| 242 | 243 |
| 243 WebContents* PrintPreviewDialogController::GetPrintPreviewForContents( | 244 WebContents* PrintPreviewDialogController::GetPrintPreviewForContents( |
| 244 WebContents* contents) const { | 245 WebContents* contents) const { |
| 245 // |preview_dialog_map_| is keyed by the preview dialog, so if find() | 246 // |preview_dialog_map_| is keyed by the preview dialog, so if find() |
| 246 // succeeds, then |contents| is the preview dialog. | 247 // succeeds, then |contents| is the preview dialog. |
| 247 PrintPreviewDialogMap::const_iterator it = preview_dialog_map_.find(contents); | 248 PrintPreviewDialogMap::const_iterator it = preview_dialog_map_.find(contents); |
| 248 if (it != preview_dialog_map_.end()) | 249 if (it != preview_dialog_map_.end()) |
| 249 return contents; | 250 return contents; |
| 250 | 251 |
| 251 for (it = preview_dialog_map_.begin(); | 252 for (it = preview_dialog_map_.begin(); |
| 252 it != preview_dialog_map_.end(); | 253 it != preview_dialog_map_.end(); |
| 253 ++it) { | 254 ++it) { |
| 254 // If |contents| is an initiator. | 255 // If |contents| is an initiator tab. |
| 255 if (contents == it->second) { | 256 if (contents == it->second) { |
| 256 // Return the associated preview dialog. | 257 // Return the associated preview dialog. |
| 257 return it->first; | 258 return it->first; |
| 258 } | 259 } |
| 259 } | 260 } |
| 260 return NULL; | 261 return NULL; |
| 261 } | 262 } |
| 262 | 263 |
| 263 WebContents* PrintPreviewDialogController::GetInitiator( | 264 WebContents* PrintPreviewDialogController::GetInitiatorTab( |
| 264 WebContents* preview_dialog) { | 265 WebContents* preview_dialog) { |
| 265 PrintPreviewDialogMap::iterator it = preview_dialog_map_.find(preview_dialog); | 266 PrintPreviewDialogMap::iterator it = preview_dialog_map_.find(preview_dialog); |
| 266 return (it != preview_dialog_map_.end()) ? it->second : NULL; | 267 return (it != preview_dialog_map_.end()) ? it->second : NULL; |
| 267 } | 268 } |
| 268 | 269 |
| 269 void PrintPreviewDialogController::Observe( | 270 void PrintPreviewDialogController::Observe( |
| 270 int type, | 271 int type, |
| 271 const content::NotificationSource& source, | 272 const content::NotificationSource& source, |
| 272 const content::NotificationDetails& details) { | 273 const content::NotificationDetails& details) { |
| 273 if (type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED) { | 274 if (type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 289 bool PrintPreviewDialogController::IsPrintPreviewDialog(WebContents* contents) { | 290 bool PrintPreviewDialogController::IsPrintPreviewDialog(WebContents* contents) { |
| 290 return IsPrintPreviewURL(contents->GetURL()); | 291 return IsPrintPreviewURL(contents->GetURL()); |
| 291 } | 292 } |
| 292 | 293 |
| 293 // static | 294 // static |
| 294 bool PrintPreviewDialogController::IsPrintPreviewURL(const GURL& url) { | 295 bool PrintPreviewDialogController::IsPrintPreviewURL(const GURL& url) { |
| 295 return (url.SchemeIs(chrome::kChromeUIScheme) && | 296 return (url.SchemeIs(chrome::kChromeUIScheme) && |
| 296 url.host() == chrome::kChromeUIPrintHost); | 297 url.host() == chrome::kChromeUIPrintHost); |
| 297 } | 298 } |
| 298 | 299 |
| 299 void PrintPreviewDialogController::EraseInitiatorInfo( | 300 void PrintPreviewDialogController::EraseInitiatorTabInfo( |
| 300 WebContents* preview_dialog) { | 301 WebContents* preview_dialog) { |
| 301 PrintPreviewDialogMap::iterator it = preview_dialog_map_.find(preview_dialog); | 302 PrintPreviewDialogMap::iterator it = preview_dialog_map_.find(preview_dialog); |
| 302 if (it == preview_dialog_map_.end()) | 303 if (it == preview_dialog_map_.end()) |
| 303 return; | 304 return; |
| 304 | 305 |
| 305 RemoveObservers(it->second, INITIATOR); | 306 RemoveObservers(it->second); |
| 306 preview_dialog_map_[preview_dialog] = NULL; | 307 preview_dialog_map_[preview_dialog] = NULL; |
| 307 } | 308 } |
| 308 | 309 |
| 309 PrintPreviewDialogController::~PrintPreviewDialogController() {} | 310 PrintPreviewDialogController::~PrintPreviewDialogController() {} |
| 310 | 311 |
| 311 void PrintPreviewDialogController::OnRendererProcessClosed( | 312 void PrintPreviewDialogController::OnRendererProcessClosed( |
| 312 content::RenderProcessHost* rph) { | 313 content::RenderProcessHost* rph) { |
| 313 // Store contents in a vector and deal with them after iterating through | 314 // Store contents in a vector and deal with them after iterating through |
| 314 // |preview_dialog_map_| because RemoveFoo() can change |preview_dialog_map_|. | 315 // |preview_dialog_map_| because RemoveFoo() can change |preview_dialog_map_|. |
| 315 std::vector<WebContents*> closed_initiators; | 316 std::vector<WebContents*> closed_initiator_tabs; |
| 316 std::vector<WebContents*> closed_preview_dialogs; | 317 std::vector<WebContents*> closed_preview_dialogs; |
| 317 for (PrintPreviewDialogMap::iterator iter = preview_dialog_map_.begin(); | 318 for (PrintPreviewDialogMap::iterator iter = preview_dialog_map_.begin(); |
| 318 iter != preview_dialog_map_.end(); ++iter) { | 319 iter != preview_dialog_map_.end(); ++iter) { |
| 319 WebContents* preview_dialog = iter->first; | 320 WebContents* preview_dialog = iter->first; |
| 320 WebContents* initiator = iter->second; | 321 WebContents* initiator_tab = iter->second; |
| 321 if (preview_dialog->GetRenderProcessHost() == rph) { | 322 if (preview_dialog->GetRenderProcessHost() == rph) { |
| 322 closed_preview_dialogs.push_back(preview_dialog); | 323 closed_preview_dialogs.push_back(preview_dialog); |
| 323 } else if (initiator && | 324 } else if (initiator_tab && |
| 324 initiator->GetRenderProcessHost() == rph) { | 325 initiator_tab->GetRenderProcessHost() == rph) { |
| 325 closed_initiators.push_back(initiator); | 326 closed_initiator_tabs.push_back(initiator_tab); |
| 326 } | 327 } |
| 327 } | 328 } |
| 328 | 329 |
| 329 for (size_t i = 0; i < closed_preview_dialogs.size(); ++i) { | 330 for (size_t i = 0; i < closed_preview_dialogs.size(); ++i) { |
| 330 RemovePreviewDialog(closed_preview_dialogs[i]); | 331 RemovePreviewDialog(closed_preview_dialogs[i]); |
| 331 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( | 332 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
| 332 closed_preview_dialogs[i]->GetWebUI()->GetController()); | 333 closed_preview_dialogs[i]->GetWebUI()->GetController()); |
| 333 if (print_preview_ui) | 334 if (print_preview_ui) |
| 334 print_preview_ui->OnPrintPreviewDialogClosed(); | 335 print_preview_ui->OnPrintPreviewDialogClosed(); |
| 335 } | 336 } |
| 336 | 337 |
| 337 for (size_t i = 0; i < closed_initiators.size(); ++i) | 338 for (size_t i = 0; i < closed_initiator_tabs.size(); ++i) |
| 338 RemoveInitiator(closed_initiators[i]); | 339 RemoveInitiatorTab(closed_initiator_tabs[i]); |
| 339 } | 340 } |
| 340 | 341 |
| 341 void PrintPreviewDialogController::OnWebContentsDestroyed( | 342 void PrintPreviewDialogController::OnWebContentsDestroyed( |
| 342 WebContents* contents) { | 343 WebContents* contents) { |
| 343 WebContents* preview_dialog = GetPrintPreviewForContents(contents); | 344 WebContents* preview_dialog = GetPrintPreviewForContents(contents); |
| 344 if (!preview_dialog) { | 345 if (!preview_dialog) { |
| 345 NOTREACHED(); | 346 NOTREACHED(); |
| 346 return; | 347 return; |
| 347 } | 348 } |
| 348 | 349 |
| 349 if (contents == preview_dialog) | 350 if (contents == preview_dialog) |
| 350 RemovePreviewDialog(contents); | 351 RemovePreviewDialog(contents); |
| 351 else | 352 else |
| 352 RemoveInitiator(contents); | 353 RemoveInitiatorTab(contents); |
| 353 } | 354 } |
| 354 | 355 |
| 355 void PrintPreviewDialogController::OnNavEntryCommitted( | 356 void PrintPreviewDialogController::OnNavEntryCommitted( |
| 356 WebContents* contents, content::LoadCommittedDetails* details) { | 357 WebContents* contents, content::LoadCommittedDetails* details) { |
| 357 WebContents* preview_dialog = GetPrintPreviewForContents(contents); | 358 WebContents* preview_dialog = GetPrintPreviewForContents(contents); |
| 358 if (!preview_dialog) { | 359 if (!preview_dialog) { |
| 359 NOTREACHED(); | 360 NOTREACHED(); |
| 360 return; | 361 return; |
| 361 } | 362 } |
| 362 | 363 |
| 363 DCHECK_EQ(contents, preview_dialog); | 364 if (contents == preview_dialog) { |
| 365 // Preview dialog navigated. |
| 366 if (details) { |
| 367 content::PageTransition transition_type = |
| 368 details->entry->GetTransitionType(); |
| 369 content::NavigationType nav_type = details->type; |
| 364 | 370 |
| 365 // Preview dialog navigated. | 371 // New |preview_dialog| is created. Don't update/erase map entry. |
| 366 if (details) { | 372 if (waiting_for_new_preview_page_ && |
| 367 content::PageTransition transition_type = | 373 transition_type == content::PAGE_TRANSITION_AUTO_TOPLEVEL && |
| 368 details->entry->GetTransitionType(); | 374 nav_type == content::NAVIGATION_TYPE_NEW_PAGE) { |
| 369 content::NavigationType nav_type = details->type; | 375 waiting_for_new_preview_page_ = false; |
| 376 SaveInitiatorTabTitle(preview_dialog); |
| 377 return; |
| 378 } |
| 370 | 379 |
| 371 // New |preview_dialog| is created. Don't update/erase map entry. | 380 // Cloud print sign-in causes a reload. |
| 372 if (waiting_for_new_preview_page_ && | 381 if (!waiting_for_new_preview_page_ && |
| 373 transition_type == content::PAGE_TRANSITION_AUTO_TOPLEVEL && | 382 transition_type == content::PAGE_TRANSITION_RELOAD && |
| 374 nav_type == content::NAVIGATION_TYPE_NEW_PAGE) { | 383 nav_type == content::NAVIGATION_TYPE_EXISTING_PAGE && |
| 375 waiting_for_new_preview_page_ = false; | 384 IsPrintPreviewURL(details->previous_url)) { |
| 376 SaveInitiatorTitle(preview_dialog); | 385 return; |
| 377 return; | 386 } |
| 378 } | 387 } |
| 379 | 388 NOTREACHED(); |
| 380 // Cloud print sign-in causes a reload. | 389 return; |
| 381 if (!waiting_for_new_preview_page_ && | |
| 382 transition_type == content::PAGE_TRANSITION_RELOAD && | |
| 383 nav_type == content::NAVIGATION_TYPE_EXISTING_PAGE && | |
| 384 IsPrintPreviewURL(details->previous_url)) { | |
| 385 return; | |
| 386 } | |
| 387 } | 390 } |
| 388 | 391 |
| 389 NOTREACHED(); | 392 RemoveInitiatorTab(contents); |
| 390 } | 393 } |
| 391 | 394 |
| 392 WebContents* PrintPreviewDialogController::CreatePrintPreviewDialog( | 395 WebContents* PrintPreviewDialogController::CreatePrintPreviewDialog( |
| 393 WebContents* initiator) { | 396 WebContents* initiator_tab) { |
| 394 base::AutoReset<bool> auto_reset(&is_creating_print_preview_dialog_, true); | 397 base::AutoReset<bool> auto_reset(&is_creating_print_preview_dialog_, true); |
| 395 Profile* profile = | 398 Profile* profile = |
| 396 Profile::FromBrowserContext(initiator->GetBrowserContext()); | 399 Profile::FromBrowserContext(initiator_tab->GetBrowserContext()); |
| 397 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) { | 400 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) { |
| 398 // Chrome Frame only ever runs on the native desktop, so it is safe to | 401 // Chrome Frame only ever runs on the native desktop, so it is safe to |
| 399 // create the popup on the native desktop. | 402 // create the popup on the native desktop. |
| 400 Browser* current_browser = new Browser( | 403 Browser* current_browser = new Browser( |
| 401 Browser::CreateParams(Browser::TYPE_POPUP, profile, | 404 Browser::CreateParams(Browser::TYPE_POPUP, profile, |
| 402 chrome::GetActiveDesktop())); | 405 chrome::GetActiveDesktop())); |
| 403 if (!current_browser) { | 406 if (!current_browser) { |
| 404 NOTREACHED() << "Failed to create popup browser window"; | 407 NOTREACHED() << "Failed to create popup browser window"; |
| 405 return NULL; | 408 return NULL; |
| 406 } | 409 } |
| 407 } | 410 } |
| 408 | 411 |
| 409 // |web_dialog_ui_delegate| deletes itself in | 412 // |web_dialog_ui_delegate| deletes itself in |
| 410 // PrintPreviewDialogDelegate::OnDialogClosed(). | 413 // PrintPreviewDialogDelegate::OnDialogClosed(). |
| 411 WebDialogDelegate* web_dialog_delegate = | 414 WebDialogDelegate* web_dialog_delegate = |
| 412 new PrintPreviewDialogDelegate(initiator); | 415 new PrintPreviewDialogDelegate(initiator_tab); |
| 413 // |web_dialog_delegate|'s owner is |constrained_delegate|. | 416 // |web_dialog_delegate|'s owner is |constrained_delegate|. |
| 414 PrintPreviewWebContentDelegate* pp_wcd = | 417 PrintPreviewWebContentDelegate* pp_wcd = |
| 415 new PrintPreviewWebContentDelegate(profile, initiator); | 418 new PrintPreviewWebContentDelegate(profile, initiator_tab); |
| 416 ConstrainedWebDialogDelegate* constrained_delegate = | 419 ConstrainedWebDialogDelegate* constrained_delegate = |
| 417 CreateConstrainedWebDialog(profile, | 420 CreateConstrainedWebDialog(profile, |
| 418 web_dialog_delegate, | 421 web_dialog_delegate, |
| 419 pp_wcd, | 422 pp_wcd, |
| 420 initiator); | 423 initiator_tab); |
| 421 WebContents* preview_dialog = constrained_delegate->GetWebContents(); | 424 WebContents* preview_dialog = constrained_delegate->GetWebContents(); |
| 422 EnableInternalPDFPluginForContents(preview_dialog); | 425 EnableInternalPDFPluginForContents(preview_dialog); |
| 423 PrintViewManager::CreateForWebContents(preview_dialog); | 426 PrintViewManager::CreateForWebContents(preview_dialog); |
| 424 | 427 |
| 425 // Add an entry to the map. | 428 // Add an entry to the map. |
| 426 preview_dialog_map_[preview_dialog] = initiator; | 429 preview_dialog_map_[preview_dialog] = initiator_tab; |
| 427 waiting_for_new_preview_page_ = true; | 430 waiting_for_new_preview_page_ = true; |
| 428 | 431 |
| 429 AddObservers(initiator, INITIATOR); | 432 AddObservers(initiator_tab); |
| 430 AddObservers(preview_dialog, PREVIEW_DIALOG); | 433 AddObservers(preview_dialog); |
| 431 | 434 |
| 432 return preview_dialog; | 435 return preview_dialog; |
| 433 } | 436 } |
| 434 | 437 |
| 435 void PrintPreviewDialogController::SaveInitiatorTitle( | 438 void PrintPreviewDialogController::SaveInitiatorTabTitle( |
| 436 WebContents* preview_dialog) { | 439 WebContents* preview_dialog) { |
| 437 WebContents* initiator = GetInitiator(preview_dialog); | 440 WebContents* initiator_tab = GetInitiatorTab(preview_dialog); |
| 438 if (initiator && preview_dialog->GetWebUI()) { | 441 if (initiator_tab && preview_dialog->GetWebUI()) { |
| 439 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( | 442 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
| 440 preview_dialog->GetWebUI()->GetController()); | 443 preview_dialog->GetWebUI()->GetController()); |
| 441 print_preview_ui->SetInitiatorTitle( | 444 print_preview_ui->SetInitiatorTabTitle( |
| 442 PrintViewManager::FromWebContents(initiator)->RenderSourceName()); | 445 PrintViewManager::FromWebContents(initiator_tab)->RenderSourceName()); |
| 443 } | 446 } |
| 444 } | 447 } |
| 445 | 448 |
| 446 void PrintPreviewDialogController::AddObservers(WebContents* contents, | 449 void PrintPreviewDialogController::AddObservers(WebContents* contents) { |
| 447 ContentsType contents_type) { | |
| 448 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 450 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 449 content::Source<WebContents>(contents)); | 451 content::Source<WebContents>(contents)); |
| 450 if (contents_type == PREVIEW_DIALOG) { | 452 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 451 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 453 content::Source<NavigationController>(&contents->GetController())); |
| 452 content::Source<NavigationController>(&contents->GetController())); | |
| 453 } | |
| 454 | 454 |
| 455 // Multiple sites may share the same RenderProcessHost, so check if this | 455 // Multiple sites may share the same RenderProcessHost, so check if this |
| 456 // notification has already been added. | 456 // notification has already been added. |
| 457 content::Source<content::RenderProcessHost> rph_source( | 457 content::Source<content::RenderProcessHost> rph_source( |
| 458 contents->GetRenderProcessHost()); | 458 contents->GetRenderProcessHost()); |
| 459 if (!registrar_.IsRegistered(this, | 459 if (!registrar_.IsRegistered(this, |
| 460 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, rph_source)) { | 460 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, rph_source)) { |
| 461 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 461 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 462 rph_source); | 462 rph_source); |
| 463 } | 463 } |
| 464 } | 464 } |
| 465 | 465 |
| 466 void PrintPreviewDialogController::RemoveObservers(WebContents* contents, | 466 void PrintPreviewDialogController::RemoveObservers(WebContents* contents) { |
| 467 ContentsType contents_type) { | |
| 468 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 467 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 469 content::Source<WebContents>(contents)); | 468 content::Source<WebContents>(contents)); |
| 470 if (contents_type == PREVIEW_DIALOG) { | 469 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 471 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 470 content::Source<NavigationController>(&contents->GetController())); |
| 472 content::Source<NavigationController>(&contents->GetController())); | |
| 473 } | |
| 474 | 471 |
| 475 // Multiple sites may share the same RenderProcessHost, so check if this | 472 // Multiple sites may share the same RenderProcessHost, so check if this |
| 476 // notification has already been added. | 473 // notification has already been added. |
| 477 content::Source<content::RenderProcessHost> rph_source( | 474 content::Source<content::RenderProcessHost> rph_source( |
| 478 contents->GetRenderProcessHost()); | 475 contents->GetRenderProcessHost()); |
| 479 if (registrar_.IsRegistered(this, | 476 if (registrar_.IsRegistered(this, |
| 480 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, rph_source)) { | 477 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, rph_source)) { |
| 481 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 478 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 482 rph_source); | 479 rph_source); |
| 483 } | 480 } |
| 484 } | 481 } |
| 485 | 482 |
| 486 void PrintPreviewDialogController::RemoveInitiator( | 483 void PrintPreviewDialogController::RemoveInitiatorTab( |
| 487 WebContents* initiator) { | 484 WebContents* initiator_tab) { |
| 488 WebContents* preview_dialog = GetPrintPreviewForContents(initiator); | 485 WebContents* preview_dialog = GetPrintPreviewForContents(initiator_tab); |
| 489 DCHECK(preview_dialog); | 486 DCHECK(preview_dialog); |
| 490 // Update the map entry first, so when the print preview dialog gets destroyed | 487 // Update the map entry first, so when the print preview dialog gets destroyed |
| 491 // and reaches RemovePreviewDialog(), it does not attempt to also remove the | 488 // and reaches RemovePreviewDialog(), it does not attempt to also remove the |
| 492 // initiator's observers. | 489 // initiator tab's observers. |
| 493 preview_dialog_map_[preview_dialog] = NULL; | 490 preview_dialog_map_[preview_dialog] = NULL; |
| 494 RemoveObservers(initiator, INITIATOR); | 491 RemoveObservers(initiator_tab); |
| 495 | 492 |
| 496 PrintViewManager::FromWebContents(initiator)->PrintPreviewDone(); | 493 PrintViewManager::FromWebContents(initiator_tab)->PrintPreviewDone(); |
| 497 | 494 |
| 498 // initiator is closed. Close the print preview dialog too. | 495 // Initiator tab is closed. Close the print preview dialog too. |
| 499 PrintPreviewUI* print_preview_ui = | 496 PrintPreviewUI* print_preview_ui = |
| 500 static_cast<PrintPreviewUI*>(preview_dialog->GetWebUI()->GetController()); | 497 static_cast<PrintPreviewUI*>(preview_dialog->GetWebUI()->GetController()); |
| 501 if (print_preview_ui) | 498 if (print_preview_ui) |
| 502 print_preview_ui->OnInitiatorClosed(); | 499 print_preview_ui->OnInitiatorTabClosed(); |
| 503 } | 500 } |
| 504 | 501 |
| 505 void PrintPreviewDialogController::RemovePreviewDialog( | 502 void PrintPreviewDialogController::RemovePreviewDialog( |
| 506 WebContents* preview_dialog) { | 503 WebContents* preview_dialog) { |
| 507 // Remove the initiator's observers before erasing the mapping. | 504 // Remove the initiator tab's observers before erasing the mapping. |
| 508 WebContents* initiator = GetInitiator(preview_dialog); | 505 WebContents* initiator_tab = GetInitiatorTab(preview_dialog); |
| 509 if (initiator) { | 506 if (initiator_tab) { |
| 510 RemoveObservers(initiator, INITIATOR); | 507 RemoveObservers(initiator_tab); |
| 511 PrintViewManager::FromWebContents(initiator)->PrintPreviewDone(); | 508 PrintViewManager::FromWebContents(initiator_tab)->PrintPreviewDone(); |
| 512 } | 509 } |
| 513 | 510 |
| 514 // Print preview WebContents is destroyed. Notify |PrintPreviewUI| to abort | 511 // Print preview WebContents is destroyed. Notify |PrintPreviewUI| to abort |
| 515 // the initiator preview request. | 512 // the initiator tab preview request. |
| 516 PrintPreviewUI* print_preview_ui = | 513 PrintPreviewUI* print_preview_ui = |
| 517 static_cast<PrintPreviewUI*>(preview_dialog->GetWebUI()->GetController()); | 514 static_cast<PrintPreviewUI*>(preview_dialog->GetWebUI()->GetController()); |
| 518 if (print_preview_ui) | 515 if (print_preview_ui) |
| 519 print_preview_ui->OnPrintPreviewDialogDestroyed(); | 516 print_preview_ui->OnPrintPreviewDialogDestroyed(); |
| 520 | 517 |
| 521 preview_dialog_map_.erase(preview_dialog); | 518 preview_dialog_map_.erase(preview_dialog); |
| 522 RemoveObservers(preview_dialog, PREVIEW_DIALOG); | 519 RemoveObservers(preview_dialog); |
| 523 } | 520 } |
| 524 | 521 |
| 525 } // namespace printing | 522 } // namespace printing |
| OLD | NEW |