-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pip2: Add NavigationThrottle for Document PiP to cancel navigations
This CL adds a navigation throttle to prevent navigations in document picture-in-picture windows and close the PiP window. (cherry picked from commit d9e92f2) Bug: 1455964 Change-Id: I75b918516d12c794be7a048a7dbc3c9d32f1a2d4 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4629929 Reviewed-by: Fr <beaufort.francois@gmail.com> Reviewed-by: Scott Violet <sky@chromium.org> Reviewed-by: Robert Kroeger <rjkroege@chromium.org> Commit-Queue: Tommy Steimel <steimel@chromium.org> Reviewed-by: Frank Liberato <liberato@chromium.org> Cr-Original-Commit-Position: refs/heads/main@{#1161837} Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4646489 Reviewed-by: Avi Drissman <avi@chromium.org> Cr-Commit-Position: refs/branch-heads/5845@{#99} Cr-Branched-From: 5a5dff6-refs/heads/main@{#1160321}
- Loading branch information
1 parent
b546cdd
commit f642bc8
Showing
9 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
content/browser/picture_in_picture/document_picture_in_picture_navigation_throttle.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "content/browser/picture_in_picture/document_picture_in_picture_navigation_throttle.h" | ||
|
||
#include "base/functional/bind.h" | ||
#include "content/public/browser/browser_thread.h" | ||
#include "content/public/browser/navigation_handle.h" | ||
#include "content/public/browser/web_contents.h" | ||
|
||
namespace content { | ||
|
||
// static | ||
std::unique_ptr<DocumentPictureInPictureNavigationThrottle> | ||
DocumentPictureInPictureNavigationThrottle::MaybeCreateThrottleFor( | ||
NavigationHandle* handle) { | ||
// We prevent the main frame of document picture-in-picture windows from doing | ||
// cross-document navigation. | ||
if (!handle->IsInMainFrame() || handle->IsSameDocument() || | ||
!handle->GetWebContents() || | ||
!handle->GetWebContents()->GetPictureInPictureOptions().has_value()) { | ||
return nullptr; | ||
} | ||
return std::make_unique<DocumentPictureInPictureNavigationThrottle>( | ||
base::PassKey<DocumentPictureInPictureNavigationThrottle>(), handle); | ||
} | ||
|
||
DocumentPictureInPictureNavigationThrottle:: | ||
DocumentPictureInPictureNavigationThrottle( | ||
base::PassKey<DocumentPictureInPictureNavigationThrottle>, | ||
NavigationHandle* handle) | ||
: NavigationThrottle(handle) {} | ||
|
||
DocumentPictureInPictureNavigationThrottle:: | ||
~DocumentPictureInPictureNavigationThrottle() = default; | ||
|
||
NavigationThrottle::ThrottleCheckResult | ||
DocumentPictureInPictureNavigationThrottle::WillStartRequest() { | ||
return ClosePiPWindowAndCancelNavigation(); | ||
} | ||
|
||
NavigationThrottle::ThrottleCheckResult | ||
DocumentPictureInPictureNavigationThrottle::WillCommitWithoutUrlLoader() { | ||
return ClosePiPWindowAndCancelNavigation(); | ||
} | ||
|
||
const char* DocumentPictureInPictureNavigationThrottle::GetNameForLogging() { | ||
return "DocumentPictureInPictureNavigationThrottle"; | ||
} | ||
|
||
NavigationThrottle::ThrottleCheckResult | ||
DocumentPictureInPictureNavigationThrottle:: | ||
ClosePiPWindowAndCancelNavigation() { | ||
// We are not allowed to synchronously close the WebContents here, so we must | ||
// do it asynchronously. | ||
content::GetUIThreadTaskRunner({})->PostTask( | ||
FROM_HERE, | ||
base::BindOnce(&WebContents::ClosePage, | ||
navigation_handle()->GetWebContents()->GetWeakPtr())); | ||
|
||
return CANCEL_AND_IGNORE; | ||
} | ||
|
||
} // namespace content |
43 changes: 43 additions & 0 deletions
43
content/browser/picture_in_picture/document_picture_in_picture_navigation_throttle.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef CONTENT_BROWSER_PICTURE_IN_PICTURE_DOCUMENT_PICTURE_IN_PICTURE_NAVIGATION_THROTTLE_H_ | ||
#define CONTENT_BROWSER_PICTURE_IN_PICTURE_DOCUMENT_PICTURE_IN_PICTURE_NAVIGATION_THROTTLE_H_ | ||
|
||
#include <memory> | ||
|
||
#include "base/types/pass_key.h" | ||
#include "content/public/browser/navigation_throttle.h" | ||
|
||
namespace content { | ||
|
||
// The DocumentPictureInPictureNavigationThrottle is used to prevent document | ||
// picture-in-picture windows from navigating and closes them when they attempt | ||
// to navigate. | ||
class DocumentPictureInPictureNavigationThrottle : public NavigationThrottle { | ||
public: | ||
static std::unique_ptr<DocumentPictureInPictureNavigationThrottle> | ||
MaybeCreateThrottleFor(NavigationHandle* handle); | ||
|
||
DocumentPictureInPictureNavigationThrottle( | ||
base::PassKey<DocumentPictureInPictureNavigationThrottle>, | ||
NavigationHandle* handle); | ||
DocumentPictureInPictureNavigationThrottle( | ||
const DocumentPictureInPictureNavigationThrottle&) = delete; | ||
DocumentPictureInPictureNavigationThrottle& operator=( | ||
const DocumentPictureInPictureNavigationThrottle&) = delete; | ||
~DocumentPictureInPictureNavigationThrottle() override; | ||
|
||
// NavigationThrottle: | ||
ThrottleCheckResult WillStartRequest() override; | ||
ThrottleCheckResult WillCommitWithoutUrlLoader() override; | ||
const char* GetNameForLogging() override; | ||
|
||
private: | ||
ThrottleCheckResult ClosePiPWindowAndCancelNavigation(); | ||
}; | ||
|
||
} // namespace content | ||
|
||
#endif // CONTENT_BROWSER_PICTURE_IN_PICTURE_DOCUMENT_PICTURE_IN_PICTURE_NAVIGATION_THROTTLE_H_ |
62 changes: 62 additions & 0 deletions
62
...nt/browser/picture_in_picture/document_picture_in_picture_navigation_throttle_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "content/browser/renderer_host/render_frame_host_impl.h" | ||
#include "content/public/test/test_renderer_host.h" | ||
#include "content/test/navigation_simulator_impl.h" | ||
#include "content/test/test_web_contents.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace content { | ||
|
||
class DocumentPictureInPictureNavigationThrottleTest | ||
: public RenderViewHostTestHarness { | ||
public: | ||
DocumentPictureInPictureNavigationThrottleTest() | ||
: RenderViewHostTestHarness( | ||
base::test::TaskEnvironment::TimeSource::MOCK_TIME) {} | ||
DocumentPictureInPictureNavigationThrottleTest( | ||
const DocumentPictureInPictureNavigationThrottleTest&) = delete; | ||
DocumentPictureInPictureNavigationThrottleTest& operator=( | ||
const DocumentPictureInPictureNavigationThrottleTest&) = delete; | ||
~DocumentPictureInPictureNavigationThrottleTest() override = default; | ||
}; | ||
|
||
TEST_F(DocumentPictureInPictureNavigationThrottleTest, | ||
ClosesPictureInPictureWindowOnNavigationStart) { | ||
// Simulate that we're inside a document picture-in-picture window. | ||
blink::mojom::PictureInPictureWindowOptions options; | ||
static_cast<TestWebContents*>(web_contents()) | ||
->SetPictureInPictureOptions(options); | ||
|
||
// Simulate a navigation, which should fail. | ||
auto nav_simulator = NavigationSimulatorImpl::CreateRendererInitiated( | ||
GURL("https://example.test/"), main_rfh()); | ||
nav_simulator->Start(); | ||
EXPECT_TRUE(nav_simulator->HasFailed()); | ||
|
||
// The RenderFrameHost should be asynchronously asked to close. First, | ||
// RunUntilIdle() to request the close, and then wait out the unload timer via | ||
// FastForwardBy(). | ||
task_environment()->RunUntilIdle(); | ||
task_environment()->FastForwardBy(base::Milliseconds(500)); | ||
EXPECT_TRUE( | ||
static_cast<RenderFrameHostImpl*>(main_rfh())->IsPageReadyToBeClosed()); | ||
} | ||
|
||
TEST_F(DocumentPictureInPictureNavigationThrottleTest, | ||
SameDocumentNavigationsOkay) { | ||
// Simulate that we're inside a document picture-in-picture window. | ||
blink::mojom::PictureInPictureWindowOptions options; | ||
static_cast<TestWebContents*>(web_contents()) | ||
->SetPictureInPictureOptions(options); | ||
|
||
// Simulate a same-document navigation, which should succeed. | ||
auto nav_simulator = | ||
NavigationSimulatorImpl::CreateRendererInitiated(GURL("#"), main_rfh()); | ||
nav_simulator->CommitSameDocument(); | ||
EXPECT_FALSE(nav_simulator->HasFailed()); | ||
} | ||
|
||
} // namespace content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters