#ifndef CHROME_RENDERER_CHROME_RENDER_FRAME_OBSERVER_H_
#define CHROME_RENDERER_CHROME_RENDER_FRAME_OBSERVER_H_
#include <memory>
#include <string>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#include "chrome/common/buildflags.h"
#include "chrome/common/chrome_render_frame.mojom.h"
#include "components/safe_browsing/buildflags.h"
#include "content/public/renderer/render_frame_observer.h"
#include "mojo/public/cpp/bindings/associated_receiver_set.h"
#include "mojo/public/cpp/bindings/pending_associated_receiver.h"
#include "services/service_manager/public/cpp/binder_registry.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
#if !BUILDFLAG(IS_ANDROID)
#include "chrome/common/actor.mojom.h"
#include "chrome/common/actor/task_id.h"
#include "chrome/renderer/actor/tool_executor.h"
#endif
class SkBitmap;
namespace actor {
class Journal;
class PageStabilityMonitor;
}
namespace gfx {
class Size;
}
namespace optimization_guide {
class PageTextAgent;
}
namespace safe_browsing {
class PhishingClassifierDelegate;
class PhishingImageEmbedderDelegate;
}
namespace translate {
class TranslateAgent;
}
namespace web_cache {
class WebCacheImpl;
}
class ChromeRenderFrameObserver : public content::RenderFrameObserver,
public chrome::mojom::ChromeRenderFrame {
public:
ChromeRenderFrameObserver(content::RenderFrame* render_frame,
web_cache::WebCacheImpl* web_cache_impl);
ChromeRenderFrameObserver(const ChromeRenderFrameObserver&) = delete;
ChromeRenderFrameObserver& operator=(const ChromeRenderFrameObserver&) =
delete;
~ChromeRenderFrameObserver() override;
service_manager::BinderRegistry* registry() { return ®istry_; }
blink::AssociatedInterfaceRegistry* associated_interfaces() {
return &associated_interfaces_;
}
#if BUILDFLAG(IS_ANDROID)
static std::string GetCCTClientHeader(
const blink::LocalFrameToken& frame_token);
#endif
private:
friend class ChromeRenderFrameObserverTest;
void OnInterfaceRequestForFrame(
const std::string& interface_name,
mojo::ScopedMessagePipeHandle* interface_pipe) override;
bool OnAssociatedInterfaceRequestForFrame(
const std::string& interface_name,
mojo::ScopedInterfaceEndpointHandle* handle) override;
void ReadyToCommitNavigation(
blink::WebDocumentLoader* document_loader) override;
void DidSetPageLifecycleState(
blink::BFCacheStateChange bfcache_change) override;
void DidFinishLoad() override;
void DidCreateNewDocument() override;
void DidCommitProvisionalLoad(ui::PageTransition transition) override;
void DidClearWindowObject() override;
void DidMeaningfulLayout(blink::WebMeaningfulLayout layout_type) override;
void OnDestruct() override;
void WillDetach(blink::DetachReason detach_reason) override;
void SetWindowFeatures(
blink::mojom::WindowFeaturesPtr window_features) override;
void ExecuteWebUIJavaScript(const std::u16string& javascript) override;
void RequestImageForContextNode(
int32_t thumbnail_min_area_pixels,
const gfx::Size& thumbnail_max_size_pixels,
chrome::mojom::ImageFormat image_format,
int32_t quality,
RequestImageForContextNodeCallback callback) override;
void RequestBitmapForContextNode(
RequestBitmapForContextNodeCallback callback) override;
void RequestBitmapForContextNodeWithBoundsHint(
RequestBitmapForContextNodeWithBoundsHintCallback callback) override;
void RequestBoundsHintForAllImages(
RequestBoundsHintForAllImagesCallback callback) override;
void FindImageElements(blink::WebElement element,
std::vector<blink::WebElement>& images);
void RequestReloadImageForContextNode() override;
#if BUILDFLAG(IS_ANDROID)
void SetCCTClientHeader(const std::string& header) override;
#endif
void GetMediaFeedURL(GetMediaFeedURLCallback callback) override;
void LoadBlockedPlugins(const std::string& identifier) override;
void SetShouldDeferMediaLoad(bool should_defer) override;
#if !BUILDFLAG(IS_ANDROID)
void InvokeTool(actor::mojom::ToolInvocationPtr request,
InvokeToolCallback callback) override;
void CancelTool(const actor::TaskId& task_id) override;
void StartActorJournal(
mojo::PendingAssociatedRemote<actor::mojom::JournalClient> client)
override;
void CreatePageStabilityMonitor(
mojo::PendingReceiver<actor::mojom::PageStabilityMonitor> monitor,
const actor::TaskId& task_id,
bool supports_paint_stability) override;
#endif
void SetClientSidePhishingDetection();
void OnRenderFrameObserverRequest(
mojo::PendingAssociatedReceiver<chrome::mojom::ChromeRenderFrame>
receiver);
void CapturePageText(blink::WebMeaningfulLayout layout_type);
bool ShouldCapturePageTextForTranslateOrPhishing(
blink::WebMeaningfulLayout layout_type) const;
static bool NeedsDownscale(const gfx::Size& original_image_size,
int32_t requested_image_min_area_pixels,
const gfx::Size& requested_image_max_size);
static SkBitmap Downscale(const SkBitmap& image,
int requested_image_min_area_pixels,
const gfx::Size& requested_image_max_size);
static bool NeedsEncodeImage(const std::string& mime_type,
chrome::mojom::ImageFormat image_format);
static bool IsAnimatedWebp(const std::vector<uint8_t>& image_data);
raw_ptr<translate::TranslateAgent> translate_agent_;
raw_ptr<optimization_guide::PageTextAgent> page_text_agent_;
#if BUILDFLAG(SAFE_BROWSING_AVAILABLE)
raw_ptr<safe_browsing::PhishingClassifierDelegate> phishing_classifier_ =
nullptr;
raw_ptr<safe_browsing::PhishingImageEmbedderDelegate>
phishing_image_embedder_ = nullptr;
#endif
#if !BUILDFLAG(IS_ANDROID)
std::unique_ptr<actor::Journal> actor_journal_;
#endif
raw_ptr<web_cache::WebCacheImpl> web_cache_impl_;
#if !BUILDFLAG(IS_ANDROID)
std::vector<std::u16string> webui_javascript_;
#endif
#if !BUILDFLAG(IS_ANDROID)
std::unique_ptr<actor::ToolExecutor> tool_executor_;
std::unique_ptr<actor::PageStabilityMonitor> page_stability_monitor_;
#endif
mojo::AssociatedReceiverSet<chrome::mojom::ChromeRenderFrame> receivers_;
service_manager::BinderRegistry registry_;
blink::AssociatedInterfaceRegistry associated_interfaces_;
};
#endif