1 // Copyright 2013 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/ash/chrome_new_window_delegate.h"
7 #include "ash/wm/window_util.h"
8 #include "chrome/browser/profiles/profile_manager.h"
9 #include "chrome/browser/sessions/tab_restore_service.h"
10 #include "chrome/browser/sessions/tab_restore_service_factory.h"
11 #include "chrome/browser/sessions/tab_restore_service_observer.h"
12 #include "chrome/browser/ui/ash/chrome_shell_delegate.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_commands.h"
15 #include "chrome/browser/ui/browser_finder.h"
16 #include "chrome/browser/ui/browser_window.h"
17 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
21 void RestoreTabUsingProfile(Profile
* profile
) {
22 TabRestoreService
* service
= TabRestoreServiceFactory::GetForProfile(profile
);
23 service
->RestoreMostRecentEntry(NULL
, chrome::HOST_DESKTOP_TYPE_ASH
);
26 // Returns the browser for the active window, if any.
27 Browser
* GetBrowserForActiveWindow() {
28 return chrome::FindBrowserWithWindow(ash::wm::GetActiveWindow());
33 ChromeNewWindowDelegate::ChromeNewWindowDelegate() {}
34 ChromeNewWindowDelegate::~ChromeNewWindowDelegate() {}
36 // TabRestoreHelper is used to restore a tab. In particular when the user
37 // attempts to a restore a tab if the TabRestoreService hasn't finished loading
38 // this waits for it. Once the TabRestoreService finishes loading the tab is
40 class ChromeNewWindowDelegate::TabRestoreHelper
41 : public TabRestoreServiceObserver
{
43 TabRestoreHelper(ChromeNewWindowDelegate
* delegate
,
45 TabRestoreService
* service
)
46 : delegate_(delegate
),
48 tab_restore_service_(service
) {
49 tab_restore_service_
->AddObserver(this);
52 virtual ~TabRestoreHelper() {
53 tab_restore_service_
->RemoveObserver(this);
56 TabRestoreService
* tab_restore_service() { return tab_restore_service_
; }
58 virtual void TabRestoreServiceChanged(TabRestoreService
* service
) OVERRIDE
{
61 virtual void TabRestoreServiceDestroyed(TabRestoreService
* service
) OVERRIDE
{
63 delegate_
->tab_restore_helper_
.reset();
66 virtual void TabRestoreServiceLoaded(TabRestoreService
* service
) OVERRIDE
{
67 RestoreTabUsingProfile(profile_
);
69 delegate_
->tab_restore_helper_
.reset();
73 ChromeNewWindowDelegate
* delegate_
;
75 TabRestoreService
* tab_restore_service_
;
77 DISALLOW_COPY_AND_ASSIGN(TabRestoreHelper
);
80 void ChromeNewWindowDelegate::NewTab() {
81 Browser
* browser
= GetBrowserForActiveWindow();
82 if (browser
&& browser
->is_type_tabbed()) {
83 chrome::NewTab(browser
);
87 chrome::ScopedTabbedBrowserDisplayer
displayer(
88 ProfileManager::GetActiveUserProfile(),
89 chrome::HOST_DESKTOP_TYPE_ASH
);
90 chrome::NewTab(displayer
.browser());
93 void ChromeNewWindowDelegate::NewWindow(bool is_incognito
) {
94 Profile
* profile
= ProfileManager::GetActiveUserProfile();
95 chrome::NewEmptyWindow(
96 is_incognito
? profile
->GetOffTheRecordProfile() : profile
,
97 chrome::HOST_DESKTOP_TYPE_ASH
);
100 void ChromeNewWindowDelegate::RestoreTab() {
101 if (tab_restore_helper_
.get()) {
102 DCHECK(!tab_restore_helper_
->tab_restore_service()->IsLoaded());
106 Browser
* browser
= GetBrowserForActiveWindow();
107 Profile
* profile
= browser
? browser
->profile() : NULL
;
109 profile
= ProfileManager::GetActiveUserProfile();
110 if (profile
->IsOffTheRecord())
112 TabRestoreService
* service
=
113 TabRestoreServiceFactory::GetForProfile(profile
);
117 if (service
->IsLoaded()) {
118 RestoreTabUsingProfile(profile
);
120 tab_restore_helper_
.reset(new TabRestoreHelper(this, profile
, service
));
121 service
->LoadTabsFromLastSession();
125 void ChromeNewWindowDelegate::ShowTaskManager() {
126 chrome::OpenTaskManager(NULL
);
129 void ChromeNewWindowDelegate::OpenFeedbackPage() {
130 chrome::OpenFeedbackDialog(GetBrowserForActiveWindow());