No longer register app window placement preference keys on
[chromium-blink-merge.git] / chrome / browser / ui / ash / chrome_new_window_delegate.cc
blobe0d4a53a4cf7deecd3581382c2e93d8c4144ca54
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"
19 namespace {
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());
31 } // namespace
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
39 // restored.
40 class ChromeNewWindowDelegate::TabRestoreHelper
41 : public TabRestoreServiceObserver {
42 public:
43 TabRestoreHelper(ChromeNewWindowDelegate* delegate,
44 Profile* profile,
45 TabRestoreService* service)
46 : delegate_(delegate),
47 profile_(profile),
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 {
62 // This destroys us.
63 delegate_->tab_restore_helper_.reset();
66 virtual void TabRestoreServiceLoaded(TabRestoreService* service) OVERRIDE {
67 RestoreTabUsingProfile(profile_);
68 // This destroys us.
69 delegate_->tab_restore_helper_.reset();
72 private:
73 ChromeNewWindowDelegate* delegate_;
74 Profile* profile_;
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);
84 return;
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());
103 return;
106 Browser* browser = GetBrowserForActiveWindow();
107 Profile* profile = browser ? browser->profile() : NULL;
108 if (!profile)
109 profile = ProfileManager::GetActiveUserProfile();
110 if (profile->IsOffTheRecord())
111 return;
112 TabRestoreService* service =
113 TabRestoreServiceFactory::GetForProfile(profile);
114 if (!service)
115 return;
117 if (service->IsLoaded()) {
118 RestoreTabUsingProfile(profile);
119 } else {
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());