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

Conversation

SergeyZhukovsky
Copy link
Member

@SergeyZhukovsky SergeyZhukovsky commented Sep 12, 2023

Resolves brave/brave-browser#32808

Submitter Checklist:

  • I confirm that no security/privacy review is needed and no other type of reviews are needed, or that I have requested them
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Squashed any review feedback or "fixup" commits before merge, so that history is a record of what happened in the repo, not your PR
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally:
    • npm run test -- brave_browser_tests, npm run test -- brave_unit_tests wiki
    • npm run lint, npm run presubmit wiki, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

Screenshot_20230908_135618

@SergeyZhukovsky SergeyZhukovsky self-assigned this Sep 12, 2023
@github-actions github-actions bot added the potential-layer-violation-fixes This PR touches a BUILD.gn file with check_includes=false label Sep 12, 2023
Copy link
Member

@petemill petemill left a comment

Choose a reason for hiding this comment

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

This is great but as of #20068 we no longer observe the TabStripModel and no longer need to change active / target WebContents. All platforms will now only observe 1 WebContents at a time when opened.

So I think we can simplify this - make the same AIChatUIPageHandler implementation for both platforms, and have it accept a single target WebContents in the ctor. AIChatUI can select the currently active target WebContents per-platform (L91) - tab_strip_model->GetActiveWebContents() for desktop and your specific GetActiveWebContents for android.

What do you think?

@SergeyZhukovsky
Copy link
Member Author

This is great but as of #20068 we no longer observe the TabStripModel and no longer need to change active / target WebContents. All platforms will now only observe 1 WebContents at a time when opened.

So I think we can simplify this - make the same AIChatUIPageHandler implementation for both platforms, and have it accept a single target WebContents in the ctor. AIChatUI can select the currently active target WebContents per-platform (L91) - tab_strip_model->GetActiveWebContents() for desktop and your specific GetActiveWebContents for android.

What do you think?

ok, sounds good, going to do it that way in that case.

@SergeyZhukovsky
Copy link
Member Author

@petemill I re-made it following the new code requirements, it's good for another review round.

Copy link
Member

@petemill petemill left a comment

Choose a reason for hiding this comment

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

Thanks for re-working. Just some minor feedback and this looks great

web_ui()->GetWebContents()) != TabStripModel::kNoTab;
#else
web_contents = GetActiveWebContents(profile_);
is_standalone = web_contents == nullptr;
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't the is_standalone for both platforms be based on whether this WebUI is the active Tab vs a separate WebContents, i.e. for both platforms:

is_standalone = web_contends !== web_ui->GetWebContents()

?

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah perhaps it's better to do that way for the feature standalone option on Android

Copy link
Member Author

@SergeyZhukovsky SergeyZhukovsky Sep 18, 2023

Choose a reason for hiding this comment

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

actually that's not right as web_contents is the current page contents vs web_ui->GetWebContents() is an actual web ui with the chat contents. So I think Android detection is correct it means we have a chat opened only if we don't have an active tab and the web ui, but for desktop I'm not sure exactly what do we check there, we are looking for an index of the chat web ui and we assume it's not a standalone if we found it. As to me the check should be
is_standalone = web_contents == web_ui->GetWebContents();

@@ -41,7 +44,9 @@ class AIChatUI : public ui::UntrustedWebUIController {

base::WeakPtr<ui::MojoBubbleWebUIController::Embedder> embedder_;
raw_ptr<Profile> profile_ = nullptr;
#if !BUILDFLAG(IS_ANDROID)
raw_ptr<Browser> browser_ = nullptr;
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need browser_ for either platform now

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah I noticed that also about the member, I just didn't remove as thought we kept it for some feature interaction. I will remove it.

public AIChatTabHelper::Observer,
public content::WebContentsObserver {
public:
AIChatUIPageHandler(
content::WebContents* owner_web_contents,
TabStripModel* tab_strip_model,
bool is_standalone,
content::WebContents* web_contents,
Copy link
Member

Choose a reason for hiding this comment

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

nit: perhaps a more specific name, e.g. chat_context_web_contents?

public AIChatTabHelper::Observer,
public content::WebContentsObserver {
public:
AIChatUIPageHandler(
content::WebContents* owner_web_contents,
TabStripModel* tab_strip_model,
bool is_standalone,
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we even need is_standalone? Perhaps consumer just passes nullptr for the WebContents param?

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah it could be detected that way, I will change it

@SergeyZhukovsky
Copy link
Member Author

@petemill it's good for another review round.

Copy link
Member

@petemill petemill left a comment

Choose a reason for hiding this comment

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

👍

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.

@SergeyZhukovsky SergeyZhukovsky merged commit 0d8d5f1 into master Sep 20, 2023
6 checks passed
@SergeyZhukovsky SergeyZhukovsky deleted the android_ai_handler branch September 20, 2023 18:49
@github-actions github-actions bot added this to the 1.60.x - Nightly milestone Sep 20, 2023
Copy link
Member

@thypon thypon left a comment

Choose a reason for hiding this comment

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

security gtg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
potential-layer-violation-fixes This PR touches a BUILD.gn file with check_includes=false
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement AI chat WebUI handlers for Android
3 participants