Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / ash / chrome_new_window_delegate.cc
blobf45ec8103b4c1569a3447ccd63d53bce2158e6d4
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 ~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 {
59 // This destroys us.
60 delegate_->tab_restore_helper_.reset();
63 void TabRestoreServiceLoaded(TabRestoreService* service) override {
64 RestoreTabUsingProfile(profile_);
65 // This destroys us.
66 delegate_->tab_restore_helper_.reset();
69 private:
70 ChromeNewWindowDelegate* delegate_;
71 Profile* profile_;
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);
81 return;
84 chrome::ScopedTabbedBrowserDisplayer displayer(
85 ProfileManager::GetActiveUserProfile(),
86 chrome::HOST_DESKTOP_TYPE_ASH);
87 chrome::NewTab(displayer.browser());
90 void ChromeNewWindowDelegate::NewWindow(bool is_incognito) {
91 Profile* profile = ProfileManager::GetActiveUserProfile();
92 chrome::NewEmptyWindow(
93 is_incognito ? profile->GetOffTheRecordProfile() : profile,
94 chrome::HOST_DESKTOP_TYPE_ASH);
97 void ChromeNewWindowDelegate::RestoreTab() {
98 if (tab_restore_helper_.get()) {
99 DCHECK(!tab_restore_helper_->tab_restore_service()->IsLoaded());
100 return;
103 Browser* browser = GetBrowserForActiveWindow();
104 Profile* profile = browser ? browser->profile() : NULL;
105 if (!profile)
106 profile = ProfileManager::GetActiveUserProfile();
107 if (profile->IsOffTheRecord())
108 return;
109 TabRestoreService* service =
110 TabRestoreServiceFactory::GetForProfile(profile);
111 if (!service)
112 return;
114 if (service->IsLoaded()) {
115 RestoreTabUsingProfile(profile);
116 } else {
117 tab_restore_helper_.reset(new TabRestoreHelper(this, profile, service));
118 service->LoadTabsFromLastSession();
122 void ChromeNewWindowDelegate::ShowTaskManager() {
123 chrome::OpenTaskManager(NULL);
126 void ChromeNewWindowDelegate::OpenFeedbackPage() {
127 chrome::OpenFeedbackDialog(GetBrowserForActiveWindow());