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

Adds WebUI handler for Android AI #20129

Merged
merged 1 commit into from
Sep 20, 2023
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
4 changes: 2 additions & 2 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
}

Expand Down Expand Up @@ -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",
]
Expand Down
44 changes: 38 additions & 6 deletions browser/ui/webui/ai_chat/ai_chat_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <utility>

#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"
Expand All @@ -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)
Expand Down Expand Up @@ -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<ai_chat::AIChatUIPageHandler>(
web_ui()->GetWebContents(), browser_->tab_strip_model(), profile_,
std::move(receiver));
raw_ptr<Browser> 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<ai_chat::AIChatUIPageHandler>(
web_ui()->GetWebContents(), web_contents, profile_, std::move(receiver));
}

std::unique_ptr<content::WebUIController>
Expand Down
2 changes: 0 additions & 2 deletions browser/ui/webui/ai_chat/ai_chat_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -41,7 +40,6 @@ class AIChatUI : public ui::UntrustedWebUIController {

base::WeakPtr<ui::MojoBubbleWebUIController::Embedder> embedder_;
raw_ptr<Profile> profile_ = nullptr;
raw_ptr<Browser> browser_ = nullptr;

WEB_UI_CONTROLLER_TYPE_DECL();
};
Expand Down
25 changes: 9 additions & 16 deletions browser/ui/webui/ai_chat/ai_chat_ui_page_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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<ai_chat::mojom::PageHandler> 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() ==
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's strange, this is meant to check visibility of owner_web_contents. I guess it's an existing bug, but if you have to make any other changes to this PR then please change it. But it's fine to leave it and we'll fix it separately.

content::Visibility::VISIBLE)
? true
: false;
active_chat_tab_helper_->OnConversationActiveChanged(is_visible);
} else {
// TODO(petemill): Enable conversation without the TabHelper. Conversation
Expand Down
4 changes: 1 addition & 3 deletions browser/ui/webui/ai_chat/ai_chat_ui_page_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"

class TabStripModel;

namespace content {
class WebContents;
}
Expand All @@ -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<ai_chat::mojom::PageHandler> receiver);

Expand Down