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 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 } | 208 } |
209 | 209 |
210 // static | 210 // static |
211 PrintPreviewDialogController* PrintPreviewDialogController::GetInstance() { | 211 PrintPreviewDialogController* PrintPreviewDialogController::GetInstance() { |
212 if (!g_browser_process) | 212 if (!g_browser_process) |
213 return NULL; | 213 return NULL; |
214 return g_browser_process->print_preview_dialog_controller(); | 214 return g_browser_process->print_preview_dialog_controller(); |
215 } | 215 } |
216 | 216 |
217 // static | 217 // static |
218 void PrintPreviewDialogController::PrintPreview(WebContents* tab) { | 218 void PrintPreviewDialogController::PrintPreview(WebContents* initiator_tab) { |
219 if (tab->ShowingInterstitialPage()) | 219 if (initiator_tab->ShowingInterstitialPage()) |
220 return; | 220 return; |
221 | 221 |
222 PrintPreviewDialogController* tab_controller = GetInstance(); | 222 PrintPreviewDialogController* dialog_controller = GetInstance(); |
223 if (!tab_controller) | 223 if (!dialog_controller) |
224 return; | 224 return; |
225 if (!tab_controller->GetOrCreatePreviewTab(tab)) | 225 if (!dialog_controller->GetOrCreatePreviewDialog(initiator_tab)) |
226 PrintViewManager::FromWebContents(tab)->PrintPreviewDone(); | 226 PrintViewManager::FromWebContents(initiator_tab)->PrintPreviewDone(); |
227 } | 227 } |
228 | 228 |
229 WebContents* PrintPreviewDialogController::GetOrCreatePreviewDialog( | 229 WebContents* PrintPreviewDialogController::GetOrCreatePreviewDialog( |
230 WebContents* initiator_tab) { | 230 WebContents* initiator_tab) { |
231 return GetOrCreatePreviewTab(initiator_tab); | |
232 } | |
233 | |
234 WebContents* PrintPreviewDialogController::GetOrCreatePreviewTab( | |
235 WebContents* initiator_tab) { | |
236 DCHECK(initiator_tab); | 231 DCHECK(initiator_tab); |
237 | 232 |
238 // Get the print preview tab for |initiator_tab|. | 233 // Get the print preview dialog for |initiator_tab|. |
239 WebContents* preview_tab = GetPrintPreviewForTab(initiator_tab); | 234 WebContents* preview_dialog = GetPrintPreviewForContents(initiator_tab); |
240 if (!preview_tab) | 235 if (!preview_dialog) |
241 return CreatePrintPreviewTab(initiator_tab); | 236 return CreatePrintPreviewTab(initiator_tab); |
242 | 237 |
243 // Show the initiator tab holding the existing preview tab. | 238 // Show the initiator tab holding the existing preview dialog. |
244 initiator_tab->GetDelegate()->ActivateContents(initiator_tab); | 239 initiator_tab->GetDelegate()->ActivateContents(initiator_tab); |
245 return preview_tab; | 240 return preview_dialog; |
246 } | 241 } |
247 | 242 |
248 WebContents* PrintPreviewDialogController::GetPrintPreviewForContents( | 243 WebContents* PrintPreviewDialogController::GetPrintPreviewForContents( |
249 WebContents* contents) const { | 244 WebContents* contents) const { |
250 return GetPrintPreviewForTab(contents); | 245 // |preview_tab_map_| is keyed by the preview dialog, so if find() succeeds, |
251 } | 246 // then |contents| is the preview dialog. |
252 | 247 PrintPreviewTabMap::const_iterator it = preview_tab_map_.find(contents); |
253 WebContents* PrintPreviewDialogController::GetPrintPreviewForTab( | |
254 WebContents* tab) const { | |
255 // |preview_tab_map_| is keyed by the preview tab, so if find() succeeds, then | |
256 // |tab| is the preview tab. | |
257 PrintPreviewTabMap::const_iterator it = preview_tab_map_.find(tab); | |
258 if (it != preview_tab_map_.end()) | 248 if (it != preview_tab_map_.end()) |
259 return tab; | 249 return contents; |
260 | 250 |
261 for (it = preview_tab_map_.begin(); it != preview_tab_map_.end(); ++it) { | 251 for (it = preview_tab_map_.begin(); it != preview_tab_map_.end(); ++it) { |
262 // If |tab| is an initiator tab. | 252 // If |contents| is an initiator tab. |
263 if (tab == it->second) { | 253 if (contents == it->second) { |
264 // Return the associated preview tab. | 254 // Return the associated preview tab. |
265 return it->first; | 255 return it->first; |
266 } | 256 } |
267 } | 257 } |
268 return NULL; | 258 return NULL; |
269 } | 259 } |
270 | 260 |
271 WebContents* PrintPreviewDialogController::GetInitiatorTab( | 261 WebContents* PrintPreviewDialogController::GetInitiatorTab( |
272 WebContents* preview_tab) { | 262 WebContents* preview_tab) { |
273 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); | 263 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); |
(...skipping 19 matching lines...) Expand all Loading... |
293 content::Details<content::LoadCommittedDetails>(details).ptr()); | 283 content::Details<content::LoadCommittedDetails>(details).ptr()); |
294 } | 284 } |
295 } | 285 } |
296 | 286 |
297 // static | 287 // static |
298 bool PrintPreviewDialogController::IsPrintPreviewDialog(WebContents* contents) { | 288 bool PrintPreviewDialogController::IsPrintPreviewDialog(WebContents* contents) { |
299 return IsPrintPreviewURL(contents->GetURL()); | 289 return IsPrintPreviewURL(contents->GetURL()); |
300 } | 290 } |
301 | 291 |
302 // static | 292 // static |
303 bool PrintPreviewDialogController::IsPrintPreviewTab(WebContents* tab) { | |
304 return IsPrintPreviewURL(tab->GetURL()); | |
305 } | |
306 | |
307 // static | |
308 bool PrintPreviewDialogController::IsPrintPreviewURL(const GURL& url) { | 293 bool PrintPreviewDialogController::IsPrintPreviewURL(const GURL& url) { |
309 return (url.SchemeIs(chrome::kChromeUIScheme) && | 294 return (url.SchemeIs(chrome::kChromeUIScheme) && |
310 url.host() == chrome::kChromeUIPrintHost); | 295 url.host() == chrome::kChromeUIPrintHost); |
311 } | 296 } |
312 | 297 |
313 void PrintPreviewDialogController::EraseInitiatorTabInfo( | 298 void PrintPreviewDialogController::EraseInitiatorTabInfo( |
314 WebContents* preview_tab) { | 299 WebContents* preview_tab) { |
315 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); | 300 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); |
316 if (it == preview_tab_map_.end()) | 301 if (it == preview_tab_map_.end()) |
317 return; | 302 return; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 closed_preview_tabs[i]->GetWebUI()->GetController()); | 335 closed_preview_tabs[i]->GetWebUI()->GetController()); |
351 if (print_preview_ui) | 336 if (print_preview_ui) |
352 print_preview_ui->OnPrintPreviewDialogClosed(); | 337 print_preview_ui->OnPrintPreviewDialogClosed(); |
353 } | 338 } |
354 | 339 |
355 for (size_t i = 0; i < closed_initiator_tabs.size(); ++i) | 340 for (size_t i = 0; i < closed_initiator_tabs.size(); ++i) |
356 RemoveInitiatorTab(closed_initiator_tabs[i]); | 341 RemoveInitiatorTab(closed_initiator_tabs[i]); |
357 } | 342 } |
358 | 343 |
359 void PrintPreviewDialogController::OnWebContentsDestroyed(WebContents* tab) { | 344 void PrintPreviewDialogController::OnWebContentsDestroyed(WebContents* tab) { |
360 WebContents* preview_tab = GetPrintPreviewForTab(tab); | 345 WebContents* preview_tab = GetPrintPreviewForContents(tab); |
361 if (!preview_tab) { | 346 if (!preview_tab) { |
362 NOTREACHED(); | 347 NOTREACHED(); |
363 return; | 348 return; |
364 } | 349 } |
365 | 350 |
366 if (tab == preview_tab) | 351 if (tab == preview_tab) |
367 RemovePreviewTab(tab); | 352 RemovePreviewTab(tab); |
368 else | 353 else |
369 RemoveInitiatorTab(tab); | 354 RemoveInitiatorTab(tab); |
370 } | 355 } |
371 | 356 |
372 void PrintPreviewDialogController::OnNavEntryCommitted( | 357 void PrintPreviewDialogController::OnNavEntryCommitted( |
373 WebContents* tab, content::LoadCommittedDetails* details) { | 358 WebContents* tab, content::LoadCommittedDetails* details) { |
374 WebContents* preview_tab = GetPrintPreviewForTab(tab); | 359 WebContents* preview_tab = GetPrintPreviewForContents(tab); |
375 if (!preview_tab) { | 360 if (!preview_tab) { |
376 NOTREACHED(); | 361 NOTREACHED(); |
377 return; | 362 return; |
378 } | 363 } |
379 bool source_tab_is_preview_tab = (tab == preview_tab); | 364 bool source_tab_is_preview_tab = (tab == preview_tab); |
380 | 365 |
381 if (source_tab_is_preview_tab) { | 366 if (source_tab_is_preview_tab) { |
382 // Preview tab navigated. | 367 // Preview tab navigated. |
383 if (details) { | 368 if (details) { |
384 content::PageTransition transition_type = | 369 content::PageTransition transition_type = |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 tab->GetRenderProcessHost()); | 477 tab->GetRenderProcessHost()); |
493 if (registrar_.IsRegistered(this, | 478 if (registrar_.IsRegistered(this, |
494 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, rph_source)) { | 479 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, rph_source)) { |
495 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 480 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
496 rph_source); | 481 rph_source); |
497 } | 482 } |
498 } | 483 } |
499 | 484 |
500 void PrintPreviewDialogController::RemoveInitiatorTab( | 485 void PrintPreviewDialogController::RemoveInitiatorTab( |
501 WebContents* initiator_tab) { | 486 WebContents* initiator_tab) { |
502 WebContents* preview_tab = GetPrintPreviewForTab(initiator_tab); | 487 WebContents* preview_tab = GetPrintPreviewForContents(initiator_tab); |
503 DCHECK(preview_tab); | 488 DCHECK(preview_tab); |
504 // Update the map entry first, so when the print preview tab gets destroyed | 489 // Update the map entry first, so when the print preview tab gets destroyed |
505 // and reaches RemovePreviewTab(), it does not attempt to also remove the | 490 // and reaches RemovePreviewTab(), it does not attempt to also remove the |
506 // initiator tab's observers. | 491 // initiator tab's observers. |
507 preview_tab_map_[preview_tab] = NULL; | 492 preview_tab_map_[preview_tab] = NULL; |
508 RemoveObservers(initiator_tab); | 493 RemoveObservers(initiator_tab); |
509 | 494 |
510 PrintViewManager::FromWebContents(initiator_tab)->PrintPreviewDone(); | 495 PrintViewManager::FromWebContents(initiator_tab)->PrintPreviewDone(); |
511 | 496 |
512 // Initiator tab is closed. Close the print preview tab too. | 497 // Initiator tab is closed. Close the print preview tab too. |
(...skipping 16 matching lines...) Expand all Loading... |
529 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( | 514 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
530 preview_tab->GetWebUI()->GetController()); | 515 preview_tab->GetWebUI()->GetController()); |
531 if (print_preview_ui) | 516 if (print_preview_ui) |
532 print_preview_ui->OnPrintPreviewDialogDestroyed(); | 517 print_preview_ui->OnPrintPreviewDialogDestroyed(); |
533 | 518 |
534 preview_tab_map_.erase(preview_tab); | 519 preview_tab_map_.erase(preview_tab); |
535 RemoveObservers(preview_tab); | 520 RemoveObservers(preview_tab); |
536 } | 521 } |
537 | 522 |
538 } // namespace printing | 523 } // namespace printing |
OLD | NEW |