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.
4
5 #include "chrome/browser/ui/browser_tab_restore_service_delegate.h"
6
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 "content/public/browser/navigation_controller.h"
14
15 using content::NavigationController;
16 using content::SessionStorageNamespace;
17 using content::WebContents;
18
ShowBrowserWindow()19 void BrowserTabRestoreServiceDelegate::ShowBrowserWindow() {
20 browser_->window()->Show();
21 }
22
GetSessionID() const23 const SessionID& BrowserTabRestoreServiceDelegate::GetSessionID() const {
24 return browser_->session_id();
25 }
26
GetTabCount() const27 int BrowserTabRestoreServiceDelegate::GetTabCount() const {
28 return browser_->tab_strip_model()->count();
29 }
30
GetSelectedIndex() const31 int BrowserTabRestoreServiceDelegate::GetSelectedIndex() const {
32 return browser_->tab_strip_model()->active_index();
33 }
34
GetAppName() const35 std::string BrowserTabRestoreServiceDelegate::GetAppName() const {
36 return browser_->app_name();
37 }
38
GetWebContentsAt(int index) const39 WebContents* BrowserTabRestoreServiceDelegate::GetWebContentsAt(
40 int index) const {
41 return browser_->tab_strip_model()->GetWebContentsAt(index);
42 }
43
GetActiveWebContents() const44 WebContents* BrowserTabRestoreServiceDelegate::GetActiveWebContents() const {
45 return browser_->tab_strip_model()->GetActiveWebContents();
46 }
47
IsTabPinned(int index) const48 bool BrowserTabRestoreServiceDelegate::IsTabPinned(int index) const {
49 return browser_->tab_strip_model()->IsTabPinned(index);
50 }
51
AddRestoredTab(const std::vector<sessions::SerializedNavigationEntry> & navigations,int tab_index,int selected_navigation,const std::string & extension_app_id,bool select,bool pin,bool from_last_session,SessionStorageNamespace * storage_namespace,const std::string & user_agent_override)52 WebContents* BrowserTabRestoreServiceDelegate::AddRestoredTab(
53 const std::vector<sessions::SerializedNavigationEntry>& navigations,
54 int tab_index,
55 int selected_navigation,
56 const std::string& extension_app_id,
57 bool select,
58 bool pin,
59 bool from_last_session,
60 SessionStorageNamespace* storage_namespace,
61 const std::string& user_agent_override) {
62 return chrome::AddRestoredTab(browser_, navigations, tab_index,
63 selected_navigation, extension_app_id, select,
64 pin, from_last_session, storage_namespace,
65 user_agent_override);
66 }
67
ReplaceRestoredTab(const std::vector<sessions::SerializedNavigationEntry> & navigations,int selected_navigation,bool from_last_session,const std::string & extension_app_id,SessionStorageNamespace * session_storage_namespace,const std::string & user_agent_override)68 WebContents* BrowserTabRestoreServiceDelegate::ReplaceRestoredTab(
69 const std::vector<sessions::SerializedNavigationEntry>& navigations,
70 int selected_navigation,
71 bool from_last_session,
72 const std::string& extension_app_id,
73 SessionStorageNamespace* session_storage_namespace,
74 const std::string& user_agent_override) {
75 return chrome::ReplaceRestoredTab(browser_, navigations, selected_navigation,
76 from_last_session, extension_app_id,
77 session_storage_namespace, user_agent_override);
78 }
79
CloseTab()80 void BrowserTabRestoreServiceDelegate::CloseTab() {
81 chrome::CloseTab(browser_);
82 }
83
84 // Implementations of TabRestoreServiceDelegate static methods
85
86 // static
Create(Profile * profile,chrome::HostDesktopType host_desktop_type,const std::string & app_name)87 TabRestoreServiceDelegate* TabRestoreServiceDelegate::Create(
88 Profile* profile,
89 chrome::HostDesktopType host_desktop_type,
90 const std::string& app_name) {
91 Browser* browser;
92 if (app_name.empty()) {
93 browser = new Browser(Browser::CreateParams(profile, host_desktop_type));
94 } else {
95 // Only trusted app popup windows should ever be restored.
96 browser = new Browser(
97 Browser::CreateParams::CreateForApp(
98 app_name, true /* trusted_source */, gfx::Rect(), profile,
99 host_desktop_type));
100 }
101 if (browser)
102 return browser->tab_restore_service_delegate();
103 else
104 return NULL;
105 }
106
107 // static
108 TabRestoreServiceDelegate*
FindDelegateForWebContents(const WebContents * contents)109 TabRestoreServiceDelegate::FindDelegateForWebContents(
110 const WebContents* contents) {
111 Browser* browser = chrome::FindBrowserWithWebContents(contents);
112 return browser ? browser->tab_restore_service_delegate() : NULL;
113 }
114
115 // static
FindDelegateWithID(SessionID::id_type desired_id,chrome::HostDesktopType host_desktop_type)116 TabRestoreServiceDelegate* TabRestoreServiceDelegate::FindDelegateWithID(
117 SessionID::id_type desired_id,
118 chrome::HostDesktopType host_desktop_type) {
119 Browser* browser = chrome::FindBrowserWithID(desired_id);
120 return (browser && browser->host_desktop_type() == host_desktop_type) ?
121 browser->tab_restore_service_delegate() : NULL;
122 }
123