Skip to content

Commit

Permalink
[AI Chat]: Make it possible to Open AI Chat when history is disabled (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fallaciousreasoning authored Dec 12, 2024
1 parent db39474 commit 477cb62
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 35 deletions.
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

0 comments on commit 477cb62

Please sign in to comment.