Bundle pepper PDF plugin on all platforms (issue #1331).

- Add new libpdf.so library on Linux and PDF.plugin app bundle on OS X.
- Move scaled resources from cef.pak into separate cef_100_percent.pak and cef_200_percent.pak files.
- Password-protected PDF files are not currently supported.
- No fallback is provided for PDF files that contain unsupported features.

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@1758 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt
2014-07-07 22:17:33 +00:00
parent 479e69fd4e
commit 81f8883b4a
14 changed files with 315 additions and 34 deletions

View File

@@ -35,6 +35,7 @@
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/strings/utf_string_conversions.h"
#include "content/browser/gpu/compositor_util.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/common/view_messages.h"
@@ -2431,6 +2432,14 @@ bool CefBrowserHostImpl::OnMessageReceived(const IPC::Message& message) {
IPC_MESSAGE_HANDLER(CefHostMsg_Request, OnRequest)
IPC_MESSAGE_HANDLER(CefHostMsg_Response, OnResponse)
IPC_MESSAGE_HANDLER(CefHostMsg_ResponseAck, OnResponseAck)
IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PDFHasUnsupportedFeature,
OnPDFHasUnsupportedFeature)
IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PDFSaveURLAs, OnPDFSaveURLAs)
IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PDFUpdateContentRestrictions,
OnPDFUpdateContentRestrictions)
IPC_MESSAGE_HANDLER_DELAY_REPLY(
ChromeViewHostMsg_PDFModalPromptForPassword,
OnPDFModalPromptForPassword)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
@@ -2525,6 +2534,38 @@ void CefBrowserHostImpl::OnResponseAck(int request_id) {
response_manager_->RunAckHandler(request_id);
}
void CefBrowserHostImpl::OnPDFHasUnsupportedFeature() {
// TODO(cef): Use Adobe PDF plugin instead. See PDFHasUnsupportedFeature in
// chrome/browser/ui/pdf/pdf_unsupported_feature.cc.
}
void CefBrowserHostImpl::OnPDFSaveURLAs(
const GURL& url,
const content::Referrer& referrer) {
web_contents()->SaveFrame(url, referrer);
}
void CefBrowserHostImpl::OnPDFUpdateContentRestrictions(
int content_restrictions) {
// TODO(cef): Add support for communicating PDF content restrictions.
}
void CefBrowserHostImpl::OnPDFModalPromptForPassword(
const std::string& prompt,
IPC::Message* reply_message) {
// TODO(cef): Add support for PDF password prompt.
OnPDFModalPromptForPasswordClosed(reply_message, false, base::string16());
}
void CefBrowserHostImpl::OnPDFModalPromptForPasswordClosed(
IPC::Message* reply_message,
bool success,
const base::string16& actual_value) {
ChromeViewHostMsg_PDFModalPromptForPassword::WriteReplyParams(
reply_message, base::UTF16ToUTF8(actual_value));
Send(reply_message);
}
// content::NotificationObserver methods.
// -----------------------------------------------------------------------------

View File

@@ -331,18 +331,6 @@ class CefBrowserHostImpl : public CefBrowserHost,
// can only be dereferenced on, the UI thread.
base::WeakPtr<CefBrowserHostImpl> GetWeakPtr();
private:
class DevToolsWebContentsObserver;
static CefRefPtr<CefBrowserHostImpl> CreateInternal(
const CefWindowInfo& window_info,
const CefBrowserSettings& settings,
CefRefPtr<CefClient> client,
content::WebContents* web_contents,
scoped_refptr<CefBrowserInfo> browser_info,
CefWindowHandle opener,
CefRefPtr<CefRequestContext> request_context);
// content::WebContentsDelegate methods.
virtual content::WebContents* OpenURLFromTab(
content::WebContents* source,
@@ -443,6 +431,18 @@ class CefBrowserHostImpl : public CefBrowserHost,
// Override to provide a thread safe implementation.
virtual bool Send(IPC::Message* message) OVERRIDE;
private:
class DevToolsWebContentsObserver;
static CefRefPtr<CefBrowserHostImpl> CreateInternal(
const CefWindowInfo& window_info,
const CefBrowserSettings& settings,
CefRefPtr<CefClient> client,
content::WebContents* web_contents,
scoped_refptr<CefBrowserInfo> browser_info,
CefWindowHandle opener,
CefRefPtr<CefRequestContext> request_context);
// content::WebContentsObserver::OnMessageReceived() message handlers.
void OnFrameIdentified(int64 frame_id,
int64 parent_frame_id,
@@ -456,6 +456,16 @@ class CefBrowserHostImpl : public CefBrowserHost,
void OnRequest(const Cef_Request_Params& params);
void OnResponse(const Cef_Response_Params& params);
void OnResponseAck(int request_id);
void OnPDFHasUnsupportedFeature();
void OnPDFSaveURLAs(const GURL& url,
const content::Referrer& referrer);
void OnPDFUpdateContentRestrictions(int content_restrictions);
void OnPDFModalPromptForPassword(const std::string& prompt,
IPC::Message* reply_message);
void OnPDFModalPromptForPasswordClosed(IPC::Message* reply_message,
bool success,
const base::string16& actual_value);
// content::NotificationObserver methods.
virtual void Observe(int type,

View File

@@ -202,6 +202,28 @@ IPC_MESSAGE_ROUTED1(CefHostMsg_ResponseAck,
int /* request_id */)
// Pepper PDF plugin messages excerpted from chrome/common/render_messages.h.
// Including all of render_messages.h would bring in a number of chrome
// dependencies that are better off avoided.
// The currently displayed PDF has an unsupported feature.
IPC_MESSAGE_ROUTED0(ChromeViewHostMsg_PDFHasUnsupportedFeature)
// Brings up SaveAs... dialog to save specified URL.
IPC_MESSAGE_ROUTED2(ChromeViewHostMsg_PDFSaveURLAs,
GURL /* url */,
content::Referrer /* referrer */)
// Updates the content restrictions, i.e. to disable print/copy.
IPC_MESSAGE_ROUTED1(ChromeViewHostMsg_PDFUpdateContentRestrictions,
int /* restrictions */)
// Brings up a Password... dialog for protected documents.
IPC_SYNC_MESSAGE_ROUTED1_1(ChromeViewHostMsg_PDFModalPromptForPassword,
std::string /* prompt */,
std::string /* actual_value */)
// Singly-included section for struct and custom IPC traits.
#ifndef CEF_LIBCEF_COMMON_CEF_MESSAGES_H_
#define CEF_LIBCEF_COMMON_CEF_MESSAGES_H_

View File

@@ -10,18 +10,60 @@
#include "libcef/common/scheme_registration.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/strings/string_piece.h"
#include "base/strings/stringprintf.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/pepper_plugin_info.h"
#include "content/public/common/user_agent.h"
#include "ppapi/shared_impl/ppapi_permissions.h"
#include "ui/base/resource/resource_bundle.h"
namespace {
CefContentClient* g_content_client = NULL;
const char kPDFPluginName[] = "Chrome PDF Viewer";
const char kPDFPluginMimeType[] = "application/pdf";
const char kPDFPluginExtension[] = "pdf";
const char kPDFPluginDescription[] = "Portable Document Format";
const uint32 kPDFPluginPermissions = ppapi::PERMISSION_PRIVATE |
ppapi::PERMISSION_DEV;
// Appends the known built-in plugins to the given vector.
// Based on chrome/common/chrome_content_client.cc.
void ComputeBuiltInPlugins(std::vector<content::PepperPluginInfo>* plugins) {
// PDF.
//
// Once we're sandboxed, we can't know if the PDF plugin is available or not;
// but (on Linux) this function is always called once before we're sandboxed.
// So the first time through test if the file is available and then skip the
// check on subsequent calls if yes.
static bool skip_pdf_file_check = false;
base::FilePath path;
if (PathService::Get(chrome::FILE_PDF_PLUGIN, &path)) {
if (skip_pdf_file_check || base::PathExists(path)) {
content::PepperPluginInfo pdf;
pdf.path = path;
pdf.name = kPDFPluginName;
// Only in-process loading is currently supported. See issue #1331.
pdf.is_out_of_process = false;
content::WebPluginMimeType pdf_mime_type(kPDFPluginMimeType,
kPDFPluginExtension,
kPDFPluginDescription);
pdf.mime_types.push_back(pdf_mime_type);
pdf.permissions = kPDFPluginPermissions;
plugins->push_back(pdf);
skip_pdf_file_check = true;
}
}
}
} // namespace
CefContentClient::CefContentClient(CefRefPtr<CefApp> application)
@@ -42,6 +84,11 @@ CefContentClient* CefContentClient::Get() {
return g_content_client;
}
void CefContentClient::AddPepperPlugins(
std::vector<content::PepperPluginInfo>* plugins) {
ComputeBuiltInPlugins(plugins);
}
void CefContentClient::AddAdditionalSchemes(
std::vector<std::string>* standard_schemes,
std::vector<std::string>* savable_schemes) {

View File

@@ -26,6 +26,8 @@ class CefContentClient : public content::ContentClient,
static CefContentClient* Get();
// content::ContentClient methods.
virtual void AddPepperPlugins(
std::vector<content::PepperPluginInfo>* plugins) OVERRIDE;
virtual void AddAdditionalSchemes(
std::vector<std::string>* standard_schemes,
std::vector<std::string>* savable_schemes) OVERRIDE;

View File

@@ -21,11 +21,13 @@
#include "base/strings/string_util.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "content/public/browser/browser_main_runner.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/main_function_params.h"
#include "ui/base/layout.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_paths.h"
#include "ui/base/ui_base_switches.h"
@@ -47,10 +49,6 @@
#include "components/breakpad/app/breakpad_linux.h"
#endif
#if defined(WIN_PDF_METAFILE_FOR_PRINTING)
#include "chrome/common/chrome_paths.h"
#endif
namespace {
base::LazyInstance<CefBreakpadClient>::Leaky g_shell_breakpad_client =
@@ -119,8 +117,6 @@ base::FilePath GetLibrariesFilePath() {
#endif // !defined(OS_MACOSX)
#if defined(WIN_PDF_METAFILE_FOR_PRINTING)
// File name of the internal PDF plugin on different platforms.
const base::FilePath::CharType kInternalPDFPluginFileName[] =
#if defined(OS_WIN)
@@ -137,7 +133,15 @@ void OverridePdfPluginPath() {
PathService::Override(chrome::FILE_PDF_PLUGIN, plugin_path);
}
#endif // defined(WIN_PDF_METAFILE_FOR_PRINTING)
// Returns true if |scale_factor| is supported by this platform.
// Same as ResourceBundle::IsScaleFactorSupported.
bool IsScaleFactorSupported(ui::ScaleFactor scale_factor) {
const std::vector<ui::ScaleFactor>& supported_scale_factors =
ui::GetSupportedScaleFactors();
return std::find(supported_scale_factors.begin(),
supported_scale_factors.end(),
scale_factor) != supported_scale_factors.end();
}
// Used to run the UI on a separate thread.
class CefUIThread : public base::Thread {
@@ -408,9 +412,7 @@ void CefMainDelegate::PreSandboxStartup() {
}
#endif
#if defined(WIN_PDF_METAFILE_FOR_PRINTING)
OverridePdfPluginPath();
#endif
if (command_line.HasSwitch(switches::kDisablePackLoading))
content_client_.set_pack_loading_disabled(true);
@@ -502,7 +504,8 @@ void CefMainDelegate::ShutdownBrowser() {
void CefMainDelegate::InitializeResourceBundle() {
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
base::FilePath cef_pak_file, devtools_pak_file, locales_dir;
base::FilePath cef_pak_file, cef_100_percent_pak_file,
cef_200_percent_pak_file, devtools_pak_file, locales_dir;
if (!content_client_.pack_loading_disabled()) {
base::FilePath resources_dir;
@@ -515,6 +518,10 @@ void CefMainDelegate::InitializeResourceBundle() {
if (!resources_dir.empty()) {
cef_pak_file = resources_dir.Append(FILE_PATH_LITERAL("cef.pak"));
cef_100_percent_pak_file =
resources_dir.Append(FILE_PATH_LITERAL("cef_100_percent.pak"));
cef_200_percent_pak_file =
resources_dir.Append(FILE_PATH_LITERAL("cef_200_percent.pak"));
devtools_pak_file =
resources_dir.Append(FILE_PATH_LITERAL("devtools_resources.pak"));
}
@@ -532,20 +539,49 @@ void CefMainDelegate::InitializeResourceBundle() {
const std::string loaded_locale =
ui::ResourceBundle::InitSharedInstanceWithLocale(locale,
&content_client_);
ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance();
if (!content_client_.pack_loading_disabled()) {
CHECK(!loaded_locale.empty()) << "Locale could not be found for " << locale;
if (loaded_locale.empty())
LOG(ERROR) << "Could not load locale pak for " << locale;
content_client_.set_allow_pack_file_load(true);
if (base::PathExists(cef_pak_file)) {
ResourceBundle::GetSharedInstance().AddDataPackFromPath(
cef_pak_file, ui::SCALE_FACTOR_NONE);
resource_bundle.AddDataPackFromPath(cef_pak_file, ui::SCALE_FACTOR_NONE);
} else {
NOTREACHED() << "Could not load cef.pak";
LOG(ERROR) << "Could not load cef.pak";
}
// On OS X and Linux/Aura always load the 1x data pack first as the 2x data
// pack contains both 1x and 2x images.
const bool load_100_percent =
#if defined(OS_WIN)
IsScaleFactorSupported(ui::SCALE_FACTOR_100P);
#else
true;
#endif
if (load_100_percent) {
if (base::PathExists(cef_100_percent_pak_file)) {
resource_bundle.AddDataPackFromPath(
cef_100_percent_pak_file, ui::SCALE_FACTOR_100P);
} else {
LOG(ERROR) << "Could not load cef_100_percent.pak";
}
}
if (IsScaleFactorSupported(ui::SCALE_FACTOR_200P)) {
if (base::PathExists(cef_200_percent_pak_file)) {
resource_bundle.AddDataPackFromPath(
cef_200_percent_pak_file, ui::SCALE_FACTOR_200P);
} else {
LOG(ERROR) << "Could not load cef_200_percent.pak";
}
}
if (base::PathExists(devtools_pak_file)) {
ResourceBundle::GetSharedInstance().AddDataPackFromPath(
resource_bundle.AddDataPackFromPath(
devtools_pak_file, ui::SCALE_FACTOR_NONE);
}

View File

@@ -31,10 +31,13 @@ MSVC_POP_WARNING();
#include "libcef/renderer/webkit_glue.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/renderer/loadtimes_extension_bindings.h"
#include "chrome/renderer/pepper/ppb_pdf_impl.h"
#include "chrome/renderer/printing/print_web_view_helper.h"
#include "content/child/child_thread.h"
#include "content/child/worker_task_runner.h"
@@ -48,6 +51,7 @@ MSVC_POP_WARNING();
#include "content/renderer/render_frame_impl.h"
#include "ipc/ipc_sync_channel.h"
#include "media/base/media.h"
#include "ppapi/c/private/ppb_pdf.h"
#include "third_party/WebKit/public/platform/WebPrerenderingSupport.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/platform/WebURL.h"
@@ -64,6 +68,10 @@ MSVC_POP_WARNING();
#include "third_party/WebKit/public/web/WebWorkerInfo.h"
#include "v8/include/v8.h"
#if defined(OS_WIN)
#include "base/win/iat_patch_function.h"
#endif
namespace {
// Stub implementation of blink::WebPrerenderingSupport.
@@ -141,6 +149,43 @@ class CefWebWorkerTaskRunner : public base::SequencedTaskRunner,
int worker_id_;
};
#if defined(OS_WIN)
static base::win::IATPatchFunction g_iat_patch_createdca;
HDC WINAPI CreateDCAPatch(LPCSTR driver_name,
LPCSTR device_name,
LPCSTR output,
const void* init_data) {
DCHECK(std::string("DISPLAY") == std::string(driver_name));
DCHECK(!device_name);
DCHECK(!output);
DCHECK(!init_data);
// CreateDC fails behind the sandbox, but not CreateCompatibleDC.
return CreateCompatibleDC(NULL);
}
static base::win::IATPatchFunction g_iat_patch_get_font_data;
DWORD WINAPI GetFontDataPatch(HDC hdc,
DWORD table,
DWORD offset,
LPVOID buffer,
DWORD length) {
int rv = GetFontData(hdc, table, offset, buffer, length);
if (rv == GDI_ERROR && hdc) {
HFONT font = static_cast<HFONT>(GetCurrentObject(hdc, OBJ_FONT));
LOGFONT logfont;
if (GetObject(font, sizeof(LOGFONT), &logfont)) {
std::vector<char> font_data;
content::RenderThread::Get()->PreCacheFont(logfont);
rv = GetFontData(hdc, table, offset, buffer, length);
content::RenderThread::Get()->ReleaseCachedFonts();
}
}
return rv;
}
#endif // OS_WIN
} // namespace
CefContentRendererClient::CefContentRendererClient()
@@ -212,6 +257,19 @@ void CefContentRendererClient::WebKitInitialized() {
blink::WebRuntimeFeatures::enableMediaStream(
command_line.HasSwitch(switches::kEnableMediaStream));
#if defined(OS_WIN)
// Need to patch a few functions for font loading to work correctly.
// From chrome/renderer/chrome_render_process_observer.cc.
base::FilePath pdf;
if (PathService::Get(chrome::FILE_PDF_PLUGIN, &pdf) &&
base::PathExists(pdf)) {
g_iat_patch_createdca.Patch(
pdf.value().c_str(), "gdi32.dll", "CreateDCA", CreateDCAPatch);
g_iat_patch_get_font_data.Patch(
pdf.value().c_str(), "gdi32.dll", "GetFontData", GetFontDataPatch);
}
#endif // defined(OS_WIN)
const CefContentClient::SchemeInfoList* schemes =
CefContentClient::Get()->GetCustomSchemes();
if (!schemes->empty()) {
@@ -608,6 +666,16 @@ void CefContentRendererClient::DidCreateScriptContext(
}
}
const void* CefContentRendererClient::CreatePPAPIInterface(
const std::string& interface_name) {
#if defined(ENABLE_PLUGINS)
// Used for in-process PDF plugin.
if (interface_name == PPB_PDF_INTERFACE)
return PPB_PDF_Impl::GetInterface();
#endif
return NULL;
}
void CefContentRendererClient::WillReleaseScriptContext(
blink::WebFrame* frame, v8::Handle<v8::Context> context, int world_id) {
// Notify the render process handler.

View File

@@ -93,6 +93,8 @@ class CefContentRendererClient : public content::ContentRendererClient,
v8::Handle<v8::Context> context,
int extension_group,
int world_id) OVERRIDE;
virtual const void* CreatePPAPIInterface(
const std::string& interface_name) OVERRIDE;
void WillReleaseScriptContext(blink::WebFrame* frame,
v8::Handle<v8::Context> context,