11e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
21e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
31e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// found in the LICENSE file.
41e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
51e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/ui/ash/chrome_new_window_delegate.h"
61e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
71e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ash/wm/window_util.h"
81e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/profiles/profile_manager.h"
91e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/sessions/tab_restore_service.h"
101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/sessions/tab_restore_service_factory.h"
111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/sessions/tab_restore_service_observer.h"
121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/ui/ash/chrome_shell_delegate.h"
131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/ui/browser.h"
141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/ui/browser_commands.h"
151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/ui/browser_finder.h"
161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/ui/browser_window.h"
170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)namespace {
201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void RestoreTabUsingProfile(Profile* profile) {
221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  TabRestoreService* service = TabRestoreServiceFactory::GetForProfile(profile);
231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  service->RestoreMostRecentEntry(NULL, chrome::HOST_DESKTOP_TYPE_ASH);
241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Returns the browser for the active window, if any.
270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)Browser* GetBrowserForActiveWindow() {
280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  return chrome::FindBrowserWithWindow(ash::wm::GetActiveWindow());
290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}  // namespace
321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)ChromeNewWindowDelegate::ChromeNewWindowDelegate() {}
341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)ChromeNewWindowDelegate::~ChromeNewWindowDelegate() {}
351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// TabRestoreHelper is used to restore a tab. In particular when the user
371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// attempts to a restore a tab if the TabRestoreService hasn't finished loading
381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// this waits for it. Once the TabRestoreService finishes loading the tab is
391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// restored.
401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)class ChromeNewWindowDelegate::TabRestoreHelper
411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    : public TabRestoreServiceObserver {
421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public:
431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  TabRestoreHelper(ChromeNewWindowDelegate* delegate,
441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                   Profile* profile,
451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                   TabRestoreService* service)
461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      : delegate_(delegate),
471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        profile_(profile),
481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        tab_restore_service_(service) {
491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    tab_restore_service_->AddObserver(this);
501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual ~TabRestoreHelper() {
531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    tab_restore_service_->RemoveObserver(this);
541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  TabRestoreService* tab_restore_service() { return tab_restore_service_; }
571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual void TabRestoreServiceChanged(TabRestoreService* service) OVERRIDE {
591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual void TabRestoreServiceDestroyed(TabRestoreService* service) OVERRIDE {
621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // This destroys us.
631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    delegate_->tab_restore_helper_.reset();
641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual void TabRestoreServiceLoaded(TabRestoreService* service) OVERRIDE {
671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    RestoreTabUsingProfile(profile_);
681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // This destroys us.
691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    delegate_->tab_restore_helper_.reset();
701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) private:
731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ChromeNewWindowDelegate* delegate_;
741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  Profile* profile_;
751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  TabRestoreService* tab_restore_service_;
761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TabRestoreHelper);
781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)};
791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void ChromeNewWindowDelegate::NewTab() {
810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  Browser* browser = GetBrowserForActiveWindow();
820f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (browser && browser->is_type_tabbed()) {
830f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    chrome::NewTab(browser);
840f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return;
850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  chrome::ScopedTabbedBrowserDisplayer displayer(
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ProfileManager::GetActiveUserProfile(),
890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      chrome::HOST_DESKTOP_TYPE_ASH);
900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  chrome::NewTab(displayer.browser());
911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void ChromeNewWindowDelegate::NewWindow(bool is_incognito) {
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  Profile* profile = ProfileManager::GetActiveUserProfile();
951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  chrome::NewEmptyWindow(
961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      is_incognito ? profile->GetOffTheRecordProfile() : profile,
971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      chrome::HOST_DESKTOP_TYPE_ASH);
981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void ChromeNewWindowDelegate::RestoreTab() {
1011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (tab_restore_helper_.get()) {
1021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    DCHECK(!tab_restore_helper_->tab_restore_service()->IsLoaded());
1031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return;
1041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
1051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1060f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  Browser* browser = GetBrowserForActiveWindow();
1071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  Profile* profile = browser ? browser->profile() : NULL;
1081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (!profile)
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    profile = ProfileManager::GetActiveUserProfile();
1101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (profile->IsOffTheRecord())
1111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return;
1121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  TabRestoreService* service =
1131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      TabRestoreServiceFactory::GetForProfile(profile);
1141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (!service)
1151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return;
1161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (service->IsLoaded()) {
1181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    RestoreTabUsingProfile(profile);
1191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  } else {
1201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    tab_restore_helper_.reset(new TabRestoreHelper(this, profile, service));
1211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    service->LoadTabsFromLastSession();
1221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
1231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void ChromeNewWindowDelegate::ShowTaskManager() {
1261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  chrome::OpenTaskManager(NULL);
1271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void ChromeNewWindowDelegate::OpenFeedbackPage() {
1300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  chrome::OpenFeedbackDialog(GetBrowserForActiveWindow());
1311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
132