Opening links clicked from the answer card in a new tab.

Bug=712331

Review-Url: https://codereview.chromium.org/2840673002
Cr-Commit-Position: refs/heads/master@{#466755}
diff --git a/chrome/browser/ui/app_list/search_answer_web_contents_delegate.cc b/chrome/browser/ui/app_list/search_answer_web_contents_delegate.cc
index d90fe66..c417022e 100644
--- a/chrome/browser/ui/app_list/search_answer_web_contents_delegate.cc
+++ b/chrome/browser/ui/app_list/search_answer_web_contents_delegate.cc
@@ -6,10 +6,14 @@
 
 #include "base/command_line.h"
 #include "base/strings/utf_string_conversions.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/ui/browser_navigator.h"
+#include "chrome/browser/ui/browser_navigator_params.h"
 #include "content/public/browser/navigation_handle.h"
 #include "content/public/browser/render_view_host.h"
 #include "content/public/browser/web_contents.h"
 #include "content/public/browser/web_contents_delegate.h"
+#include "content/public/common/renderer_preferences.h"
 #include "net/http/http_response_headers.h"
 #include "net/http/http_status_code.h"
 #include "ui/app_list/app_list_model.h"
@@ -20,15 +24,21 @@
 namespace app_list {
 
 SearchAnswerWebContentsDelegate::SearchAnswerWebContentsDelegate(
-    content::BrowserContext* browser_context,
+    Profile* profile,
     app_list::AppListModel* model)
-    : model_(model),
-      web_view_(base::MakeUnique<views::WebView>(browser_context)),
+    : profile_(profile),
+      model_(model),
+      web_view_(base::MakeUnique<views::WebView>(profile)),
       web_contents_(
           content::WebContents::Create(content::WebContents::CreateParams(
-              browser_context,
-              content::SiteInstance::Create(browser_context)))),
+              profile,
+              content::SiteInstance::Create(profile)))),
       answer_server_url_(switches::AnswerServerUrl()) {
+  // We need the OpenURLFromTab() to get called.
+  web_contents_->GetMutableRendererPrefs()
+      ->browser_handles_all_top_level_requests = true;
+  web_contents_->GetRenderViewHost()->SyncRendererPrefs();
+
   Observe(web_contents_.get());
   web_contents_->SetDelegate(this);
   web_view_->set_owned_by_client();
@@ -79,6 +89,24 @@
   web_view_->SetPreferredSize(pref_size);
 }
 
+content::WebContents* SearchAnswerWebContentsDelegate::OpenURLFromTab(
+    content::WebContents* source,
+    const content::OpenURLParams& params) {
+  if (!params.user_gesture)
+    return WebContentsDelegate::OpenURLFromTab(source, params);
+
+  // Open the user-clicked link in a new browser tab. This will automatically
+  // close the app list.
+  chrome::NavigateParams new_tab_params(profile_, params.url,
+                                        params.transition);
+  new_tab_params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
+  new_tab_params.window_action = chrome::NavigateParams::SHOW_WINDOW;
+
+  chrome::Navigate(&new_tab_params);
+
+  return new_tab_params.target_contents;
+}
+
 void SearchAnswerWebContentsDelegate::DidFinishNavigation(
     content::NavigationHandle* navigation_handle) {
   if (navigation_handle->GetURL() != current_request_url_)
diff --git a/chrome/browser/ui/app_list/search_answer_web_contents_delegate.h b/chrome/browser/ui/app_list/search_answer_web_contents_delegate.h
index aa295c1..9959e108 100644
--- a/chrome/browser/ui/app_list/search_answer_web_contents_delegate.h
+++ b/chrome/browser/ui/app_list/search_answer_web_contents_delegate.h
@@ -12,14 +12,12 @@
 #include "content/public/browser/web_contents_observer.h"
 #include "url/gurl.h"
 
+class Profile;
+
 namespace app_list {
 class AppListModel;
 }
 
-namespace content {
-class BrowserContext;
-}
-
 namespace views {
 class View;
 class WebView;
@@ -31,7 +29,7 @@
 class SearchAnswerWebContentsDelegate : public content::WebContentsDelegate,
                                         public content::WebContentsObserver {
  public:
-  SearchAnswerWebContentsDelegate(content::BrowserContext* browser_context,
+  SearchAnswerWebContentsDelegate(Profile* profile,
                                   app_list::AppListModel* model);
 
   ~SearchAnswerWebContentsDelegate() override;
@@ -48,6 +46,9 @@
   // content::WebContentsDelegate overrides:
   void UpdatePreferredSize(content::WebContents* web_contents,
                            const gfx::Size& pref_size) override;
+  content::WebContents* OpenURLFromTab(
+      content::WebContents* source,
+      const content::OpenURLParams& params) override;
 
   // content::WebContentsObserver overrides:
   void DidFinishNavigation(
@@ -55,6 +56,9 @@
   void DidStopLoading() override;
 
  private:
+  // Unowned pointer to the associated profile.
+  Profile* const profile_;
+
   // Unowned pointer to app list model.
   app_list::AppListModel* const model_;