Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(797)

Side by Side Diff: chrome/browser/ui/browser_commands.cc

Issue 10696148: Move TabStripModelDelegate off Browser into its own class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/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/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/bookmarks/bookmark_editor.h" 10 #include "chrome/browser/bookmarks/bookmark_editor.h"
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 using content::Referrer; 76 using content::Referrer;
77 using content::SSLStatus; 77 using content::SSLStatus;
78 using content::UserMetricsAction; 78 using content::UserMetricsAction;
79 using content::WebContents; 79 using content::WebContents;
80 80
81 namespace chrome { 81 namespace chrome {
82 namespace { 82 namespace {
83 83
84 WebContents* GetOrCloneTabForDisposition(Browser* browser, 84 WebContents* GetOrCloneTabForDisposition(Browser* browser,
85 WindowOpenDisposition disposition) { 85 WindowOpenDisposition disposition) {
86 TabContents* current_tab = chrome::GetActiveTabContents(browser); 86 TabContents* current_tab = GetActiveTabContents(browser);
87 switch (disposition) { 87 switch (disposition) {
88 case NEW_FOREGROUND_TAB: 88 case NEW_FOREGROUND_TAB:
89 case NEW_BACKGROUND_TAB: { 89 case NEW_BACKGROUND_TAB: {
90 current_tab = current_tab->Clone(); 90 current_tab = current_tab->Clone();
91 browser->tab_strip_model()->AddTabContents( 91 browser->tab_strip_model()->AddTabContents(
92 current_tab, -1, content::PAGE_TRANSITION_LINK, 92 current_tab, -1, content::PAGE_TRANSITION_LINK,
93 disposition == NEW_FOREGROUND_TAB ? TabStripModel::ADD_ACTIVE : 93 disposition == NEW_FOREGROUND_TAB ? TabStripModel::ADD_ACTIVE :
94 TabStripModel::ADD_NONE); 94 TabStripModel::ADD_NONE);
95 break; 95 break;
96 } 96 }
97 case NEW_WINDOW: { 97 case NEW_WINDOW: {
98 current_tab = current_tab->Clone(); 98 current_tab = current_tab->Clone();
99 Browser* b = Browser::Create(browser->profile()); 99 Browser* b = Browser::Create(browser->profile());
100 b->tab_strip_model()->AddTabContents( 100 b->tab_strip_model()->AddTabContents(
101 current_tab, -1, content::PAGE_TRANSITION_LINK, 101 current_tab, -1, content::PAGE_TRANSITION_LINK,
102 TabStripModel::ADD_ACTIVE); 102 TabStripModel::ADD_ACTIVE);
103 b->window()->Show(); 103 b->window()->Show();
104 break; 104 break;
105 } 105 }
106 default: 106 default:
107 break; 107 break;
108 } 108 }
109 return current_tab->web_contents(); 109 return current_tab->web_contents();
110 } 110 }
111 111
112 void ReloadInternal(Browser* browser, 112 void ReloadInternal(Browser* browser,
113 WindowOpenDisposition disposition, 113 WindowOpenDisposition disposition,
114 bool ignore_cache) { 114 bool ignore_cache) {
115 // If we are showing an interstitial, treat this as an OpenURL. 115 // If we are showing an interstitial, treat this as an OpenURL.
116 WebContents* current_tab = chrome::GetActiveWebContents(browser); 116 WebContents* current_tab = GetActiveWebContents(browser);
117 if (current_tab && current_tab->ShowingInterstitialPage()) { 117 if (current_tab && current_tab->ShowingInterstitialPage()) {
118 NavigationEntry* entry = current_tab->GetController().GetActiveEntry(); 118 NavigationEntry* entry = current_tab->GetController().GetActiveEntry();
119 DCHECK(entry); // Should exist if interstitial is showing. 119 DCHECK(entry); // Should exist if interstitial is showing.
120 browser->OpenURL(OpenURLParams( 120 browser->OpenURL(OpenURLParams(
121 entry->GetURL(), Referrer(), disposition, 121 entry->GetURL(), Referrer(), disposition,
122 content::PAGE_TRANSITION_RELOAD, false)); 122 content::PAGE_TRANSITION_RELOAD, false));
123 return; 123 return;
124 } 124 }
125 125
126 // As this is caused by a user action, give the focus to the page. 126 // As this is caused by a user action, give the focus to the page.
(...skipping 19 matching lines...) Expand all
146 bool PrintPreviewShowing(const Browser* browser) { 146 bool PrintPreviewShowing(const Browser* browser) {
147 TabContents* contents = GetActiveTabContents(browser); 147 TabContents* contents = GetActiveTabContents(browser);
148 printing::PrintPreviewTabController* controller = 148 printing::PrintPreviewTabController* controller =
149 printing::PrintPreviewTabController::GetInstance(); 149 printing::PrintPreviewTabController::GetInstance();
150 return controller && (controller->GetPrintPreviewForTab(contents) || 150 return controller && (controller->GetPrintPreviewForTab(contents) ||
151 controller->is_creating_print_preview_tab()); 151 controller->is_creating_print_preview_tab());
152 } 152 }
153 153
154 bool IsNTPModeForInstantExtendedAPI(const Browser* browser) { 154 bool IsNTPModeForInstantExtendedAPI(const Browser* browser) {
155 return browser->search_model() && 155 return browser->search_model() &&
156 chrome::search::IsInstantExtendedAPIEnabled(browser->profile()) && 156 search::IsInstantExtendedAPIEnabled(browser->profile()) &&
157 browser->search_model()->mode().is_ntp(); 157 browser->search_model()->mode().is_ntp();
158 } 158 }
159 159
160 } // namespace 160 } // namespace
161 161
162 bool IsCommandEnabled(Browser* browser, int command) { 162 bool IsCommandEnabled(Browser* browser, int command) {
163 return browser->command_controller()->command_updater()->IsCommandEnabled( 163 return browser->command_controller()->command_updater()->IsCommandEnabled(
164 command); 164 command);
165 } 165 }
166 166
167 bool SupportsCommand(Browser* browser, int command) { 167 bool SupportsCommand(Browser* browser, int command) {
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 SessionServiceFactory::GetForProfile(profile->GetOriginalProfile()); 243 SessionServiceFactory::GetForProfile(profile->GetOriginalProfile());
244 if (!session_service || 244 if (!session_service ||
245 !session_service->RestoreIfNecessary(std::vector<GURL>())) { 245 !session_service->RestoreIfNecessary(std::vector<GURL>())) {
246 OpenEmptyWindow(profile->GetOriginalProfile()); 246 OpenEmptyWindow(profile->GetOriginalProfile());
247 } 247 }
248 } 248 }
249 } 249 }
250 250
251 Browser* OpenEmptyWindow(Profile* profile) { 251 Browser* OpenEmptyWindow(Profile* profile) {
252 Browser* browser = Browser::Create(profile); 252 Browser* browser = Browser::Create(profile);
253 browser->AddBlankTab(true); 253 AddBlankTab(browser, true);
254 browser->window()->Show(); 254 browser->window()->Show();
255 return browser; 255 return browser;
256 } 256 }
257 257
258 void OpenWindowWithRestoredTabs(Profile* profile) { 258 void OpenWindowWithRestoredTabs(Profile* profile) {
259 TabRestoreService* service = TabRestoreServiceFactory::GetForProfile(profile); 259 TabRestoreService* service = TabRestoreServiceFactory::GetForProfile(profile);
260 if (service) 260 if (service)
261 service->RestoreMostRecentEntry(NULL); 261 service->RestoreMostRecentEntry(NULL);
262 } 262 }
263 263
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 if (!location_bar) 340 if (!location_bar)
341 return; 341 return;
342 342
343 WindowOpenDisposition open_disposition = 343 WindowOpenDisposition open_disposition =
344 location_bar->GetWindowOpenDisposition(); 344 location_bar->GetWindowOpenDisposition();
345 if (browser->OpenInstant(open_disposition)) 345 if (browser->OpenInstant(open_disposition))
346 return; 346 return;
347 347
348 GURL url(location_bar->GetInputString()); 348 GURL url(location_bar->GetInputString());
349 349
350 chrome::NavigateParams params(browser, url, 350 NavigateParams params(browser, url, location_bar->GetPageTransition());
351 location_bar->GetPageTransition());
352 params.disposition = open_disposition; 351 params.disposition = open_disposition;
353 // Use ADD_INHERIT_OPENER so that all pages opened by the omnibox at least 352 // Use ADD_INHERIT_OPENER so that all pages opened by the omnibox at least
354 // inherit the opener. In some cases the tabstrip will determine the group 353 // inherit the opener. In some cases the tabstrip will determine the group
355 // should be inherited, in which case the group is inherited instead of the 354 // should be inherited, in which case the group is inherited instead of the
356 // opener. 355 // opener.
357 params.tabstrip_add_types = 356 params.tabstrip_add_types =
358 TabStripModel::ADD_FORCE_INDEX | TabStripModel::ADD_INHERIT_OPENER; 357 TabStripModel::ADD_FORCE_INDEX | TabStripModel::ADD_INHERIT_OPENER;
359 chrome::Navigate(&params); 358 Navigate(&params);
360 359
361 DCHECK(browser->profile()->GetExtensionService()); 360 DCHECK(browser->profile()->GetExtensionService());
362 if (browser->profile()->GetExtensionService()->IsInstalledApp(url)) { 361 if (browser->profile()->GetExtensionService()->IsInstalledApp(url)) {
363 AppLauncherHandler::RecordAppLaunchType( 362 AppLauncherHandler::RecordAppLaunchType(
364 extension_misc::APP_LAUNCH_OMNIBOX_LOCATION); 363 extension_misc::APP_LAUNCH_OMNIBOX_LOCATION);
365 } 364 }
366 } 365 }
367 366
368 void Stop(Browser* browser) { 367 void Stop(Browser* browser) {
369 content::RecordAction(UserMetricsAction("Stop")); 368 content::RecordAction(UserMetricsAction("Stop"));
(...skipping 17 matching lines...) Expand all
387 386
388 void NewTab(Browser* browser) { 387 void NewTab(Browser* browser) {
389 content::RecordAction(UserMetricsAction("NewTab")); 388 content::RecordAction(UserMetricsAction("NewTab"));
390 // TODO(asvitkine): This is invoked programmatically from several places. 389 // TODO(asvitkine): This is invoked programmatically from several places.
391 // Audit the code and change it so that the histogram only gets collected for 390 // Audit the code and change it so that the histogram only gets collected for
392 // user-initiated commands. 391 // user-initiated commands.
393 UMA_HISTOGRAM_ENUMERATION("Tab.NewTab", TabStripModel::NEW_TAB_COMMAND, 392 UMA_HISTOGRAM_ENUMERATION("Tab.NewTab", TabStripModel::NEW_TAB_COMMAND,
394 TabStripModel::NEW_TAB_ENUM_COUNT); 393 TabStripModel::NEW_TAB_ENUM_COUNT);
395 394
396 if (browser->is_type_tabbed()) { 395 if (browser->is_type_tabbed()) {
397 browser->AddBlankTab(true); 396 AddBlankTab(browser, true);
398 GetActiveWebContents(browser)->GetView()->RestoreFocus(); 397 GetActiveWebContents(browser)->GetView()->RestoreFocus();
399 } else { 398 } else {
400 Browser* b = browser::FindOrCreateTabbedBrowser(browser->profile()); 399 Browser* b = browser::FindOrCreateTabbedBrowser(browser->profile());
401 b->AddBlankTab(true); 400 AddBlankTab(b, true);
402 b->window()->Show(); 401 b->window()->Show();
403 // The call to AddBlankTab above did not set the focus to the tab as its 402 // The call to AddBlankTab above did not set the focus to the tab as its
404 // window was not active, so we have to do it explicitly. 403 // window was not active, so we have to do it explicitly.
405 // See http://crbug.com/6380. 404 // See http://crbug.com/6380.
406 chrome::GetActiveWebContents(b)->GetView()->RestoreFocus(); 405 GetActiveWebContents(b)->GetView()->RestoreFocus();
407 } 406 }
408 } 407 }
409 408
410 void CloseTab(Browser* browser) { 409 void CloseTab(Browser* browser) {
411 content::RecordAction(UserMetricsAction("CloseTab_Accelerator")); 410 content::RecordAction(UserMetricsAction("CloseTab_Accelerator"));
412 browser->tab_strip_model()->CloseSelectedTabs(); 411 browser->tab_strip_model()->CloseSelectedTabs();
413 } 412 }
414 413
415 void RestoreTab(Browser* browser) { 414 void RestoreTab(Browser* browser) {
416 content::RecordAction(UserMetricsAction("RestoreTab")); 415 content::RecordAction(UserMetricsAction("RestoreTab"));
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 } 466 }
468 } 467 }
469 468
470 void SelectLastTab(Browser* browser) { 469 void SelectLastTab(Browser* browser) {
471 content::RecordAction(UserMetricsAction("SelectLastTab")); 470 content::RecordAction(UserMetricsAction("SelectLastTab"));
472 browser->tab_strip_model()->SelectLastTab(); 471 browser->tab_strip_model()->SelectLastTab();
473 } 472 }
474 473
475 void DuplicateTab(Browser* browser) { 474 void DuplicateTab(Browser* browser) {
476 content::RecordAction(UserMetricsAction("Duplicate")); 475 content::RecordAction(UserMetricsAction("Duplicate"));
477 browser->DuplicateContentsAt(browser->active_index()); 476 DuplicateTabAt(browser, browser->active_index());
478 } 477 }
479 478
480 bool CanDuplicateTab(const Browser* browser) { 479 bool CanDuplicateTab(const Browser* browser) {
481 WebContents* contents = GetActiveWebContents(browser); 480 WebContents* contents = GetActiveWebContents(browser);
482 return contents && contents->GetController().GetLastCommittedEntry(); 481 return contents && contents->GetController().GetLastCommittedEntry();
483 } 482 }
484 483
484 void DuplicateTabAt(Browser* browser, int index) {
485 TabContents* contents = GetTabContentsAt(browser, index);
486 CHECK(contents);
487 TabContents* contents_dupe = contents->Clone();
488
489 bool pinned = false;
490 if (browser->CanSupportWindowFeature(Browser::FEATURE_TABSTRIP)) {
491 // If this is a tabbed browser, just create a duplicate tab inside the same
492 // window next to the tab being duplicated.
493 int index = browser->tab_strip_model()->GetIndexOfTabContents(contents);
494 pinned = browser->tab_strip_model()->IsTabPinned(index);
495 int add_types = TabStripModel::ADD_ACTIVE |
496 TabStripModel::ADD_INHERIT_GROUP |
497 (pinned ? TabStripModel::ADD_PINNED : 0);
498 browser->tab_strip_model()->InsertTabContentsAt(
499 index + 1, contents_dupe, add_types);
500 } else {
501 Browser* browser = NULL;
502 if (browser->is_app()) {
503 CHECK(!browser->is_type_popup());
504 CHECK(!browser->is_type_panel());
505 browser = Browser::CreateWithParams(
506 Browser::CreateParams::CreateForApp(Browser::TYPE_POPUP,
507 browser->app_name(),
508 gfx::Rect(),
509 browser->profile()));
510 } else if (browser->is_type_popup()) {
511 browser = Browser::CreateWithParams(
512 Browser::CreateParams(Browser::TYPE_POPUP, browser->profile()));
513 }
514
515 // Preserve the size of the original window. The new window has already
516 // been given an offset by the OS, so we shouldn't copy the old bounds.
517 BrowserWindow* new_window = browser->window();
518 new_window->SetBounds(gfx::Rect(new_window->GetRestoredBounds().origin(),
519 browser->window()->GetRestoredBounds().size()));
520
521 // We need to show the browser now. Otherwise ContainerWin assumes the
522 // WebContents is invisible and won't size it.
523 browser->window()->Show();
524
525 // The page transition below is only for the purpose of inserting the tab.
526 AddTab(browser, contents_dupe, content::PAGE_TRANSITION_LINK);
527 }
528
529 SessionService* session_service =
530 SessionServiceFactory::GetForProfileIfExisting(browser->profile());
531 if (session_service)
532 session_service->TabRestored(contents_dupe, pinned);
533 }
534
535 bool CanDuplicateTabAt(Browser* browser, int index) {
536 content::NavigationController& nc =
537 GetWebContentsAt(browser, index)->GetController();
538 return nc.GetWebContents() && nc.GetLastCommittedEntry();
539 }
540
485 void ConvertPopupToTabbedBrowser(Browser* browser) { 541 void ConvertPopupToTabbedBrowser(Browser* browser) {
486 content::RecordAction(UserMetricsAction("ShowAsTab")); 542 content::RecordAction(UserMetricsAction("ShowAsTab"));
487 TabContents* contents = 543 TabContents* contents =
488 browser->tab_strip_model()->DetachTabContentsAt(browser->active_index()); 544 browser->tab_strip_model()->DetachTabContentsAt(browser->active_index());
489 Browser* b = Browser::Create(browser->profile()); 545 Browser* b = Browser::Create(browser->profile());
490 b->tab_strip_model()->AppendTabContents(contents, true); 546 b->tab_strip_model()->AppendTabContents(contents, true);
491 b->window()->Show(); 547 b->window()->Show();
492 } 548 }
493 549
494 void Exit() { 550 void Exit() {
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after
904 // been given an offset by the OS, so we shouldn't copy the old bounds. 960 // been given an offset by the OS, so we shouldn't copy the old bounds.
905 BrowserWindow* new_window = b->window(); 961 BrowserWindow* new_window = b->window();
906 new_window->SetBounds(gfx::Rect(new_window->GetRestoredBounds().origin(), 962 new_window->SetBounds(gfx::Rect(new_window->GetRestoredBounds().origin(),
907 browser->window()->GetRestoredBounds().size())); 963 browser->window()->GetRestoredBounds().size()));
908 964
909 // We need to show the browser now. Otherwise ContainerWin assumes the 965 // We need to show the browser now. Otherwise ContainerWin assumes the
910 // WebContents is invisible and won't size it. 966 // WebContents is invisible and won't size it.
911 b->window()->Show(); 967 b->window()->Show();
912 968
913 // The page transition below is only for the purpose of inserting the tab. 969 // The page transition below is only for the purpose of inserting the tab.
914 chrome::AddTab(b, view_source_contents, content::PAGE_TRANSITION_LINK); 970 AddTab(b, view_source_contents, content::PAGE_TRANSITION_LINK);
915 } 971 }
916 972
917 SessionService* session_service = 973 SessionService* session_service =
918 SessionServiceFactory::GetForProfileIfExisting(browser->profile()); 974 SessionServiceFactory::GetForProfileIfExisting(browser->profile());
919 if (session_service) 975 if (session_service)
920 session_service->TabRestored(view_source_contents, false); 976 session_service->TabRestored(view_source_contents, false);
921 } 977 }
922 978
923 void ViewSelectedSource(Browser* browser) { 979 void ViewSelectedSource(Browser* browser) {
924 ViewSource(browser, chrome::GetActiveTabContents(browser)); 980 ViewSource(browser, GetActiveTabContents(browser));
925 } 981 }
926 982
927 bool CanViewSource(const Browser* browser) { 983 bool CanViewSource(const Browser* browser) {
928 return chrome::GetActiveWebContents(browser)->GetController().CanViewSource(); 984 return GetActiveWebContents(browser)->GetController().CanViewSource();
929 } 985 }
930 986
931 void CreateApplicationShortcuts(Browser* browser) { 987 void CreateApplicationShortcuts(Browser* browser) {
932 content::RecordAction(UserMetricsAction("CreateShortcut")); 988 content::RecordAction(UserMetricsAction("CreateShortcut"));
933 chrome::GetActiveTabContents(browser)->extension_tab_helper()-> 989 GetActiveTabContents(browser)->extension_tab_helper()->
934 CreateApplicationShortcuts(); 990 CreateApplicationShortcuts();
935 } 991 }
936 992
937 bool CanCreateApplicationShortcuts(const Browser* browser) { 993 bool CanCreateApplicationShortcuts(const Browser* browser) {
938 return chrome::GetActiveTabContents(browser)->extension_tab_helper()-> 994 return GetActiveTabContents(browser)->extension_tab_helper()->
939 CanCreateApplicationShortcuts(); 995 CanCreateApplicationShortcuts();
940 } 996 }
941 997
942 void ConvertTabToAppWindow(Browser* browser, 998 void ConvertTabToAppWindow(Browser* browser,
943 content::WebContents* contents) { 999 content::WebContents* contents) {
944 const GURL& url = contents->GetController().GetActiveEntry()->GetURL(); 1000 const GURL& url = contents->GetController().GetActiveEntry()->GetURL();
945 std::string app_name = web_app::GenerateApplicationNameFromURL(url); 1001 std::string app_name = web_app::GenerateApplicationNameFromURL(url);
946 1002
947 int index = browser->tab_strip_model()->GetIndexOfWebContents(contents); 1003 int index = browser->tab_strip_model()->GetIndexOfWebContents(contents);
948 if (index >= 0) 1004 if (index >= 0)
949 browser->tab_strip_model()->DetachTabContentsAt(index); 1005 browser->tab_strip_model()->DetachTabContentsAt(index);
950 1006
951 Browser* app_browser = Browser::CreateWithParams( 1007 Browser* app_browser = Browser::CreateWithParams(
952 Browser::CreateParams::CreateForApp( 1008 Browser::CreateParams::CreateForApp(
953 Browser::TYPE_POPUP, app_name, gfx::Rect(), browser->profile())); 1009 Browser::TYPE_POPUP, app_name, gfx::Rect(), browser->profile()));
954 TabContents* tab_contents = TabContents::FromWebContents(contents); 1010 TabContents* tab_contents = TabContents::FromWebContents(contents);
955 if (!tab_contents) 1011 if (!tab_contents)
956 tab_contents = new TabContents(contents); 1012 tab_contents = new TabContents(contents);
957 app_browser->tab_strip_model()->AppendTabContents(tab_contents, true); 1013 app_browser->tab_strip_model()->AppendTabContents(tab_contents, true);
958 1014
959 contents->GetMutableRendererPrefs()->can_accept_load_drops = false; 1015 contents->GetMutableRendererPrefs()->can_accept_load_drops = false;
960 contents->GetRenderViewHost()->SyncRendererPrefs(); 1016 contents->GetRenderViewHost()->SyncRendererPrefs();
961 app_browser->window()->Show(); 1017 app_browser->window()->Show();
962 } 1018 }
963 1019
964 } // namespace chrome 1020 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/ui/browser_commands.h ('k') | chrome/browser/ui/browser_navigator_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698