OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/browser.h" | 5 #include "chrome/browser/browser.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "app/animation.h" | 10 #include "app/animation.h" |
(...skipping 620 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 return true; | 631 return true; |
632 | 632 |
633 ProcessPendingTabs(); | 633 ProcessPendingTabs(); |
634 return false; | 634 return false; |
635 } | 635 } |
636 | 636 |
637 void Browser::OnWindowClosing() { | 637 void Browser::OnWindowClosing() { |
638 if (!ShouldCloseWindow()) | 638 if (!ShouldCloseWindow()) |
639 return; | 639 return; |
640 | 640 |
| 641 bool exiting = false; |
| 642 |
641 #if defined(OS_WIN) || defined(OS_LINUX) | 643 #if defined(OS_WIN) || defined(OS_LINUX) |
642 // We don't want to do this on Mac since closing all windows isn't a sign | 644 // We don't want to do this on Mac since closing all windows isn't a sign |
643 // that the app is shutting down. | 645 // that the app is shutting down. |
644 if (BrowserList::size() == 1) | 646 if (BrowserList::size() == 1) { |
645 browser_shutdown::OnShutdownStarting(browser_shutdown::WINDOW_CLOSE); | 647 browser_shutdown::OnShutdownStarting(browser_shutdown::WINDOW_CLOSE); |
| 648 exiting = true; |
| 649 } |
646 #endif | 650 #endif |
647 | 651 |
648 // Don't use HasSessionService here, we want to force creation of the | 652 // Don't use HasSessionService here, we want to force creation of the |
649 // session service so that user can restore what was open. | 653 // session service so that user can restore what was open. |
650 SessionService* session_service = profile()->GetSessionService(); | 654 SessionService* session_service = profile()->GetSessionService(); |
651 if (session_service) | 655 if (session_service) |
652 session_service->WindowClosing(session_id()); | 656 session_service->WindowClosing(session_id()); |
653 | 657 |
654 TabRestoreService* tab_restore_service = profile()->GetTabRestoreService(); | 658 TabRestoreService* tab_restore_service = profile()->GetTabRestoreService(); |
655 if (tab_restore_service) | 659 if (tab_restore_service) |
656 tab_restore_service->BrowserClosing(this); | 660 tab_restore_service->BrowserClosing(this); |
657 | 661 |
| 662 // TODO(sky): convert session/tab restore to use notification. |
| 663 NotificationService::current()->Notify( |
| 664 NotificationType::BROWSER_CLOSING, |
| 665 Source<Browser>(this), |
| 666 Details<bool>(&exiting)); |
| 667 |
658 CloseAllTabs(); | 668 CloseAllTabs(); |
659 } | 669 } |
660 | 670 |
661 //////////////////////////////////////////////////////////////////////////////// | 671 //////////////////////////////////////////////////////////////////////////////// |
662 // In-progress download termination handling: | 672 // In-progress download termination handling: |
663 | 673 |
664 void Browser::InProgressDownloadResponse(bool cancel_downloads) { | 674 void Browser::InProgressDownloadResponse(bool cancel_downloads) { |
665 if (cancel_downloads) { | 675 if (cancel_downloads) { |
666 cancel_download_confirmation_state_ = RESPONSE_RECEIVED; | 676 cancel_download_confirmation_state_ = RESPONSE_RECEIVED; |
667 CloseWindow(); | 677 CloseWindow(); |
668 return; | 678 return; |
669 } | 679 } |
670 | 680 |
671 // Sets the confirmation state to NOT_PROMPTED so that if the user tries to | 681 // Sets the confirmation state to NOT_PROMPTED so that if the user tries to |
672 // close again we'll show the warning again. | 682 // close again we'll show the warning again. |
673 cancel_download_confirmation_state_ = NOT_PROMPTED; | 683 cancel_download_confirmation_state_ = NOT_PROMPTED; |
674 | 684 |
675 // Show the download page so the user can figure-out what downloads are still | 685 // Show the download page so the user can figure-out what downloads are still |
676 // in-progress. | 686 // in-progress. |
677 ShowDownloadsTab(); | 687 ShowDownloadsTab(); |
678 } | 688 } |
679 | 689 |
680 //////////////////////////////////////////////////////////////////////////////// | 690 //////////////////////////////////////////////////////////////////////////////// |
681 // Browser, Tab adding/showing functions: | 691 // Browser, Tab adding/showing functions: |
682 | 692 |
683 TabContents* Browser::AddTabWithURL( | 693 TabContents* Browser::AddTabWithURL( |
684 const GURL& url, const GURL& referrer, PageTransition::Type transition, | 694 const GURL& url, const GURL& referrer, PageTransition::Type transition, |
685 bool foreground, int index, bool force_index, | 695 bool foreground, int index, bool force_index, |
686 SiteInstance* instance) { | 696 SiteInstance* instance) { |
| 697 int add_types = 0; |
| 698 if (force_index) |
| 699 add_types |= ADD_FORCE_INDEX; |
| 700 if (foreground) |
| 701 add_types |= ADD_SELECTED; |
| 702 return AddTabWithURL(url, referrer, transition, index, add_types, instance, |
| 703 std::string()); |
| 704 } |
| 705 |
| 706 TabContents* Browser::AddTabWithURL(const GURL& url, |
| 707 const GURL& referrer, |
| 708 PageTransition::Type transition, |
| 709 int index, |
| 710 int add_types, |
| 711 SiteInstance* instance, |
| 712 const std::string& app_extension_id) { |
687 TabContents* contents = NULL; | 713 TabContents* contents = NULL; |
688 if (type_ == TYPE_NORMAL || tabstrip_model()->empty()) { | 714 if (type_ == TYPE_NORMAL || tabstrip_model()->empty()) { |
689 GURL url_to_load = url; | 715 GURL url_to_load = url; |
690 if (url_to_load.is_empty()) | 716 if (url_to_load.is_empty()) |
691 url_to_load = GetHomePage(); | 717 url_to_load = GetHomePage(); |
692 contents = CreateTabContentsForURL(url_to_load, referrer, profile_, | 718 contents = CreateTabContentsForURL(url_to_load, referrer, profile_, |
693 transition, false, instance); | 719 transition, false, instance); |
694 tabstrip_model_.AddTabContents(contents, index, force_index, | 720 contents->SetAppExtensionById(app_extension_id); |
695 transition, foreground); | 721 // TODO(sky): TabStripModel::AddTabContents should take add_types directly. |
| 722 tabstrip_model_.AddTabContents(contents, index, |
| 723 (add_types & ADD_FORCE_INDEX) != 0, |
| 724 transition, |
| 725 (add_types & ADD_SELECTED) != 0); |
| 726 tabstrip_model_.SetTabPinned( |
| 727 tabstrip_model_.GetIndexOfTabContents(contents), |
| 728 (add_types & ADD_PINNED) != 0); |
| 729 |
696 // By default, content believes it is not hidden. When adding contents | 730 // By default, content believes it is not hidden. When adding contents |
697 // in the background, tell it that it's hidden. | 731 // in the background, tell it that it's hidden. |
698 if (!foreground) | 732 if ((add_types & ADD_SELECTED) == 0) { |
| 733 // TODO(sky): see if this is really needed. I suspect not as |
| 734 // TabStripModel::AddTabContents invokes HideContents if not foreground. |
699 contents->WasHidden(); | 735 contents->WasHidden(); |
| 736 } |
700 } else { | 737 } else { |
701 // We're in an app window or a popup window. Find an existing browser to | 738 // We're in an app window or a popup window. Find an existing browser to |
702 // open this URL in, creating one if none exists. | 739 // open this URL in, creating one if none exists. |
703 Browser* b = GetOrCreateTabbedBrowser(profile_); | 740 Browser* b = GetOrCreateTabbedBrowser(profile_); |
704 contents = b->AddTabWithURL(url, referrer, transition, foreground, index, | 741 contents = b->AddTabWithURL(url, referrer, transition, index, add_types, |
705 force_index, instance); | 742 instance, app_extension_id); |
706 b->window()->Show(); | 743 b->window()->Show(); |
707 } | 744 } |
708 return contents; | 745 return contents; |
709 } | 746 } |
710 | 747 |
711 TabContents* Browser::AddTab(TabContents* tab_contents, | 748 TabContents* Browser::AddTab(TabContents* tab_contents, |
712 PageTransition::Type type) { | 749 PageTransition::Type type) { |
713 tabstrip_model_.AddTabContents(tab_contents, -1, false, type, true); | 750 tabstrip_model_.AddTabContents(tab_contents, -1, false, type, true); |
714 return tab_contents; | 751 return tab_contents; |
715 } | 752 } |
(...skipping 25 matching lines...) Expand all Loading... |
741 TabContents* Browser::AddRestoredTab( | 778 TabContents* Browser::AddRestoredTab( |
742 const std::vector<TabNavigation>& navigations, | 779 const std::vector<TabNavigation>& navigations, |
743 int tab_index, | 780 int tab_index, |
744 int selected_navigation, | 781 int selected_navigation, |
745 const std::string& app_extension_id, | 782 const std::string& app_extension_id, |
746 bool select, | 783 bool select, |
747 bool pin, | 784 bool pin, |
748 bool from_last_session) { | 785 bool from_last_session) { |
749 TabContents* new_tab = new TabContents(profile(), NULL, | 786 TabContents* new_tab = new TabContents(profile(), NULL, |
750 MSG_ROUTING_NONE, tabstrip_model_.GetSelectedTabContents()); | 787 MSG_ROUTING_NONE, tabstrip_model_.GetSelectedTabContents()); |
751 SetAppExtensionById(new_tab, app_extension_id); | 788 new_tab->SetAppExtensionById(app_extension_id); |
752 new_tab->controller().RestoreFromState(navigations, selected_navigation, | 789 new_tab->controller().RestoreFromState(navigations, selected_navigation, |
753 from_last_session); | 790 from_last_session); |
754 | 791 |
755 bool really_pin = | 792 bool really_pin = |
756 (pin && tab_index == tabstrip_model()->IndexOfFirstNonMiniTab()); | 793 (pin && tab_index == tabstrip_model()->IndexOfFirstNonMiniTab()); |
757 tabstrip_model_.InsertTabContentsAt(tab_index, new_tab, select, false); | 794 tabstrip_model_.InsertTabContentsAt(tab_index, new_tab, select, false); |
758 if (really_pin) | 795 if (really_pin) |
759 tabstrip_model_.SetTabPinned(tab_index, true); | 796 tabstrip_model_.SetTabPinned(tab_index, true); |
760 if (select) | 797 if (select) |
761 window_->Activate(); | 798 window_->Activate(); |
762 if (profile_->HasSessionService()) { | 799 if (profile_->HasSessionService()) { |
763 SessionService* session_service = profile_->GetSessionService(); | 800 SessionService* session_service = profile_->GetSessionService(); |
764 if (session_service) | 801 if (session_service) |
765 session_service->TabRestored(&new_tab->controller(), really_pin); | 802 session_service->TabRestored(&new_tab->controller(), really_pin); |
766 } | 803 } |
767 return new_tab; | 804 return new_tab; |
768 } | 805 } |
769 | 806 |
770 void Browser::ReplaceRestoredTab( | 807 void Browser::ReplaceRestoredTab( |
771 const std::vector<TabNavigation>& navigations, | 808 const std::vector<TabNavigation>& navigations, |
772 int selected_navigation, | 809 int selected_navigation, |
773 bool from_last_session, | 810 bool from_last_session, |
774 const std::string& app_extension_id) { | 811 const std::string& app_extension_id) { |
775 TabContents* replacement = new TabContents(profile(), NULL, | 812 TabContents* replacement = new TabContents(profile(), NULL, |
776 MSG_ROUTING_NONE, tabstrip_model_.GetSelectedTabContents()); | 813 MSG_ROUTING_NONE, tabstrip_model_.GetSelectedTabContents()); |
777 SetAppExtensionById(replacement, app_extension_id); | 814 replacement->SetAppExtensionById(app_extension_id); |
778 replacement->controller().RestoreFromState(navigations, selected_navigation, | 815 replacement->controller().RestoreFromState(navigations, selected_navigation, |
779 from_last_session); | 816 from_last_session); |
780 | 817 |
781 tabstrip_model_.ReplaceNavigationControllerAt( | 818 tabstrip_model_.ReplaceNavigationControllerAt( |
782 tabstrip_model_.selected_index(), | 819 tabstrip_model_.selected_index(), |
783 &replacement->controller()); | 820 &replacement->controller()); |
784 } | 821 } |
785 | 822 |
786 bool Browser::CanRestoreTab() { | 823 bool Browser::CanRestoreTab() { |
787 TabRestoreService* service = profile_->GetTabRestoreService(); | 824 TabRestoreService* service = profile_->GetTabRestoreService(); |
(...skipping 2600 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3388 if (TabHasUnloadListener(contents)) { | 3425 if (TabHasUnloadListener(contents)) { |
3389 // If the page has unload listeners, then we tell the renderer to fire | 3426 // If the page has unload listeners, then we tell the renderer to fire |
3390 // them. Once they have fired, we'll get a message back saying whether | 3427 // them. Once they have fired, we'll get a message back saying whether |
3391 // to proceed closing the page or not, which sends us back to this method | 3428 // to proceed closing the page or not, which sends us back to this method |
3392 // with the HasUnloadListener bit cleared. | 3429 // with the HasUnloadListener bit cleared. |
3393 contents->render_view_host()->FirePageBeforeUnload(false); | 3430 contents->render_view_host()->FirePageBeforeUnload(false); |
3394 return true; | 3431 return true; |
3395 } | 3432 } |
3396 return false; | 3433 return false; |
3397 } | 3434 } |
3398 | |
3399 void Browser::SetAppExtensionById(TabContents* contents, | |
3400 const std::string& app_extension_id) { | |
3401 if (app_extension_id.empty()) | |
3402 return; | |
3403 | |
3404 ExtensionsService* extension_service = profile()->GetExtensionsService(); | |
3405 if (extension_service && extension_service->is_ready()) { | |
3406 Extension* extension = | |
3407 extension_service->GetExtensionById(app_extension_id, false); | |
3408 if (extension) | |
3409 contents->SetAppExtension(extension); | |
3410 } | |
3411 } | |
OLD | NEW |