/
ai_chat_tab_helper.h
135 lines (108 loc) · 5.15 KB
/
ai_chat_tab_helper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* Copyright (c) 2023 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_COMPONENTS_AI_CHAT_CONTENT_BROWSER_AI_CHAT_TAB_HELPER_H_
#define BRAVE_COMPONENTS_AI_CHAT_CONTENT_BROWSER_AI_CHAT_TAB_HELPER_H_
#include <memory>
#include <string>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "brave/components/ai_chat/core/browser/conversation_driver.h"
#include "brave/components/ai_chat/core/browser/engine/engine_consumer.h"
#include "brave/components/ai_chat/core/common/mojom/ai_chat.mojom.h"
#include "brave/components/ai_chat/core/common/mojom/page_content_extractor.mojom.h"
#include "components/favicon/core/favicon_driver_observer.h"
#include "components/prefs/pref_change_registrar.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
#include "mojo/public/cpp/bindings/associated_receiver.h"
#include "mojo/public/cpp/bindings/pending_associated_receiver.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
class PrefService;
namespace content {
class ScopedAccessibilityMode;
}
namespace ai_chat {
class AIChatMetrics;
// Provides context to an AI Chat conversation in the form of the Tab's content
class AIChatTabHelper : public content::WebContentsObserver,
public content::WebContentsUserData<AIChatTabHelper>,
public mojom::PageContentExtractorHost,
public favicon::FaviconDriverObserver,
public ConversationDriver {
public:
static void BindPageContentExtractorHost(
content::RenderFrameHost* rfh,
mojo::PendingAssociatedReceiver<mojom::PageContentExtractorHost>
receiver);
AIChatTabHelper(const AIChatTabHelper&) = delete;
AIChatTabHelper& operator=(const AIChatTabHelper&) = delete;
~AIChatTabHelper() override;
void SetOnPDFA11yInfoLoadedCallbackForTesting(base::OnceClosure cb);
// mojom::PageContentExtractorHost
void OnInterceptedPageContentChanged() override;
private:
friend class content::WebContentsUserData<AIChatTabHelper>;
// To observe PDF InnerWebContents for "Finished loading PDF" event which
// means PDF content has been loaded to an accessibility tree.
class PDFA11yInfoLoadObserver : public content::WebContentsObserver {
public:
~PDFA11yInfoLoadObserver() override;
explicit PDFA11yInfoLoadObserver(content::WebContents* web_contents,
AIChatTabHelper* helper);
private:
void AccessibilityEventReceived(
const content::AXEventNotificationDetails& details) override;
raw_ptr<AIChatTabHelper> helper_;
};
AIChatTabHelper(
content::WebContents* web_contents,
AIChatMetrics* ai_chat_metrics,
base::RepeatingCallback<mojo::PendingRemote<skus::mojom::SkusService>()>
skus_service_getter,
PrefService* local_state_prefs,
const std::string& channel_name);
void OnPDFA11yInfoLoaded();
// content::WebContentsObserver
void WebContentsDestroyed() override;
void DidFinishNavigation(
content::NavigationHandle* navigation_handle) override;
void TitleWasSet(content::NavigationEntry* entry) override;
void InnerWebContentsAttached(content::WebContents* inner_web_contents,
content::RenderFrameHost* render_frame_host,
bool is_full_page) override;
// favicon::FaviconDriverObserver
void OnFaviconUpdated(favicon::FaviconDriver* favicon_driver,
NotificationIconType notification_icon_type,
const GURL& icon_url,
bool icon_url_changed,
const gfx::Image& image) override;
// ai_chat::ConversationDriver
GURL GetPageURL() const override;
void GetPageContent(GetPageContentCallback callback,
std::string_view invalidation_token) override;
std::u16string GetPageTitle() const override;
void BindPageContentExtractorReceiver(
mojo::PendingAssociatedReceiver<mojom::PageContentExtractorHost>
receiver);
raw_ptr<AIChatMetrics> ai_chat_metrics_;
bool is_same_document_navigation_ = false;
int64_t pending_navigation_id_;
bool is_pdf_a11y_info_loaded_ = false;
GetPageContentCallback pending_get_page_content_callback_;
std::unique_ptr<PDFA11yInfoLoadObserver> pdf_load_observer_;
base::OnceClosure on_pdf_a11y_info_loaded_cb_;
// A scoper only used for PDF viewing.
std::unique_ptr<content::ScopedAccessibilityMode> scoped_accessibility_mode_;
mojo::AssociatedReceiver<mojom::PageContentExtractorHost>
page_content_extractor_receiver_{this};
base::WeakPtrFactory<AIChatTabHelper> weak_ptr_factory_{this};
WEB_CONTENTS_USER_DATA_KEY_DECL();
};
} // namespace ai_chat
#endif // BRAVE_COMPONENTS_AI_CHAT_CONTENT_BROWSER_AI_CHAT_TAB_HELPER_H_