| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/ui/ash/chrome_new_window_delegate.h" | |
| 6 | |
| 7 #include "ash/content/keyboard_overlay/keyboard_overlay_view.h" | |
| 8 #include "ash/wm/window_util.h" | |
| 9 #include "base/macros.h" | |
| 10 #include "chrome/browser/chromeos/file_manager/app_id.h" | |
| 11 #include "chrome/browser/extensions/api/terminal/terminal_extension_helper.h" | |
| 12 #include "chrome/browser/extensions/extension_service.h" | |
| 13 #include "chrome/browser/extensions/extension_util.h" | |
| 14 #include "chrome/browser/profiles/profile_manager.h" | |
| 15 #include "chrome/browser/sessions/tab_restore_service_factory.h" | |
| 16 #include "chrome/browser/ui/ash/chrome_shell_delegate.h" | |
| 17 #include "chrome/browser/ui/browser.h" | |
| 18 #include "chrome/browser/ui/browser_commands.h" | |
| 19 #include "chrome/browser/ui/browser_finder.h" | |
| 20 #include "chrome/browser/ui/browser_window.h" | |
| 21 #include "chrome/browser/ui/chrome_pages.h" | |
| 22 #include "chrome/browser/ui/extensions/app_launch_params.h" | |
| 23 #include "chrome/browser/ui/extensions/application_launch.h" | |
| 24 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" | |
| 25 #include "chrome/browser/ui/webui/chrome_web_contents_handler.h" | |
| 26 #include "chrome/common/url_constants.h" | |
| 27 #include "components/sessions/core/tab_restore_service.h" | |
| 28 #include "components/sessions/core/tab_restore_service_observer.h" | |
| 29 #include "content/public/browser/web_contents.h" | |
| 30 #include "extensions/browser/extension_system.h" | |
| 31 #include "extensions/common/constants.h" | |
| 32 #include "ui/base/window_open_disposition.h" | |
| 33 | |
| 34 namespace { | |
| 35 | |
| 36 void RestoreTabUsingProfile(Profile* profile) { | |
| 37 sessions::TabRestoreService* service = | |
| 38 TabRestoreServiceFactory::GetForProfile(profile); | |
| 39 service->RestoreMostRecentEntry(nullptr); | |
| 40 } | |
| 41 | |
| 42 // Returns the browser for the active window, if any. | |
| 43 Browser* GetBrowserForActiveWindow() { | |
| 44 return chrome::FindBrowserWithWindow(ash::wm::GetActiveWindow()); | |
| 45 } | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 ChromeNewWindowDelegate::ChromeNewWindowDelegate() {} | |
| 50 ChromeNewWindowDelegate::~ChromeNewWindowDelegate() {} | |
| 51 | |
| 52 // TabRestoreHelper is used to restore a tab. In particular when the user | |
| 53 // attempts to a restore a tab if the TabRestoreService hasn't finished loading | |
| 54 // this waits for it. Once the TabRestoreService finishes loading the tab is | |
| 55 // restored. | |
| 56 class ChromeNewWindowDelegate::TabRestoreHelper | |
| 57 : public sessions::TabRestoreServiceObserver { | |
| 58 public: | |
| 59 TabRestoreHelper(ChromeNewWindowDelegate* delegate, | |
| 60 Profile* profile, | |
| 61 sessions::TabRestoreService* service) | |
| 62 : delegate_(delegate), profile_(profile), tab_restore_service_(service) { | |
| 63 tab_restore_service_->AddObserver(this); | |
| 64 } | |
| 65 | |
| 66 ~TabRestoreHelper() override { tab_restore_service_->RemoveObserver(this); } | |
| 67 | |
| 68 sessions::TabRestoreService* tab_restore_service() { | |
| 69 return tab_restore_service_; | |
| 70 } | |
| 71 | |
| 72 void TabRestoreServiceChanged(sessions::TabRestoreService* service) override { | |
| 73 } | |
| 74 | |
| 75 void TabRestoreServiceDestroyed( | |
| 76 sessions::TabRestoreService* service) override { | |
| 77 // This destroys us. | |
| 78 delegate_->tab_restore_helper_.reset(); | |
| 79 } | |
| 80 | |
| 81 void TabRestoreServiceLoaded(sessions::TabRestoreService* service) override { | |
| 82 RestoreTabUsingProfile(profile_); | |
| 83 // This destroys us. | |
| 84 delegate_->tab_restore_helper_.reset(); | |
| 85 } | |
| 86 | |
| 87 private: | |
| 88 ChromeNewWindowDelegate* delegate_; | |
| 89 Profile* profile_; | |
| 90 sessions::TabRestoreService* tab_restore_service_; | |
| 91 | |
| 92 DISALLOW_COPY_AND_ASSIGN(TabRestoreHelper); | |
| 93 }; | |
| 94 | |
| 95 void ChromeNewWindowDelegate::NewTab() { | |
| 96 Browser* browser = GetBrowserForActiveWindow(); | |
| 97 if (browser && browser->is_type_tabbed()) { | |
| 98 chrome::NewTab(browser); | |
| 99 return; | |
| 100 } | |
| 101 | |
| 102 // Display a browser, setting the focus to the location bar after it is shown. | |
| 103 { | |
| 104 chrome::ScopedTabbedBrowserDisplayer displayer( | |
| 105 ProfileManager::GetActiveUserProfile()); | |
| 106 browser = displayer.browser(); | |
| 107 chrome::NewTab(browser); | |
| 108 } | |
| 109 | |
| 110 browser->SetFocusToLocationBar(false); | |
| 111 } | |
| 112 | |
| 113 void ChromeNewWindowDelegate::NewWindow(bool is_incognito) { | |
| 114 Browser* browser = GetBrowserForActiveWindow(); | |
| 115 Profile* profile = (browser && browser->profile()) | |
| 116 ? browser->profile()->GetOriginalProfile() | |
| 117 : ProfileManager::GetActiveUserProfile(); | |
| 118 chrome::NewEmptyWindow(is_incognito ? profile->GetOffTheRecordProfile() | |
| 119 : profile); | |
| 120 } | |
| 121 | |
| 122 void ChromeNewWindowDelegate::OpenFileManager() { | |
| 123 using file_manager::kFileManagerAppId; | |
| 124 Profile* const profile = ProfileManager::GetActiveUserProfile(); | |
| 125 const ExtensionService* const service = | |
| 126 extensions::ExtensionSystem::Get(profile)->extension_service(); | |
| 127 if (!service || | |
| 128 !extensions::util::IsAppLaunchableWithoutEnabling(kFileManagerAppId, | |
| 129 profile)) { | |
| 130 return; | |
| 131 } | |
| 132 | |
| 133 const extensions::Extension* const extension = | |
| 134 service->GetInstalledExtension(kFileManagerAppId); | |
| 135 OpenApplication(CreateAppLaunchParamsUserContainer( | |
| 136 profile, extension, WindowOpenDisposition::NEW_FOREGROUND_TAB, | |
| 137 extensions::SOURCE_KEYBOARD)); | |
| 138 } | |
| 139 | |
| 140 void ChromeNewWindowDelegate::OpenCrosh() { | |
| 141 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
| 142 GURL crosh_url = | |
| 143 extensions::TerminalExtensionHelper::GetCroshExtensionURL(profile); | |
| 144 if (!crosh_url.is_valid()) | |
| 145 return; | |
| 146 chrome::ScopedTabbedBrowserDisplayer displayer(profile); | |
| 147 Browser* browser = displayer.browser(); | |
| 148 content::WebContents* page = browser->OpenURL(content::OpenURLParams( | |
| 149 crosh_url, content::Referrer(), WindowOpenDisposition::NEW_FOREGROUND_TAB, | |
| 150 ui::PAGE_TRANSITION_GENERATED, false)); | |
| 151 browser->window()->Show(); | |
| 152 browser->window()->Activate(); | |
| 153 page->Focus(); | |
| 154 } | |
| 155 | |
| 156 void ChromeNewWindowDelegate::OpenGetHelp() { | |
| 157 Profile* const profile = ProfileManager::GetActiveUserProfile(); | |
| 158 chrome::ShowHelpForProfile(profile, chrome::HELP_SOURCE_KEYBOARD); | |
| 159 } | |
| 160 | |
| 161 void ChromeNewWindowDelegate::RestoreTab() { | |
| 162 if (tab_restore_helper_.get()) { | |
| 163 DCHECK(!tab_restore_helper_->tab_restore_service()->IsLoaded()); | |
| 164 return; | |
| 165 } | |
| 166 | |
| 167 Browser* browser = GetBrowserForActiveWindow(); | |
| 168 Profile* profile = browser ? browser->profile() : NULL; | |
| 169 if (!profile) | |
| 170 profile = ProfileManager::GetActiveUserProfile(); | |
| 171 if (profile->IsOffTheRecord()) | |
| 172 return; | |
| 173 sessions::TabRestoreService* service = | |
| 174 TabRestoreServiceFactory::GetForProfile(profile); | |
| 175 if (!service) | |
| 176 return; | |
| 177 | |
| 178 if (service->IsLoaded()) { | |
| 179 RestoreTabUsingProfile(profile); | |
| 180 } else { | |
| 181 tab_restore_helper_.reset(new TabRestoreHelper(this, profile, service)); | |
| 182 service->LoadTabsFromLastSession(); | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 void ChromeNewWindowDelegate::ShowKeyboardOverlay() { | |
| 187 // TODO(mazda): Move the show logic to ash (http://crbug.com/124222). | |
| 188 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
| 189 std::string url(chrome::kChromeUIKeyboardOverlayURL); | |
| 190 ash::KeyboardOverlayView::ShowDialog(profile, new ChromeWebContentsHandler, | |
| 191 GURL(url)); | |
| 192 } | |
| 193 | |
| 194 void ChromeNewWindowDelegate::ShowTaskManager() { | |
| 195 chrome::OpenTaskManager(NULL); | |
| 196 } | |
| 197 | |
| 198 void ChromeNewWindowDelegate::OpenFeedbackPage() { | |
| 199 chrome::OpenFeedbackDialog(GetBrowserForActiveWindow()); | |
| 200 } | |
| OLD | NEW |