Elim cr-checkbox
[chromium-blink-merge.git] / chrome / browser / ui / browser_tab_restore_service_delegate.cc
blob2e783a28bde78530a8af93ef91ed5345cfb610c0
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(
43 int index) const {
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,
59 int tab_index,
60 int selected_navigation,
61 const std::string& extension_app_id,
62 bool select,
63 bool pin,
64 bool from_last_session,
65 const sessions::TabClientData* tab_client_data,
66 const std::string& user_agent_override) {
67 SessionStorageNamespace* storage_namespace =
68 tab_client_data
69 ? static_cast<const sessions::ContentTabClientData*>(tab_client_data)
70 ->session_storage_namespace()
71 : nullptr;
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 =
88 tab_client_data
89 ? static_cast<const sessions::ContentTabClientData*>(tab_client_data)
90 ->session_storage_namespace()
91 : nullptr;
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_);
104 // static
105 sessions::TabRestoreServiceDelegate* BrowserTabRestoreServiceDelegate::Create(
106 Profile* profile,
107 chrome::HostDesktopType host_desktop_type,
108 const std::string& app_name) {
109 Browser* browser;
110 if (app_name.empty()) {
111 browser = new Browser(Browser::CreateParams(profile, host_desktop_type));
112 } else {
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,
117 host_desktop_type));
119 if (browser)
120 return browser->tab_restore_service_delegate();
121 else
122 return NULL;
125 // static
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;
133 // static
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;