Elim cr-checkbox
[chromium-blink-merge.git] / chrome / browser / ui / ash / chrome_new_window_delegate.cc
blob34c5a9cfc9c310a291e28274f3b1c29800a561e6
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"
19 namespace {
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());
32 } // namespace
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
40 // restored.
41 class ChromeNewWindowDelegate::TabRestoreHelper
42 : public sessions::TabRestoreServiceObserver {
43 public:
44 TabRestoreHelper(ChromeNewWindowDelegate* delegate,
45 Profile* profile,
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 {
62 // This destroys us.
63 delegate_->tab_restore_helper_.reset();
66 void TabRestoreServiceLoaded(sessions::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 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);
84 return;
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());
108 return;
111 Browser* browser = GetBrowserForActiveWindow();
112 Profile* profile = browser ? browser->profile() : NULL;
113 if (!profile)
114 profile = ProfileManager::GetActiveUserProfile();
115 if (profile->IsOffTheRecord())
116 return;
117 sessions::TabRestoreService* service =
118 TabRestoreServiceFactory::GetForProfile(profile);
119 if (!service)
120 return;
122 if (service->IsLoaded()) {
123 RestoreTabUsingProfile(profile);
124 } else {
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());