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