Skip to content

Commit

Permalink
fix: build when pdf component is disabled (#33513)
Browse files Browse the repository at this point in the history
  • Loading branch information
deepak1556 committed Mar 30, 2022
1 parent b711860 commit afe0116
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 60 deletions.
6 changes: 2 additions & 4 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,6 @@ source_set("electron_lib") {
"//components/network_session_configurator/common",
"//components/omnibox/browser:buildflags",
"//components/os_crypt",
"//components/pdf/browser",
"//components/pdf/browser:interceptors",
"//components/pdf/common",
"//components/pdf/renderer",
"//components/pref_registry",
"//components/prefs",
"//components/security_state/content",
Expand Down Expand Up @@ -700,6 +696,8 @@ source_set("electron_lib") {
deps += [
"//chrome/browser/resources/pdf:resources",
"//components/pdf/browser",
"//components/pdf/browser:interceptors",
"//components/pdf/common",
"//components/pdf/renderer",
"//pdf",
]
Expand Down
19 changes: 7 additions & 12 deletions chromium_src/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ static_library("chrome") {
"//chrome/browser/plugins/pdf_iframe_navigation_throttle.cc",
"//chrome/browser/plugins/pdf_iframe_navigation_throttle.h",
]
deps += [
"//components/pdf/browser",
"//components/pdf/renderer",
]
}
}

Expand Down Expand Up @@ -334,15 +338,6 @@ source_set("plugins") {
"//chrome/browser/renderer_host/pepper/pepper_isolated_file_system_message_filter.cc",
"//chrome/browser/renderer_host/pepper/pepper_isolated_file_system_message_filter.h",
]
deps += [
"//media:media_buildflags",
"//ppapi/buildflags",
"//ppapi/proxy:ipc",
"//services/device/public/mojom",
]
if (enable_pdf_viewer) {
deps += [ "//components/pdf/browser" ]
}

# renderer side
sources += [
Expand All @@ -351,16 +346,16 @@ source_set("plugins") {
"//chrome/renderer/pepper/pepper_shared_memory_message_filter.cc",
"//chrome/renderer/pepper/pepper_shared_memory_message_filter.h",
]
if (enable_pdf_viewer) {
deps += [ "//components/pdf/renderer" ]
}

deps += [
"//components/strings",
"//media:media_buildflags",
"//ppapi/buildflags",
"//ppapi/host",
"//ppapi/proxy",
"//ppapi/proxy:ipc",
"//ppapi/shared_impl",
"//services/device/public/mojom",
"//skia",
]
}
Expand Down
12 changes: 6 additions & 6 deletions shell/app/electron_content_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
#endif // defined(WIDEVINE_CDM_AVAILABLE)

#if BUILDFLAG(ENABLE_PDF_VIEWER)
#include "components/pdf/renderer/internal_plugin_renderer_helpers.h"
#include "chrome/common/pdf_util.h"
#include "components/pdf/common/internal_plugin_helpers.h"
#include "pdf/pdf.h" // nogncheck
#include "shell/common/electron_constants.h"
#endif // BUILDFLAG(ENABLE_PDF_VIEWER)
Expand Down Expand Up @@ -111,11 +112,11 @@ void ComputeBuiltInPlugins(std::vector<content::PepperPluginInfo>* plugins) {
content::PepperPluginInfo pdf_info;
pdf_info.is_internal = true;
pdf_info.is_out_of_process = true;
pdf_info.name = "Chromium PDF Viewer";
pdf_info.name = kPDFInternalPluginName;
pdf_info.description = "Portable Document Format";
// This isn't a real file path; it's just used as a unique identifier.
pdf_info.path = base::FilePath(kPdfPluginPath);
content::WebPluginMimeType pdf_mime_type(kPdfPluginMimeType, "pdf",
content::WebPluginMimeType pdf_mime_type(pdf::kInternalPluginMimeType, "pdf",
"Portable Document Format");
pdf_info.mime_types.push_back(pdf_mime_type);
plugins->push_back(pdf_info);
Expand All @@ -126,12 +127,11 @@ void ComputeBuiltInPlugins(std::vector<content::PepperPluginInfo>* plugins) {
// here.
content::WebPluginInfo info;
info.type = content::WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN;
info.name = u"Chromium PDF Viewer";
info.name = base::ASCIIToUTF16(kPDFExtensionPluginName);
// This isn't a real file path; it's just used as a unique identifier.
info.path = base::FilePath::FromUTF8Unsafe(extension_misc::kPdfExtensionId);
info.background_color = content::WebPluginInfo::kDefaultBackgroundColor;
info.mime_types.emplace_back("application/pdf", "pdf",
"Portable Document Format");
info.mime_types.emplace_back(kPDFMimeType, "pdf", "Portable Document Format");
content::PluginService::GetInstance()->RefreshPlugins();
content::PluginService::GetInstance()->RegisterInternalPlugin(info, true);
#endif // BUILDFLAG(ENABLE_PDF_VIEWER)
Expand Down
1 change: 1 addition & 0 deletions shell/browser/electron_api_ipc_handler_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/memory/weak_ptr.h"
#include "content/public/browser/web_contents_observer.h"
#include "electron/shell/common/api/api.mojom.h"
#include "mojo/public/cpp/bindings/associated_receiver.h"
#include "shell/browser/api/electron_api_web_contents.h"

namespace content {
Expand Down
11 changes: 6 additions & 5 deletions shell/browser/electron_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,11 @@
#include "base/strings/utf_string_conversions.h"
#include "base/task/post_task.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/pdf/chrome_pdf_stream_delegate.h"
#include "chrome/browser/plugins/pdf_iframe_navigation_throttle.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/chrome_version.h"
#include "components/net_log/chrome_net_log.h"
#include "components/network_hints/common/network_hints.mojom.h"
#include "components/pdf/browser/pdf_navigation_throttle.h"
#include "components/pdf/browser/pdf_url_loader_request_interceptor.h"
#include "components/pdf/common/internal_plugin_helpers.h"
#include "content/browser/keyboard_lock/keyboard_lock_service_impl.h" // nogncheck
#include "content/browser/site_instance_impl.h" // nogncheck
#include "content/public/browser/browser_main_runner.h"
Expand All @@ -52,6 +47,7 @@
#include "content/public/browser/site_instance.h"
#include "content/public/browser/tts_controller.h"
#include "content/public/browser/tts_platform.h"
#include "content/public/browser/url_loader_request_interceptor.h"
#include "content/public/common/content_descriptors.h"
#include "content/public/common/content_paths.h"
#include "content/public/common/content_switches.h"
Expand Down Expand Up @@ -212,7 +208,12 @@
#endif

#if BUILDFLAG(ENABLE_PDF_VIEWER)
#include "chrome/browser/pdf/chrome_pdf_stream_delegate.h"
#include "chrome/browser/plugins/pdf_iframe_navigation_throttle.h" // nogncheck
#include "components/pdf/browser/pdf_navigation_throttle.h"
#include "components/pdf/browser/pdf_url_loader_request_interceptor.h"
#include "components/pdf/browser/pdf_web_contents_helper.h" // nogncheck
#include "components/pdf/common/internal_plugin_helpers.h"
#endif

using content::BrowserThread;
Expand Down
1 change: 1 addition & 0 deletions shell/browser/electron_web_contents_utility_handler_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/memory/weak_ptr.h"
#include "content/public/browser/web_contents_observer.h"
#include "electron/shell/common/api/api.mojom.h"
#include "mojo/public/cpp/bindings/associated_receiver.h"
#include "shell/browser/api/electron_api_web_contents.h"

namespace content {
Expand Down
3 changes: 2 additions & 1 deletion shell/common/electron_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const char kRunAsNode[] = "ELECTRON_RUN_AS_NODE";
#endif

#if BUILDFLAG(ENABLE_PDF_VIEWER)
const char kPdfPluginMimeType[] = "application/x-google-chrome-pdf";
const char kPDFExtensionPluginName[] = "Chromium PDF Viewer";
const char kPDFInternalPluginName[] = "Chromium PDF Plugin";
const base::FilePath::CharType kPdfPluginPath[] =
FILE_PATH_LITERAL("internal-pdf-viewer");
#endif // BUILDFLAG(ENABLE_PDF_VIEWER)
Expand Down
4 changes: 2 additions & 2 deletions shell/common/electron_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ extern const char kRunAsNode[];
#endif

#if BUILDFLAG(ENABLE_PDF_VIEWER)
// The MIME type used for the PDF plugin.
extern const char kPdfPluginMimeType[];
extern const char kPDFExtensionPluginName[];
extern const char kPDFInternalPluginName[];
extern const base::FilePath::CharType kPdfPluginPath[];
#endif // BUILDFLAG(ENABLE_PDF_VIEWER)

Expand Down
61 changes: 31 additions & 30 deletions shell/renderer/renderer_client_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
#include "base/command_line.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "chrome/common/pdf_util.h"
#include "components/network_hints/renderer/web_prescient_networking_impl.h"
#include "components/pdf/renderer/pdf_internal_plugin_delegate.h"
#include "content/common/buildflags.h"
#include "content/public/common/content_constants.h"
#include "content/public/common/content_switches.h"
Expand Down Expand Up @@ -68,6 +66,9 @@
#endif

#if BUILDFLAG(ENABLE_PDF_VIEWER)
#include "chrome/common/pdf_util.h"
#include "components/pdf/common/internal_plugin_helpers.h"
#include "components/pdf/renderer/pdf_internal_plugin_delegate.h"
#include "shell/common/electron_constants.h"
#endif // BUILDFLAG(ENABLE_PDF_VIEWER)

Expand Down Expand Up @@ -111,6 +112,25 @@ std::vector<std::string> ParseSchemesCLISwitch(base::CommandLine* command_line,
base::SPLIT_WANT_NONEMPTY);
}

#if BUILDFLAG(ENABLE_PDF_VIEWER)
class ChromePdfInternalPluginDelegate final
: public pdf::PdfInternalPluginDelegate {
public:
ChromePdfInternalPluginDelegate() = default;
ChromePdfInternalPluginDelegate(const ChromePdfInternalPluginDelegate&) =
delete;
ChromePdfInternalPluginDelegate& operator=(
const ChromePdfInternalPluginDelegate&) = delete;
~ChromePdfInternalPluginDelegate() override = default;

// `pdf::PdfInternalPluginDelegate`:
bool IsAllowedOrigin(const url::Origin& origin) const override {
return origin.scheme() == extensions::kExtensionScheme &&
origin.host() == extension_misc::kPdfExtensionId;
}
};
#endif // BUILDFLAG(ENABLE_PDF_VIEWER)

// static
RendererClientBase* g_renderer_client_base = nullptr;

Expand Down Expand Up @@ -311,29 +331,12 @@ void RendererClientBase::DidClearWindowObject(
render_frame->GetWebFrame()->ExecuteScript(blink::WebScriptSource("void 0"));
}

class ChromePdfInternalPluginDelegate final
: public pdf::PdfInternalPluginDelegate {
public:
ChromePdfInternalPluginDelegate() = default;
ChromePdfInternalPluginDelegate(const ChromePdfInternalPluginDelegate&) =
delete;
ChromePdfInternalPluginDelegate& operator=(
const ChromePdfInternalPluginDelegate&) = delete;
~ChromePdfInternalPluginDelegate() override = default;

// `pdf::PdfInternalPluginDelegate`:
bool IsAllowedOrigin(const url::Origin& origin) const override {
return origin.scheme() == extensions::kExtensionScheme &&
origin.host() == extension_misc::kPdfExtensionId;
}
};

bool RendererClientBase::OverrideCreatePlugin(
content::RenderFrame* render_frame,
const blink::WebPluginParams& params,
blink::WebPlugin** plugin) {
#if BUILDFLAG(ENABLE_PDF_VIEWER)
if (params.mime_type.Utf8() == kPdfPluginMimeType) {
if (params.mime_type.Utf8() == pdf::kInternalPluginMimeType) {
*plugin = pdf::CreateInternalPlugin(
std::move(params), render_frame,
std::make_unique<ChromePdfInternalPluginDelegate>());
Expand All @@ -342,7 +345,6 @@ bool RendererClientBase::OverrideCreatePlugin(
#endif // BUILDFLAG(ENABLE_PDF_VIEWER)

if (params.mime_type.Utf8() == content::kBrowserPluginMimeType ||
params.mime_type.Utf8() == kPdfPluginMimeType ||
render_frame->GetBlinkPreferences().enable_plugins)
return false;

Expand Down Expand Up @@ -371,19 +373,18 @@ bool RendererClientBase::IsPluginHandledExternally(
#if BUILDFLAG(ENABLE_PDF_VIEWER)
DCHECK(plugin_element.HasHTMLTagName("object") ||
plugin_element.HasHTMLTagName("embed"));
if (mime_type == "application/x-google-chrome-pdf") {
if (mime_type == pdf::kInternalPluginMimeType) {
if (IsPdfInternalPluginAllowedOrigin(
render_frame->GetWebFrame()->GetSecurityOrigin())) {
return true;
}

content::WebPluginInfo info;
info.type = content::WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS;
const char16_t kPDFExtensionPluginName[] = u"Chromium PDF Viewer";
info.name = kPDFExtensionPluginName;
info.path = base::FilePath::FromUTF8Unsafe("internal-pdf-viewer");
info.name = base::ASCIIToUTF16(kPDFInternalPluginName);
info.path = base::FilePath(kPdfPluginPath);
info.background_color = content::WebPluginInfo::kDefaultBackgroundColor;
info.mime_types.emplace_back("application/x-google-chrome-pdf", "pdf",
info.mime_types.emplace_back(pdf::kInternalPluginMimeType, "pdf",
"Portable Document Format");
return extensions::MimeHandlerViewContainerManager::Get(
content::RenderFrame::FromWebFrame(
Expand All @@ -396,12 +397,10 @@ bool RendererClientBase::IsPluginHandledExternally(
// electron_content_client.cc / ComputeBuiltInPlugins.
content::WebPluginInfo info;
info.type = content::WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN;
const char16_t kPDFExtensionPluginName[] = u"Chromium PDF Viewer";
info.name = kPDFExtensionPluginName;
info.name = base::ASCIIToUTF16(kPDFExtensionPluginName);
info.path = base::FilePath::FromUTF8Unsafe(extension_misc::kPdfExtensionId);
info.background_color = content::WebPluginInfo::kDefaultBackgroundColor;
info.mime_types.emplace_back("application/pdf", "pdf",
"Portable Document Format");
info.mime_types.emplace_back(kPDFMimeType, "pdf", "Portable Document Format");
return extensions::MimeHandlerViewContainerManager::Get(
content::RenderFrame::FromWebFrame(
plugin_element.GetDocument().GetFrame()),
Expand All @@ -415,6 +414,7 @@ bool RendererClientBase::IsPluginHandledExternally(
v8::Local<v8::Object> RendererClientBase::GetScriptableObject(
const blink::WebElement& plugin_element,
v8::Isolate* isolate) {
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
// If there is a MimeHandlerView that can provide the scriptable object then
// MaybeCreateMimeHandlerView must have been called before and a container
// manager should exist.
Expand All @@ -424,6 +424,7 @@ v8::Local<v8::Object> RendererClientBase::GetScriptableObject(
false /* create_if_does_not_exist */);
if (container_manager)
return container_manager->GetScriptableObject(plugin_element, isolate);
#endif
return v8::Local<v8::Object>();
}

Expand Down

0 comments on commit afe0116

Please sign in to comment.