diff options
author | scheib@chromium.org <scheib@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-07 20:41:18 +0000 |
---|---|---|
committer | scheib@chromium.org <scheib@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-07 20:41:18 +0000 |
commit | 1bf7b0d15fd000e972afb8de34f0b928bf73f167 (patch) | |
tree | e6388689c8913d400e030cb9b56782ff3e2bd94d | |
parent | 3da08d22d0e1c1482d475861252f1bc0edec5acf (diff) | |
download | chromium_src-1bf7b0d15fd000e972afb8de34f0b928bf73f167.zip chromium_src-1bf7b0d15fd000e972afb8de34f0b928bf73f167.tar.gz chromium_src-1bf7b0d15fd000e972afb8de34f0b928bf73f167.tar.bz2 |
Open links in correct profile if we are the system default browser.
When a link is clicked in a packaged app it will be launched into the
system default browser. This change adds detection logic to see if
we are the default browser, and if so, open the URL directly without
going through the system handler to launch externally.
BUG=337022
TEST=Manual testing needed for all platforms:
- Find helper app http://crbug.com/337022#c9
- Test with both Chrome as default and non default browser
1. Open multiple profile windows
2. Load and Launch app from one profile (chrome://extensions, developer mode)
3. Activate another profile's window
4. Click on link in app, verify that link is opened in correct profile
(old behavior would open link in last active browser window, wrong profile)
Review URL: https://codereview.chromium.org/140483004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@249764 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/ui/apps/chrome_shell_window_delegate.cc | 81 |
1 files changed, 68 insertions, 13 deletions
diff --git a/chrome/browser/ui/apps/chrome_shell_window_delegate.cc b/chrome/browser/ui/apps/chrome_shell_window_delegate.cc index 957be81..4d12672 100644 --- a/chrome/browser/ui/apps/chrome_shell_window_delegate.cc +++ b/chrome/browser/ui/apps/chrome_shell_window_delegate.cc @@ -9,6 +9,7 @@ #include "chrome/browser/file_select_helper.h" #include "chrome/browser/media/media_capture_devices_dispatcher.h" #include "chrome/browser/platform_util.h" +#include "chrome/browser/shell_integration.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_dialogs.h" #include "chrome/browser/ui/browser_tabstrip.h" @@ -36,6 +37,63 @@ namespace { bool disable_external_open_for_testing_ = false; +// Opens a URL with Chromium (not external browser) with the right profile. +content::WebContents* OpenURLFromTabInternal( + Profile* profile, + content::WebContents* source, + const content::OpenURLParams& params) { + // Force all links to open in a new tab, even if they were trying to open a + // window. + chrome::NavigateParams new_tab_params( + static_cast<Browser*>(NULL), params.url, params.transition); + new_tab_params.disposition = params.disposition == NEW_BACKGROUND_TAB + ? params.disposition + : NEW_FOREGROUND_TAB; + new_tab_params.initiating_profile = profile; + chrome::Navigate(&new_tab_params); + + return new_tab_params.target_contents; +} + +// Helper class that opens a URL based on if this browser instance is the +// default system browser. If it is the default, open the URL directly instead +// of asking the system to open it. +class OpenURLFromTabBasedOnBrowserDefault + : public ShellIntegration::DefaultWebClientObserver { + public: + OpenURLFromTabBasedOnBrowserDefault(content::WebContents* source, + const content::OpenURLParams& params) + : source_(source), params_(params) {} + + // Opens a URL when called with the result of if this is the default system + // browser or not. + virtual void SetDefaultWebClientUIState( + ShellIntegration::DefaultWebClientUIState state) OVERRIDE { + Profile* profile = + Profile::FromBrowserContext(source_->GetBrowserContext()); + DCHECK(profile); + if (!profile) + return; + switch (state) { + case ShellIntegration::STATE_PROCESSING: + break; + case ShellIntegration::STATE_IS_DEFAULT: + OpenURLFromTabInternal(profile, source_, params_); + break; + case ShellIntegration::STATE_NOT_DEFAULT: + case ShellIntegration::STATE_UNKNOWN: + platform_util::OpenExternal(profile, params_.url); + break; + } + } + + virtual bool IsOwnedByWorker() OVERRIDE { return true; } + + private: + content::WebContents* source_; + const content::OpenURLParams params_; +}; + } // namespace ShellWindowLinkDelegate::ShellWindowLinkDelegate() {} @@ -48,10 +106,16 @@ content::WebContents* ShellWindowLinkDelegate::OpenURLFromTab( content::WebContents* source, const content::OpenURLParams& params) { if (source) { - platform_util::OpenExternal( - Profile::FromBrowserContext(source->GetBrowserContext()), params.url); + scoped_refptr<ShellIntegration::DefaultWebClientWorker> + check_if_default_browser_worker = + new ShellIntegration::DefaultBrowserWorker( + new OpenURLFromTabBasedOnBrowserDefault(source, params)); + // Object lifetime notes: The OpenURLFromTabBasedOnBrowserDefault is owned + // by check_if_default_browser_worker. StartCheckIsDefault() takes lifetime + // ownership of check_if_default_browser_worker and will clean up after + // the asynchronous tasks. + check_if_default_browser_worker->StartCheckIsDefault(); } - delete source; return NULL; } @@ -87,16 +151,7 @@ content::WebContents* ChromeShellWindowDelegate::OpenURLFromTab( Profile* profile, content::WebContents* source, const content::OpenURLParams& params) { - // Force all links to open in a new tab, even if they were trying to open a - // window. - chrome::NavigateParams new_tab_params( - static_cast<Browser*>(NULL), params.url, params.transition); - new_tab_params.disposition = params.disposition == NEW_BACKGROUND_TAB ? - params.disposition : NEW_FOREGROUND_TAB; - new_tab_params.initiating_profile = profile; - chrome::Navigate(&new_tab_params); - - return new_tab_params.target_contents; + return OpenURLFromTabInternal(profile, source, params); } void ChromeShellWindowDelegate::AddNewContents( |