Skip to content
Permalink
Browse files

feat: enable pdf viewer (#21794)

  • Loading branch information
nornagon committed Feb 13, 2020
1 parent 85f9c77 commit d590f2efe3abaeda909885855c2b901345284b55
Showing with 1,579 additions and 210 deletions.
  1. +11 −3 BUILD.gn
  2. +1 −1 buildflags/buildflags.gni
  3. +6 −0 chromium_src/BUILD.gn
  4. +5 −1 electron_paks.gni
  5. +2 −1 electron_resources.grd
  6. +16 −8 filenames.gni
  7. +2 −0 patches/chromium/.patches
  8. +24 −0 patches/chromium/hack_plugin_response_interceptor_to_point_to_electron.patch
  9. +22 −0 patches/chromium/use_electron_resources_in_pdf_util.patch
  10. +22 −5 shell/app/electron_content_client.cc
  11. +0 −9 shell/app/electron_main_delegate.cc
  12. +2 −1 shell/browser/api/electron_api_session.cc
  13. +174 −8 shell/browser/electron_browser_client.cc
  14. +12 −0 shell/browser/electron_browser_client.h
  15. +35 −0 shell/browser/extensions/api/BUILD.gn
  16. +113 −0 shell/browser/extensions/api/resources_private/resources_private_api.cc
  17. +31 −0 shell/browser/extensions/api/resources_private/resources_private_api.h
  18. +74 −0 shell/browser/extensions/api/streams_private/streams_private_api.cc
  19. +44 −0 shell/browser/extensions/api/streams_private/streams_private_api.h
  20. +180 −0 shell/browser/extensions/api/tabs/tabs_api.cc
  21. +42 −0 shell/browser/extensions/api/tabs/tabs_api.h
  22. +0 −3 shell/browser/extensions/electron_browser_context_keyed_service_factories.cc
  23. +89 −0 shell/browser/extensions/electron_component_extension_resource_manager.cc
  24. +50 −0 shell/browser/extensions/electron_component_extension_resource_manager.h
  25. +2 −0 shell/browser/extensions/electron_extension_loader.h
  26. +33 −0 shell/browser/extensions/electron_extension_system.cc
  27. +2 −0 shell/browser/extensions/electron_extension_system.h
  28. +28 −0 shell/browser/extensions/electron_extensions_api_client.cc
  29. +3 −0 shell/browser/extensions/electron_extensions_api_client.h
  30. +1 −3 shell/browser/extensions/electron_extensions_browser_api_provider.cc
  31. +39 −5 shell/browser/extensions/electron_extensions_browser_client.cc
  32. +4 −0 shell/browser/extensions/electron_extensions_browser_client.h
  33. +63 −0 shell/browser/plugins/plugin_utils.cc
  34. +34 −0 shell/browser/plugins/plugin_utils.h
  35. +3 −4 shell/browser/ui/inspectable_web_contents_impl.cc
  36. +2 −5 shell/common/electron_constants.cc
  37. +2 −6 shell/common/electron_constants.h
  38. +13 −1 shell/common/extensions/api/BUILD.gn
  39. +17 −1 shell/common/extensions/api/_api_features.json
  40. +9 −0 shell/common/extensions/api/_permission_features.json
  41. +23 −0 shell/common/extensions/api/resources_private.idl
  42. +256 −0 shell/common/extensions/api/tabs.json
  43. +5 −29 shell/common/extensions/electron_extensions_api_provider.cc
  44. +0 −1 shell/common/extensions/electron_extensions_client.cc
  45. +55 −0 shell/renderer/renderer_client_base.cc
  46. +10 −0 shell/renderer/renderer_client_base.h
  47. +12 −104 spec-main/chromium-spec.ts
  48. BIN {spec/fixtures/assets → spec-main/fixtures}/cat.pdf
  49. +6 −0 spec-main/fixtures/pages/pdf-in-iframe.html
  50. +0 −6 spec/fixtures/pages/pdf-in-iframe.html
  51. +0 −5 spec/fixtures/pages/pdf-in-nested-iframe.html
@@ -629,8 +629,6 @@ source_set("electron_lib") {
deps += [ "//components/printing/common:mojo_interfaces" ]
}

deps += [ "shell/common/extensions/api:extensions_features" ]
deps += [ "shell/common/extensions/api" ]
deps += [
"//components/pref_registry",
"//components/user_prefs",
@@ -642,12 +640,22 @@ source_set("electron_lib") {
]
if (enable_electron_extensions) {
sources += filenames.lib_sources_extensions
deps += [
"shell/browser/extensions/api:api_registration",
"shell/common/extensions/api",
"shell/common/extensions/api:extensions_features",
"//chrome/browser/resources:component_extension_resources",
"//components/zoom",
]
}

if (enable_pdf) {
# Printing depends on some //pdf code, so it needs to be built even if the
# pdf viewer isn't enabled.
deps += [ "//pdf" ]
deps += [
"//pdf",
"//pdf:features",
]
}
if (enable_pdf_viewer) {
deps += [
@@ -14,7 +14,7 @@ declare_args() {

enable_view_api = false

enable_pdf_viewer = false
enable_pdf_viewer = true

enable_tts = true

@@ -232,6 +232,12 @@ static_library("chrome") {

if (enable_electron_extensions) {
sources += [
"//chrome/browser/extensions/chrome_url_request_util.cc",
"//chrome/browser/extensions/chrome_url_request_util.h",
"//chrome/browser/pdf/pdf_extension_util.cc",
"//chrome/browser/pdf/pdf_extension_util.h",
"//chrome/browser/plugins/plugin_response_interceptor_url_loader_throttle.cc",
"//chrome/browser/plugins/plugin_response_interceptor_url_loader_throttle.h",
"//chrome/renderer/extensions/extension_hooks_delegate.cc",
"//chrome/renderer/extensions/extension_hooks_delegate.h",
"//chrome/renderer/extensions/tabs_hooks_delegate.cc",
@@ -92,10 +92,14 @@ template("electron_extra_paks") {
}
if (enable_electron_extensions) {
sources += [
"$root_gen_dir/chrome/component_extension_resources.pak",
"$root_gen_dir/extensions/extensions_renderer_resources.pak",
"$root_gen_dir/extensions/extensions_resources.pak",
]
deps += [ "//extensions:extensions_resources" ]
deps += [
"//chrome/browser/resources:component_extension_resources",
"//extensions:extensions_resources",
]
}
}
}
@@ -17,7 +17,8 @@
<part file="electron_strings.grdp" />
</messages>
<includes>
<include name="IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE" file="${target_gen_dir}\shell_devtools_discovery_page.html" use_base_dir="false" type="BINDATA" />
<include name="IDR_CONTENT_SHELL_DEVTOOLS_DISCOVERY_PAGE" file="${target_gen_dir}/shell_devtools_discovery_page.html" use_base_dir="false" type="BINDATA" />
<include name="IDR_PDF_MANIFEST" file="../chrome/browser/resources/pdf/manifest.json" type="BINDATA" />
</includes>
</release>
</grit>
@@ -279,6 +279,8 @@ filenames = {
"shell/browser/notifications/win/win32_notification.h",
"shell/browser/notifications/win/windows_toast_notification.cc",
"shell/browser/notifications/win/windows_toast_notification.h",
"shell/browser/plugins/plugin_utils.cc",
"shell/browser/plugins/plugin_utils.h",
"shell/browser/pref_store_delegate.cc",
"shell/browser/pref_store_delegate.h",
"shell/browser/relauncher.cc",
@@ -594,14 +596,18 @@ filenames = {
]

lib_sources_extensions = [
"shell/browser/extensions/api/resources_private/resources_private_api.cc",
"shell/browser/extensions/api/resources_private/resources_private_api.h",
"shell/browser/extensions/api/runtime/electron_runtime_api_delegate.cc",
"shell/browser/extensions/api/runtime/electron_runtime_api_delegate.h",
"shell/browser/extensions/api/tabs/tabs_api.cc",
"shell/browser/extensions/api/tabs/tabs_api.h",
"shell/browser/extensions/electron_extensions_browser_client.cc",
"shell/browser/extensions/electron_extensions_browser_client.h",
"shell/browser/extensions/api/streams_private/streams_private_api.cc",
"shell/browser/extensions/api/streams_private/streams_private_api.h",
"shell/browser/extensions/electron_browser_context_keyed_service_factories.cc",
"shell/browser/extensions/electron_browser_context_keyed_service_factories.h",
"shell/browser/extensions/electron_component_extension_resource_manager.cc",
"shell/browser/extensions/electron_component_extension_resource_manager.h",
"shell/browser/extensions/electron_display_info_provider.cc",
"shell/browser/extensions/electron_display_info_provider.h",
"shell/browser/extensions/electron_extension_host_delegate.cc",
@@ -614,24 +620,26 @@ filenames = {
"shell/browser/extensions/electron_extension_system_factory.h",
"shell/browser/extensions/electron_extension_web_contents_observer.cc",
"shell/browser/extensions/electron_extension_web_contents_observer.h",
"shell/browser/extensions/electron_navigation_ui_data.cc",
"shell/browser/extensions/electron_navigation_ui_data.h",
"shell/browser/extensions/electron_process_manager_delegate.cc",
"shell/browser/extensions/electron_process_manager_delegate.h",
"shell/browser/extensions/electron_extensions_api_client.cc",
"shell/browser/extensions/electron_extensions_api_client.h",
"shell/browser/extensions/electron_extensions_browser_api_provider.cc",
"shell/browser/extensions/electron_extensions_browser_api_provider.h",
"shell/browser/extensions/electron_extensions_browser_client.cc",
"shell/browser/extensions/electron_extensions_browser_client.h",
"shell/browser/extensions/electron_messaging_delegate.cc",
"shell/browser/extensions/electron_messaging_delegate.h",
"shell/browser/extensions/electron_navigation_ui_data.cc",
"shell/browser/extensions/electron_navigation_ui_data.h",
"shell/browser/extensions/electron_process_manager_delegate.cc",
"shell/browser/extensions/electron_process_manager_delegate.h",
"shell/common/extensions/electron_extensions_api_provider.cc",
"shell/common/extensions/electron_extensions_api_provider.h",
"shell/common/extensions/electron_extensions_client.cc",
"shell/common/extensions/electron_extensions_client.h",
"shell/renderer/extensions/electron_extensions_renderer_client.cc",
"shell/renderer/extensions/electron_extensions_renderer_client.h",
"shell/renderer/extensions/electron_extensions_dispatcher_delegate.cc",
"shell/renderer/extensions/electron_extensions_dispatcher_delegate.h",
"shell/renderer/extensions/electron_extensions_renderer_client.cc",
"shell/renderer/extensions/electron_extensions_renderer_client.h",
]

app_sources = [
@@ -82,3 +82,5 @@ accessible_pane_view.patch
fixme_grit_conflicts.patch
fix_use_the_new_mediaplaypause_key_listener_for_internal_chrome.patch
fix_use_native_window_button_positions_when_macos_locale_is_rtl.patch
use_electron_resources_in_pdf_util.patch
hack_plugin_response_interceptor_to_point_to_electron.patch
@@ -0,0 +1,24 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jeremy Apthorp <nornagon@nornagon.net>
Date: Mon, 10 Feb 2020 11:52:23 -0800
Subject: hack plugin response interceptor to point to electron

chrome's streams_private_api does prerender and other things and would
require a largeish patch to get working, so just redirect it to our
implementation instead.

diff --git a/chrome/browser/plugins/plugin_response_interceptor_url_loader_throttle.cc b/chrome/browser/plugins/plugin_response_interceptor_url_loader_throttle.cc
index 1c9aeb8fa71d054159c2ccccccbe57aa474d417e..0bd6412f2c79f77f936720b1a0795b50ecefd26c 100644
--- a/chrome/browser/plugins/plugin_response_interceptor_url_loader_throttle.cc
+++ b/chrome/browser/plugins/plugin_response_interceptor_url_loader_throttle.cc
@@ -8,8 +8,8 @@
#include "base/feature_list.h"
#include "base/guid.h"
#include "base/task/post_task.h"
-#include "chrome/browser/extensions/api/streams_private/streams_private_api.h"
-#include "chrome/browser/plugins/plugin_utils.h"
+#include "electron/shell/browser/extensions/api/streams_private/streams_private_api.h"
+#include "electron/shell/browser/plugins/plugin_utils.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_utils.h"
@@ -0,0 +1,22 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jeremy Apthorp <nornagon@nornagon.net>
Date: Mon, 10 Feb 2020 10:37:48 -0800
Subject: use electron resources in pdf_util

Without this, the ID for IDR_PDF_MANIFEST will be wrong on linux
and cause a DCHECK(), since the resource will be loaded as an empty
string.

diff --git a/chrome/browser/pdf/pdf_extension_util.cc b/chrome/browser/pdf/pdf_extension_util.cc
index 877d0ba2f48359243527ddebf1d91132b3d5c455..cd69de498c4968ade0ffabf45b72d4d63b70b10f 100644
--- a/chrome/browser/pdf/pdf_extension_util.cc
+++ b/chrome/browser/pdf/pdf_extension_util.cc
@@ -6,7 +6,7 @@

#include "base/strings/string_util.h"
#include "chrome/common/chrome_content_client.h"
-#include "chrome/grit/browser_resources.h"
+#include "electron/grit/electron_resources.h"
#include "ui/base/resource/resource_bundle.h"

namespace pdf_extension_util {
@@ -33,12 +33,13 @@
#endif // defined(WIDEVINE_CDM_AVAILABLE)

#if BUILDFLAG(ENABLE_PDF_VIEWER)
#include "pdf/pdf.h" // nogncheck
#include "pdf/pdf_ppapi.h" // nogncheck
#include "shell/common/electron_constants.h" // nogncheck
#endif // BUILDFLAG(ENABLE_PDF_VIEWER)
#include "pdf/pdf.h" // nogncheck
#include "pdf/pdf_ppapi.h" // nogncheck
#include "shell/common/electron_constants.h"
#endif // BUILDFLAG(ENABLE_PDF_VIEWER)

#if BUILDFLAG(ENABLE_PLUGINS)
#include "content/public/browser/plugin_service.h"
#include "content/public/common/pepper_plugin_info.h"
#include "ppapi/shared_impl/ppapi_permissions.h"
#endif // BUILDFLAG(ENABLE_PLUGINS)
@@ -155,7 +156,8 @@ void ComputeBuiltInPlugins(std::vector<content::PepperPluginInfo>* plugins) {
pdf_info.is_out_of_process = true;
pdf_info.name = "Chromium PDF Viewer";
pdf_info.description = "Portable Document Format";
pdf_info.path = base::FilePath(FILE_PATH_LITERAL("internal-pdf-viewer"));
// 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",
"Portable Document Format");
pdf_info.mime_types.push_back(pdf_mime_type);
@@ -166,6 +168,21 @@ void ComputeBuiltInPlugins(std::vector<content::PepperPluginInfo>* plugins) {
chrome_pdf::PPP_ShutdownModule;
pdf_info.permissions = ppapi::PERMISSION_PDF | ppapi::PERMISSION_DEV;
plugins->push_back(pdf_info);

// NB. in Chrome, this plugin isn't registered until the PDF extension is
// loaded. However, in Electron, we load the PDF extension unconditionally
// when it is enabled in the build, so we're OK to load the plugin eagerly
// here.
content::WebPluginInfo info;
info.type = content::WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN;
info.name = base::UTF8ToUTF16("Chromium PDF Viewer");
// 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");
content::PluginService::GetInstance()->RefreshPlugins();
content::PluginService::GetInstance()->RegisterInternalPlugin(info, true);
#endif // BUILDFLAG(ENABLE_PDF_VIEWER)
}
#endif // BUILDFLAG(ENABLE_PLUGINS)
@@ -118,15 +118,6 @@ void LoadResourceBundle(const std::string& locale) {
bundle.ReloadLocaleResources(locale);
bundle.AddDataPackFromPath(pak_dir.Append(FILE_PATH_LITERAL("resources.pak")),
ui::SCALE_FACTOR_NONE);
#if BUILDFLAG(ENABLE_PDF_VIEWER)
NOTIMPLEMENTED()
<< "Hi, whoever's fixing PDF support! Thanks! The pdf "
"viewer resources haven't been ported over to the GN build yet, so "
"you'll probably need to change this bit of code.";
bundle.AddDataPackFromPath(
pak_dir.Append(FILE_PATH_LITERAL("pdf_viewer_resources.pak")),
ui::GetSupportedScaleFactors()[0]);
#endif // BUILDFLAG(ENABLE_PDF_VIEWER)
}

ElectronMainDelegate::ElectronMainDelegate() = default;
@@ -702,7 +702,8 @@ v8::Local<v8::Value> Session::GetAllExtensions() {
auto installed_extensions = registry->GenerateInstalledExtensionsSet();
std::vector<const extensions::Extension*> extensions_vector;
for (const auto& extension : *installed_extensions) {
extensions_vector.emplace_back(extension.get());
if (extension->location() != extensions::Manifest::COMPONENT)
extensions_vector.emplace_back(extension.get());
}
return gin::ConvertToV8(isolate(), extensions_vector);
}

0 comments on commit d590f2e

Please sign in to comment.
You can’t perform that action at this time.