Index: chrome/browser/ui/extensions/shell_window.cc |
diff --git a/chrome/browser/ui/extensions/shell_window.cc b/chrome/browser/ui/extensions/shell_window.cc |
index 5ba6c0a9b32a78d50caf95340f1e68b603ffe0b0..8b0ef9899d245176a515e642e5e65181eae8529c 100644 |
--- a/chrome/browser/ui/extensions/shell_window.cc |
+++ b/chrome/browser/ui/extensions/shell_window.cc |
@@ -4,6 +4,8 @@ |
#include "chrome/browser/ui/extensions/shell_window.h" |
+#include "base/command_line.h" |
+#include "base/memory/singleton.h" |
#include "base/utf_string_conversions.h" |
#include "chrome/browser/extensions/extension_process_manager.h" |
#include "chrome/browser/extensions/extension_system.h" |
@@ -13,10 +15,12 @@ |
#include "chrome/browser/infobars/infobar_tab_helper.h" |
#include "chrome/browser/intents/web_intents_util.h" |
#include "chrome/browser/lifetime/application_lifetime.h" |
+#include "chrome/browser/platform_util.h" |
#include "chrome/browser/profiles/profile.h" |
#include "chrome/browser/sessions/session_id.h" |
#include "chrome/browser/ui/browser.h" |
#include "chrome/browser/ui/browser_finder.h" |
+#include "chrome/browser/ui/browser_list.h" |
#include "chrome/browser/ui/browser_tabstrip.h" |
#include "chrome/browser/ui/browser_window.h" |
#include "chrome/browser/ui/extensions/native_shell_window.h" |
@@ -64,6 +68,79 @@ void SuspendRenderViewHost(RenderViewHost* rvh) { |
} // namespace |
+// ExternalUrlController is a singleton class for link navigation. |
+// It helps to open URL in system default browser. |
+class ExternalUrlController : public content::WebContentsDelegate { |
+ public: |
+ static ExternalUrlController* GetInstance(); |
+ |
+ private: |
+ ExternalUrlController() {} |
+ friend struct DefaultSingletonTraits<ExternalUrlController>; |
+ |
+ // content::WebContentsDelegate implementation. |
+ virtual content::WebContents* OpenURLFromTab( |
+ content::WebContents* source, |
+ const content::OpenURLParams& params) OVERRIDE; |
+ |
+ // For test usage. |
+ content::WebContents* OpenInNewTab( |
+ const content::OpenURLParams& params); |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ExternalUrlController); |
+}; |
+ |
+// static |
+ExternalUrlController* ExternalUrlController::GetInstance() { |
+ return Singleton<ExternalUrlController>::get(); |
+} |
+ |
+content::WebContents* ExternalUrlController::OpenURLFromTab( |
+ content::WebContents* source, |
+ const content::OpenURLParams& params) { |
+ // Delete useless web content first to |
+ // avoid a potential leak in a render process host. |
+ delete source; |
+ |
+ // This should only happen in PlatformAppBrowserTest. |
+ CommandLine* command_line = CommandLine::ForCurrentProcess(); |
+ if (command_line->HasSwitch("OpenLinkTest")) { |
Mihai Parparita -not on Chrome
2012/09/29 00:28:44
Test behavior is normally not overridden via comma
|
+ return OpenInNewTab(params); |
+ } |
+ |
+#if defined(OS_MACOSX) |
+ // This must run on the UI thread on OS X. |
+ platform_util::OpenExternal(params.url); |
+#else |
+ // Otherwise put this work on the file thread. On Windows ShellExecute may |
+ // block for a significant amount of time, and it shouldn't hurt on Linux. |
+ BrowserThread::PostTask( |
+ BrowserThread::FILE, |
+ FROM_HERE, |
+ base::Bind(&platform_util::OpenExternal, params.url)); |
+#endif |
+ |
+ return NULL; |
+} |
+ |
+content::WebContents* ExternalUrlController::OpenInNewTab( |
+ const content::OpenURLParams& params) { |
+ WindowOpenDisposition disposition = params.disposition; |
+ content::OpenURLParams new_tab_params = params; |
+ new_tab_params.disposition = |
+ disposition == NEW_BACKGROUND_TAB ? disposition : NEW_FOREGROUND_TAB; |
+ for (BrowserList::const_iterator i = BrowserList::begin(); |
+ i != BrowserList::end(); ++i) { |
+ if ((*i)->is_type_tabbed()) { |
+ Browser* browser = *i; |
+ WebContents* new_tab = browser->OpenURL(new_tab_params); |
+ browser->window()->Show(); |
+ return new_tab; |
+ } |
+ } |
+ return NULL; |
+} |
+ |
ShellWindow::CreateParams::CreateParams() |
: frame(ShellWindow::CreateParams::FRAME_CHROME), |
bounds(-1, -1, kDefaultWidth, kDefaultHeight), |
@@ -263,6 +340,10 @@ void ShellWindow::AddNewContents(WebContents* source, |
DCHECK(source == web_contents_); |
DCHECK(Profile::FromBrowserContext(new_contents->GetBrowserContext()) == |
profile_); |
+ |
+#if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) |
+ new_contents->SetDelegate(ExternalUrlController::GetInstance()); |
+#else |
Browser* browser = browser::FindOrCreateTabbedBrowser(profile_); |
// Force all links to open in a new tab, even if they were trying to open a |
// new window. |
@@ -270,6 +351,7 @@ void ShellWindow::AddNewContents(WebContents* source, |
disposition == NEW_BACKGROUND_TAB ? disposition : NEW_FOREGROUND_TAB; |
chrome::AddWebContents(browser, NULL, new_contents, disposition, initial_pos, |
user_gesture, was_blocked); |
+#endif |
} |
void ShellWindow::HandleKeyboardEvent( |