1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_RENDERER_CHROME_RENDER_FRAME_OBSERVER_H_
6 #define CHROME_RENDERER_CHROME_RENDER_FRAME_OBSERVER_H_
11 #include "base/memory/raw_ptr.h"
12 #include "build/build_config.h"
13 #include "chrome/common/chrome_render_frame.mojom.h"
14 #include "chrome/renderer/companion/visual_search/visual_search_classifier_agent.h"
15 #include "components/safe_browsing/buildflags.h"
16 #include "content/public/renderer/render_frame_observer.h"
17 #include "mojo/public/cpp/bindings/associated_receiver_set.h"
18 #include "mojo/public/cpp/bindings/pending_associated_receiver.h"
19 #include "services/service_manager/public/cpp/binder_registry.h"
20 #include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
28 namespace optimization_guide {
32 namespace safe_browsing {
33 class PhishingClassifierDelegate;
34 class PhishingImageEmbedderDelegate;
45 // This class holds the Chrome specific parts of RenderFrame, and has the same
47 class ChromeRenderFrameObserver : public content::RenderFrameObserver,
48 public chrome::mojom::ChromeRenderFrame {
50 ChromeRenderFrameObserver(content::RenderFrame* render_frame,
51 web_cache::WebCacheImpl* web_cache_impl);
53 ChromeRenderFrameObserver(const ChromeRenderFrameObserver&) = delete;
54 ChromeRenderFrameObserver& operator=(const ChromeRenderFrameObserver&) =
57 ~ChromeRenderFrameObserver() override;
59 service_manager::BinderRegistry* registry() { return ®istry_; }
60 blink::AssociatedInterfaceRegistry* associated_interfaces() {
61 return &associated_interfaces_;
64 #if BUILDFLAG(IS_ANDROID)
65 // This is called on the main thread for subresources or worker threads for
67 static std::string GetCCTClientHeader(int render_frame_id);
71 friend class ChromeRenderFrameObserverTest;
73 // RenderFrameObserver implementation.
74 void OnInterfaceRequestForFrame(
75 const std::string& interface_name,
76 mojo::ScopedMessagePipeHandle* interface_pipe) override;
77 bool OnAssociatedInterfaceRequestForFrame(
78 const std::string& interface_name,
79 mojo::ScopedInterfaceEndpointHandle* handle) override;
80 void ReadyToCommitNavigation(
81 blink::WebDocumentLoader* document_loader) override;
82 void DidFinishLoad() override;
83 void DidCreateNewDocument() override;
84 void DidCommitProvisionalLoad(ui::PageTransition transition) override;
85 void DidClearWindowObject() override;
86 void DidMeaningfulLayout(blink::WebMeaningfulLayout layout_type) override;
87 void OnDestruct() override;
88 void DraggableRegionsChanged() override;
90 // chrome::mojom::ChromeRenderFrame:
91 void SetWindowFeatures(
92 blink::mojom::WindowFeaturesPtr window_features) override;
93 void ExecuteWebUIJavaScript(const std::u16string& javascript) override;
94 void RequestImageForContextNode(
95 int32_t thumbnail_min_area_pixels,
96 const gfx::Size& thumbnail_max_size_pixels,
97 chrome::mojom::ImageFormat image_format,
99 RequestImageForContextNodeCallback callback) override;
100 void RequestBitmapForContextNode(
101 RequestBitmapForContextNodeCallback callback) override;
102 void RequestReloadImageForContextNode() override;
103 #if BUILDFLAG(IS_ANDROID)
104 void SetCCTClientHeader(const std::string& header) override;
106 void GetMediaFeedURL(GetMediaFeedURLCallback callback) override;
107 void LoadBlockedPlugins(const std::string& identifier) override;
109 // Initialize a |phishing_classifier_delegate_|.
110 void SetClientSidePhishingDetection();
112 // Initialize a |visual_search_classifier_agent_|.
113 void SetVisualSearchClassifierAgent();
115 void OnRenderFrameObserverRequest(
116 mojo::PendingAssociatedReceiver<chrome::mojom::ChromeRenderFrame>
119 // Captures page information using the top (main) frame of a frame tree.
120 // Currently, this page information is just the text content of the local
121 // frames, collected and concatenated until a certain limit (kMaxIndexChars)
123 void CapturePageText(blink::WebMeaningfulLayout layout_type);
125 // Returns true if |CapturePageText| should be run for Translate or Phishing.
126 bool ShouldCapturePageTextForTranslateOrPhishing(
127 blink::WebMeaningfulLayout layout_type) const;
129 // Check if the image need to downscale.
130 static bool NeedsDownscale(const gfx::Size& original_image_size,
131 int32_t requested_image_min_area_pixels,
132 const gfx::Size& requested_image_max_size);
134 // If the source image is null or occupies less area than
135 // |requested_image_min_area_pixels|, we return the image unmodified.
136 // Otherwise, we scale down the image so that the width and height do not
137 // exceed |requested_image_max_size|, preserving the original aspect ratio.
138 static SkBitmap Downscale(const SkBitmap& image,
139 int requested_image_min_area_pixels,
140 const gfx::Size& requested_image_max_size);
142 // Check if the image need to encode to fit requested image format.
143 static bool NeedsEncodeImage(const std::string& image_extension,
144 chrome::mojom::ImageFormat image_format);
146 // Check if the image is an animated Webp image by looking for animation
148 static bool IsAnimatedWebp(const std::vector<uint8_t>& image_data);
150 // Have the same lifetime as us.
151 raw_ptr<translate::TranslateAgent, ExperimentalRenderer> translate_agent_;
152 raw_ptr<optimization_guide::PageTextAgent, ExperimentalRenderer>
154 #if BUILDFLAG(SAFE_BROWSING_AVAILABLE)
155 raw_ptr<safe_browsing::PhishingClassifierDelegate, ExperimentalRenderer>
156 phishing_classifier_ = nullptr;
157 raw_ptr<safe_browsing::PhishingImageEmbedderDelegate, ExperimentalRenderer>
158 phishing_image_embedder_ = nullptr;
161 // Owned by ChromeContentRendererClient and outlive us.
162 raw_ptr<web_cache::WebCacheImpl, ExperimentalRenderer> web_cache_impl_;
164 #if !BUILDFLAG(IS_ANDROID)
165 // Save the JavaScript to preload if ExecuteWebUIJavaScript is invoked.
166 std::vector<std::u16string> webui_javascript_;
168 // Add visual search agent to suggest visually relevant items on the page.
169 raw_ptr<companion::visual_search::VisualSearchClassifierAgent,
170 ExperimentalRenderer>
171 visual_classifier_ = nullptr;
174 mojo::AssociatedReceiverSet<chrome::mojom::ChromeRenderFrame> receivers_;
176 service_manager::BinderRegistry registry_;
177 blink::AssociatedInterfaceRegistry associated_interfaces_;
180 #endif // CHROME_RENDERER_CHROME_RENDER_FRAME_OBSERVER_H_