15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser_tab_restore_service_delegate.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser_commands.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser_finder.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser_tabrestore.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser_window.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/tabs/tab_strip_model.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/navigation_controller.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::NavigationController;
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::SessionStorageNamespace;
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::WebContents;
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void BrowserTabRestoreServiceDelegate::ShowBrowserWindow() {
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  browser_->window()->Show();
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const SessionID& BrowserTabRestoreServiceDelegate::GetSessionID() const {
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return browser_->session_id();
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int BrowserTabRestoreServiceDelegate::GetTabCount() const {
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return browser_->tab_strip_model()->count();
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int BrowserTabRestoreServiceDelegate::GetSelectedIndex() const {
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return browser_->tab_strip_model()->active_index();
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string BrowserTabRestoreServiceDelegate::GetAppName() const {
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return browser_->app_name();
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WebContents* BrowserTabRestoreServiceDelegate::GetWebContentsAt(
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int index) const {
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return browser_->tab_strip_model()->GetWebContentsAt(index);
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WebContents* BrowserTabRestoreServiceDelegate::GetActiveWebContents() const {
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return browser_->tab_strip_model()->GetActiveWebContents();
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BrowserTabRestoreServiceDelegate::IsTabPinned(int index) const {
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return browser_->tab_strip_model()->IsTabPinned(index);
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WebContents* BrowserTabRestoreServiceDelegate::AddRestoredTab(
53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const std::vector<sessions::SerializedNavigationEntry>& navigations,
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int tab_index,
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int selected_navigation,
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& extension_app_id,
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool select,
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool pin,
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool from_last_session,
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      SessionStorageNamespace* storage_namespace,
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& user_agent_override) {
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return chrome::AddRestoredTab(browser_, navigations, tab_index,
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                selected_navigation, extension_app_id, select,
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                pin, from_last_session, storage_namespace,
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                user_agent_override);
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
683240926e260ce088908e02ac07a6cf7b0c0cbf44Ben MurdochWebContents* BrowserTabRestoreServiceDelegate::ReplaceRestoredTab(
69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const std::vector<sessions::SerializedNavigationEntry>& navigations,
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int selected_navigation,
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool from_last_session,
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& extension_app_id,
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      SessionStorageNamespace* session_storage_namespace,
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& user_agent_override) {
753240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  return chrome::ReplaceRestoredTab(browser_, navigations, selected_navigation,
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             from_last_session, extension_app_id,
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             session_storage_namespace, user_agent_override);
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void BrowserTabRestoreServiceDelegate::CloseTab() {
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  chrome::CloseTab(browser_);
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Implementations of TabRestoreServiceDelegate static methods
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TabRestoreServiceDelegate* TabRestoreServiceDelegate::Create(
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Profile* profile,
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    chrome::HostDesktopType host_desktop_type,
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& app_name) {
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Browser* browser;
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (app_name.empty()) {
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    browser = new Browser(Browser::CreateParams(profile, host_desktop_type));
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
95a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    // Only trusted app popup windows should ever be restored.
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    browser = new Browser(
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        Browser::CreateParams::CreateForApp(
98a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch            app_name, true /* trusted_source */, gfx::Rect(), profile,
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            host_desktop_type));
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (browser)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return browser->tab_restore_service_delegate();
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  else
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TabRestoreServiceDelegate*
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TabRestoreServiceDelegate::FindDelegateForWebContents(
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        const WebContents* contents) {
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Browser* browser = chrome::FindBrowserWithWebContents(contents);
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return browser ? browser->tab_restore_service_delegate() : NULL;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TabRestoreServiceDelegate* TabRestoreServiceDelegate::FindDelegateWithID(
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SessionID::id_type desired_id,
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    chrome::HostDesktopType host_desktop_type) {
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Browser* browser = chrome::FindBrowserWithID(desired_id);
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return (browser && browser->host_desktop_type() == host_desktop_type) ?
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)             browser->tab_restore_service_delegate() : NULL;
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
123