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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/browser_commands.h"
7 #include "base/command_line.h"
8 #include "base/metrics/histogram.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/app/chrome_command_ids.h"
12 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/browsing_data/browsing_data_helper.h"
15 #include "chrome/browser/browsing_data/browsing_data_remover.h"
16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/devtools/devtools_window.h"
18 #include "chrome/browser/dom_distiller/tab_utils.h"
19 #include "chrome/browser/favicon/favicon_tab_helper.h"
20 #include "chrome/browser/lifetime/application_lifetime.h"
21 #include "chrome/browser/platform_util.h"
22 #include "chrome/browser/prefs/incognito_mode_prefs.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/rlz/rlz.h"
25 #include "chrome/browser/search/search.h"
26 #include "chrome/browser/sessions/session_service_factory.h"
27 #include "chrome/browser/sessions/tab_restore_service.h"
28 #include "chrome/browser/sessions/tab_restore_service_delegate.h"
29 #include "chrome/browser/sessions/tab_restore_service_factory.h"
30 #include "chrome/browser/signin/signin_header_helper.h"
31 #include "chrome/browser/translate/chrome_translate_client.h"
32 #include "chrome/browser/ui/accelerator_utils.h"
33 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
34 #include "chrome/browser/ui/browser.h"
35 #include "chrome/browser/ui/browser_command_controller.h"
36 #include "chrome/browser/ui/browser_dialogs.h"
37 #include "chrome/browser/ui/browser_instant_controller.h"
38 #include "chrome/browser/ui/browser_tab_restore_service_delegate.h"
39 #include "chrome/browser/ui/browser_tabstrip.h"
40 #include "chrome/browser/ui/browser_window.h"
41 #include "chrome/browser/ui/chrome_pages.h"
42 #include "chrome/browser/ui/exclusive_access/fullscreen_controller.h"
43 #include "chrome/browser/ui/find_bar/find_bar.h"
44 #include "chrome/browser/ui/find_bar/find_bar_controller.h"
45 #include "chrome/browser/ui/find_bar/find_tab_helper.h"
46 #include "chrome/browser/ui/location_bar/location_bar.h"
47 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
48 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
49 #include "chrome/browser/ui/search/search_tab_helper.h"
50 #include "chrome/browser/ui/status_bubble.h"
51 #include "chrome/browser/ui/tab_contents/core_tab_helper.h"
52 #include "chrome/browser/ui/tab_dialogs.h"
53 #include "chrome/browser/ui/tabs/tab_strip_model.h"
54 #include "chrome/browser/upgrade_detector.h"
55 #include "chrome/common/chrome_switches.h"
56 #include "chrome/common/chrome_version_info.h"
57 #include "chrome/common/content_restriction.h"
58 #include "chrome/common/pref_names.h"
59 #include "components/bookmarks/browser/bookmark_model.h"
60 #include "components/bookmarks/browser/bookmark_utils.h"
61 #include "components/google/core/browser/google_util.h"
62 #include "components/translate/core/browser/language_state.h"
63 #include "components/ui/zoom/page_zoom.h"
64 #include "components/ui/zoom/zoom_controller.h"
65 #include "components/web_modal/popup_manager.h"
66 #include "content/public/browser/devtools_agent_host.h"
67 #include "content/public/browser/navigation_controller.h"
68 #include "content/public/browser/navigation_entry.h"
69 #include "content/public/browser/notification_service.h"
70 #include "content/public/browser/page_navigator.h"
71 #include "content/public/browser/render_view_host.h"
72 #include "content/public/browser/render_widget_host_view.h"
73 #include "content/public/browser/user_metrics.h"
74 #include "content/public/browser/web_contents.h"
75 #include "content/public/common/page_state.h"
76 #include "content/public/common/renderer_preferences.h"
77 #include "content/public/common/url_constants.h"
78 #include "content/public/common/url_utils.h"
79 #include "content/public/common/user_agent.h"
80 #include "net/base/escape.h"
81 #include "ui/events/keycodes/keyboard_codes.h"
84 #include "chrome/browser/ui/metro_pin_tab_helper_win.h"
87 #if defined(ENABLE_EXTENSIONS)
88 #include "chrome/browser/extensions/api/commands/command_service.h"
89 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
90 #include "chrome/browser/extensions/tab_helper.h"
91 #include "chrome/browser/web_applications/web_app.h"
92 #include "chrome/common/extensions/extension_metrics.h"
93 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
94 #include "extensions/browser/extension_registry.h"
95 #include "extensions/browser/extension_system.h"
96 #include "extensions/common/extension.h"
97 #include "extensions/common/extension_set.h"
100 #if defined(ENABLE_PRINTING)
101 #include "chrome/browser/printing/print_view_manager_common.h"
102 #if defined(ENABLE_PRINT_PREVIEW)
103 #include "chrome/browser/printing/print_preview_dialog_controller.h"
104 #endif // defined(ENABLE_PRINT_PREVIEW)
105 #endif // defined(ENABLE_PRINTING)
108 const char kOsOverrideForTabletSite
[] = "Linux; Android 4.0.3";
111 using base::UserMetricsAction
;
112 using bookmarks::BookmarkModel
;
113 using content::NavigationController
;
114 using content::NavigationEntry
;
115 using content::OpenURLParams
;
116 using content::Referrer
;
117 using content::SSLStatus
;
118 using content::WebContents
;
123 bool CanBookmarkCurrentPageInternal(const Browser
* browser
,
124 bool check_remove_bookmark_ui
) {
125 BookmarkModel
* model
=
126 BookmarkModelFactory::GetForProfile(browser
->profile());
127 return browser_defaults::bookmarks_enabled
&&
128 browser
->profile()->GetPrefs()->GetBoolean(
129 bookmarks::prefs::kEditBookmarksEnabled
) &&
130 model
&& model
->loaded() && browser
->is_type_tabbed() &&
131 (!check_remove_bookmark_ui
||
132 !chrome::ShouldRemoveBookmarkThisPageUI(browser
->profile()));
135 #if defined(ENABLE_EXTENSIONS)
136 bool GetBookmarkOverrideCommand(
138 const extensions::Extension
** extension
,
139 extensions::Command
* command
,
140 extensions::CommandService::ExtensionCommandType
* command_type
) {
143 DCHECK(command_type
);
145 ui::Accelerator bookmark_page_accelerator
=
146 chrome::GetPrimaryChromeAcceleratorForCommandId(IDC_BOOKMARK_PAGE
);
147 if (bookmark_page_accelerator
.key_code() == ui::VKEY_UNKNOWN
)
150 extensions::CommandService
* command_service
=
151 extensions::CommandService::Get(profile
);
152 const extensions::ExtensionSet
& extension_set
=
153 extensions::ExtensionRegistry::Get(profile
)->enabled_extensions();
154 for (extensions::ExtensionSet::const_iterator i
= extension_set
.begin();
155 i
!= extension_set
.end();
157 extensions::Command prospective_command
;
158 extensions::CommandService::ExtensionCommandType prospective_command_type
;
159 if (command_service
->GetSuggestedExtensionCommand(
160 (*i
)->id(), bookmark_page_accelerator
, &prospective_command
,
161 &prospective_command_type
)) {
162 *extension
= i
->get();
163 *command
= prospective_command
;
164 *command_type
= prospective_command_type
;
172 // Based on |disposition|, creates a new tab as necessary, and returns the
173 // appropriate tab to navigate. If that tab is the current tab, reverts the
174 // location bar contents, since all browser-UI-triggered navigations should
175 // revert any omnibox edits in the current tab.
176 WebContents
* GetTabAndRevertIfNecessary(Browser
* browser
,
177 WindowOpenDisposition disposition
) {
178 WebContents
* current_tab
= browser
->tab_strip_model()->GetActiveWebContents();
179 switch (disposition
) {
180 case NEW_FOREGROUND_TAB
:
181 case NEW_BACKGROUND_TAB
: {
182 WebContents
* new_tab
= current_tab
->Clone();
183 browser
->tab_strip_model()->AddWebContents(
184 new_tab
, -1, ui::PAGE_TRANSITION_LINK
,
185 (disposition
== NEW_FOREGROUND_TAB
) ?
186 TabStripModel::ADD_ACTIVE
: TabStripModel::ADD_NONE
);
190 WebContents
* new_tab
= current_tab
->Clone();
191 Browser
* new_browser
= new Browser(Browser::CreateParams(
192 browser
->profile(), browser
->host_desktop_type()));
193 new_browser
->tab_strip_model()->AddWebContents(
194 new_tab
, -1, ui::PAGE_TRANSITION_LINK
,
195 TabStripModel::ADD_ACTIVE
);
196 new_browser
->window()->Show();
200 browser
->window()->GetLocationBar()->Revert();
205 void ReloadInternal(Browser
* browser
,
206 WindowOpenDisposition disposition
,
208 // As this is caused by a user action, give the focus to the page.
210 // Also notify RenderViewHostDelegate of the user gesture; this is
211 // normally done in Browser::Navigate, but a reload bypasses Navigate.
212 WebContents
* new_tab
= GetTabAndRevertIfNecessary(browser
, disposition
);
213 new_tab
->UserGestureDone();
214 if (!new_tab
->FocusLocationBarByDefault())
217 new_tab
->GetController().ReloadIgnoringCache(true);
219 new_tab
->GetController().Reload(true);
222 bool IsShowingWebContentsModalDialog(Browser
* browser
) {
223 WebContents
* web_contents
=
224 browser
->tab_strip_model()->GetActiveWebContents();
228 // In test code we may not have a popup manager.
229 if (!browser
->popup_manager())
232 // TODO(gbillock): This is currently called in production by the CanPrint
233 // method, and may be too restrictive if we allow print preview to overlap.
234 // Re-assess how to queue print preview after we know more about popup
235 // management policy.
236 return browser
->popup_manager()->IsWebModalDialogActive(web_contents
);
239 #if defined(ENABLE_BASIC_PRINTING)
240 bool PrintPreviewShowing(const Browser
* browser
) {
241 #if defined(ENABLE_PRINT_PREVIEW)
242 WebContents
* contents
= browser
->tab_strip_model()->GetActiveWebContents();
243 printing::PrintPreviewDialogController
* controller
=
244 printing::PrintPreviewDialogController::GetInstance();
245 return controller
&& (controller
->GetPrintPreviewForContents(contents
) ||
246 controller
->is_creating_print_preview_dialog());
251 #endif // ENABLE_BASIC_PRINTING
255 bool IsCommandEnabled(Browser
* browser
, int command
) {
256 return browser
->command_controller()->command_updater()->IsCommandEnabled(
260 bool SupportsCommand(Browser
* browser
, int command
) {
261 return browser
->command_controller()->command_updater()->SupportsCommand(
265 bool ExecuteCommand(Browser
* browser
, int command
) {
266 return browser
->command_controller()->command_updater()->ExecuteCommand(
270 bool ExecuteCommandWithDisposition(Browser
* browser
,
272 WindowOpenDisposition disposition
) {
273 return browser
->command_controller()->command_updater()->
274 ExecuteCommandWithDisposition(command
, disposition
);
277 void UpdateCommandEnabled(Browser
* browser
, int command
, bool enabled
) {
278 browser
->command_controller()->command_updater()->UpdateCommandEnabled(
282 void AddCommandObserver(Browser
* browser
,
284 CommandObserver
* observer
) {
285 browser
->command_controller()->command_updater()->AddCommandObserver(
289 void RemoveCommandObserver(Browser
* browser
,
291 CommandObserver
* observer
) {
292 browser
->command_controller()->command_updater()->RemoveCommandObserver(
296 int GetContentRestrictions(const Browser
* browser
) {
297 int content_restrictions
= 0;
298 WebContents
* current_tab
= browser
->tab_strip_model()->GetActiveWebContents();
300 CoreTabHelper
* core_tab_helper
=
301 CoreTabHelper::FromWebContents(current_tab
);
302 content_restrictions
= core_tab_helper
->content_restrictions();
303 NavigationEntry
* last_committed_entry
=
304 current_tab
->GetController().GetLastCommittedEntry();
305 if (!content::IsSavableURL(
306 last_committed_entry
? last_committed_entry
->GetURL() : GURL()) ||
307 current_tab
->ShowingInterstitialPage())
308 content_restrictions
|= CONTENT_RESTRICTION_SAVE
;
309 if (current_tab
->ShowingInterstitialPage())
310 content_restrictions
|= CONTENT_RESTRICTION_PRINT
;
312 return content_restrictions
;
315 void NewEmptyWindow(Profile
* profile
, HostDesktopType desktop_type
) {
316 bool incognito
= profile
->IsOffTheRecord();
317 PrefService
* prefs
= profile
->GetPrefs();
319 if (IncognitoModePrefs::GetAvailability(prefs
) ==
320 IncognitoModePrefs::DISABLED
) {
323 } else if (profile
->IsGuestSession() ||
324 (browser_defaults::kAlwaysOpenIncognitoWindow
&&
325 IncognitoModePrefs::ShouldLaunchIncognito(
326 *base::CommandLine::ForCurrentProcess(), prefs
))) {
331 content::RecordAction(UserMetricsAction("NewIncognitoWindow"));
332 OpenEmptyWindow(profile
->GetOffTheRecordProfile(), desktop_type
);
334 content::RecordAction(UserMetricsAction("NewWindow"));
335 SessionService
* session_service
=
336 SessionServiceFactory::GetForProfileForSessionRestore(
337 profile
->GetOriginalProfile());
338 if (!session_service
||
339 !session_service
->RestoreIfNecessary(std::vector
<GURL
>())) {
340 OpenEmptyWindow(profile
->GetOriginalProfile(), desktop_type
);
345 Browser
* OpenEmptyWindow(Profile
* profile
, HostDesktopType desktop_type
) {
346 Browser
* browser
= new Browser(
347 Browser::CreateParams(Browser::TYPE_TABBED
, profile
, desktop_type
));
348 AddTabAt(browser
, GURL(), -1, true);
349 browser
->window()->Show();
353 void OpenWindowWithRestoredTabs(Profile
* profile
,
354 HostDesktopType host_desktop_type
) {
355 TabRestoreService
* service
= TabRestoreServiceFactory::GetForProfile(profile
);
357 service
->RestoreMostRecentEntry(NULL
, host_desktop_type
);
360 void OpenURLOffTheRecord(Profile
* profile
,
362 chrome::HostDesktopType desktop_type
) {
363 ScopedTabbedBrowserDisplayer
displayer(profile
->GetOffTheRecordProfile(),
365 AddSelectedTabWithURL(displayer
.browser(), url
,
366 ui::PAGE_TRANSITION_LINK
);
369 bool CanGoBack(const Browser
* browser
) {
370 return browser
->tab_strip_model()->GetActiveWebContents()->
371 GetController().CanGoBack();
374 void GoBack(Browser
* browser
, WindowOpenDisposition disposition
) {
375 content::RecordAction(UserMetricsAction("Back"));
377 if (CanGoBack(browser
)) {
378 WebContents
* current_tab
=
379 browser
->tab_strip_model()->GetActiveWebContents();
380 WebContents
* new_tab
= GetTabAndRevertIfNecessary(browser
, disposition
);
381 // If we are on an interstitial page and clone the tab, it won't be copied
382 // to the new tab, so we don't need to go back.
383 if ((new_tab
== current_tab
) || !current_tab
->ShowingInterstitialPage())
384 new_tab
->GetController().GoBack();
388 bool CanGoForward(const Browser
* browser
) {
389 return browser
->tab_strip_model()->GetActiveWebContents()->
390 GetController().CanGoForward();
393 void GoForward(Browser
* browser
, WindowOpenDisposition disposition
) {
394 content::RecordAction(UserMetricsAction("Forward"));
395 if (CanGoForward(browser
)) {
396 GetTabAndRevertIfNecessary(browser
, disposition
)->
397 GetController().GoForward();
401 bool NavigateToIndexWithDisposition(Browser
* browser
,
403 WindowOpenDisposition disposition
) {
404 NavigationController
* controller
=
405 &GetTabAndRevertIfNecessary(browser
, disposition
)->GetController();
406 if (index
< 0 || index
>= controller
->GetEntryCount())
408 controller
->GoToIndex(index
);
412 void Reload(Browser
* browser
, WindowOpenDisposition disposition
) {
413 content::RecordAction(UserMetricsAction("Reload"));
414 ReloadInternal(browser
, disposition
, false);
417 void ReloadIgnoringCache(Browser
* browser
, WindowOpenDisposition disposition
) {
418 content::RecordAction(UserMetricsAction("ReloadIgnoringCache"));
419 ReloadInternal(browser
, disposition
, true);
422 bool CanReload(const Browser
* browser
) {
423 return !browser
->is_devtools();
426 void Home(Browser
* browser
, WindowOpenDisposition disposition
) {
427 content::RecordAction(UserMetricsAction("Home"));
429 std::string extra_headers
;
430 #if defined(ENABLE_RLZ) && !defined(OS_IOS)
431 // If the home page is a Google home page, add the RLZ header to the request.
432 PrefService
* pref_service
= browser
->profile()->GetPrefs();
434 if (google_util::IsGoogleHomePageUrl(
435 GURL(pref_service
->GetString(prefs::kHomePage
)))) {
436 extra_headers
= RLZTracker::GetAccessPointHttpHeader(
437 RLZTracker::ChromeHomePage());
440 #endif // defined(ENABLE_RLZ) && !defined(OS_IOS)
442 GURL url
= browser
->profile()->GetHomePage();
444 #if defined(ENABLE_EXTENSIONS)
445 // With bookmark apps enabled, hosted apps should return to their launch page
446 // when the home button is pressed.
447 if (browser
->is_app()) {
448 const extensions::Extension
* extension
=
449 extensions::ExtensionRegistry::Get(browser
->profile())
451 web_app::GetExtensionIdFromApplicationName(browser
->app_name()),
452 extensions::ExtensionRegistry::EVERYTHING
);
456 url
= extensions::AppLaunchInfo::GetLaunchWebURL(extension
);
460 OpenURLParams
params(
461 url
, Referrer(), disposition
,
462 ui::PageTransitionFromInt(
463 ui::PAGE_TRANSITION_AUTO_BOOKMARK
|
464 ui::PAGE_TRANSITION_HOME_PAGE
),
466 params
.extra_headers
= extra_headers
;
467 browser
->OpenURL(params
);
470 void OpenCurrentURL(Browser
* browser
) {
471 content::RecordAction(UserMetricsAction("LoadURL"));
472 LocationBar
* location_bar
= browser
->window()->GetLocationBar();
476 GURL
url(location_bar
->GetDestinationURL());
478 ui::PageTransition page_transition
= location_bar
->GetPageTransition();
479 ui::PageTransition
page_transition_without_qualifier(
480 ui::PageTransitionStripQualifier(page_transition
));
481 WindowOpenDisposition open_disposition
=
482 location_bar
->GetWindowOpenDisposition();
483 // A PAGE_TRANSITION_TYPED means the user has typed a URL. We do not want to
484 // open URLs with instant_controller since in some cases it disregards it
485 // and performs a search instead. For example, when using CTRL-Enter, the
486 // location_bar is aware of the URL but instant is not.
487 // Instant should also not handle PAGE_TRANSITION_RELOAD because its knowledge
488 // of the omnibox text may be stale if the user focuses in the omnibox and
489 // presses enter without typing anything.
490 if (page_transition_without_qualifier
!= ui::PAGE_TRANSITION_TYPED
&&
491 page_transition_without_qualifier
!= ui::PAGE_TRANSITION_RELOAD
&&
492 browser
->instant_controller() &&
493 browser
->instant_controller()->OpenInstant(open_disposition
, url
))
496 NavigateParams
params(browser
, url
, page_transition
);
497 params
.disposition
= open_disposition
;
498 // Use ADD_INHERIT_OPENER so that all pages opened by the omnibox at least
499 // inherit the opener. In some cases the tabstrip will determine the group
500 // should be inherited, in which case the group is inherited instead of the
502 params
.tabstrip_add_types
=
503 TabStripModel::ADD_FORCE_INDEX
| TabStripModel::ADD_INHERIT_OPENER
;
506 #if defined(ENABLE_EXTENSIONS)
507 DCHECK(extensions::ExtensionSystem::Get(
508 browser
->profile())->extension_service());
509 const extensions::Extension
* extension
=
510 extensions::ExtensionRegistry::Get(browser
->profile())
511 ->enabled_extensions().GetAppByURL(url
);
513 extensions::RecordAppLaunchType(extension_misc::APP_LAUNCH_OMNIBOX_LOCATION
,
514 extension
->GetType());
519 void Stop(Browser
* browser
) {
520 content::RecordAction(UserMetricsAction("Stop"));
521 browser
->tab_strip_model()->GetActiveWebContents()->Stop();
524 void NewWindow(Browser
* browser
) {
525 NewEmptyWindow(browser
->profile()->GetOriginalProfile(),
526 browser
->host_desktop_type());
529 void NewIncognitoWindow(Browser
* browser
) {
530 NewEmptyWindow(browser
->profile()->GetOffTheRecordProfile(),
531 browser
->host_desktop_type());
534 void CloseWindow(Browser
* browser
) {
535 content::RecordAction(UserMetricsAction("CloseWindow"));
536 browser
->window()->Close();
539 void NewTab(Browser
* browser
) {
540 content::RecordAction(UserMetricsAction("NewTab"));
541 // TODO(asvitkine): This is invoked programmatically from several places.
542 // Audit the code and change it so that the histogram only gets collected for
543 // user-initiated commands.
544 UMA_HISTOGRAM_ENUMERATION("Tab.NewTab", TabStripModel::NEW_TAB_COMMAND
,
545 TabStripModel::NEW_TAB_ENUM_COUNT
);
547 if (browser
->is_type_tabbed()) {
548 AddTabAt(browser
, GURL(), -1, true);
549 browser
->tab_strip_model()->GetActiveWebContents()->RestoreFocus();
551 ScopedTabbedBrowserDisplayer
displayer(browser
->profile(),
552 browser
->host_desktop_type());
553 Browser
* b
= displayer
.browser();
554 AddTabAt(b
, GURL(), -1, true);
556 // The call to AddBlankTabAt above did not set the focus to the tab as its
557 // window was not active, so we have to do it explicitly.
558 // See http://crbug.com/6380.
559 b
->tab_strip_model()->GetActiveWebContents()->RestoreFocus();
563 void CloseTab(Browser
* browser
) {
564 content::RecordAction(UserMetricsAction("CloseTab_Accelerator"));
565 browser
->tab_strip_model()->CloseSelectedTabs();
568 bool CanZoomIn(content::WebContents
* contents
) {
569 ui_zoom::ZoomController
* zoom_controller
=
570 ui_zoom::ZoomController::FromWebContents(contents
);
571 return zoom_controller
->GetZoomPercent() != contents
->GetMaximumZoomPercent();
574 bool CanZoomOut(content::WebContents
* contents
) {
575 ui_zoom::ZoomController
* zoom_controller
=
576 ui_zoom::ZoomController::FromWebContents(contents
);
577 return zoom_controller
->GetZoomPercent() !=
578 contents
->GetMinimumZoomPercent();
581 bool CanResetZoom(content::WebContents
* contents
) {
582 ui_zoom::ZoomController
* zoom_controller
=
583 ui_zoom::ZoomController::FromWebContents(contents
);
584 return !zoom_controller
->IsAtDefaultZoom() ||
585 !zoom_controller
->PageScaleFactorIsOne();
588 TabStripModelDelegate::RestoreTabType
GetRestoreTabType(
589 const Browser
* browser
) {
590 TabRestoreService
* service
=
591 TabRestoreServiceFactory::GetForProfile(browser
->profile());
592 if (!service
|| service
->entries().empty())
593 return TabStripModelDelegate::RESTORE_NONE
;
594 if (service
->entries().front()->type
== TabRestoreService::WINDOW
)
595 return TabStripModelDelegate::RESTORE_WINDOW
;
596 return TabStripModelDelegate::RESTORE_TAB
;
599 void SelectNextTab(Browser
* browser
) {
600 content::RecordAction(UserMetricsAction("SelectNextTab"));
601 browser
->tab_strip_model()->SelectNextTab();
604 void SelectPreviousTab(Browser
* browser
) {
605 content::RecordAction(UserMetricsAction("SelectPrevTab"));
606 browser
->tab_strip_model()->SelectPreviousTab();
609 void MoveTabNext(Browser
* browser
) {
610 content::RecordAction(UserMetricsAction("MoveTabNext"));
611 browser
->tab_strip_model()->MoveTabNext();
614 void MoveTabPrevious(Browser
* browser
) {
615 content::RecordAction(UserMetricsAction("MoveTabPrevious"));
616 browser
->tab_strip_model()->MoveTabPrevious();
619 void SelectNumberedTab(Browser
* browser
, int index
) {
620 if (index
< browser
->tab_strip_model()->count()) {
621 content::RecordAction(UserMetricsAction("SelectNumberedTab"));
622 browser
->tab_strip_model()->ActivateTabAt(index
, true);
626 void SelectLastTab(Browser
* browser
) {
627 content::RecordAction(UserMetricsAction("SelectLastTab"));
628 browser
->tab_strip_model()->SelectLastTab();
631 void DuplicateTab(Browser
* browser
) {
632 content::RecordAction(UserMetricsAction("Duplicate"));
633 DuplicateTabAt(browser
, browser
->tab_strip_model()->active_index());
636 bool CanDuplicateTab(const Browser
* browser
) {
637 WebContents
* contents
= browser
->tab_strip_model()->GetActiveWebContents();
638 return contents
&& contents
->GetController().GetLastCommittedEntry();
641 WebContents
* DuplicateTabAt(Browser
* browser
, int index
) {
642 WebContents
* contents
= browser
->tab_strip_model()->GetWebContentsAt(index
);
644 WebContents
* contents_dupe
= contents
->Clone();
647 if (browser
->CanSupportWindowFeature(Browser::FEATURE_TABSTRIP
)) {
648 // If this is a tabbed browser, just create a duplicate tab inside the same
649 // window next to the tab being duplicated.
650 int index
= browser
->tab_strip_model()->GetIndexOfWebContents(contents
);
651 pinned
= browser
->tab_strip_model()->IsTabPinned(index
);
652 int add_types
= TabStripModel::ADD_ACTIVE
|
653 TabStripModel::ADD_INHERIT_GROUP
|
654 (pinned
? TabStripModel::ADD_PINNED
: 0);
655 browser
->tab_strip_model()->InsertWebContentsAt(
656 index
+ 1, contents_dupe
, add_types
);
658 Browser
* new_browser
= NULL
;
659 if (browser
->is_app() && !browser
->is_type_popup()) {
660 new_browser
= new Browser(
661 Browser::CreateParams::CreateForApp(browser
->app_name(),
662 browser
->is_trusted_source(),
665 browser
->host_desktop_type()));
667 new_browser
= new Browser(
668 Browser::CreateParams(browser
->type(), browser
->profile(),
669 browser
->host_desktop_type()));
671 // Preserve the size of the original window. The new window has already
672 // been given an offset by the OS, so we shouldn't copy the old bounds.
673 BrowserWindow
* new_window
= new_browser
->window();
674 new_window
->SetBounds(gfx::Rect(new_window
->GetRestoredBounds().origin(),
675 browser
->window()->GetRestoredBounds().size()));
677 // We need to show the browser now. Otherwise ContainerWin assumes the
678 // WebContents is invisible and won't size it.
679 new_browser
->window()->Show();
681 // The page transition below is only for the purpose of inserting the tab.
682 new_browser
->tab_strip_model()->AddWebContents(
684 ui::PAGE_TRANSITION_LINK
,
685 TabStripModel::ADD_ACTIVE
);
688 SessionService
* session_service
=
689 SessionServiceFactory::GetForProfileIfExisting(browser
->profile());
691 session_service
->TabRestored(contents_dupe
, pinned
);
692 return contents_dupe
;
695 bool CanDuplicateTabAt(Browser
* browser
, int index
) {
696 content::NavigationController
& nc
=
697 browser
->tab_strip_model()->GetWebContentsAt(index
)->GetController();
698 return nc
.GetWebContents() && nc
.GetLastCommittedEntry();
701 void ConvertPopupToTabbedBrowser(Browser
* browser
) {
702 content::RecordAction(UserMetricsAction("ShowAsTab"));
703 TabStripModel
* tab_strip
= browser
->tab_strip_model();
704 WebContents
* contents
=
705 tab_strip
->DetachWebContentsAt(tab_strip
->active_index());
706 Browser
* b
= new Browser(Browser::CreateParams(browser
->profile(),
707 browser
->host_desktop_type()));
708 b
->tab_strip_model()->AppendWebContents(contents
, true);
713 content::RecordAction(UserMetricsAction("Exit"));
714 chrome::AttemptUserExit();
717 void BookmarkCurrentPageIgnoringExtensionOverrides(Browser
* browser
) {
718 content::RecordAction(UserMetricsAction("Star"));
720 BookmarkModel
* model
=
721 BookmarkModelFactory::GetForProfile(browser
->profile());
722 if (!model
|| !model
->loaded())
723 return; // Ignore requests until bookmarks are loaded.
726 base::string16 title
;
727 WebContents
* web_contents
=
728 browser
->tab_strip_model()->GetActiveWebContents();
729 GetURLAndTitleToBookmark(web_contents
, &url
, &title
);
730 bool is_bookmarked_by_any
= model
->IsBookmarked(url
);
731 if (!is_bookmarked_by_any
&&
732 web_contents
->GetBrowserContext()->IsOffTheRecord()) {
733 // If we're incognito the favicon may not have been saved. Save it now
734 // so that bookmarks have an icon for the page.
735 FaviconTabHelper::FromWebContents(web_contents
)->SaveFavicon();
737 bool was_bookmarked_by_user
= bookmarks::IsBookmarkedByUser(model
, url
);
738 bookmarks::AddIfNotBookmarked(model
, url
, title
);
739 bool is_bookmarked_by_user
= bookmarks::IsBookmarkedByUser(model
, url
);
740 // Make sure the model actually added a bookmark before showing the star. A
741 // bookmark isn't created if the url is invalid.
742 if (browser
->window()->IsActive() && is_bookmarked_by_user
) {
743 // Only show the bubble if the window is active, otherwise we may get into
744 // weird situations where the bubble is deleted as soon as it is shown.
745 browser
->window()->ShowBookmarkBubble(url
, was_bookmarked_by_user
);
749 void BookmarkCurrentPageAllowingExtensionOverrides(Browser
* browser
) {
750 DCHECK(!chrome::ShouldRemoveBookmarkThisPageUI(browser
->profile()));
752 #if defined(ENABLE_EXTENSIONS)
753 const extensions::Extension
* extension
= NULL
;
754 extensions::Command command
;
755 extensions::CommandService::ExtensionCommandType command_type
;
756 if (GetBookmarkOverrideCommand(browser
->profile(),
760 switch (command_type
) {
761 case extensions::CommandService::NAMED
:
762 browser
->window()->ExecuteExtensionCommand(extension
, command
);
764 case extensions::CommandService::BROWSER_ACTION
:
765 case extensions::CommandService::PAGE_ACTION
:
766 // BookmarkCurrentPage is called through a user gesture, so it is safe
767 // to grant the active tab permission.
768 extensions::ExtensionActionAPI::Get(browser
->profile())->
769 ShowExtensionActionPopup(extension
, browser
, true);
775 BookmarkCurrentPageIgnoringExtensionOverrides(browser
);
778 bool CanBookmarkCurrentPage(const Browser
* browser
) {
779 return CanBookmarkCurrentPageInternal(browser
, true);
782 void BookmarkAllTabs(Browser
* browser
) {
783 content::RecordAction(UserMetricsAction("BookmarkAllTabs"));
784 chrome::ShowBookmarkAllTabsDialog(browser
);
787 bool CanBookmarkAllTabs(const Browser
* browser
) {
788 return browser
->tab_strip_model()->count() > 1 &&
789 !chrome::ShouldRemoveBookmarkOpenPagesUI(browser
->profile()) &&
790 CanBookmarkCurrentPageInternal(browser
, false);
793 void Translate(Browser
* browser
) {
794 if (!browser
->window()->IsActive())
797 WebContents
* web_contents
=
798 browser
->tab_strip_model()->GetActiveWebContents();
799 ChromeTranslateClient
* chrome_translate_client
=
800 ChromeTranslateClient::FromWebContents(web_contents
);
802 translate::TranslateStep step
= translate::TRANSLATE_STEP_BEFORE_TRANSLATE
;
803 if (chrome_translate_client
) {
804 if (chrome_translate_client
->GetLanguageState().translation_pending())
805 step
= translate::TRANSLATE_STEP_TRANSLATING
;
806 else if (chrome_translate_client
->GetLanguageState().IsPageTranslated())
807 step
= translate::TRANSLATE_STEP_AFTER_TRANSLATE
;
809 browser
->window()->ShowTranslateBubble(
810 web_contents
, step
, translate::TranslateErrors::NONE
, true);
813 void ManagePasswordsForPage(Browser
* browser
) {
814 WebContents
* web_contents
=
815 browser
->tab_strip_model()->GetActiveWebContents();
816 ManagePasswordsUIController
* controller
=
817 ManagePasswordsUIController::FromWebContents(web_contents
);
818 TabDialogs::FromWebContents(web_contents
)->ShowManagePasswordsBubble(
819 !controller
->IsAutomaticallyOpeningBubble());
823 void TogglePagePinnedToStartScreen(Browser
* browser
) {
824 MetroPinTabHelper::FromWebContents(
825 browser
->tab_strip_model()->GetActiveWebContents())->
826 TogglePinnedToStartScreen();
830 void SavePage(Browser
* browser
) {
831 content::RecordAction(UserMetricsAction("SavePage"));
832 WebContents
* current_tab
= browser
->tab_strip_model()->GetActiveWebContents();
833 if (current_tab
&& current_tab
->GetContentsMimeType() == "application/pdf")
834 content::RecordAction(UserMetricsAction("PDF.SavePage"));
835 current_tab
->OnSavePage();
838 bool CanSavePage(const Browser
* browser
) {
839 // LocalState can be NULL in tests.
840 if (g_browser_process
->local_state() &&
841 !g_browser_process
->local_state()->GetBoolean(
842 prefs::kAllowFileSelectionDialogs
)) {
845 return !browser
->is_devtools() &&
846 !(GetContentRestrictions(browser
) & CONTENT_RESTRICTION_SAVE
);
849 void ShowFindBar(Browser
* browser
) {
850 browser
->GetFindBarController()->Show();
853 void ShowWebsiteSettings(Browser
* browser
,
854 content::WebContents
* web_contents
,
856 const SSLStatus
& ssl
) {
857 browser
->window()->ShowWebsiteSettings(
858 Profile::FromBrowserContext(web_contents
->GetBrowserContext()),
859 web_contents
, url
, ssl
);
862 void Print(Browser
* browser
) {
863 #if defined(ENABLE_PRINTING)
864 printing::StartPrint(
865 browser
->tab_strip_model()->GetActiveWebContents(),
866 browser
->profile()->GetPrefs()->GetBoolean(prefs::kPrintPreviewDisabled
),
868 #endif // defined(ENABLE_PRINTING)
871 bool CanPrint(Browser
* browser
) {
872 // Do not print when printing is disabled via pref or policy.
873 // Do not print when a constrained window is showing. It's confusing.
874 // TODO(gbillock): Need to re-assess the call to
875 // IsShowingWebContentsModalDialog after a popup management policy is
876 // refined -- we will probably want to just queue the print request, not
878 return browser
->profile()->GetPrefs()->GetBoolean(prefs::kPrintingEnabled
) &&
879 !(IsShowingWebContentsModalDialog(browser
) ||
880 GetContentRestrictions(browser
) & CONTENT_RESTRICTION_PRINT
);
883 #if defined(ENABLE_BASIC_PRINTING)
884 void BasicPrint(Browser
* browser
) {
885 printing::StartBasicPrint(browser
->tab_strip_model()->GetActiveWebContents());
888 bool CanBasicPrint(Browser
* browser
) {
889 // If printing is not disabled via pref or policy, it is always possible to
890 // advanced print when the print preview is visible. The exception to this
891 // is under Win8 ash, since showing the advanced print dialog will open it
892 // modally on the Desktop and hang the browser.
894 if (chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_ASH
)
898 return browser
->profile()->GetPrefs()->GetBoolean(prefs::kPrintingEnabled
) &&
899 (PrintPreviewShowing(browser
) || CanPrint(browser
));
901 #endif // ENABLE_BASIC_PRINTING
903 void EmailPageLocation(Browser
* browser
) {
904 content::RecordAction(UserMetricsAction("EmailPageLocation"));
905 WebContents
* wc
= browser
->tab_strip_model()->GetActiveWebContents();
908 std::string title
= net::EscapeQueryParamValue(
909 base::UTF16ToUTF8(wc
->GetTitle()), false);
910 std::string page_url
= net::EscapeQueryParamValue(wc
->GetURL().spec(), false);
911 std::string mailto
= std::string("mailto:?subject=Fwd:%20") +
912 title
+ "&body=%0A%0A" + page_url
;
913 platform_util::OpenExternal(browser
->profile(), GURL(mailto
));
916 bool CanEmailPageLocation(const Browser
* browser
) {
917 return browser
->toolbar_model()->ShouldDisplayURL() &&
918 browser
->tab_strip_model()->GetActiveWebContents()->GetURL().is_valid();
921 void Cut(Browser
* browser
) {
922 content::RecordAction(UserMetricsAction("Cut"));
923 browser
->window()->Cut();
926 void Copy(Browser
* browser
) {
927 content::RecordAction(UserMetricsAction("Copy"));
928 browser
->window()->Copy();
931 void Paste(Browser
* browser
) {
932 content::RecordAction(UserMetricsAction("Paste"));
933 browser
->window()->Paste();
936 void Find(Browser
* browser
) {
937 content::RecordAction(UserMetricsAction("Find"));
938 FindInPage(browser
, false, false);
941 void FindNext(Browser
* browser
) {
942 content::RecordAction(UserMetricsAction("FindNext"));
943 FindInPage(browser
, true, true);
946 void FindPrevious(Browser
* browser
) {
947 content::RecordAction(UserMetricsAction("FindPrevious"));
948 FindInPage(browser
, true, false);
951 void FindInPage(Browser
* browser
, bool find_next
, bool forward_direction
) {
952 ShowFindBar(browser
);
954 base::string16 find_text
;
955 FindTabHelper
* find_helper
= FindTabHelper::FromWebContents(
956 browser
->tab_strip_model()->GetActiveWebContents());
957 #if defined(OS_MACOSX)
958 // We always want to search for the current contents of the find bar on
959 // OS X. For regular profile it's always the current find pboard. For
960 // Incognito window it's the newest value of the find pboard content and
962 FindBar
* find_bar
= browser
->GetFindBarController()->find_bar();
963 find_text
= find_bar
->GetFindText();
965 find_helper
->StartFinding(find_text
, forward_direction
, false);
969 void Zoom(Browser
* browser
, content::PageZoom zoom
) {
970 ui_zoom::PageZoom::Zoom(browser
->tab_strip_model()->GetActiveWebContents(),
974 void FocusToolbar(Browser
* browser
) {
975 content::RecordAction(UserMetricsAction("FocusToolbar"));
976 browser
->window()->FocusToolbar();
979 void FocusLocationBar(Browser
* browser
) {
980 content::RecordAction(UserMetricsAction("FocusLocation"));
981 browser
->window()->SetFocusToLocationBar(true);
984 void FocusSearch(Browser
* browser
) {
985 // TODO(beng): replace this with FocusLocationBar
986 content::RecordAction(UserMetricsAction("FocusSearch"));
987 browser
->window()->GetLocationBar()->FocusSearch();
990 void FocusAppMenu(Browser
* browser
) {
991 content::RecordAction(UserMetricsAction("FocusAppMenu"));
992 browser
->window()->FocusAppMenu();
995 void FocusBookmarksToolbar(Browser
* browser
) {
996 content::RecordAction(UserMetricsAction("FocusBookmarksToolbar"));
997 browser
->window()->FocusBookmarksToolbar();
1000 void FocusInfobars(Browser
* browser
) {
1001 content::RecordAction(UserMetricsAction("FocusInfobars"));
1002 browser
->window()->FocusInfobars();
1005 void FocusNextPane(Browser
* browser
) {
1006 content::RecordAction(UserMetricsAction("FocusNextPane"));
1007 browser
->window()->RotatePaneFocus(true);
1010 void FocusPreviousPane(Browser
* browser
) {
1011 content::RecordAction(UserMetricsAction("FocusPreviousPane"));
1012 browser
->window()->RotatePaneFocus(false);
1015 void ToggleDevToolsWindow(Browser
* browser
, DevToolsToggleAction action
) {
1016 if (action
.type() == DevToolsToggleAction::kShowConsole
)
1017 content::RecordAction(UserMetricsAction("DevTools_ToggleConsole"));
1019 content::RecordAction(UserMetricsAction("DevTools_ToggleWindow"));
1020 DevToolsWindow::ToggleDevToolsWindow(browser
, action
);
1023 bool CanOpenTaskManager() {
1024 #if defined(ENABLE_TASK_MANAGER)
1031 void OpenTaskManager(Browser
* browser
) {
1032 #if defined(ENABLE_TASK_MANAGER)
1033 content::RecordAction(UserMetricsAction("TaskManager"));
1034 chrome::ShowTaskManager(browser
);
1040 void OpenFeedbackDialog(Browser
* browser
) {
1041 content::RecordAction(UserMetricsAction("Feedback"));
1042 chrome::ShowFeedbackPage(browser
, std::string(), std::string());
1045 void ToggleBookmarkBar(Browser
* browser
) {
1046 content::RecordAction(UserMetricsAction("ShowBookmarksBar"));
1047 ToggleBookmarkBarWhenVisible(browser
->profile());
1050 void ShowAppMenu(Browser
* browser
) {
1051 // We record the user metric for this event in WrenchMenu::RunMenu.
1052 browser
->window()->ShowAppMenu();
1055 void ShowAvatarMenu(Browser
* browser
) {
1056 browser
->window()->ShowAvatarBubbleFromAvatarButton(
1057 BrowserWindow::AVATAR_BUBBLE_MODE_DEFAULT
,
1058 signin::ManageAccountsParams());
1061 void ShowFastUserSwitcher(Browser
* browser
) {
1062 browser
->window()->ShowAvatarBubbleFromAvatarButton(
1063 BrowserWindow::AVATAR_BUBBLE_MODE_FAST_USER_SWITCH
,
1064 signin::ManageAccountsParams());
1067 void OpenUpdateChromeDialog(Browser
* browser
) {
1068 if (UpgradeDetector::GetInstance()->is_outdated_install()) {
1069 content::NotificationService::current()->Notify(
1070 chrome::NOTIFICATION_OUTDATED_INSTALL
,
1071 content::NotificationService::AllSources(),
1072 content::NotificationService::NoDetails());
1073 } else if (UpgradeDetector::GetInstance()->is_outdated_install_no_au()) {
1074 content::NotificationService::current()->Notify(
1075 chrome::NOTIFICATION_OUTDATED_INSTALL_NO_AU
,
1076 content::NotificationService::AllSources(),
1077 content::NotificationService::NoDetails());
1079 content::RecordAction(UserMetricsAction("UpdateChrome"));
1080 browser
->window()->ShowUpdateChromeDialog();
1084 void ToggleSpeechInput(Browser
* browser
) {
1085 SearchTabHelper
* search_tab_helper
=
1086 SearchTabHelper::FromWebContents(
1087 browser
->tab_strip_model()->GetActiveWebContents());
1088 // |search_tab_helper| can be null in unit tests.
1089 if (search_tab_helper
)
1090 search_tab_helper
->ToggleVoiceSearch();
1093 void DistillCurrentPage(Browser
* browser
) {
1094 DistillCurrentPageAndView(browser
->tab_strip_model()->GetActiveWebContents());
1097 bool CanRequestTabletSite(WebContents
* current_tab
) {
1098 return current_tab
&&
1099 current_tab
->GetController().GetLastCommittedEntry() != NULL
;
1102 bool IsRequestingTabletSite(Browser
* browser
) {
1103 WebContents
* current_tab
= browser
->tab_strip_model()->GetActiveWebContents();
1106 content::NavigationEntry
* entry
=
1107 current_tab
->GetController().GetLastCommittedEntry();
1110 return entry
->GetIsOverridingUserAgent();
1113 void ToggleRequestTabletSite(Browser
* browser
) {
1114 WebContents
* current_tab
= browser
->tab_strip_model()->GetActiveWebContents();
1117 NavigationController
& controller
= current_tab
->GetController();
1118 NavigationEntry
* entry
= controller
.GetLastCommittedEntry();
1121 if (entry
->GetIsOverridingUserAgent()) {
1122 entry
->SetIsOverridingUserAgent(false);
1124 entry
->SetIsOverridingUserAgent(true);
1125 chrome::VersionInfo version_info
;
1126 std::string product
= version_info
.ProductNameAndVersionForUserAgent();
1127 current_tab
->SetUserAgentOverride(content::BuildUserAgentFromOSAndProduct(
1128 kOsOverrideForTabletSite
, product
));
1130 controller
.ReloadOriginalRequestURL(true);
1133 void ToggleFullscreenMode(Browser
* browser
) {
1135 browser
->exclusive_access_manager()
1136 ->fullscreen_controller()
1137 ->ToggleBrowserFullscreenMode();
1140 void ClearCache(Browser
* browser
) {
1141 BrowsingDataRemover
* remover
=
1142 BrowsingDataRemover::CreateForUnboundedRange(browser
->profile());
1143 remover
->Remove(BrowsingDataRemover::REMOVE_CACHE
,
1144 BrowsingDataHelper::UNPROTECTED_WEB
);
1145 // BrowsingDataRemover takes care of deleting itself when done.
1148 bool IsDebuggerAttachedToCurrentTab(Browser
* browser
) {
1149 WebContents
* contents
= browser
->tab_strip_model()->GetActiveWebContents();
1151 content::DevToolsAgentHost::IsDebuggerAttached(contents
) : false;
1154 void ViewSource(Browser
* browser
, WebContents
* contents
) {
1157 // Use the last committed entry, since the pending entry hasn't loaded yet and
1158 // won't be copied into the cloned tab.
1159 NavigationEntry
* entry
= contents
->GetController().GetLastCommittedEntry();
1163 ViewSource(browser
, contents
, entry
->GetURL(), entry
->GetPageState());
1166 void ViewSource(Browser
* browser
,
1167 WebContents
* contents
,
1169 const content::PageState
& page_state
) {
1170 content::RecordAction(UserMetricsAction("ViewSource"));
1173 WebContents
* view_source_contents
= contents
->Clone();
1174 DCHECK(view_source_contents
->GetController().CanPruneAllButLastCommitted());
1175 view_source_contents
->GetController().PruneAllButLastCommitted();
1176 NavigationEntry
* last_committed_entry
=
1177 view_source_contents
->GetController().GetLastCommittedEntry();
1178 if (!last_committed_entry
)
1181 GURL view_source_url
=
1182 GURL(content::kViewSourceScheme
+ std::string(":") + url
.spec());
1183 last_committed_entry
->SetVirtualURL(view_source_url
);
1185 // Do not restore scroller position.
1186 last_committed_entry
->SetPageState(page_state
.RemoveScrollOffset());
1188 // Do not restore title, derive it from the url.
1189 last_committed_entry
->SetTitle(base::string16());
1191 // Now show view-source entry.
1192 if (browser
->CanSupportWindowFeature(Browser::FEATURE_TABSTRIP
)) {
1193 // If this is a tabbed browser, just create a duplicate tab inside the same
1194 // window next to the tab being duplicated.
1195 int index
= browser
->tab_strip_model()->GetIndexOfWebContents(contents
);
1196 int add_types
= TabStripModel::ADD_ACTIVE
|
1197 TabStripModel::ADD_INHERIT_GROUP
;
1198 browser
->tab_strip_model()->InsertWebContentsAt(
1200 view_source_contents
,
1203 Browser
* b
= new Browser(
1204 Browser::CreateParams(Browser::TYPE_TABBED
, browser
->profile(),
1205 browser
->host_desktop_type()));
1207 // Preserve the size of the original window. The new window has already
1208 // been given an offset by the OS, so we shouldn't copy the old bounds.
1209 BrowserWindow
* new_window
= b
->window();
1210 new_window
->SetBounds(gfx::Rect(new_window
->GetRestoredBounds().origin(),
1211 browser
->window()->GetRestoredBounds().size()));
1213 // We need to show the browser now. Otherwise ContainerWin assumes the
1214 // WebContents is invisible and won't size it.
1215 b
->window()->Show();
1217 // The page transition below is only for the purpose of inserting the tab.
1218 b
->tab_strip_model()->AddWebContents(view_source_contents
, -1,
1219 ui::PAGE_TRANSITION_LINK
,
1220 TabStripModel::ADD_ACTIVE
);
1223 SessionService
* session_service
=
1224 SessionServiceFactory::GetForProfileIfExisting(browser
->profile());
1225 if (session_service
)
1226 session_service
->TabRestored(view_source_contents
, false);
1229 void ViewSelectedSource(Browser
* browser
) {
1230 ViewSource(browser
, browser
->tab_strip_model()->GetActiveWebContents());
1233 bool CanViewSource(const Browser
* browser
) {
1234 return !browser
->is_devtools() &&
1235 browser
->tab_strip_model()->GetActiveWebContents()->GetController().
1239 #if defined(ENABLE_EXTENSIONS)
1240 void CreateApplicationShortcuts(Browser
* browser
) {
1241 content::RecordAction(UserMetricsAction("CreateShortcut"));
1242 extensions::TabHelper::FromWebContents(
1243 browser
->tab_strip_model()->GetActiveWebContents())->
1244 CreateApplicationShortcuts();
1247 void CreateBookmarkAppFromCurrentWebContents(Browser
* browser
) {
1248 content::RecordAction(UserMetricsAction("CreateHostedApp"));
1249 extensions::TabHelper::FromWebContents(
1250 browser
->tab_strip_model()->GetActiveWebContents())->
1251 CreateHostedAppFromWebContents();
1254 bool CanCreateApplicationShortcuts(const Browser
* browser
) {
1255 return extensions::TabHelper::FromWebContents(
1256 browser
->tab_strip_model()->GetActiveWebContents())->
1257 CanCreateApplicationShortcuts();
1260 bool CanCreateBookmarkApp(const Browser
* browser
) {
1261 return extensions::TabHelper::FromWebContents(
1262 browser
->tab_strip_model()->GetActiveWebContents())
1263 ->CanCreateBookmarkApp();
1266 void ConvertTabToAppWindow(Browser
* browser
,
1267 content::WebContents
* contents
) {
1268 const GURL
& url
= contents
->GetController().GetLastCommittedEntry()->GetURL();
1269 std::string app_name
= web_app::GenerateApplicationNameFromURL(url
);
1271 int index
= browser
->tab_strip_model()->GetIndexOfWebContents(contents
);
1273 browser
->tab_strip_model()->DetachWebContentsAt(index
);
1275 Browser
* app_browser
= new Browser(
1276 Browser::CreateParams::CreateForApp(app_name
,
1277 true /* trusted_source */,
1280 browser
->host_desktop_type()));
1281 app_browser
->tab_strip_model()->AppendWebContents(contents
, true);
1283 contents
->GetMutableRendererPrefs()->can_accept_load_drops
= false;
1284 contents
->GetRenderViewHost()->SyncRendererPrefs();
1285 app_browser
->window()->Show();
1287 #endif // defined(ENABLE_EXTENSIONS)
1289 } // namespace chrome