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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/pdf/chrome_pdf_stream_delegate.h"
#include <optional>
#include <string>
#include <utility>
#include "base/check_op.h"
#include "base/feature_list.h"
#include "base/memory/weak_ptr.h"
#include "base/no_destructor.h"
#include "base/numerics/safe_conversions.h"
#include "chrome/browser/pdf/pdf_pref_names.h"
#include "chrome/browser/pdf/pdf_viewer_stream_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/grit/pdf_resources.h"
#include "components/pdf/browser/pdf_stream_delegate.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/document_user_data.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h"
#include "extensions/common/api/mime_handler.mojom.h"
#include "extensions/common/constants.h"
#include "net/http/http_response_headers.h"
#include "pdf/pdf_features.h"
#include "printing/buildflags/buildflags.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/color_palette.h"
#include "url/gurl.h"
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
#include "chrome/common/webui_url_constants.h"
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
namespace {
// Determines whether the PDF viewer should use Skia renderer based on the
// user's choice, the enterprise policy and the finch experiment. The priority
// hierarchy is: enterprise policy > user choice > finch experiment.
bool ShouldEnableSkiaRenderer(content::WebContents* contents) {
CHECK(contents);
const PrefService* prefs =
Profile::FromBrowserContext(contents->GetBrowserContext())->GetPrefs();
// When the enterprise policy is set.
if (prefs->IsManagedPreference(prefs::kPdfUseSkiaRendererEnabled)) {
return prefs->GetBoolean(prefs::kPdfUseSkiaRendererEnabled);
}
// When the enterprise policy is not set, use finch/feature flag choice.
return base::FeatureList::IsEnabled(
chrome_pdf::features::kPdfUseSkiaRenderer);
}
// Associates a `pdf::PdfStreamDelegate::StreamInfo` with the PDF extension's
// or Print Preview's `blink::Document`.
// `ChromePdfStreamDelegate::MapToOriginalUrl()` initializes this in
// `PdfNavigationThrottle`, and then `ChromePdfStreamDelegate::GetStreamInfo()`
// returns the stashed result to `PdfURLLoaderRequestInterceptor`.
class StreamInfoHelper : public content::DocumentUserData<StreamInfoHelper> {
public:
std::optional<pdf::PdfStreamDelegate::StreamInfo> TakeStreamInfo() {
return std::move(stream_info_);
}
private:
friend class content::DocumentUserData<StreamInfoHelper>;
DOCUMENT_USER_DATA_KEY_DECL();
StreamInfoHelper(content::RenderFrameHost* embedder_frame,
pdf::PdfStreamDelegate::StreamInfo stream_info)
: content::DocumentUserData<StreamInfoHelper>(embedder_frame),
stream_info_(std::move(stream_info)) {}
std::optional<pdf::PdfStreamDelegate::StreamInfo> stream_info_;
};
DOCUMENT_USER_DATA_KEY_IMPL(StreamInfoHelper);
} // namespace
ChromePdfStreamDelegate::ChromePdfStreamDelegate() = default;
ChromePdfStreamDelegate::~ChromePdfStreamDelegate() = default;
std::optional<GURL> ChromePdfStreamDelegate::MapToOriginalUrl(
content::NavigationHandle& navigation_handle) {
// The embedder frame's `Document` is used to store `StreamInfoHelper`.
content::RenderFrameHost* embedder_frame = navigation_handle.GetParentFrame();
StreamInfoHelper* helper =
StreamInfoHelper::GetForCurrentDocument(embedder_frame);
if (helper) {
// PDF viewer and Print Preview only do this once per `blink::Document`.
return std::nullopt;
}
GURL original_url;
StreamInfo info;
content::WebContents* contents = navigation_handle.GetWebContents();
base::WeakPtr<extensions::StreamContainer> stream;
content::RenderFrameHost* embedder_parent_frame = embedder_frame->GetParent();
if (chrome_pdf::features::IsOopifPdfEnabled()) {
if (embedder_parent_frame) {
// For the PDF viewer, the `embedder_frame` is the PDF extension frame.
// The `StreamContainer` is stored using the PDF viewer's embedder frame,
// which is the parent of the extension frame.
auto* pdf_viewer_stream_manager =
pdf::PdfViewerStreamManager::FromWebContents(contents);
if (pdf_viewer_stream_manager) {
stream = pdf_viewer_stream_manager->GetStreamContainer(
embedder_parent_frame);
}
}
} else {
extensions::MimeHandlerViewGuest* guest =
extensions::MimeHandlerViewGuest::FromNavigationHandle(
&navigation_handle);
if (guest) {
stream = guest->GetStreamWeakPtr();
}
}
const GURL& stream_url = navigation_handle.GetURL();
if (stream) {
if (stream->extension_id() != extension_misc::kPdfExtensionId ||
stream->stream_url() != stream_url ||
!stream->pdf_plugin_attributes()) {
return std::nullopt;
}
CHECK_EQ(embedder_frame->GetLastCommittedURL().host(),
extension_misc::kPdfExtensionId);
original_url = stream->original_url();
info.background_color = base::checked_cast<SkColor>(
stream->pdf_plugin_attributes()->background_color);
info.full_frame = !stream->embedded();
info.allow_javascript = stream->pdf_plugin_attributes()->allow_javascript;
info.use_skia = ShouldEnableSkiaRenderer(contents);
if (chrome_pdf::features::IsOopifPdfEnabled()) {
net::HttpResponseHeaders* response_headers = stream->response_headers();
if (response_headers) {
std::optional<std::string> coep_header =
response_headers->GetNormalizedHeader(
"Cross-Origin-Embedder-Policy");
if (coep_header.has_value()) {
info.coep_header = coep_header.value();
}
}
}
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
} else if (stream_url.GetWithEmptyPath() ==
chrome::kChromeUIUntrustedPrintURL) {
CHECK_EQ(embedder_frame->GetLastCommittedURL().host(),
chrome::kChromeUIPrintHost);
// Print Preview doesn't have access to `chrome.mimeHandlerPrivate`, so just
// use values that match those set by `PDFViewerPPElement`.
original_url = stream_url;
info.background_color = gfx::kGoogleGrey300;
info.full_frame = false;
info.allow_javascript = false;
info.use_skia = ShouldEnableSkiaRenderer(contents);
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
} else {
return std::nullopt;
}
static const base::NoDestructor<std::string> injected_script(
ui::ResourceBundle::GetSharedInstance().LoadDataResourceString(
IDR_PDF_PDF_INTERNAL_PLUGIN_WRAPPER_ROLLUP_JS));
info.stream_url = stream_url;
info.original_url = original_url;
info.injected_script = injected_script.get();
StreamInfoHelper::CreateForCurrentDocument(embedder_frame, std::move(info));
return original_url;
}
std::optional<pdf::PdfStreamDelegate::StreamInfo>
ChromePdfStreamDelegate::GetStreamInfo(
content::RenderFrameHost* embedder_frame) {
if (!embedder_frame) {
return std::nullopt;
}
StreamInfoHelper* helper =
StreamInfoHelper::GetForCurrentDocument(embedder_frame);
if (!helper) {
return std::nullopt;
}
// Only the call immediately following `MapToOriginalUrl()` requires a valid
// `StreamInfo`; subsequent calls should just get nothing.
return helper->TakeStreamInfo();
}
void ChromePdfStreamDelegate::OnPdfEmbedderSandboxed(
content::FrameTreeNodeId frame_tree_node_id) {
// Clean up the stream for a sandboxed embedder frame, as sandboxed frames
// should be unable to instantiate the PDF viewer.
CHECK(chrome_pdf::features::IsOopifPdfEnabled());
auto* web_contents =
content::WebContents::FromFrameTreeNodeId(frame_tree_node_id);
if (!web_contents) {
return;
}
auto* pdf_viewer_stream_manager =
pdf::PdfViewerStreamManager::FromWebContents(web_contents);
if (!pdf_viewer_stream_manager) {
return;
}
// The stream should always be unclaimed, since the navigation hasn't
// committed.
pdf_viewer_stream_manager->DeleteUnclaimedStreamInfo(frame_tree_node_id);
}
bool ChromePdfStreamDelegate::ShouldAllowPdfExtensionFrameNavigation(
content::NavigationHandle* navigation_handle) {
// If PdfOopif is enabled, or if this is an about:blank navigation, allow it.
if (chrome_pdf::features::IsOopifPdfEnabled() ||
navigation_handle->GetURL().IsAboutBlank()) {
return true;
}
// Verify this is a guest, otherwise allow the navigation to proceed.
auto* guest =
extensions::MimeHandlerViewGuest::FromNavigationHandle(navigation_handle);
if (!guest) {
return true;
}
// Since this is the PDF delegate, don't suppress navigations for other stream
// types (should they exist).
base::WeakPtr<extensions::StreamContainer> stream = guest->GetStreamWeakPtr();
if (!stream || stream->extension_id() != extension_misc::kPdfExtensionId) {
return true;
}
return url::IsSameOriginWith(stream->handler_url(),
navigation_handle->GetURL());
}
bool ChromePdfStreamDelegate::ShouldAllowPdfFrameNavigation(
content::NavigationHandle* navigation_handle) {
// Blocks any non-setup navigations in the PDF extension frame and the PDF
// content frame.
// OOPIF PDF viewer only.
if (!chrome_pdf::features::IsOopifPdfEnabled()) {
return true;
}
auto* pdf_viewer_stream_manager =
pdf::PdfViewerStreamManager::FromWebContents(
navigation_handle->GetWebContents());
if (!pdf_viewer_stream_manager) {
return true;
}
// The parent frame should always exist after main frame navigations are
// filtered out in `PdfNavigationThrottle::WillStartRequest()`. The
// parent frame could be the PDF extension frame, PDF embedder frame, or an
// unrelated frame.
content::RenderFrameHost* parent_frame = navigation_handle->GetParentFrame();
CHECK(parent_frame);
const GURL& url = navigation_handle->GetURL();
// If `parent_frame` is the PDF embedder frame and thus has an
// `extensions::StreamContainer`, then the current frame could be the PDF
// extension frame.
base::WeakPtr<extensions::StreamContainer> stream =
pdf_viewer_stream_manager->GetStreamContainer(parent_frame);
content::FrameTreeNodeId frame_tree_node_id =
navigation_handle->GetFrameTreeNodeId();
if (stream) {
// Allow navigations for unrelated frames, which might be injected by
// unrelated extensions. Only allow the PDF extension frame to navigate to
// the extension URL once.
return !pdf_viewer_stream_manager->IsPdfExtensionFrameTreeNodeId(
parent_frame, frame_tree_node_id) ||
(!pdf_viewer_stream_manager->DidPdfExtensionFinishNavigation(
parent_frame) &&
url == stream->handler_url());
}
// If this navigation is for a PDF content frame, then there should be a
// grandparent frame (the PDF embedder frame) with a stream container. If this
// navigation is unrelated to PDFs, then there may or may not be a grandparent
// frame, and there will not be a stream container. In that case, the
// navigation should not be blocked.
content::RenderFrameHost* grandparent_frame = parent_frame->GetParent();
if (!grandparent_frame) {
return true;
}
stream = pdf_viewer_stream_manager->GetStreamContainer(grandparent_frame);
if (!stream) {
return true;
}
// Allow navigations for unrelated frames, which might be injected by
// unrelated extensions. Only allow the PDF content frame to navigate to the
// original PDF URL once.
return !pdf_viewer_stream_manager->IsPdfContentFrameTreeNodeId(
grandparent_frame, frame_tree_node_id) ||
(!pdf_viewer_stream_manager->DidPdfContentNavigate(
grandparent_frame) &&
url == stream->original_url());
}
|