Commit f360d0bc authored by David Black's avatar David Black Committed by Commit Bot

Add support for target="_blank".

There are instances in which we want links to be able to open new
tabs in the browser. With this change, they will be able to do so by
specifying target="_blank".

Bug: b:115782814
Change-Id: Id4a08c5fbde342c723f47e363c9cf97e76f1578e
Reviewed-on: https://chromium-review.googlesource.com/1227502Reviewed-by: default avatarXiaohui Chen <xiaohuic@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: David Black <dmblack@google.com>
Cr-Commit-Position: refs/heads/master@{#592142}
parent acf978fd
...@@ -214,6 +214,7 @@ void AssistantController::OnDeepLinkReceived( ...@@ -214,6 +214,7 @@ void AssistantController::OnDeepLinkReceived(
void AssistantController::ShouldOpenUrlFromTab( void AssistantController::ShouldOpenUrlFromTab(
const GURL& url, const GURL& url,
WindowOpenDisposition disposition,
ash::mojom::ManagedWebContentsOpenUrlDelegate::ShouldOpenUrlFromTabCallback ash::mojom::ManagedWebContentsOpenUrlDelegate::ShouldOpenUrlFromTabCallback
callback) { callback) {
// We always handle deep links ourselves. // We always handle deep links ourselves.
...@@ -223,10 +224,13 @@ void AssistantController::ShouldOpenUrlFromTab( ...@@ -223,10 +224,13 @@ void AssistantController::ShouldOpenUrlFromTab(
return; return;
} }
AssistantUiMode ui_mode = assistant_ui_controller_->model()->ui_mode();
// When in main UI mode, WebContents should not navigate as they are hosting // When in main UI mode, WebContents should not navigate as they are hosting
// Assistant cards. Instead, we route navigation attempts to the browser. // Assistant cards. Instead, we route navigation attempts to the browser. We
if (assistant_ui_controller_->model()->ui_mode() == // also respect open |disposition| to launch in the browser if appropriate.
AssistantUiMode::kMainUi) { if (ui_mode == AssistantUiMode::kMainUi ||
disposition == WindowOpenDisposition::NEW_FOREGROUND_TAB) {
std::move(callback).Run(/*should_open=*/false); std::move(callback).Run(/*should_open=*/false);
OpenUrl(url); OpenUrl(url);
return; return;
......
...@@ -112,6 +112,7 @@ class ASH_EXPORT AssistantController ...@@ -112,6 +112,7 @@ class ASH_EXPORT AssistantController
// mojom::ManagedWebContentsOpenUrlDelegate: // mojom::ManagedWebContentsOpenUrlDelegate:
void ShouldOpenUrlFromTab( void ShouldOpenUrlFromTab(
const GURL& url, const GURL& url,
WindowOpenDisposition disposition,
mojom::ManagedWebContentsOpenUrlDelegate::ShouldOpenUrlFromTabCallback mojom::ManagedWebContentsOpenUrlDelegate::ShouldOpenUrlFromTabCallback
callback) override; callback) override;
......
...@@ -77,6 +77,7 @@ mojom("interfaces_internal") { ...@@ -77,6 +77,7 @@ mojom("interfaces_internal") {
"//skia/public/interfaces", "//skia/public/interfaces",
"//ui/accessibility:ax_enums_mojo", "//ui/accessibility:ax_enums_mojo",
"//ui/base/ime/chromeos/public/interfaces", "//ui/base/ime/chromeos/public/interfaces",
"//ui/base/mojo",
"//ui/display/mojo:interfaces", "//ui/display/mojo:interfaces",
"//ui/events/mojo:interfaces", "//ui/events/mojo:interfaces",
"//ui/gfx/geometry/mojo", "//ui/gfx/geometry/mojo",
......
...@@ -6,6 +6,7 @@ module ash.mojom; ...@@ -6,6 +6,7 @@ module ash.mojom;
import "components/account_id/interfaces/account_id.mojom"; import "components/account_id/interfaces/account_id.mojom";
import "mojo/public/mojom/base/unguessable_token.mojom"; import "mojo/public/mojom/base/unguessable_token.mojom";
import "ui/base/mojo/window_open_disposition.mojom";
import "ui/gfx/geometry/mojo/geometry.mojom"; import "ui/gfx/geometry/mojo/geometry.mojom";
import "url/mojom/url.mojom"; import "url/mojom/url.mojom";
...@@ -58,7 +59,9 @@ struct ManagedWebContentsParams { ...@@ -58,7 +59,9 @@ struct ManagedWebContentsParams {
// Interface for a delegate to handle top level browser requests for a // Interface for a delegate to handle top level browser requests for a
// managed WebContents. // managed WebContents.
interface ManagedWebContentsOpenUrlDelegate { interface ManagedWebContentsOpenUrlDelegate {
// Invoked on a top level browser request to navigate to |url|. Return false // Invoked on a top level browser request to navigate to |url| with the
// to consume the navigation attempt or true to allow it to continue. // specified window open |disposition|. Return false to consume the
ShouldOpenUrlFromTab(url.mojom.Url url) => (bool should_open); // navigation attempt or true to allow it to continue.
ShouldOpenUrlFromTab(url.mojom.Url url,
ui.mojom.WindowOpenDisposition disposition) => (bool should_open);
}; };
...@@ -26,6 +26,11 @@ ...@@ -26,6 +26,11 @@
// ManagedWebContents ---------------------------------------------------------- // ManagedWebContents ----------------------------------------------------------
// This class wraps a WebContents and associated view. ManagedWebContents are
// owned/managed by WebContentsManager on behalf of embedders in other code
// areas/processes that cannot depend directly on chrome/browser. Instances are
// created/destroyed via calls to WebContentsManager::ManageWebContents and
// WebContentsManager::ReleaseWebContents respectively.
class ManagedWebContents : public content::WebContentsDelegate, class ManagedWebContents : public content::WebContentsDelegate,
public content::WebContentsObserver { public content::WebContentsObserver {
public: public:
...@@ -66,22 +71,30 @@ class ManagedWebContents : public content::WebContentsDelegate, ...@@ -66,22 +71,30 @@ class ManagedWebContents : public content::WebContentsDelegate,
web_view_->SetPreferredSize(new_size); web_view_->SetPreferredSize(new_size);
} }
bool ShouldCreateWebContents(
content::WebContents* web_contents,
content::RenderFrameHost* opener,
content::SiteInstance* source_site_instance,
int32_t route_id,
int32_t main_frame_route_id,
int32_t main_frame_widget_route_id,
content::mojom::WindowContainerType window_container_type,
const GURL& opener_url,
const std::string& frame_name,
const GURL& target_url,
const std::string& partition_id,
content::SessionStorageNamespace* session_storage_namespace) override {
// This is fired when trying to open links in a new tab, e.g.:
// <a href="https://www.google.com/" target="_blank">Link</a>
HandleNavigationAttempt(target_url,
WindowOpenDisposition::NEW_FOREGROUND_TAB);
return false;
}
content::WebContents* OpenURLFromTab( content::WebContents* OpenURLFromTab(
content::WebContents* source, content::WebContents* source,
const content::OpenURLParams& params) override { const content::OpenURLParams& params) override {
if (!open_url_delegate_) HandleNavigationAttempt(params.url, params.disposition);
return content::WebContentsDelegate::OpenURLFromTab(source, params);
open_url_delegate_->ShouldOpenUrlFromTab(
params.url,
base::BindOnce(
[](base::WeakPtr<ManagedWebContents> managed_web_contents,
const GURL& url, bool should_open) {
if (should_open && managed_web_contents)
managed_web_contents->NavigateToUrl(url);
},
weak_factory_.GetWeakPtr(), params.url));
return nullptr; return nullptr;
} }
...@@ -174,6 +187,22 @@ class ManagedWebContents : public content::WebContentsDelegate, ...@@ -174,6 +187,22 @@ class ManagedWebContents : public content::WebContentsDelegate,
} }
} }
void HandleNavigationAttempt(const GURL& url,
WindowOpenDisposition disposition) {
if (!open_url_delegate_)
return;
open_url_delegate_->ShouldOpenUrlFromTab(
url, disposition,
base::BindOnce(
[](const base::WeakPtr<ManagedWebContents>& managed_web_contents,
const GURL& url, bool should_open) {
if (should_open && managed_web_contents)
managed_web_contents->NavigateToUrl(url);
},
weak_factory_.GetWeakPtr(), url));
}
ash::mojom::WebContentsManager::ManageWebContentsCallback callback_; ash::mojom::WebContentsManager::ManageWebContentsCallback callback_;
std::unique_ptr<content::WebContents> web_contents_; std::unique_ptr<content::WebContents> web_contents_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment