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