From e897f712084f3ad69159350119985abb8d916e2d Mon Sep 17 00:00:00 2001 From: Serg Date: Mon, 18 Sep 2023 14:41:58 -0400 Subject: [PATCH] Adds WebUI handler for Android AI --- browser/ui/BUILD.gn | 4 +- browser/ui/webui/ai_chat/ai_chat_ui.cc | 44 ++++++++++++++++--- browser/ui/webui/ai_chat/ai_chat_ui.h | 2 - .../webui/ai_chat/ai_chat_ui_page_handler.cc | 25 ++++------- .../webui/ai_chat/ai_chat_ui_page_handler.h | 4 +- 5 files changed, 50 insertions(+), 29 deletions(-) diff --git a/browser/ui/BUILD.gn b/browser/ui/BUILD.gn index f7104233aa79..3f2745086678 100644 --- a/browser/ui/BUILD.gn +++ b/browser/ui/BUILD.gn @@ -71,6 +71,8 @@ source_set("ui") { sources += [ "webui/ai_chat/ai_chat_ui.cc", "webui/ai_chat/ai_chat_ui.h", + "webui/ai_chat/ai_chat_ui_page_handler.cc", + "webui/ai_chat/ai_chat_ui_page_handler.h", ] } @@ -214,8 +216,6 @@ source_set("ui") { if (enable_ai_chat) { sources += [ - "webui/ai_chat/ai_chat_ui_page_handler.cc", - "webui/ai_chat/ai_chat_ui_page_handler.h", "webui/settings/brave_settings_leo_assistant_handler.cc", "webui/settings/brave_settings_leo_assistant_handler.h", ] diff --git a/browser/ui/webui/ai_chat/ai_chat_ui.cc b/browser/ui/webui/ai_chat/ai_chat_ui.cc index c2280fa7e944..2919fe58f27f 100644 --- a/browser/ui/webui/ai_chat/ai_chat_ui.cc +++ b/browser/ui/webui/ai_chat/ai_chat_ui.cc @@ -8,6 +8,7 @@ #include #include "brave/browser/ui/side_panel/ai_chat/ai_chat_side_panel_utils.h" +#include "brave/browser/ui/webui/ai_chat/ai_chat_ui_page_handler.h" #include "brave/browser/ui/webui/brave_webui_source.h" #include "brave/components/ai_chat/browser/constants.h" #include "brave/components/ai_chat/common/pref_names.h" @@ -23,8 +24,30 @@ #include "content/public/common/url_constants.h" #if !BUILDFLAG(IS_ANDROID) -#include "brave/browser/ui/webui/ai_chat/ai_chat_ui_page_handler.h" #include "chrome/browser/ui/browser.h" +#else +#include "chrome/browser/ui/android/tab_model/tab_model.h" +#include "chrome/browser/ui/android/tab_model/tab_model_list.h" +#endif + +#if BUILDFLAG(IS_ANDROID) +namespace { +content::WebContents* GetActiveWebContents(content::BrowserContext* context) { + auto tab_models = TabModelList::models(); + auto iter = base::ranges::find_if( + tab_models, [](const auto& model) { return model->IsActiveModel(); }); + if (iter == tab_models.end()) { + return nullptr; + } + + auto* active_contents = (*iter)->GetActiveWebContents(); + if (!active_contents) { + return nullptr; + } + DCHECK_EQ(active_contents->GetBrowserContext(), context); + return active_contents; +} +} // namespace #endif AIChatUI::AIChatUI(content::WebUI* web_ui) @@ -79,13 +102,22 @@ void AIChatUI::BindInterface( embedder_->ShowUI(); } + content::WebContents* web_contents = nullptr; #if !BUILDFLAG(IS_ANDROID) - browser_ = ai_chat::GetBrowserForWebContents(web_ui()->GetWebContents()); - DCHECK(browser_); - page_handler_ = std::make_unique( - web_ui()->GetWebContents(), browser_->tab_strip_model(), profile_, - std::move(receiver)); + raw_ptr browser = + ai_chat::GetBrowserForWebContents(web_ui()->GetWebContents()); + DCHECK(browser); + TabStripModel* tab_strip_model = browser->tab_strip_model(); + DCHECK(tab_strip_model); + web_contents = tab_strip_model->GetActiveWebContents(); +#else + web_contents = GetActiveWebContents(profile_); #endif + if (web_contents == web_ui()->GetWebContents()) { + web_contents = nullptr; + } + page_handler_ = std::make_unique( + web_ui()->GetWebContents(), web_contents, profile_, std::move(receiver)); } std::unique_ptr diff --git a/browser/ui/webui/ai_chat/ai_chat_ui.h b/browser/ui/webui/ai_chat/ai_chat_ui.h index d7273af3119c..a0d63fdd089c 100644 --- a/browser/ui/webui/ai_chat/ai_chat_ui.h +++ b/browser/ui/webui/ai_chat/ai_chat_ui.h @@ -16,7 +16,6 @@ #include "ui/webui/mojo_web_ui_controller.h" #include "ui/webui/untrusted_web_ui_controller.h" -class Browser; class Profile; class AIChatUI : public ui::UntrustedWebUIController { @@ -41,7 +40,6 @@ class AIChatUI : public ui::UntrustedWebUIController { base::WeakPtr embedder_; raw_ptr profile_ = nullptr; - raw_ptr browser_ = nullptr; WEB_UI_CONTROLLER_TYPE_DECL(); }; diff --git a/browser/ui/webui/ai_chat/ai_chat_ui_page_handler.cc b/browser/ui/webui/ai_chat/ai_chat_ui_page_handler.cc index e340362a5990..05e021f6d971 100644 --- a/browser/ui/webui/ai_chat/ai_chat_ui_page_handler.cc +++ b/browser/ui/webui/ai_chat/ai_chat_ui_page_handler.cc @@ -17,7 +17,6 @@ #include "brave/components/ai_chat/common/pref_names.h" #include "chrome/browser/favicon/favicon_service_factory.h" #include "chrome/browser/profiles/profile.h" -#include "chrome/browser/ui/tabs/tab_strip_model.h" #include "components/favicon/core/favicon_service.h" #include "components/prefs/pref_service.h" #include "content/public/browser/visibility.h" @@ -36,28 +35,22 @@ using mojom::ConversationTurnVisibility; AIChatUIPageHandler::AIChatUIPageHandler( content::WebContents* owner_web_contents, - TabStripModel* tab_strip_model, + content::WebContents* chat_context_web_contents, Profile* profile, mojo::PendingReceiver receiver) : content::WebContentsObserver(owner_web_contents), profile_(profile), receiver_(this, std::move(receiver)) { - DCHECK(tab_strip_model); - // Detect if we are in target-tab mode, or standalone mode. Standalone mode - // means Chat is opened as its own tab in the tab strip and not a side panel. - bool is_standalone = (tab_strip_model->GetIndexOfWebContents( - owner_web_contents) != TabStripModel::kNoTab); - if (!is_standalone) { - auto* web_contents = tab_strip_model->GetActiveWebContents(); - if (!web_contents) { - return; - } + // Standalone mode means Chat is opened as its own tab in the tab strip and + // not a side panel. chat_context_web_contents is nullptr in that case + if (chat_context_web_contents != nullptr) { active_chat_tab_helper_ = - ai_chat::AIChatTabHelper::FromWebContents(web_contents); + ai_chat::AIChatTabHelper::FromWebContents(chat_context_web_contents); chat_tab_helper_observation_.Observe(active_chat_tab_helper_); - bool is_visible = - (web_contents->GetVisibility() == content::Visibility::VISIBLE) ? true - : false; + bool is_visible = (chat_context_web_contents->GetVisibility() == + content::Visibility::VISIBLE) + ? true + : false; active_chat_tab_helper_->OnConversationActiveChanged(is_visible); } else { // TODO(petemill): Enable conversation without the TabHelper. Conversation diff --git a/browser/ui/webui/ai_chat/ai_chat_ui_page_handler.h b/browser/ui/webui/ai_chat/ai_chat_ui_page_handler.h index 624f06d92094..54b78c551636 100644 --- a/browser/ui/webui/ai_chat/ai_chat_ui_page_handler.h +++ b/browser/ui/webui/ai_chat/ai_chat_ui_page_handler.h @@ -23,8 +23,6 @@ #include "mojo/public/cpp/bindings/receiver.h" #include "mojo/public/cpp/bindings/remote.h" -class TabStripModel; - namespace content { class WebContents; } @@ -40,7 +38,7 @@ class AIChatUIPageHandler : public ai_chat::mojom::PageHandler, public: AIChatUIPageHandler( content::WebContents* owner_web_contents, - TabStripModel* tab_strip_model, + content::WebContents* chat_context_web_contents, Profile* profile, mojo::PendingReceiver receiver);