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_factory.h"
10 #include "chrome/browser/ui/ash/chrome_shell_delegate.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_commands.h"
13 #include "chrome/browser/ui/browser_finder.h"
14 #include "chrome/browser/ui/browser_window.h"
15 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
16 #include "components/sessions/core/tab_restore_service.h"
17 #include "components/sessions/core/tab_restore_service_observer.h"
21 void RestoreTabUsingProfile(Profile
* profile
) {
22 sessions::TabRestoreService
* service
=
23 TabRestoreServiceFactory::GetForProfile(profile
);
24 service
->RestoreMostRecentEntry(NULL
, chrome::HOST_DESKTOP_TYPE_ASH
);
27 // Returns the browser for the active window, if any.
28 Browser
* GetBrowserForActiveWindow() {
29 return chrome::FindBrowserWithWindow(ash::wm::GetActiveWindow());
34 ChromeNewWindowDelegate::ChromeNewWindowDelegate() {}
35 ChromeNewWindowDelegate::~ChromeNewWindowDelegate() {}
37 // TabRestoreHelper is used to restore a tab. In particular when the user
38 // attempts to a restore a tab if the TabRestoreService hasn't finished loading
39 // this waits for it. Once the TabRestoreService finishes loading the tab is
41 class ChromeNewWindowDelegate::TabRestoreHelper
42 : public sessions::TabRestoreServiceObserver
{
44 TabRestoreHelper(ChromeNewWindowDelegate
* delegate
,
46 sessions::TabRestoreService
* service
)
47 : delegate_(delegate
), profile_(profile
), tab_restore_service_(service
) {
48 tab_restore_service_
->AddObserver(this);
51 ~TabRestoreHelper() override
{ tab_restore_service_
->RemoveObserver(this); }
53 sessions::TabRestoreService
* tab_restore_service() {
54 return tab_restore_service_
;
57 void TabRestoreServiceChanged(sessions::TabRestoreService
* service
) override
{
60 void TabRestoreServiceDestroyed(
61 sessions::TabRestoreService
* service
) override
{
63 delegate_
->tab_restore_helper_
.reset();
66 void TabRestoreServiceLoaded(sessions::TabRestoreService
* service
) override
{
67 RestoreTabUsingProfile(profile_
);
69 delegate_
->tab_restore_helper_
.reset();
73 ChromeNewWindowDelegate
* delegate_
;
75 sessions::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 // Display a browser, setting the focus to the location bar after it is shown.
89 chrome::ScopedTabbedBrowserDisplayer
displayer(
90 ProfileManager::GetActiveUserProfile(), chrome::HOST_DESKTOP_TYPE_ASH
);
91 browser
= displayer
.browser();
92 chrome::NewTab(browser
);
95 browser
->SetFocusToLocationBar(false);
98 void ChromeNewWindowDelegate::NewWindow(bool is_incognito
) {
99 Profile
* profile
= ProfileManager::GetActiveUserProfile();
100 chrome::NewEmptyWindow(
101 is_incognito
? profile
->GetOffTheRecordProfile() : profile
,
102 chrome::HOST_DESKTOP_TYPE_ASH
);
105 void ChromeNewWindowDelegate::RestoreTab() {
106 if (tab_restore_helper_
.get()) {
107 DCHECK(!tab_restore_helper_
->tab_restore_service()->IsLoaded());
111 Browser
* browser
= GetBrowserForActiveWindow();
112 Profile
* profile
= browser
? browser
->profile() : NULL
;
114 profile
= ProfileManager::GetActiveUserProfile();
115 if (profile
->IsOffTheRecord())
117 sessions::TabRestoreService
* service
=
118 TabRestoreServiceFactory::GetForProfile(profile
);
122 if (service
->IsLoaded()) {
123 RestoreTabUsingProfile(profile
);
125 tab_restore_helper_
.reset(new TabRestoreHelper(this, profile
, service
));
126 service
->LoadTabsFromLastSession();
130 void ChromeNewWindowDelegate::ShowTaskManager() {
131 chrome::OpenTaskManager(NULL
);
134 void ChromeNewWindowDelegate::OpenFeedbackPage() {
135 chrome::OpenFeedbackDialog(GetBrowserForActiveWindow());