| 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> | |
| 10 | 9 |
| 11 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
| 12 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
| 15 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/plugins/chrome_plugin_service_filter.h" | 15 #include "chrome/browser/plugins/chrome_plugin_service_filter.h" |
| 17 #include "chrome/browser/printing/print_view_manager.h" | 16 #include "chrome/browser/printing/print_view_manager.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
| 20 #include "chrome/browser/ui/browser_finder.h" | 19 #include "chrome/browser/ui/browser_finder.h" |
| 21 #include "chrome/browser/ui/browser_navigator.h" | 20 #include "chrome/browser/ui/browser_navigator.h" |
| 22 #include "chrome/browser/ui/browser_window.h" | 21 #include "chrome/browser/ui/browser_window.h" |
| 23 #include "chrome/browser/ui/host_desktop.h" | 22 #include "chrome/browser/ui/host_desktop.h" |
| 24 #include "chrome/browser/ui/webui/chrome_web_contents_handler.h" | 23 #include "chrome/browser/ui/webui/chrome_web_contents_handler.h" |
| 25 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h" | 24 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h" |
| 26 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" | 25 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" |
| 27 #include "chrome/common/chrome_content_client.h" | 26 #include "chrome/common/chrome_content_client.h" |
| 28 #include "chrome/common/chrome_paths.h" | 27 #include "chrome/common/chrome_paths.h" |
| 29 #include "chrome/common/url_constants.h" | 28 #include "chrome/common/url_constants.h" |
| 30 #include "components/web_modal/web_contents_modal_dialog_host.h" | 29 #include "components/web_modal/web_contents_modal_dialog_host.h" |
| 31 #include "content/public/browser/navigation_controller.h" | 30 #include "content/public/browser/navigation_controller.h" |
| 32 #include "content/public/browser/navigation_details.h" | 31 #include "content/public/browser/navigation_details.h" |
| 33 #include "content/public/browser/navigation_entry.h" | 32 #include "content/public/browser/navigation_entry.h" |
| 34 #include "content/public/browser/notification_details.h" | |
| 35 #include "content/public/browser/notification_source.h" | |
| 36 #include "content/public/browser/plugin_service.h" | 33 #include "content/public/browser/plugin_service.h" |
| 37 #include "content/public/browser/render_frame_host.h" | 34 #include "content/public/browser/render_frame_host.h" |
| 38 #include "content/public/browser/render_process_host.h" | 35 #include "content/public/browser/render_process_host.h" |
| 36 #include "content/public/browser/render_process_host_observer.h" |
| 39 #include "content/public/browser/render_view_host.h" | 37 #include "content/public/browser/render_view_host.h" |
| 40 #include "content/public/browser/web_contents.h" | 38 #include "content/public/browser/web_contents.h" |
| 41 #include "content/public/browser/web_contents_delegate.h" | 39 #include "content/public/browser/web_contents_delegate.h" |
| 40 #include "content/public/browser/web_contents_observer.h" |
| 42 #include "content/public/browser/web_contents_view.h" | 41 #include "content/public/browser/web_contents_view.h" |
| 43 #include "content/public/common/webplugininfo.h" | 42 #include "content/public/common/webplugininfo.h" |
| 44 #include "ui/web_dialogs/web_dialog_delegate.h" | 43 #include "ui/web_dialogs/web_dialog_delegate.h" |
| 45 #include "ui/web_dialogs/web_dialog_web_contents_delegate.h" | 44 #include "ui/web_dialogs/web_dialog_web_contents_delegate.h" |
| 46 | 45 |
| 47 using content::NativeWebKeyboardEvent; | 46 using content::NativeWebKeyboardEvent; |
| 48 using content::NavigationController; | 47 using content::NavigationController; |
| 48 using content::RenderProcessHost; |
| 49 using content::WebContents; | 49 using content::WebContents; |
| 50 using content::WebUIMessageHandler; | 50 using content::WebUIMessageHandler; |
| 51 using ui::WebDialogDelegate; | 51 using ui::WebDialogDelegate; |
| 52 using ui::WebDialogWebContentsDelegate; | 52 using ui::WebDialogWebContentsDelegate; |
| 53 | 53 |
| 54 namespace { | 54 namespace { |
| 55 | 55 |
| 56 void EnableInternalPDFPluginForContents(WebContents* preview_dialog) { | 56 void EnableInternalPDFPluginForContents(WebContents* preview_dialog) { |
| 57 // Always enable the internal PDF plugin for the print preview page. | 57 // Always enable the internal PDF plugin for the print preview page. |
| 58 base::FilePath pdf_plugin_path; | 58 base::FilePath pdf_plugin_path; |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 if (!current_browser) | 202 if (!current_browser) |
| 203 return; | 203 return; |
| 204 current_browser->window()->HandleKeyboardEvent(event); | 204 current_browser->window()->HandleKeyboardEvent(event); |
| 205 #endif | 205 #endif |
| 206 } | 206 } |
| 207 | 207 |
| 208 } // namespace | 208 } // namespace |
| 209 | 209 |
| 210 namespace printing { | 210 namespace printing { |
| 211 | 211 |
| 212 struct PrintPreviewDialogController::Operation { |
| 213 class Observer : public content::WebContentsObserver, |
| 214 public content::RenderProcessHostObserver { |
| 215 public: |
| 216 Observer(); |
| 217 virtual ~Observer(); |
| 218 |
| 219 void StartObserving(PrintPreviewDialogController* owner, |
| 220 WebContents* web_contents); |
| 221 void StopObserving(); |
| 222 |
| 223 private: |
| 224 // content::WebContentsObserver |
| 225 virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE; |
| 226 virtual void NavigationEntryCommitted( |
| 227 const content::LoadCommittedDetails& load_details) OVERRIDE; |
| 228 // content::RenderProcessHostObserver |
| 229 virtual void RenderProcessExited(RenderProcessHost* host, |
| 230 base::ProcessHandle handle, |
| 231 base::TerminationStatus status, |
| 232 int exit_code) OVERRIDE; |
| 233 virtual void RenderProcessHostDestroyed(RenderProcessHost* host) OVERRIDE; |
| 234 |
| 235 PrintPreviewDialogController* owner_; |
| 236 RenderProcessHost* host_; |
| 237 }; |
| 238 |
| 239 Operation(); |
| 240 |
| 241 WebContents* preview_dialog; |
| 242 WebContents* initiator; |
| 243 Observer preview_dialog_observer; |
| 244 Observer initiator_observer; |
| 245 |
| 246 DISALLOW_COPY_AND_ASSIGN(Operation); |
| 247 }; |
| 248 |
| 249 PrintPreviewDialogController::Operation::Operation() : preview_dialog(NULL), |
| 250 initiator(NULL) { |
| 251 } |
| 252 |
| 253 PrintPreviewDialogController::Operation::Observer::Observer() : owner_(NULL), |
| 254 host_(NULL) { |
| 255 } |
| 256 |
| 257 PrintPreviewDialogController::Operation::Observer::~Observer() { |
| 258 StopObserving(); |
| 259 } |
| 260 |
| 261 void PrintPreviewDialogController::Operation::Observer::StartObserving( |
| 262 PrintPreviewDialogController* owner, |
| 263 WebContents* web_contents) { |
| 264 owner_ = owner; |
| 265 Observe(web_contents); |
| 266 host_ = web_contents->GetRenderProcessHost(); |
| 267 host_->AddObserver(this); |
| 268 } |
| 269 |
| 270 void PrintPreviewDialogController::Operation::Observer::StopObserving() { |
| 271 Observe(NULL); |
| 272 if (host_) { |
| 273 host_->RemoveObserver(this); |
| 274 host_ = NULL; |
| 275 } |
| 276 } |
| 277 |
| 278 void PrintPreviewDialogController::Operation::Observer::WebContentsDestroyed( |
| 279 WebContents* web_contents) { |
| 280 owner_->OnWebContentsDestroyed(web_contents); |
| 281 } |
| 282 |
| 283 void |
| 284 PrintPreviewDialogController::Operation::Observer::NavigationEntryCommitted( |
| 285 const content::LoadCommittedDetails& load_details) { |
| 286 owner_->OnNavigationEntryCommitted(web_contents(), &load_details); |
| 287 } |
| 288 |
| 289 void PrintPreviewDialogController::Operation::Observer::RenderProcessExited( |
| 290 RenderProcessHost* host, |
| 291 base::ProcessHandle handle, |
| 292 base::TerminationStatus status, |
| 293 int exit_code) { |
| 294 owner_->OnRenderProcessExited(host); |
| 295 } |
| 296 |
| 297 void |
| 298 PrintPreviewDialogController::Operation::Observer::RenderProcessHostDestroyed( |
| 299 RenderProcessHost* host) { |
| 300 host_ = NULL; |
| 301 } |
| 302 |
| 212 PrintPreviewDialogController::PrintPreviewDialogController() | 303 PrintPreviewDialogController::PrintPreviewDialogController() |
| 213 : waiting_for_new_preview_page_(false), | 304 : waiting_for_new_preview_page_(false), |
| 214 is_creating_print_preview_dialog_(false) { | 305 is_creating_print_preview_dialog_(false) { |
| 215 } | 306 } |
| 216 | 307 |
| 217 // static | 308 // static |
| 218 PrintPreviewDialogController* PrintPreviewDialogController::GetInstance() { | 309 PrintPreviewDialogController* PrintPreviewDialogController::GetInstance() { |
| 219 if (!g_browser_process) | 310 if (!g_browser_process) |
| 220 return NULL; | 311 return NULL; |
| 221 return g_browser_process->print_preview_dialog_controller(); | 312 return g_browser_process->print_preview_dialog_controller(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 242 if (!preview_dialog) | 333 if (!preview_dialog) |
| 243 return CreatePrintPreviewDialog(initiator); | 334 return CreatePrintPreviewDialog(initiator); |
| 244 | 335 |
| 245 // Show the initiator holding the existing preview dialog. | 336 // Show the initiator holding the existing preview dialog. |
| 246 initiator->GetDelegate()->ActivateContents(initiator); | 337 initiator->GetDelegate()->ActivateContents(initiator); |
| 247 return preview_dialog; | 338 return preview_dialog; |
| 248 } | 339 } |
| 249 | 340 |
| 250 WebContents* PrintPreviewDialogController::GetPrintPreviewForContents( | 341 WebContents* PrintPreviewDialogController::GetPrintPreviewForContents( |
| 251 WebContents* contents) const { | 342 WebContents* contents) const { |
| 252 // |preview_dialog_map_| is keyed by the preview dialog, so if find() | 343 for (size_t i = 0; i < preview_operations_.size(); ++i) { |
| 253 // succeeds, then |contents| is the preview dialog. | 344 Operation* operation = preview_operations_[i]; |
| 254 PrintPreviewDialogMap::const_iterator it = preview_dialog_map_.find(contents); | 345 if (operation->preview_dialog == contents || |
| 255 if (it != preview_dialog_map_.end()) | 346 operation->initiator == contents) { |
| 256 return contents; | 347 return operation->preview_dialog; |
| 257 | |
| 258 for (it = preview_dialog_map_.begin(); | |
| 259 it != preview_dialog_map_.end(); | |
| 260 ++it) { | |
| 261 // If |contents| is an initiator. | |
| 262 if (contents == it->second) { | |
| 263 // Return the associated preview dialog. | |
| 264 return it->first; | |
| 265 } | 348 } |
| 266 } | 349 } |
| 267 return NULL; | 350 return NULL; |
| 268 } | 351 } |
| 269 | 352 |
| 270 WebContents* PrintPreviewDialogController::GetInitiator( | 353 WebContents* PrintPreviewDialogController::GetInitiator( |
| 271 WebContents* preview_dialog) { | 354 WebContents* preview_dialog) { |
| 272 PrintPreviewDialogMap::iterator it = preview_dialog_map_.find(preview_dialog); | 355 for (size_t i = 0; i < preview_operations_.size(); ++i) { |
| 273 return (it != preview_dialog_map_.end()) ? it->second : NULL; | 356 Operation* operation = preview_operations_[i]; |
| 274 } | 357 if (operation->preview_dialog == preview_dialog) |
| 275 | 358 return operation->initiator; |
| 276 void PrintPreviewDialogController::Observe( | |
| 277 int type, | |
| 278 const content::NotificationSource& source, | |
| 279 const content::NotificationDetails& details) { | |
| 280 if (type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED) { | |
| 281 OnRendererProcessClosed( | |
| 282 content::Source<content::RenderProcessHost>(source).ptr()); | |
| 283 } else if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { | |
| 284 OnWebContentsDestroyed(content::Source<WebContents>(source).ptr()); | |
| 285 } else { | |
| 286 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); | |
| 287 WebContents* contents = | |
| 288 content::Source<NavigationController>(source)->GetWebContents(); | |
| 289 OnNavEntryCommitted( | |
| 290 contents, | |
| 291 content::Details<content::LoadCommittedDetails>(details).ptr()); | |
| 292 } | 359 } |
| 360 return NULL; |
| 293 } | 361 } |
| 294 | 362 |
| 295 // static | 363 // static |
| 296 bool PrintPreviewDialogController::IsPrintPreviewDialog(WebContents* contents) { | 364 bool PrintPreviewDialogController::IsPrintPreviewDialog(WebContents* contents) { |
| 297 return IsPrintPreviewURL(contents->GetURL()); | 365 return IsPrintPreviewURL(contents->GetURL()); |
| 298 } | 366 } |
| 299 | 367 |
| 300 // static | 368 // static |
| 301 bool PrintPreviewDialogController::IsPrintPreviewURL(const GURL& url) { | 369 bool PrintPreviewDialogController::IsPrintPreviewURL(const GURL& url) { |
| 302 return (url.SchemeIs(chrome::kChromeUIScheme) && | 370 return (url.SchemeIs(chrome::kChromeUIScheme) && |
| 303 url.host() == chrome::kChromeUIPrintHost); | 371 url.host() == chrome::kChromeUIPrintHost); |
| 304 } | 372 } |
| 305 | 373 |
| 306 void PrintPreviewDialogController::EraseInitiatorInfo( | 374 void PrintPreviewDialogController::EraseInitiatorInfo( |
| 307 WebContents* preview_dialog) { | 375 WebContents* preview_dialog) { |
| 308 PrintPreviewDialogMap::iterator it = preview_dialog_map_.find(preview_dialog); | 376 for (size_t i = 0; i < preview_operations_.size(); ++i) { |
| 309 if (it == preview_dialog_map_.end()) | 377 Operation* operation = preview_operations_[i]; |
| 310 return; | 378 if (operation->preview_dialog == preview_dialog) { |
| 311 | 379 operation->initiator_observer.StopObserving(); |
| 312 RemoveObservers(it->second); | 380 operation->initiator = NULL; |
| 313 preview_dialog_map_[preview_dialog] = NULL; | 381 return; |
| 382 } |
| 383 } |
| 314 } | 384 } |
| 315 | 385 |
| 316 PrintPreviewDialogController::~PrintPreviewDialogController() {} | 386 PrintPreviewDialogController::~PrintPreviewDialogController() { |
| 387 DCHECK_EQ(0U, preview_operations_.size()); |
| 388 } |
| 317 | 389 |
| 318 void PrintPreviewDialogController::OnRendererProcessClosed( | 390 void PrintPreviewDialogController::OnRenderProcessExited( |
| 319 content::RenderProcessHost* rph) { | 391 RenderProcessHost* rph) { |
| 320 // Store contents in a vector and deal with them after iterating through | 392 // Store contents in a vector and deal with them after iterating through |
| 321 // |preview_dialog_map_| because RemoveFoo() can change |preview_dialog_map_|. | 393 // |preview_operations_| because RemoveFoo() can change |preview_operations_|. |
| 322 std::vector<WebContents*> closed_initiators; | 394 std::vector<WebContents*> closed_initiators; |
| 323 std::vector<WebContents*> closed_preview_dialogs; | 395 std::vector<WebContents*> closed_preview_dialogs; |
| 324 for (PrintPreviewDialogMap::iterator iter = preview_dialog_map_.begin(); | 396 for (size_t i = 0; i < preview_operations_.size(); ++i) { |
| 325 iter != preview_dialog_map_.end(); ++iter) { | 397 Operation* operation = preview_operations_[i]; |
| 326 WebContents* preview_dialog = iter->first; | 398 WebContents* preview_dialog = operation->preview_dialog; |
| 327 WebContents* initiator = iter->second; | 399 WebContents* initiator = operation->initiator; |
| 328 if (preview_dialog->GetRenderProcessHost() == rph) { | 400 if (preview_dialog->GetRenderProcessHost() == rph) { |
| 329 closed_preview_dialogs.push_back(preview_dialog); | 401 closed_preview_dialogs.push_back(preview_dialog); |
| 330 } else if (initiator && | 402 } else if (initiator && |
| 331 initiator->GetRenderProcessHost() == rph) { | 403 initiator->GetRenderProcessHost() == rph) { |
| 332 closed_initiators.push_back(initiator); | 404 closed_initiators.push_back(initiator); |
| 333 } | 405 } |
| 334 } | 406 } |
| 335 | 407 |
| 336 for (size_t i = 0; i < closed_preview_dialogs.size(); ++i) { | 408 for (size_t i = 0; i < closed_preview_dialogs.size(); ++i) { |
| 337 RemovePreviewDialog(closed_preview_dialogs[i]); | 409 RemovePreviewDialog(closed_preview_dialogs[i]); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 352 NOTREACHED(); | 424 NOTREACHED(); |
| 353 return; | 425 return; |
| 354 } | 426 } |
| 355 | 427 |
| 356 if (contents == preview_dialog) | 428 if (contents == preview_dialog) |
| 357 RemovePreviewDialog(contents); | 429 RemovePreviewDialog(contents); |
| 358 else | 430 else |
| 359 RemoveInitiator(contents); | 431 RemoveInitiator(contents); |
| 360 } | 432 } |
| 361 | 433 |
| 362 void PrintPreviewDialogController::OnNavEntryCommitted( | 434 void PrintPreviewDialogController::OnNavigationEntryCommitted( |
| 363 WebContents* contents, content::LoadCommittedDetails* details) { | 435 WebContents* contents, const content::LoadCommittedDetails* details) { |
| 364 WebContents* preview_dialog = GetPrintPreviewForContents(contents); | 436 WebContents* preview_dialog = GetPrintPreviewForContents(contents); |
| 365 if (!preview_dialog) { | 437 if (!preview_dialog) { |
| 366 NOTREACHED(); | 438 NOTREACHED(); |
| 367 return; | 439 return; |
| 368 } | 440 } |
| 369 | 441 |
| 370 if (contents == preview_dialog) { | 442 if (contents == preview_dialog) { |
| 371 // Preview dialog navigated. | 443 // Preview dialog navigated. |
| 372 if (details) { | 444 if (details) { |
| 373 content::PageTransition transition_type = | 445 content::PageTransition transition_type = |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 new PrintPreviewWebContentDelegate(profile, initiator); | 485 new PrintPreviewWebContentDelegate(profile, initiator); |
| 414 ConstrainedWebDialogDelegate* constrained_delegate = | 486 ConstrainedWebDialogDelegate* constrained_delegate = |
| 415 CreateConstrainedWebDialog(profile, | 487 CreateConstrainedWebDialog(profile, |
| 416 web_dialog_delegate, | 488 web_dialog_delegate, |
| 417 pp_wcd, | 489 pp_wcd, |
| 418 initiator); | 490 initiator); |
| 419 WebContents* preview_dialog = constrained_delegate->GetWebContents(); | 491 WebContents* preview_dialog = constrained_delegate->GetWebContents(); |
| 420 EnableInternalPDFPluginForContents(preview_dialog); | 492 EnableInternalPDFPluginForContents(preview_dialog); |
| 421 PrintViewManager::CreateForWebContents(preview_dialog); | 493 PrintViewManager::CreateForWebContents(preview_dialog); |
| 422 | 494 |
| 423 // Add an entry to the map. | |
| 424 preview_dialog_map_[preview_dialog] = initiator; | |
| 425 waiting_for_new_preview_page_ = true; | 495 waiting_for_new_preview_page_ = true; |
| 426 | 496 |
| 427 AddObservers(initiator); | 497 // Add an entry to the map. |
| 428 AddObservers(preview_dialog); | 498 Operation* operation = new Operation; |
| 499 operation->preview_dialog = preview_dialog; |
| 500 operation->initiator = initiator; |
| 501 operation->preview_dialog_observer.StartObserving(this, preview_dialog); |
| 502 operation->initiator_observer.StartObserving(this, initiator); |
| 503 preview_operations_.push_back(operation); |
| 429 | 504 |
| 430 return preview_dialog; | 505 return preview_dialog; |
| 431 } | 506 } |
| 432 | 507 |
| 433 void PrintPreviewDialogController::SaveInitiatorTitle( | 508 void PrintPreviewDialogController::SaveInitiatorTitle( |
| 434 WebContents* preview_dialog) { | 509 WebContents* preview_dialog) { |
| 435 WebContents* initiator = GetInitiator(preview_dialog); | 510 WebContents* initiator = GetInitiator(preview_dialog); |
| 436 if (initiator && preview_dialog->GetWebUI()) { | 511 if (initiator && preview_dialog->GetWebUI()) { |
| 437 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( | 512 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
| 438 preview_dialog->GetWebUI()->GetController()); | 513 preview_dialog->GetWebUI()->GetController()); |
| 439 print_preview_ui->SetInitiatorTitle( | 514 print_preview_ui->SetInitiatorTitle( |
| 440 PrintViewManager::FromWebContents(initiator)->RenderSourceName()); | 515 PrintViewManager::FromWebContents(initiator)->RenderSourceName()); |
| 441 } | 516 } |
| 442 } | 517 } |
| 443 | 518 |
| 444 void PrintPreviewDialogController::AddObservers(WebContents* contents) { | |
| 445 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | |
| 446 content::Source<WebContents>(contents)); | |
| 447 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
| 448 content::Source<NavigationController>(&contents->GetController())); | |
| 449 | |
| 450 // Multiple sites may share the same RenderProcessHost, so check if this | |
| 451 // notification has already been added. | |
| 452 content::Source<content::RenderProcessHost> rph_source( | |
| 453 contents->GetRenderProcessHost()); | |
| 454 if (!registrar_.IsRegistered(this, | |
| 455 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, rph_source)) { | |
| 456 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | |
| 457 rph_source); | |
| 458 } | |
| 459 } | |
| 460 | |
| 461 void PrintPreviewDialogController::RemoveObservers(WebContents* contents) { | |
| 462 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | |
| 463 content::Source<WebContents>(contents)); | |
| 464 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
| 465 content::Source<NavigationController>(&contents->GetController())); | |
| 466 | |
| 467 // Multiple sites may share the same RenderProcessHost, so check if this | |
| 468 // notification has already been added. | |
| 469 content::Source<content::RenderProcessHost> rph_source( | |
| 470 contents->GetRenderProcessHost()); | |
| 471 if (registrar_.IsRegistered(this, | |
| 472 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, rph_source)) { | |
| 473 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | |
| 474 rph_source); | |
| 475 } | |
| 476 } | |
| 477 | |
| 478 void PrintPreviewDialogController::RemoveInitiator( | 519 void PrintPreviewDialogController::RemoveInitiator( |
| 479 WebContents* initiator) { | 520 WebContents* initiator) { |
| 480 WebContents* preview_dialog = GetPrintPreviewForContents(initiator); | 521 WebContents* preview_dialog = GetPrintPreviewForContents(initiator); |
| 481 DCHECK(preview_dialog); | 522 DCHECK(preview_dialog); |
| 523 |
| 482 // Update the map entry first, so when the print preview dialog gets destroyed | 524 // Update the map entry first, so when the print preview dialog gets destroyed |
| 483 // and reaches RemovePreviewDialog(), it does not attempt to also remove the | 525 // and reaches RemovePreviewDialog(), it does not attempt to also remove the |
| 484 // initiator's observers. | 526 // initiator's observers. |
| 485 preview_dialog_map_[preview_dialog] = NULL; | 527 EraseInitiatorInfo(preview_dialog); |
| 486 RemoveObservers(initiator); | |
| 487 | 528 |
| 488 PrintViewManager::FromWebContents(initiator)->PrintPreviewDone(); | 529 PrintViewManager::FromWebContents(initiator)->PrintPreviewDone(); |
| 489 | 530 |
| 490 // initiator is closed. Close the print preview dialog too. | 531 // initiator is closed. Close the print preview dialog too. |
| 491 PrintPreviewUI* print_preview_ui = | 532 PrintPreviewUI* print_preview_ui = |
| 492 static_cast<PrintPreviewUI*>(preview_dialog->GetWebUI()->GetController()); | 533 static_cast<PrintPreviewUI*>(preview_dialog->GetWebUI()->GetController()); |
| 493 if (print_preview_ui) | 534 if (print_preview_ui) |
| 494 print_preview_ui->OnInitiatorClosed(); | 535 print_preview_ui->OnInitiatorClosed(); |
| 495 } | 536 } |
| 496 | 537 |
| 497 void PrintPreviewDialogController::RemovePreviewDialog( | 538 void PrintPreviewDialogController::RemovePreviewDialog( |
| 498 WebContents* preview_dialog) { | 539 WebContents* preview_dialog) { |
| 499 // Remove the initiator's observers before erasing the mapping. | 540 for (size_t i = 0; i < preview_operations_.size(); ++i) { |
| 500 WebContents* initiator = GetInitiator(preview_dialog); | 541 Operation* operation = preview_operations_[i]; |
| 501 if (initiator) { | 542 if (operation->preview_dialog == preview_dialog) { |
| 502 RemoveObservers(initiator); | 543 // Remove the initiator's observers before erasing the mapping. |
| 503 PrintViewManager::FromWebContents(initiator)->PrintPreviewDone(); | 544 if (operation->initiator) { |
| 545 operation->initiator_observer.StopObserving(); |
| 546 PrintViewManager::FromWebContents(operation->initiator)-> |
| 547 PrintPreviewDone(); |
| 548 } |
| 549 |
| 550 // Print preview WebContents is destroyed. Notify |PrintPreviewUI| to |
| 551 // abort the initiator preview request. |
| 552 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
| 553 preview_dialog->GetWebUI()->GetController()); |
| 554 if (print_preview_ui) |
| 555 print_preview_ui->OnPrintPreviewDialogDestroyed(); |
| 556 |
| 557 preview_operations_.erase(preview_operations_.begin() + i); |
| 558 delete operation; |
| 559 |
| 560 return; |
| 561 } |
| 504 } | 562 } |
| 505 | 563 NOTREACHED(); |
| 506 // Print preview WebContents is destroyed. Notify |PrintPreviewUI| to abort | |
| 507 // the initiator preview request. | |
| 508 PrintPreviewUI* print_preview_ui = | |
| 509 static_cast<PrintPreviewUI*>(preview_dialog->GetWebUI()->GetController()); | |
| 510 if (print_preview_ui) | |
| 511 print_preview_ui->OnPrintPreviewDialogDestroyed(); | |
| 512 | |
| 513 preview_dialog_map_.erase(preview_dialog); | |
| 514 RemoveObservers(preview_dialog); | |
| 515 } | 564 } |
| 516 | 565 |
| 517 } // namespace printing | 566 } // namespace printing |
| OLD | NEW |