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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/browser_tab_restore_service_delegate.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/browser_commands.h"
9 #include "chrome/browser/ui/browser_finder.h"
10 #include "chrome/browser/ui/browser_tabrestore.h"
11 #include "chrome/browser/ui/browser_window.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "components/sessions/content/content_live_tab.h"
14 #include "components/sessions/content/content_tab_client_data.h"
15 #include "content/public/browser/navigation_controller.h"
16 #include "content/public/browser/session_storage_namespace.h"
18 using content::NavigationController
;
19 using content::SessionStorageNamespace
;
20 using content::WebContents
;
22 void BrowserTabRestoreServiceDelegate::ShowBrowserWindow() {
23 browser_
->window()->Show();
26 const SessionID
& BrowserTabRestoreServiceDelegate::GetSessionID() const {
27 return browser_
->session_id();
30 int BrowserTabRestoreServiceDelegate::GetTabCount() const {
31 return browser_
->tab_strip_model()->count();
34 int BrowserTabRestoreServiceDelegate::GetSelectedIndex() const {
35 return browser_
->tab_strip_model()->active_index();
38 std::string
BrowserTabRestoreServiceDelegate::GetAppName() const {
39 return browser_
->app_name();
42 sessions::LiveTab
* BrowserTabRestoreServiceDelegate::GetLiveTabAt(
44 return sessions::ContentLiveTab::FromWebContents(
45 browser_
->tab_strip_model()->GetWebContentsAt(index
));
48 sessions::LiveTab
* BrowserTabRestoreServiceDelegate::GetActiveLiveTab() const {
49 return sessions::ContentLiveTab::FromWebContents(
50 browser_
->tab_strip_model()->GetActiveWebContents());
53 bool BrowserTabRestoreServiceDelegate::IsTabPinned(int index
) const {
54 return browser_
->tab_strip_model()->IsTabPinned(index
);
57 sessions::LiveTab
* BrowserTabRestoreServiceDelegate::AddRestoredTab(
58 const std::vector
<sessions::SerializedNavigationEntry
>& navigations
,
60 int selected_navigation
,
61 const std::string
& extension_app_id
,
64 bool from_last_session
,
65 const sessions::TabClientData
* tab_client_data
,
66 const std::string
& user_agent_override
) {
67 SessionStorageNamespace
* storage_namespace
=
69 ? static_cast<const sessions::ContentTabClientData
*>(tab_client_data
)
70 ->session_storage_namespace()
73 WebContents
* web_contents
= chrome::AddRestoredTab(
74 browser_
, navigations
, tab_index
, selected_navigation
, extension_app_id
,
75 select
, pin
, from_last_session
, storage_namespace
, user_agent_override
);
77 return sessions::ContentLiveTab::FromWebContents(web_contents
);
80 sessions::LiveTab
* BrowserTabRestoreServiceDelegate::ReplaceRestoredTab(
81 const std::vector
<sessions::SerializedNavigationEntry
>& navigations
,
82 int selected_navigation
,
83 bool from_last_session
,
84 const std::string
& extension_app_id
,
85 const sessions::TabClientData
* tab_client_data
,
86 const std::string
& user_agent_override
) {
87 SessionStorageNamespace
* storage_namespace
=
89 ? static_cast<const sessions::ContentTabClientData
*>(tab_client_data
)
90 ->session_storage_namespace()
93 WebContents
* web_contents
= chrome::ReplaceRestoredTab(
94 browser_
, navigations
, selected_navigation
, from_last_session
,
95 extension_app_id
, storage_namespace
, user_agent_override
);
97 return sessions::ContentLiveTab::FromWebContents(web_contents
);
100 void BrowserTabRestoreServiceDelegate::CloseTab() {
101 chrome::CloseTab(browser_
);
105 sessions::TabRestoreServiceDelegate
* BrowserTabRestoreServiceDelegate::Create(
107 chrome::HostDesktopType host_desktop_type
,
108 const std::string
& app_name
) {
110 if (app_name
.empty()) {
111 browser
= new Browser(Browser::CreateParams(profile
, host_desktop_type
));
113 // Only trusted app popup windows should ever be restored.
114 browser
= new Browser(
115 Browser::CreateParams::CreateForApp(
116 app_name
, true /* trusted_source */, gfx::Rect(), profile
,
120 return browser
->tab_restore_service_delegate();
126 sessions::TabRestoreServiceDelegate
*
127 BrowserTabRestoreServiceDelegate::FindDelegateForWebContents(
128 const WebContents
* contents
) {
129 Browser
* browser
= chrome::FindBrowserWithWebContents(contents
);
130 return browser
? browser
->tab_restore_service_delegate() : nullptr;
134 sessions::TabRestoreServiceDelegate
*
135 BrowserTabRestoreServiceDelegate::FindDelegateWithID(
136 SessionID::id_type desired_id
,
137 chrome::HostDesktopType host_desktop_type
) {
138 Browser
* browser
= chrome::FindBrowserWithID(desired_id
);
139 return (browser
&& browser
->host_desktop_type() == host_desktop_type
) ?
140 browser
->tab_restore_service_delegate() : NULL
;