Commit 578297c2 authored by Eric Willigers's avatar Eric Willigers Committed by Commit Bot

desktop-pwas: Implement WebAppBrowserController::IsUrlInAppScope

WebAppBrowserController::IsUrlInAppScope is now implemented.

All of the web app badging tests are now run with and without BMO
enabled.

Bug: 966290
Change-Id: I28c2c0dd7bc50a96939e9008693964f0aa8c85e0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1728170Reviewed-by: default avatarJay Harris <harrisjay@chromium.org>
Commit-Queue: Eric Willigers <ericwilligers@chromium.org>
Cr-Commit-Position: refs/heads/master@{#682612}
parent b2b335b7
...@@ -24,9 +24,6 @@ ...@@ -24,9 +24,6 @@
#include "base/values.h" #include "base/values.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h" #include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/badging/badge_manager.h"
#include "chrome/browser/badging/badge_manager_delegate.h"
#include "chrome/browser/badging/badge_manager_factory.h"
#include "chrome/browser/banners/test_app_banner_manager_desktop.h" #include "chrome/browser/banners/test_app_banner_manager_desktop.h"
#include "chrome/browser/extensions/extension_browsertest.h" #include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/extension_service.h"
...@@ -868,169 +865,6 @@ IN_PROC_BROWSER_TEST_P(HostedAppTest, InScopeHttpUrlsDisplayAppTitle) { ...@@ -868,169 +865,6 @@ IN_PROC_BROWSER_TEST_P(HostedAppTest, InScopeHttpUrlsDisplayAppTitle) {
app_browser->GetWindowTitleForCurrentTab(false)); app_browser->GetWindowTitleForCurrentTab(false));
} }
#if !defined(OS_ANDROID)
class HostedAppBadgingTest : public HostedAppTest {
public:
// Listens to BadgeManager events and forwards them to the test class.
class TestBadgeManagerDelegate : public badging::BadgeManagerDelegate {
public:
using SetBadgeCallback =
base::RepeatingCallback<void(const std::string&,
base::Optional<uint64_t>)>;
using ClearBadgeCallback =
base::RepeatingCallback<void(const std::string&)>;
using ChangeFailedCallback = base::RepeatingCallback<void()>;
TestBadgeManagerDelegate(Profile* profile,
SetBadgeCallback on_set_badge,
ClearBadgeCallback on_clear_badge,
ChangeFailedCallback on_change_failed)
: badging::BadgeManagerDelegate(profile),
on_set_badge_(on_set_badge),
on_clear_badge_(on_clear_badge),
on_change_failed_(on_change_failed) {}
void OnBadgeSet(const std::string& app_id,
base::Optional<uint64_t> contents) override {
on_set_badge_.Run(app_id, contents);
}
void OnBadgeCleared(const std::string& app_id) override {
on_clear_badge_.Run(app_id);
}
void OnBadgeChangeIgnoredForTesting() override { on_change_failed_.Run(); }
private:
SetBadgeCallback on_set_badge_;
ClearBadgeCallback on_clear_badge_;
ChangeFailedCallback on_change_failed_;
};
void SetUpCommandLine(base::CommandLine* command_line) override {
HostedAppTest::SetUpCommandLine(command_line);
command_line->AppendSwitchASCII("enable-blink-features", "Badging");
}
void SetUpOnMainThread() override {
HostedAppTest::SetUpOnMainThread();
ASSERT_TRUE(https_server()->Start());
ASSERT_TRUE(embedded_test_server()->Start());
InstallPWA(https_server()->GetURL(
"/ssl/page_with_in_scope_and_cross_site_frame.html"));
content::WebContents* web_contents =
app_browser_->tab_strip_model()->GetActiveWebContents();
// There should be exactly 3 frames:
// 1) The main frame.
// 2) A cross site frame, on https://example.com/
// 3) A sub frame in the app's scope.
auto frames = web_contents->GetAllFrames();
ASSERT_EQ(3u, frames.size());
main_frame_ = web_contents->GetMainFrame();
for (auto* frame : frames) {
if (frame->GetLastCommittedURL() == "https://example.com/")
cross_site_frame_ = frame;
else if (frame != main_frame_)
in_scope_frame_ = frame;
}
ASSERT_TRUE(main_frame_);
ASSERT_TRUE(in_scope_frame_);
ASSERT_TRUE(cross_site_frame_);
awaiter_ = std::make_unique<base::RunLoop>();
Profile* profile = app_browser_->profile();
std::unique_ptr<badging::BadgeManagerDelegate> delegate =
std::make_unique<TestBadgeManagerDelegate>(
profile,
base::BindRepeating(&HostedAppBadgingTest::OnBadgeSet,
base::Unretained(this)),
base::BindRepeating(&HostedAppBadgingTest::OnBadgeCleared,
base::Unretained(this)),
base::BindRepeating(&HostedAppBadgingTest::OnBadgeChangeFailed,
base::Unretained(this)));
badging::BadgeManagerFactory::GetInstance()
->GetForProfile(profile)
->SetDelegate(std::move(delegate));
}
void OnBadgeSet(const std::string& app_id,
base::Optional<uint64_t> badge_content) {
if (badge_content.has_value())
last_badge_content_ = badge_content;
else
was_flagged_ = true;
awaiter_->Quit();
}
void OnBadgeCleared(const std::string& app_id) {
was_cleared_ = true;
awaiter_->Quit();
}
void OnBadgeChangeFailed() {
change_failed_ = true;
awaiter_->Quit();
}
protected:
void ExecuteScriptAndWaitForBadgeChange(std::string script,
RenderFrameHost* on) {
was_cleared_ = false;
was_flagged_ = false;
change_failed_ = false;
last_badge_content_ = base::nullopt;
awaiter_.reset(new base::RunLoop());
ASSERT_TRUE(content::ExecuteScript(on, script));
if (was_cleared_ || was_flagged_ || change_failed_ ||
last_badge_content_ != base::nullopt)
return;
awaiter_->Run();
}
RenderFrameHost* main_frame_;
RenderFrameHost* in_scope_frame_;
RenderFrameHost* cross_site_frame_;
bool was_cleared_ = false;
bool was_flagged_ = false;
bool change_failed_ = false;
base::Optional<uint64_t> last_badge_content_ = base::nullopt;
private:
std::unique_ptr<base::RunLoop> awaiter_;
};
// Tests that the badge cannot be set and cleared from a cross site frame.
// TODO(https://crbug.com/966290): Move to web_app_badging_browsertest.cc
// when WebAppBrowserController::IsUrlInAppScope is implemented.
IN_PROC_BROWSER_TEST_P(HostedAppBadgingTest,
BadgeCannotBeChangedFromCrossSiteFrame) {
// Clearing from cross site frame should be a no-op.
ExecuteScriptAndWaitForBadgeChange("ExperimentalBadge.clear()",
cross_site_frame_);
ASSERT_FALSE(was_cleared_);
ASSERT_FALSE(was_flagged_);
ASSERT_TRUE(change_failed_);
// Setting from cross site frame should be a no-op.
ExecuteScriptAndWaitForBadgeChange("ExperimentalBadge.set(77)",
cross_site_frame_);
ASSERT_FALSE(was_cleared_);
ASSERT_FALSE(was_flagged_);
ASSERT_TRUE(change_failed_);
}
#endif // !defined(OS_CHROMEOS)
using HostedAppPWAOnlyTest = HostedAppTest; using HostedAppPWAOnlyTest = HostedAppTest;
using HostedAppPWAOnlyTestWithAutoupgradesDisabled = using HostedAppPWAOnlyTestWithAutoupgradesDisabled =
HostedAppTestWithAutoupgradesDisabled; HostedAppTestWithAutoupgradesDisabled;
...@@ -2863,10 +2697,3 @@ INSTANTIATE_TEST_SUITE_P( ...@@ -2863,10 +2697,3 @@ INSTANTIATE_TEST_SUITE_P(
/* no prefix */, /* no prefix */,
HostedAppSitePerProcessTest, HostedAppSitePerProcessTest,
::testing::Values(AppType::HOSTED_APP)); ::testing::Values(AppType::HOSTED_APP));
#if !defined(OS_CHROMEOS)
INSTANTIATE_TEST_SUITE_P(
/* no prefix */,
HostedAppBadgingTest,
::testing::Values(AppType::BOOKMARK_APP));
#endif // !defined(OS_CHROMEOS)
...@@ -213,8 +213,23 @@ IN_PROC_BROWSER_TEST_P(WebAppBadgingBrowserTest, ...@@ -213,8 +213,23 @@ IN_PROC_BROWSER_TEST_P(WebAppBadgingBrowserTest,
ASSERT_EQ(base::nullopt, last_badge_content_); ASSERT_EQ(base::nullopt, last_badge_content_);
} }
// TODO(https://crbug.com/966290): Add BadgeCannotBeChangedFromCrossSiteFrame // Tests that the badge cannot be set and cleared from a cross site frame.
// test from hosted_app_browsertest.cc IN_PROC_BROWSER_TEST_P(WebAppBadgingBrowserTest,
BadgeCannotBeChangedFromCrossSiteFrame) {
// Clearing from cross site frame should be a no-op.
ExecuteScriptAndWaitForBadgeChange("ExperimentalBadge.clear()",
cross_site_frame_);
ASSERT_FALSE(was_cleared_);
ASSERT_FALSE(was_flagged_);
ASSERT_TRUE(change_failed_);
// Setting from cross site frame should be a no-op.
ExecuteScriptAndWaitForBadgeChange("ExperimentalBadge.set(77)",
cross_site_frame_);
ASSERT_FALSE(was_cleared_);
ASSERT_FALSE(was_flagged_);
ASSERT_TRUE(change_failed_);
}
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(
/* no prefix */, /* no prefix */,
......
...@@ -64,8 +64,20 @@ GURL WebAppBrowserController::GetAppLaunchURL() const { ...@@ -64,8 +64,20 @@ GURL WebAppBrowserController::GetAppLaunchURL() const {
} }
bool WebAppBrowserController::IsUrlInAppScope(const GURL& url) const { bool WebAppBrowserController::IsUrlInAppScope(const GURL& url) const {
// TODO(https://crbug.com/966290): Complete implementation. base::Optional<GURL> app_scope = registrar_.GetAppScope(app_id_);
return true; if (!app_scope)
return false;
// https://w3c.github.io/manifest/#navigation-scope
// If url is same origin as scope and url path starts with scope path, return
// true. Otherwise, return false.
if (app_scope->GetOrigin() != url.GetOrigin())
return false;
std::string scope_path = app_scope->path();
std::string url_path = url.path();
return scope_path.size() <= url_path.size() &&
scope_path == url_path.substr(0, scope_path.size());
} }
std::string WebAppBrowserController::GetAppShortName() const { std::string WebAppBrowserController::GetAppShortName() const {
......
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