Skip to content

Commit

Permalink
Working TabInformer
Browse files Browse the repository at this point in the history
  • Loading branch information
fallaciousreasoning committed Jan 17, 2025
1 parent a220e96 commit 820ff66
Show file tree
Hide file tree
Showing 20 changed files with 303 additions and 273 deletions.
1 change: 1 addition & 0 deletions browser/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ brave_chrome_browser_deps = [
"//brave/browser/ntp_background",
"//brave/browser/skus",
"//brave/browser/sync",
"//brave/browser/tab_informer",
"//brave/browser/themes",
"//brave/browser/ui",
"//brave/browser/ui/webui/ads_internals",
Expand Down
8 changes: 8 additions & 0 deletions browser/tab_informer/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# Copyright (c) 2025 The Brave Authors. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.

static_library("tab_informer") {
sources = [
"tab_informer.cc",
"tab_informer.h",
"tab_informer_service_factory.cc",
"tab_informer_service_factory.h",
]

deps = [
"//brave/components/tab_informer/browser",
"//chrome/browser/ui/tabs:tab_model",
"//components/keyed_service/content",
"//content/public/browser",
]
Expand Down
68 changes: 68 additions & 0 deletions browser/tab_informer/tab_informer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) 2025 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#include "brave/browser/tab_informer/tab_informer.h"

#include <utility>

#include "base/strings/utf_string_conversions.h"
#include "brave/browser/tab_informer/tab_informer_service_factory.h"
#include "brave/components/tab_informer/browser/tab_informer_service.h"
#include "brave/components/tab_informer/common/tab_informer.mojom.h"
#include "chrome/browser/ui/tabs/tab_model.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents_observer.h"

namespace tab_informer {

namespace {

mojom::TabPtr FromWebContents(content::WebContents* web_contents) {
auto tab = mojom::Tab::New();
tab->content_id =
web_contents->GetController().GetVisibleEntry()->GetUniqueID();
tab->title = base::UTF16ToUTF8(web_contents->GetTitle());
tab->url = web_contents->GetLastCommittedURL();
return tab;
}

} // namespace

TabInformer::TabInformer(content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
tab_id_(tabs::TabModel::GetFromContents(web_contents)->GetTabHandle()) {}

TabInformer::~TabInformer() {
auto* service = TabInformerServiceFactory::GetForBrowserContext(
web_contents()->GetBrowserContext());
if (!service) {
return;
}

service->UpdateTab(tab_id_, nullptr);
}

void TabInformer::TitleWasSet(content::NavigationEntry* entry) {
UpdateTab();
}

void TabInformer::PrimaryPageChanged(content::Page& page) {
UpdateTab();
}

void TabInformer::UpdateTab() {
auto* service = TabInformerServiceFactory::GetForBrowserContext(
web_contents()->GetBrowserContext());
if (!service) {
return;
}

auto tab = FromWebContents(web_contents());
tab->id = tab_id_;
service->UpdateTab(tab_id_, std::move(tab));
}

} // namespace tab_informer
36 changes: 36 additions & 0 deletions browser/tab_informer/tab_informer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2025 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#ifndef BRAVE_BROWSER_TAB_INFORMER_TAB_INFORMER_H_
#define BRAVE_BROWSER_TAB_INFORMER_TAB_INFORMER_H_

#include "content/public/browser/web_contents_observer.h"

namespace content {
class NavigationEntry;
}

namespace tab_informer {

class TabInformer : public content::WebContentsObserver {
public:
explicit TabInformer(content::WebContents* contents);
~TabInformer() override;

TabInformer(const TabInformer&) = delete;
TabInformer& operator=(const TabInformer&) = delete;

void PrimaryPageChanged(content::Page& page) override;
void TitleWasSet(content::NavigationEntry* entry) override;

private:
int32_t tab_id_ = 0;

void UpdateTab();
};

} // namespace tab_informer

#endif // BRAVE_BROWSER_TAB_INFORMER_TAB_INFORMER_H_
2 changes: 2 additions & 0 deletions browser/tab_informer/tab_informer_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "brave/browser/tab_informer/tab_informer_service_factory.h"

#include <memory>

#include "brave/components/tab_informer/browser/tab_informer_service.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"

Expand Down
7 changes: 7 additions & 0 deletions browser/tab_informer/tab_informer_service_factory.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// Copyright (c) 2025 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#ifndef BRAVE_BROWSER_TAB_INFORMER_TAB_INFORMER_SERVICE_FACTORY_H_
#define BRAVE_BROWSER_TAB_INFORMER_TAB_INFORMER_SERVICE_FACTORY_H_

#include <memory>

#include "base/no_destructor.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"

Expand Down
3 changes: 2 additions & 1 deletion browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ source_set("ui") {

deps = [
"//brave/browser/ai_chat",
"//brave/browser/tab_informer",
"//brave/browser/brave_adaptive_captcha",
"//brave/browser/brave_ads",
"//brave/browser/brave_rewards",
"//brave/browser/skus",
"//brave/browser/tab_informer",
"//brave/components/brave_shields/core/browser",
"//brave/components/tab_informer/browser",
"//brave/components/webcompat/core/common",
"//chrome/browser/ui:browser_navigator_params_headers",
]
Expand Down
23 changes: 0 additions & 23 deletions browser/ui/ai_chat/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,3 @@ source_set("unit_tests") {
"//testing/gtest:gtest",
]
}

source_set("browser_tests") {
testonly = true

sources = []

if (!is_android) {
sources += [ "tab_informer_browsertest.cc" ]
}

defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ]

deps = [
"//base/test:test_support",
"//brave/components/ai_chat/content/browser",
"//brave/components/ai_chat/core/browser",
"//brave/components/ai_chat/core/common",
"//brave/components/ai_chat/core/common/mojom",
"//chrome/browser/ui",
"//chrome/test:test_support",
"//testing/gtest",
]
}
Loading

0 comments on commit 820ff66

Please sign in to comment.