Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AI Chat]: Make it possible to Open AI Chat on Android when history is disabled #27003

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions browser/ai_chat/ai_chat_throttle_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,50 +79,47 @@ TEST_P(AiChatThrottleUnitTest, CancelNavigationFromTab) {

test_handle.set_url(GURL(kAIChatUIURL));

#if BUILDFLAG(IS_ANDROID)
ui::PageTransition transition = ui::PageTransitionFromInt(
ui::PageTransition::PAGE_TRANSITION_FROM_ADDRESS_BAR);
#else
ui::PageTransition transition = ui::PageTransitionFromInt(
ui::PageTransition::PAGE_TRANSITION_FROM_ADDRESS_BAR |
ui::PageTransition::PAGE_TRANSITION_TYPED);
#endif

test_handle.set_page_transition(transition);

std::unique_ptr<AiChatThrottle> throttle =
AiChatThrottle::MaybeCreateThrottleFor(&test_handle);

#if !BUILDFLAG(IS_ANDROID)
if (IsAIChatHistoryEnabled()) {
EXPECT_EQ(throttle.get(), nullptr);
} else {
EXPECT_NE(throttle.get(), nullptr);
EXPECT_EQ(content::NavigationThrottle::CANCEL_AND_IGNORE,
throttle->WillStartRequest().action());
}
#else
EXPECT_EQ(throttle.get(), nullptr);
#endif
}

TEST_P(AiChatThrottleUnitTest, CancelNavigationToFrame) {
content::MockNavigationHandle test_handle(web_contents());

test_handle.set_url(GURL(kAIChatUntrustedConversationUIURL));

#if BUILDFLAG(IS_ANDROID)
ui::PageTransition transition = ui::PageTransitionFromInt(
ui::PageTransition::PAGE_TRANSITION_FROM_ADDRESS_BAR);
#else
ui::PageTransition transition = ui::PageTransitionFromInt(
ui::PageTransition::PAGE_TRANSITION_FROM_ADDRESS_BAR |
ui::PageTransition::PAGE_TRANSITION_TYPED);
#endif

test_handle.set_page_transition(transition);

std::unique_ptr<AiChatThrottle> throttle =
AiChatThrottle::MaybeCreateThrottleFor(&test_handle);

#if !BUILDFLAG(IS_ANDROID)
EXPECT_EQ(content::NavigationThrottle::CANCEL_AND_IGNORE,
throttle->WillStartRequest().action());
#else
EXPECT_EQ(throttle.get(), nullptr);
#endif
}

TEST_P(AiChatThrottleUnitTest, AllowNavigationFromPanel) {
Expand Down
29 changes: 5 additions & 24 deletions components/ai_chat/content/browser/ai_chat_throttle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,38 +54,19 @@ std::unique_ptr<AiChatThrottle> AiChatThrottle::MaybeCreateThrottleFor(
return nullptr;
}

// Purpose of this throttle is to forbid loading of chrome://leo-ai and
// related urls in tab.
// Parameters check is made different for Android and Desktop because
// there are different flags:
// --------+---------------------------------+------------------------------
// | Tab | Panel
// --------+---------------------------------+------------------------------
// Android |PAGE_TRANSITION_FROM_ADDRESS_BAR | PAGE_TRANSITION_FROM_API
// --------+---------------------------------+------------------------------
// Desktop |PAGE_TRANSITION_TYPED| | PAGE_TRANSITION_AUTO_TOPLEVEL
// |PAGE_TRANSITION_FROM_ADDRESS_BAR |
// -------------------------------------------------------------------------
//
// So for Android the only allowed transition is PAGE_TRANSITION_FROM_API
// because it is pretty unique and means the page is loaded in a custom tab
// view.
// And for the desktop just disallow PAGE_TRANSITION_FROM_ADDRESS_BAR
ui::PageTransition transition = navigation_handle->GetPageTransition();
// On Android, we only have full page chat, so we always allow loading. On
// desktop we just disallow PAGE_TRANSITION_FROM_ADDRESS_BAR.
#if BUILDFLAG(IS_ANDROID)
if (ui::PageTransitionTypeIncludingQualifiersIs(
transition, ui::PageTransition::PAGE_TRANSITION_FROM_API)) {
return nullptr;
}
return nullptr;
#else
ui::PageTransition transition = navigation_handle->GetPageTransition();
if (!ui::PageTransitionTypeIncludingQualifiersIs(
ui::PageTransitionGetQualifier(transition),
ui::PageTransition::PAGE_TRANSITION_FROM_ADDRESS_BAR)) {
return nullptr;
}
#endif // BUILDFLAG(IS_ANDROID)

return std::make_unique<AiChatThrottle>(navigation_handle);
#endif // BUILDFLAG(IS_ANDROID)
}

AiChatThrottle::AiChatThrottle(content::NavigationHandle* handle)
Expand Down
Loading