OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/idle_timer.h" | 9 #include "base/idle_timer.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 693 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
704 } | 704 } |
705 | 705 |
706 void Browser::Home(WindowOpenDisposition disposition) { | 706 void Browser::Home(WindowOpenDisposition disposition) { |
707 UserMetrics::RecordAction(L"Home", profile_); | 707 UserMetrics::RecordAction(L"Home", profile_); |
708 OpenURL(GetHomePage(), GURL(), disposition, PageTransition::AUTO_BOOKMARK); | 708 OpenURL(GetHomePage(), GURL(), disposition, PageTransition::AUTO_BOOKMARK); |
709 } | 709 } |
710 | 710 |
711 void Browser::OpenCurrentURL() { | 711 void Browser::OpenCurrentURL() { |
712 UserMetrics::RecordAction(L"LoadURL", profile_); | 712 UserMetrics::RecordAction(L"LoadURL", profile_); |
713 LocationBar* location_bar = window_->GetLocationBar(); | 713 LocationBar* location_bar = window_->GetLocationBar(); |
714 OpenURL(GURL(WideToUTF8(location_bar->GetInputString())), GURL(), | 714 OpenURLAtIndex(NULL, GURL(WideToUTF8(location_bar->GetInputString())), GURL(), |
715 location_bar->GetWindowOpenDisposition(), | 715 location_bar->GetWindowOpenDisposition(), |
716 location_bar->GetPageTransition()); | 716 location_bar->GetPageTransition(), -1, true); |
717 } | 717 } |
718 | 718 |
719 void Browser::Go(WindowOpenDisposition disposition) { | 719 void Browser::Go(WindowOpenDisposition disposition) { |
720 UserMetrics::RecordAction(L"Go", profile_); | 720 UserMetrics::RecordAction(L"Go", profile_); |
721 window_->GetLocationBar()->AcceptInputWithDisposition(disposition); | 721 window_->GetLocationBar()->AcceptInputWithDisposition(disposition); |
722 } | 722 } |
723 | 723 |
724 void Browser::Stop() { | 724 void Browser::Stop() { |
725 UserMetrics::RecordAction(L"Stop", profile_); | 725 UserMetrics::RecordAction(L"Stop", profile_); |
726 GetSelectedTabContents()->Stop(); | 726 GetSelectedTabContents()->Stop(); |
(...skipping 933 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1660 void Browser::OpenURL(const GURL& url, const GURL& referrer, | 1660 void Browser::OpenURL(const GURL& url, const GURL& referrer, |
1661 WindowOpenDisposition disposition, | 1661 WindowOpenDisposition disposition, |
1662 PageTransition::Type transition) { | 1662 PageTransition::Type transition) { |
1663 OpenURLFromTab(NULL, url, referrer, disposition, transition); | 1663 OpenURLFromTab(NULL, url, referrer, disposition, transition); |
1664 } | 1664 } |
1665 | 1665 |
1666 /////////////////////////////////////////////////////////////////////////////// | 1666 /////////////////////////////////////////////////////////////////////////////// |
1667 // Browser, TabContentsDelegate implementation: | 1667 // Browser, TabContentsDelegate implementation: |
1668 | 1668 |
1669 void Browser::OpenURLFromTab(TabContents* source, | 1669 void Browser::OpenURLFromTab(TabContents* source, |
1670 const GURL& url, const GURL& referrer, | 1670 const GURL& url, |
| 1671 const GURL& referrer, |
1671 WindowOpenDisposition disposition, | 1672 WindowOpenDisposition disposition, |
1672 PageTransition::Type transition) { | 1673 PageTransition::Type transition) { |
1673 // TODO(beng): Move all this code into a separate helper that has unit tests. | 1674 OpenURLAtIndex(source, url, referrer, disposition, transition, -1, false); |
1674 | |
1675 // No code for these yet | |
1676 DCHECK((disposition != NEW_POPUP) && (disposition != SAVE_TO_DISK)); | |
1677 | |
1678 TabContents* current_tab = source ? source : GetSelectedTabContents(); | |
1679 bool source_tab_was_frontmost = (current_tab == GetSelectedTabContents()); | |
1680 TabContents* new_contents = NULL; | |
1681 | |
1682 // If the URL is part of the same web site, then load it in the same | |
1683 // SiteInstance (and thus the same process). This is an optimization to | |
1684 // reduce process overhead; it is not necessary for compatibility. (That is, | |
1685 // the new tab will not have script connections to the previous tab, so it | |
1686 // does not need to be part of the same SiteInstance or BrowsingInstance.) | |
1687 // Default to loading in a new SiteInstance and BrowsingInstance. | |
1688 // TODO(creis): should this apply to applications? | |
1689 SiteInstance* instance = NULL; | |
1690 // Don't use this logic when "--process-per-tab" is specified. | |
1691 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kProcessPerTab)) { | |
1692 if (current_tab) { | |
1693 const GURL& current_url = current_tab->GetURL(); | |
1694 if (SiteInstance::IsSameWebSite(current_url, url)) | |
1695 instance = current_tab->GetSiteInstance(); | |
1696 } | |
1697 } | |
1698 | |
1699 // If this is not a normal window (such as a popup or an application), we can | |
1700 // only have one tab so a new tab always goes into a tabbed browser window. | |
1701 if (disposition != NEW_WINDOW && type_ != TYPE_NORMAL) { | |
1702 // If the disposition is OFF_THE_RECORD we don't want to create a new | |
1703 // browser that will itself create another OTR browser. This will result in | |
1704 // a browser leak (and crash below because no tab is created or selected). | |
1705 if (disposition == OFF_THE_RECORD) { | |
1706 OpenURLOffTheRecord(profile_, url); | |
1707 return; | |
1708 } | |
1709 | |
1710 Browser* b = GetOrCreateTabbedBrowser(); | |
1711 DCHECK(b); | |
1712 | |
1713 // If we have just created a new browser window, make sure we select the | |
1714 // tab. | |
1715 if (b->tab_count() == 0 && disposition == NEW_BACKGROUND_TAB) | |
1716 disposition = NEW_FOREGROUND_TAB; | |
1717 | |
1718 b->OpenURL(url, referrer, disposition, transition); | |
1719 b->window()->Show(); | |
1720 return; | |
1721 } | |
1722 | |
1723 if (profile_->IsOffTheRecord() && disposition == OFF_THE_RECORD) | |
1724 disposition = NEW_FOREGROUND_TAB; | |
1725 | |
1726 if (disposition == SINGLETON_TAB) { | |
1727 ShowSingleDOMUITab(url); | |
1728 return; | |
1729 } else if (disposition == NEW_WINDOW) { | |
1730 Browser* browser = Browser::Create(profile_); | |
1731 new_contents = browser->AddTabWithURL(url, referrer, transition, true, -1, | |
1732 false, instance); | |
1733 browser->window()->Show(); | |
1734 } else if ((disposition == CURRENT_TAB) && current_tab) { | |
1735 tabstrip_model_.TabNavigating(current_tab, transition); | |
1736 | |
1737 current_tab->controller().LoadURL(url, referrer, transition); | |
1738 new_contents = current_tab; | |
1739 if (GetStatusBubble()) | |
1740 GetStatusBubble()->Hide(); | |
1741 | |
1742 // Update the location bar. This is synchronous. We specfically don't update | |
1743 // the load state since the load hasn't started yet and updating it will put | |
1744 // it out of sync with the actual state like whether we're displaying a | |
1745 // favicon, which controls the throbber. If we updated it here, the throbber | |
1746 // will show the default favicon for a split second when navigating away | |
1747 // from the new tab page. | |
1748 ScheduleUIUpdate(current_tab, TabContents::INVALIDATE_URL); | |
1749 } else if (disposition == OFF_THE_RECORD) { | |
1750 OpenURLOffTheRecord(profile_, url); | |
1751 return; | |
1752 } else if (disposition != SUPPRESS_OPEN) { | |
1753 new_contents = AddTabWithURL(url, referrer, transition, | |
1754 disposition != NEW_BACKGROUND_TAB, -1, false, | |
1755 instance); | |
1756 } | |
1757 | |
1758 if (disposition != NEW_BACKGROUND_TAB && source_tab_was_frontmost) { | |
1759 // Give the focus to the newly navigated tab, if the source tab was | |
1760 // front-most. | |
1761 new_contents->Focus(); | |
1762 } | |
1763 } | 1675 } |
1764 | 1676 |
1765 void Browser::NavigationStateChanged(const TabContents* source, | 1677 void Browser::NavigationStateChanged(const TabContents* source, |
1766 unsigned changed_flags) { | 1678 unsigned changed_flags) { |
1767 // Only update the UI when something visible has changed. | 1679 // Only update the UI when something visible has changed. |
1768 if (changed_flags) | 1680 if (changed_flags) |
1769 ScheduleUIUpdate(source, changed_flags); | 1681 ScheduleUIUpdate(source, changed_flags); |
1770 | 1682 |
1771 // We don't schedule updates to commands since they will only change once per | 1683 // We don't schedule updates to commands since they will only change once per |
1772 // navigation, so we don't have to worry about flickering. | 1684 // navigation, so we don't have to worry about flickering. |
(...skipping 794 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2567 // Browser, Assorted utility functions (private): | 2479 // Browser, Assorted utility functions (private): |
2568 | 2480 |
2569 Browser* Browser::GetOrCreateTabbedBrowser() { | 2481 Browser* Browser::GetOrCreateTabbedBrowser() { |
2570 Browser* browser = BrowserList::FindBrowserWithType( | 2482 Browser* browser = BrowserList::FindBrowserWithType( |
2571 profile_, TYPE_NORMAL); | 2483 profile_, TYPE_NORMAL); |
2572 if (!browser) | 2484 if (!browser) |
2573 browser = Browser::Create(profile_); | 2485 browser = Browser::Create(profile_); |
2574 return browser; | 2486 return browser; |
2575 } | 2487 } |
2576 | 2488 |
| 2489 void Browser::OpenURLAtIndex(TabContents* source, |
| 2490 const GURL& url, |
| 2491 const GURL& referrer, |
| 2492 WindowOpenDisposition disposition, |
| 2493 PageTransition::Type transition, |
| 2494 int index, |
| 2495 bool force_index) { |
| 2496 // TODO(beng): Move all this code into a separate helper that has unit tests. |
| 2497 |
| 2498 // No code for these yet |
| 2499 DCHECK((disposition != NEW_POPUP) && (disposition != SAVE_TO_DISK)); |
| 2500 |
| 2501 TabContents* current_tab = source ? source : GetSelectedTabContents(); |
| 2502 bool source_tab_was_frontmost = (current_tab == GetSelectedTabContents()); |
| 2503 TabContents* new_contents = NULL; |
| 2504 |
| 2505 // If the URL is part of the same web site, then load it in the same |
| 2506 // SiteInstance (and thus the same process). This is an optimization to |
| 2507 // reduce process overhead; it is not necessary for compatibility. (That is, |
| 2508 // the new tab will not have script connections to the previous tab, so it |
| 2509 // does not need to be part of the same SiteInstance or BrowsingInstance.) |
| 2510 // Default to loading in a new SiteInstance and BrowsingInstance. |
| 2511 // TODO(creis): should this apply to applications? |
| 2512 SiteInstance* instance = NULL; |
| 2513 // Don't use this logic when "--process-per-tab" is specified. |
| 2514 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kProcessPerTab)) { |
| 2515 if (current_tab) { |
| 2516 const GURL& current_url = current_tab->GetURL(); |
| 2517 if (SiteInstance::IsSameWebSite(current_url, url)) |
| 2518 instance = current_tab->GetSiteInstance(); |
| 2519 } |
| 2520 } |
| 2521 |
| 2522 // If this is not a normal window (such as a popup or an application), we can |
| 2523 // only have one tab so a new tab always goes into a tabbed browser window. |
| 2524 if (disposition != NEW_WINDOW && type_ != TYPE_NORMAL) { |
| 2525 // If the disposition is OFF_THE_RECORD we don't want to create a new |
| 2526 // browser that will itself create another OTR browser. This will result in |
| 2527 // a browser leak (and crash below because no tab is created or selected). |
| 2528 if (disposition == OFF_THE_RECORD) { |
| 2529 OpenURLOffTheRecord(profile_, url); |
| 2530 return; |
| 2531 } |
| 2532 |
| 2533 Browser* b = GetOrCreateTabbedBrowser(); |
| 2534 DCHECK(b); |
| 2535 |
| 2536 // If we have just created a new browser window, make sure we select the |
| 2537 // tab. |
| 2538 if (b->tab_count() == 0 && disposition == NEW_BACKGROUND_TAB) |
| 2539 disposition = NEW_FOREGROUND_TAB; |
| 2540 |
| 2541 b->OpenURL(url, referrer, disposition, transition); |
| 2542 b->window()->Show(); |
| 2543 return; |
| 2544 } |
| 2545 |
| 2546 if (profile_->IsOffTheRecord() && disposition == OFF_THE_RECORD) |
| 2547 disposition = NEW_FOREGROUND_TAB; |
| 2548 |
| 2549 if (disposition == SINGLETON_TAB) { |
| 2550 ShowSingleDOMUITab(url); |
| 2551 return; |
| 2552 } else if (disposition == NEW_WINDOW) { |
| 2553 Browser* browser = Browser::Create(profile_); |
| 2554 new_contents = browser->AddTabWithURL(url, referrer, transition, true, |
| 2555 index, force_index, instance); |
| 2556 browser->window()->Show(); |
| 2557 } else if ((disposition == CURRENT_TAB) && current_tab) { |
| 2558 tabstrip_model_.TabNavigating(current_tab, transition); |
| 2559 |
| 2560 current_tab->controller().LoadURL(url, referrer, transition); |
| 2561 new_contents = current_tab; |
| 2562 if (GetStatusBubble()) |
| 2563 GetStatusBubble()->Hide(); |
| 2564 |
| 2565 // Update the location bar. This is synchronous. We specfically don't update |
| 2566 // the load state since the load hasn't started yet and updating it will put |
| 2567 // it out of sync with the actual state like whether we're displaying a |
| 2568 // favicon, which controls the throbber. If we updated it here, the throbber |
| 2569 // will show the default favicon for a split second when navigating away |
| 2570 // from the new tab page. |
| 2571 ScheduleUIUpdate(current_tab, TabContents::INVALIDATE_URL); |
| 2572 } else if (disposition == OFF_THE_RECORD) { |
| 2573 OpenURLOffTheRecord(profile_, url); |
| 2574 return; |
| 2575 } else if (disposition != SUPPRESS_OPEN) { |
| 2576 new_contents = AddTabWithURL(url, referrer, transition, |
| 2577 disposition != NEW_BACKGROUND_TAB, index, force_index, instance); |
| 2578 } |
| 2579 |
| 2580 if (disposition != NEW_BACKGROUND_TAB && source_tab_was_frontmost) { |
| 2581 // Give the focus to the newly navigated tab, if the source tab was |
| 2582 // front-most. |
| 2583 new_contents->Focus(); |
| 2584 } |
| 2585 } |
| 2586 |
2577 void Browser::BuildPopupWindow(TabContents* source, | 2587 void Browser::BuildPopupWindow(TabContents* source, |
2578 TabContents* new_contents, | 2588 TabContents* new_contents, |
2579 const gfx::Rect& initial_pos) { | 2589 const gfx::Rect& initial_pos) { |
2580 BuildPopupWindowHelper(source, new_contents, initial_pos, | 2590 BuildPopupWindowHelper(source, new_contents, initial_pos, |
2581 (type_ & TYPE_APP) ? TYPE_APP_POPUP : TYPE_POPUP, | 2591 (type_ & TYPE_APP) ? TYPE_APP_POPUP : TYPE_POPUP, |
2582 profile_, false); | 2592 profile_, false); |
2583 } | 2593 } |
2584 | 2594 |
2585 void Browser::BuildPopupWindowHelper(TabContents* source, | 2595 void Browser::BuildPopupWindowHelper(TabContents* source, |
2586 TabContents* new_contents, | 2596 TabContents* new_contents, |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2647 | 2657 |
2648 // We need to register the window position pref. | 2658 // We need to register the window position pref. |
2649 std::wstring window_pref(prefs::kBrowserWindowPlacement); | 2659 std::wstring window_pref(prefs::kBrowserWindowPlacement); |
2650 window_pref.append(L"_"); | 2660 window_pref.append(L"_"); |
2651 window_pref.append(app_name); | 2661 window_pref.append(app_name); |
2652 PrefService* prefs = g_browser_process->local_state(); | 2662 PrefService* prefs = g_browser_process->local_state(); |
2653 DCHECK(prefs); | 2663 DCHECK(prefs); |
2654 | 2664 |
2655 prefs->RegisterDictionaryPref(window_pref.c_str()); | 2665 prefs->RegisterDictionaryPref(window_pref.c_str()); |
2656 } | 2666 } |
OLD | NEW |