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
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/cast_receiver/renderer/content_renderer_client_mixins_impl.h"
#include "components/cast_receiver/renderer/url_rewrite_rules_provider.h"
#include "components/media_control/renderer/media_playback_options.h"
#include "components/on_load_script_injector/renderer/on_load_script_injector.h"
#include "content/public/renderer/render_frame.h"
#include "third_party/blink/public/web/web_local_frame.h"
namespace cast_receiver {
// static
std::unique_ptr<ContentRendererClientMixins>
ContentRendererClientMixins::Create(
IsCorsExemptHeadersCallback is_cors_exempt_header_callback) {
return std::make_unique<ContentRendererClientMixinsImpl>(
std::move(is_cors_exempt_header_callback));
}
ContentRendererClientMixinsImpl::ContentRendererClientMixinsImpl(
IsCorsExemptHeadersCallback is_cors_exempt_header_callback)
: is_cors_exempt_header_callback_(
std::move(is_cors_exempt_header_callback)) {
DCHECK(is_cors_exempt_header_callback_);
}
ContentRendererClientMixinsImpl::~ContentRendererClientMixinsImpl() = default;
void ContentRendererClientMixinsImpl::RenderFrameCreated(
content::RenderFrame& render_frame) {
// Add script injection support to the RenderFrame, used for bindings support
// APIs. The injector's lifetime is bound to the RenderFrame's lifetime.
new on_load_script_injector::OnLoadScriptInjector(&render_frame);
// Lifetime is tied to |render_frame| via content::RenderFrameObserver.
new media_control::MediaPlaybackOptions(&render_frame);
// Create the new UrlRewriteRulesProvider.
url_rewrite_rules_providers_.emplace(
render_frame.GetWebFrame()->GetLocalFrameToken(),
std::make_unique<UrlRewriteRulesProvider>(
&render_frame,
base::BindOnce(&ContentRendererClientMixinsImpl::OnRenderFrameRemoved,
weak_factory_.GetWeakPtr())));
}
bool ContentRendererClientMixinsImpl::DeferMediaLoad(
content::RenderFrame& render_frame,
base::OnceClosure closure) {
auto* playback_options =
media_control::MediaPlaybackOptions::Get(&render_frame);
DCHECK(playback_options);
return playback_options->RunWhenInForeground(std::move(closure));
}
std::unique_ptr<blink::URLLoaderThrottleProvider>
ContentRendererClientMixinsImpl::CreateURLLoaderThrottleProvider() {
// It is safe to use |this| here because the lifetime of this object is
// expected to match that of the ContentRendererClient with which it is
// associated, so it should outlive any WrappingURLLoaderThrottleProvider
// instances it creates.
return std::make_unique<WrappingURLLoaderThrottleProvider>(*this);
}
std::unique_ptr<blink::URLLoaderThrottleProvider>
ContentRendererClientMixinsImpl::ExtendURLLoaderThrottleProvider(
std::unique_ptr<blink::URLLoaderThrottleProvider> delegated_load_provider) {
DCHECK(delegated_load_provider);
// It is safe to use |this| here because the lifetime of this object is
// expected to match that of the ContentRendererClient with which it is
// associated, so it should outlive any WrappingURLLoaderThrottleProvider
// instances it creates.
return std::make_unique<WrappingURLLoaderThrottleProvider>(
std::move(delegated_load_provider), *this);
}
void ContentRendererClientMixinsImpl::OnRenderFrameRemoved(
const blink::LocalFrameToken& frame_token) {
size_t result = url_rewrite_rules_providers_.erase(frame_token);
if (result != 1U) {
LOG(WARNING)
<< "Can't find the URL rewrite rules provider for render frame: "
<< frame_token;
}
}
UrlRewriteRulesProvider*
ContentRendererClientMixinsImpl::GetUrlRewriteRulesProvider(
const blink::LocalFrameToken& frame_token) {
auto rules_it = url_rewrite_rules_providers_.find(frame_token);
return rules_it == url_rewrite_rules_providers_.end()
? nullptr
: rules_it->second.get();
}
bool ContentRendererClientMixinsImpl::IsCorsExemptHeader(
std::string_view header) {
return is_cors_exempt_header_callback_.Run(header);
}
} // namespace cast_receiver
|