| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/ui/browser_commands.h" | 5 #include "chrome/browser/ui/browser_commands.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 *extension = i->get(); | 160 *extension = i->get(); |
| 161 *command = prospective_command; | 161 *command = prospective_command; |
| 162 *command_type = prospective_command_type; | 162 *command_type = prospective_command_type; |
| 163 return true; | 163 return true; |
| 164 } | 164 } |
| 165 } | 165 } |
| 166 return false; | 166 return false; |
| 167 } | 167 } |
| 168 #endif | 168 #endif |
| 169 | 169 |
| 170 void BookmarkCurrentPageInternal(Browser* browser) { | |
| 171 content::RecordAction(UserMetricsAction("Star")); | |
| 172 | |
| 173 BookmarkModel* model = | |
| 174 BookmarkModelFactory::GetForProfile(browser->profile()); | |
| 175 if (!model || !model->loaded()) | |
| 176 return; // Ignore requests until bookmarks are loaded. | |
| 177 | |
| 178 GURL url; | |
| 179 base::string16 title; | |
| 180 WebContents* web_contents = | |
| 181 browser->tab_strip_model()->GetActiveWebContents(); | |
| 182 GetURLAndTitleToBookmark(web_contents, &url, &title); | |
| 183 bool is_bookmarked_by_any = model->IsBookmarked(url); | |
| 184 if (!is_bookmarked_by_any && | |
| 185 web_contents->GetBrowserContext()->IsOffTheRecord()) { | |
| 186 // If we're incognito the favicon may not have been saved. Save it now | |
| 187 // so that bookmarks have an icon for the page. | |
| 188 FaviconTabHelper::FromWebContents(web_contents)->SaveFavicon(); | |
| 189 } | |
| 190 bool was_bookmarked_by_user = bookmarks::IsBookmarkedByUser(model, url); | |
| 191 bookmarks::AddIfNotBookmarked(model, url, title); | |
| 192 bool is_bookmarked_by_user = bookmarks::IsBookmarkedByUser(model, url); | |
| 193 // Make sure the model actually added a bookmark before showing the star. A | |
| 194 // bookmark isn't created if the url is invalid. | |
| 195 if (browser->window()->IsActive() && is_bookmarked_by_user) { | |
| 196 // Only show the bubble if the window is active, otherwise we may get into | |
| 197 // weird situations where the bubble is deleted as soon as it is shown. | |
| 198 browser->window()->ShowBookmarkBubble(url, was_bookmarked_by_user); | |
| 199 } | |
| 200 } | |
| 201 | |
| 202 // Based on |disposition|, creates a new tab as necessary, and returns the | 170 // Based on |disposition|, creates a new tab as necessary, and returns the |
| 203 // appropriate tab to navigate. If that tab is the current tab, reverts the | 171 // appropriate tab to navigate. If that tab is the current tab, reverts the |
| 204 // location bar contents, since all browser-UI-triggered navigations should | 172 // location bar contents, since all browser-UI-triggered navigations should |
| 205 // revert any omnibox edits in the current tab. | 173 // revert any omnibox edits in the current tab. |
| 206 WebContents* GetTabAndRevertIfNecessary(Browser* browser, | 174 WebContents* GetTabAndRevertIfNecessary(Browser* browser, |
| 207 WindowOpenDisposition disposition) { | 175 WindowOpenDisposition disposition) { |
| 208 WebContents* current_tab = browser->tab_strip_model()->GetActiveWebContents(); | 176 WebContents* current_tab = browser->tab_strip_model()->GetActiveWebContents(); |
| 209 switch (disposition) { | 177 switch (disposition) { |
| 210 case NEW_FOREGROUND_TAB: | 178 case NEW_FOREGROUND_TAB: |
| 211 case NEW_BACKGROUND_TAB: { | 179 case NEW_BACKGROUND_TAB: { |
| (...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 737 browser->host_desktop_type())); | 705 browser->host_desktop_type())); |
| 738 b->tab_strip_model()->AppendWebContents(contents, true); | 706 b->tab_strip_model()->AppendWebContents(contents, true); |
| 739 b->window()->Show(); | 707 b->window()->Show(); |
| 740 } | 708 } |
| 741 | 709 |
| 742 void Exit() { | 710 void Exit() { |
| 743 content::RecordAction(UserMetricsAction("Exit")); | 711 content::RecordAction(UserMetricsAction("Exit")); |
| 744 chrome::AttemptUserExit(); | 712 chrome::AttemptUserExit(); |
| 745 } | 713 } |
| 746 | 714 |
| 747 void BookmarkCurrentPage(Browser* browser) { | 715 void BookmarkCurrentPageIgnoringExtensionOverrides(Browser* browser) { |
| 716 content::RecordAction(UserMetricsAction("Star")); |
| 717 |
| 718 BookmarkModel* model = |
| 719 BookmarkModelFactory::GetForProfile(browser->profile()); |
| 720 if (!model || !model->loaded()) |
| 721 return; // Ignore requests until bookmarks are loaded. |
| 722 |
| 723 GURL url; |
| 724 base::string16 title; |
| 725 WebContents* web_contents = |
| 726 browser->tab_strip_model()->GetActiveWebContents(); |
| 727 GetURLAndTitleToBookmark(web_contents, &url, &title); |
| 728 bool is_bookmarked_by_any = model->IsBookmarked(url); |
| 729 if (!is_bookmarked_by_any && |
| 730 web_contents->GetBrowserContext()->IsOffTheRecord()) { |
| 731 // If we're incognito the favicon may not have been saved. Save it now |
| 732 // so that bookmarks have an icon for the page. |
| 733 FaviconTabHelper::FromWebContents(web_contents)->SaveFavicon(); |
| 734 } |
| 735 bool was_bookmarked_by_user = bookmarks::IsBookmarkedByUser(model, url); |
| 736 bookmarks::AddIfNotBookmarked(model, url, title); |
| 737 bool is_bookmarked_by_user = bookmarks::IsBookmarkedByUser(model, url); |
| 738 // Make sure the model actually added a bookmark before showing the star. A |
| 739 // bookmark isn't created if the url is invalid. |
| 740 if (browser->window()->IsActive() && is_bookmarked_by_user) { |
| 741 // Only show the bubble if the window is active, otherwise we may get into |
| 742 // weird situations where the bubble is deleted as soon as it is shown. |
| 743 browser->window()->ShowBookmarkBubble(url, was_bookmarked_by_user); |
| 744 } |
| 745 } |
| 746 |
| 747 void BookmarkCurrentPageAllowingExtensionOverrides(Browser* browser) { |
| 748 DCHECK(!chrome::ShouldRemoveBookmarkThisPageUI(browser->profile())); | 748 DCHECK(!chrome::ShouldRemoveBookmarkThisPageUI(browser->profile())); |
| 749 | 749 |
| 750 #if defined(ENABLE_EXTENSIONS) | 750 #if defined(ENABLE_EXTENSIONS) |
| 751 const extensions::Extension* extension = NULL; | 751 const extensions::Extension* extension = NULL; |
| 752 extensions::Command command; | 752 extensions::Command command; |
| 753 extensions::CommandService::ExtensionCommandType command_type; | 753 extensions::CommandService::ExtensionCommandType command_type; |
| 754 if (GetBookmarkOverrideCommand(browser->profile(), | 754 if (GetBookmarkOverrideCommand(browser->profile(), |
| 755 &extension, | 755 &extension, |
| 756 &command, | 756 &command, |
| 757 &command_type)) { | 757 &command_type)) { |
| 758 switch (command_type) { | 758 switch (command_type) { |
| 759 case extensions::CommandService::NAMED: | 759 case extensions::CommandService::NAMED: |
| 760 browser->window()->ExecuteExtensionCommand(extension, command); | 760 browser->window()->ExecuteExtensionCommand(extension, command); |
| 761 break; | 761 break; |
| 762 case extensions::CommandService::BROWSER_ACTION: | 762 case extensions::CommandService::BROWSER_ACTION: |
| 763 case extensions::CommandService::PAGE_ACTION: | 763 case extensions::CommandService::PAGE_ACTION: |
| 764 // BookmarkCurrentPage is called through a user gesture, so it is safe | 764 // BookmarkCurrentPage is called through a user gesture, so it is safe |
| 765 // to grant the active tab permission. | 765 // to grant the active tab permission. |
| 766 extensions::ExtensionActionAPI::Get(browser->profile())-> | 766 extensions::ExtensionActionAPI::Get(browser->profile())-> |
| 767 ShowExtensionActionPopup(extension, browser, true); | 767 ShowExtensionActionPopup(extension, browser, true); |
| 768 break; | 768 break; |
| 769 } | 769 } |
| 770 return; | 770 return; |
| 771 } | 771 } |
| 772 #endif | 772 #endif |
| 773 | 773 BookmarkCurrentPageIgnoringExtensionOverrides(browser); |
| 774 BookmarkCurrentPageInternal(browser); | |
| 775 } | 774 } |
| 776 | 775 |
| 777 bool CanBookmarkCurrentPage(const Browser* browser) { | 776 bool CanBookmarkCurrentPage(const Browser* browser) { |
| 778 return CanBookmarkCurrentPageInternal(browser, true); | 777 return CanBookmarkCurrentPageInternal(browser, true); |
| 779 } | 778 } |
| 780 | 779 |
| 781 void BookmarkAllTabs(Browser* browser) { | 780 void BookmarkAllTabs(Browser* browser) { |
| 782 content::RecordAction(UserMetricsAction("BookmarkAllTabs")); | 781 content::RecordAction(UserMetricsAction("BookmarkAllTabs")); |
| 783 chrome::ShowBookmarkAllTabsDialog(browser); | 782 chrome::ShowBookmarkAllTabsDialog(browser); |
| 784 } | 783 } |
| (...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1271 browser->host_desktop_type())); | 1270 browser->host_desktop_type())); |
| 1272 app_browser->tab_strip_model()->AppendWebContents(contents, true); | 1271 app_browser->tab_strip_model()->AppendWebContents(contents, true); |
| 1273 | 1272 |
| 1274 contents->GetMutableRendererPrefs()->can_accept_load_drops = false; | 1273 contents->GetMutableRendererPrefs()->can_accept_load_drops = false; |
| 1275 contents->GetRenderViewHost()->SyncRendererPrefs(); | 1274 contents->GetRenderViewHost()->SyncRendererPrefs(); |
| 1276 app_browser->window()->Show(); | 1275 app_browser->window()->Show(); |
| 1277 } | 1276 } |
| 1278 #endif // defined(ENABLE_EXTENSIONS) | 1277 #endif // defined(ENABLE_EXTENSIONS) |
| 1279 | 1278 |
| 1280 } // namespace chrome | 1279 } // namespace chrome |
| OLD | NEW |