Browse Source

fix: build when pdf component is disabled (#33513)

Robo 3 years ago
parent
commit
afe0116d59

+ 2 - 4
BUILD.gn

@@ -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",
@@ -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",
     ]

+ 7 - 12
chromium_src/BUILD.gn

@@ -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",
+      ]
     }
   }
 
@@ -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 += [
@@ -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",
   ]
 }

+ 6 - 6
shell/app/electron_content_client.cc

@@ -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)
@@ -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);
@@ -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)

+ 1 - 0
shell/browser/electron_api_ipc_handler_impl.h

@@ -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 {

+ 6 - 5
shell/browser/electron_browser_client.cc

@@ -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"
@@ -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"
@@ -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;

+ 1 - 0
shell/browser/electron_web_contents_utility_handler_impl.h

@@ -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 {

+ 2 - 1
shell/common/electron_constants.cc

@@ -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)

+ 2 - 2
shell/common/electron_constants.h

@@ -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)
 

+ 31 - 30
shell/renderer/renderer_client_base.cc

@@ -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"
@@ -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)
 
@@ -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;
 
@@ -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>());
@@ -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;
 
@@ -371,7 +373,7 @@ 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;
@@ -379,11 +381,10 @@ bool RendererClientBase::IsPluginHandledExternally(
 
     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(
@@ -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()),
@@ -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.
@@ -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>();
 }