#include "chrome/renderer/chrome_render_frame_observer.h"
#include <stddef.h>
#include <string.h>
#include <limits>
#include <map>
#include <set>
#include <utility>
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_macros.h"
#include "base/no_destructor.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/lock.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_isolated_world_ids.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/open_search_description_document_handler.mojom.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/renderer/chrome_content_settings_agent_delegate.h"
#include "chrome/renderer/companion/visual_query/visual_query_classifier_agent.h"
#include "chrome/renderer/media/media_feeds.h"
#include "components/crash/core/common/crash_key.h"
#include "components/lens/lens_metadata.mojom.h"
#include "components/no_state_prefetch/renderer/no_state_prefetch_helper.h"
#include "components/no_state_prefetch/renderer/no_state_prefetch_utils.h"
#include "components/offline_pages/buildflags/buildflags.h"
#include "components/optimization_guide/content/renderer/page_text_agent.h"
#include "components/translate/content/renderer/translate_agent.h"
#include "components/translate/core/common/translate_util.h"
#include "components/web_cache/renderer/web_cache_impl.h"
#include "content/public/common/bindings_policy.h"
#include "content/public/common/content_features.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_frame_visitor.h"
#include "content/public/renderer/render_thread.h"
#include "content/public/renderer/window_features_converter.h"
#include "printing/buildflags/buildflags.h"
#include "services/service_manager/public/cpp/binder_registry.h"
#include "skia/ext/image_operations.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/platform/browser_interface_broker_proxy.h"
#include "third_party/blink/public/platform/web_url_request.h"
#include "third_party/blink/public/web/web_console_message.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_document_loader.h"
#include "third_party/blink/public/web/web_element.h"
#include "third_party/blink/public/web/web_frame_content_dumper.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_node.h"
#include "third_party/blink/public/web/web_security_policy.h"
#include "third_party/blink/public/web/web_view.h"
#include "third_party/libwebp/src/src/webp/decode.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/jpeg_codec.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/codec/webp_codec.h"
#include "ui/gfx/geometry/size_f.h"
#include "url/gurl.h"
#if !BUILDFLAG(IS_ANDROID)
#include "chrome/renderer/accessibility/read_anything_app_controller.h"
#include "chrome/renderer/searchbox/searchbox_extension.h"
#endif
#if BUILDFLAG(SAFE_BROWSING_AVAILABLE)
#include "components/safe_browsing/content/renderer/phishing_classifier/phishing_classifier_delegate.h"
#include "components/safe_browsing/content/renderer/phishing_classifier/phishing_image_embedder_delegate.h"
#endif
#if BUILDFLAG(ENABLE_OFFLINE_PAGES)
#include "chrome/common/mhtml_page_notifier.mojom.h"
#endif
#if BUILDFLAG(ENABLE_PLUGINS)
#include "chrome/renderer/plugins/chrome_plugin_placeholder.h"
#endif
WebDocumentLoader;
WebElement;
WebFrameContentDumper;
WebLocalFrame;
WebNode;
WebString;
RenderFrame;
static const size_t kMaxIndexChars = …;
static constexpr base::TimeDelta kLocationChangeInterval = …;
static const bool kDiscardTransparencyForContextMenu = …;
namespace {
const char kGifExtension[] = …;
const char kPngExtension[] = …;
const char kJpgExtension[] = …;
const char kWebpExtension[] = …;
#if BUILDFLAG(IS_ANDROID)
base::Lock& GetFrameHeaderMapLock() {
static base::NoDestructor<base::Lock> s;
return *s;
}
using FrameHeaderMap = std::map<blink::LocalFrameToken, std::string>;
FrameHeaderMap& GetFrameHeaderMap() {
GetFrameHeaderMapLock().AssertAcquired();
static base::NoDestructor<FrameHeaderMap> s;
return *s;
}
#endif
void UpdateLoadedOriginCrashKeys() { … }
}
ChromeRenderFrameObserver::ChromeRenderFrameObserver(
content::RenderFrame* render_frame,
web_cache::WebCacheImpl* web_cache_impl)
: … { … }
ChromeRenderFrameObserver::~ChromeRenderFrameObserver() = default;
#if BUILDFLAG(IS_ANDROID)
std::string ChromeRenderFrameObserver::GetCCTClientHeader(
const blink::LocalFrameToken& frame_token) {
base::AutoLock auto_lock(GetFrameHeaderMapLock());
auto frame_map = GetFrameHeaderMap();
auto iter = frame_map.find(frame_token);
return iter == frame_map.end() ? std::string() : iter->second;
}
#endif
void ChromeRenderFrameObserver::OnInterfaceRequestForFrame(
const std::string& interface_name,
mojo::ScopedMessagePipeHandle* interface_pipe) { … }
bool ChromeRenderFrameObserver::OnAssociatedInterfaceRequestForFrame(
const std::string& interface_name,
mojo::ScopedInterfaceEndpointHandle* handle) { … }
void ChromeRenderFrameObserver::ReadyToCommitNavigation(
WebDocumentLoader* document_loader) { … }
void ChromeRenderFrameObserver::DidSetPageLifecycleState(
bool restoring_from_bfcache) { … }
void ChromeRenderFrameObserver::DidFinishLoad() { … }
void ChromeRenderFrameObserver::DidCreateNewDocument() { … }
void ChromeRenderFrameObserver::DidCommitProvisionalLoad(
ui::PageTransition transition) { … }
void ChromeRenderFrameObserver::DidClearWindowObject() { … }
void ChromeRenderFrameObserver::DidMeaningfulLayout(
blink::WebMeaningfulLayout layout_type) { … }
void ChromeRenderFrameObserver::OnDestruct() { … }
void ChromeRenderFrameObserver::WillDetach(blink::DetachReason detach_reason) { … }
void ChromeRenderFrameObserver::SetWindowFeatures(
blink::mojom::WindowFeaturesPtr window_features) { … }
void ChromeRenderFrameObserver::ExecuteWebUIJavaScript(
const std::u16string& javascript) { … }
void ChromeRenderFrameObserver::RequestImageForContextNode(
int32_t thumbnail_min_area_pixels,
const gfx::Size& thumbnail_max_size_pixels,
chrome::mojom::ImageFormat image_format,
int32_t quality,
RequestImageForContextNodeCallback callback) { … }
void ChromeRenderFrameObserver::RequestBitmapForContextNode(
RequestBitmapForContextNodeCallback callback) { … }
void ChromeRenderFrameObserver::RequestBitmapForContextNodeWithBoundsHint(
RequestBitmapForContextNodeWithBoundsHintCallback callback) { … }
void ChromeRenderFrameObserver::RequestBoundsHintForAllImages(
RequestBoundsHintForAllImagesCallback callback) { … }
void ChromeRenderFrameObserver::FindImageElements(
blink::WebElement element,
std::vector<blink::WebElement>& images) { … }
void ChromeRenderFrameObserver::RequestReloadImageForContextNode() { … }
#if BUILDFLAG(IS_ANDROID)
void ChromeRenderFrameObserver::SetCCTClientHeader(const std::string& header) {
auto* web_frame = render_frame()->GetWebFrame();
if (!web_frame) {
return;
}
base::AutoLock auto_lock(GetFrameHeaderMapLock());
GetFrameHeaderMap()[web_frame->GetLocalFrameToken()] = header;
}
#endif
void ChromeRenderFrameObserver::GetMediaFeedURL(
GetMediaFeedURLCallback callback) { … }
void ChromeRenderFrameObserver::LoadBlockedPlugins(
const std::string& identifier) { … }
void ChromeRenderFrameObserver::SetSupportsDraggableRegions(
bool supports_draggable_regions) { … }
void ChromeRenderFrameObserver::SetShouldDeferMediaLoad(bool should_defer) { … }
void ChromeRenderFrameObserver::SetClientSidePhishingDetection() { … }
void ChromeRenderFrameObserver::SetVisualQueryClassifierAgent() { … }
void ChromeRenderFrameObserver::OnRenderFrameObserverRequest(
mojo::PendingAssociatedReceiver<chrome::mojom::ChromeRenderFrame>
receiver) { … }
bool ChromeRenderFrameObserver::ShouldCapturePageTextForTranslateOrPhishing(
blink::WebMeaningfulLayout layout_type) const { … }
void ChromeRenderFrameObserver::CapturePageText(
blink::WebMeaningfulLayout layout_type) { … }
bool ChromeRenderFrameObserver::NeedsDownscale(
const gfx::Size& original_image_size,
int32_t requested_image_min_area_pixels,
const gfx::Size& requested_image_max_size) { … }
SkBitmap ChromeRenderFrameObserver::Downscale(
const SkBitmap& image,
int requested_image_min_area_pixels,
const gfx::Size& requested_image_max_size) { … }
bool ChromeRenderFrameObserver::NeedsEncodeImage(
const std::string& image_extension,
chrome::mojom::ImageFormat image_format) { … }
bool ChromeRenderFrameObserver::IsAnimatedWebp(
const std::vector<uint8_t>& image_data) { … }