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 ~TabRestoreHelper() override
{ tab_restore_service_
->RemoveObserver(this); }
54 TabRestoreService
* tab_restore_service() { return tab_restore_service_
; }
56 void TabRestoreServiceChanged(TabRestoreService
* service
) override
{}
58 void TabRestoreServiceDestroyed(TabRestoreService
* service
) override
{
60 delegate_
->tab_restore_helper_
.reset();
63 void TabRestoreServiceLoaded(TabRestoreService
* service
) override
{
64 RestoreTabUsingProfile(profile_
);
66 delegate_
->tab_restore_helper_
.reset();
70 ChromeNewWindowDelegate
* delegate_
;
72 TabRestoreService
* tab_restore_service_
;
74 DISALLOW_COPY_AND_ASSIGN(TabRestoreHelper
);
77 void ChromeNewWindowDelegate::NewTab() {
78 Browser
* browser
= GetBrowserForActiveWindow();
79 if (browser
&& browser
->is_type_tabbed()) {
80 chrome::NewTab(browser
);
84 // Display a browser, setting the focus to the location bar after it is shown.
86 chrome::ScopedTabbedBrowserDisplayer
displayer(
87 ProfileManager::GetActiveUserProfile(), chrome::HOST_DESKTOP_TYPE_ASH
);
88 browser
= displayer
.browser();
89 chrome::NewTab(browser
);
92 browser
->SetFocusToLocationBar(false);
95 void ChromeNewWindowDelegate::NewWindow(bool is_incognito
) {
96 Profile
* profile
= ProfileManager::GetActiveUserProfile();
97 chrome::NewEmptyWindow(
98 is_incognito
? profile
->GetOffTheRecordProfile() : profile
,
99 chrome::HOST_DESKTOP_TYPE_ASH
);
102 void ChromeNewWindowDelegate::RestoreTab() {
103 if (tab_restore_helper_
.get()) {
104 DCHECK(!tab_restore_helper_
->tab_restore_service()->IsLoaded());
108 Browser
* browser
= GetBrowserForActiveWindow();
109 Profile
* profile
= browser
? browser
->profile() : NULL
;
111 profile
= ProfileManager::GetActiveUserProfile();
112 if (profile
->IsOffTheRecord())
114 TabRestoreService
* service
=
115 TabRestoreServiceFactory::GetForProfile(profile
);
119 if (service
->IsLoaded()) {
120 RestoreTabUsingProfile(profile
);
122 tab_restore_helper_
.reset(new TabRestoreHelper(this, profile
, service
));
123 service
->LoadTabsFromLastSession();
127 void ChromeNewWindowDelegate::ShowTaskManager() {
128 chrome::OpenTaskManager(NULL
);
131 void ChromeNewWindowDelegate::OpenFeedbackPage() {
132 chrome::OpenFeedbackDialog(GetBrowserForActiveWindow());