Bug 1632277 - Part 1: Launch PDFs in app mode when default r=jaws,tkikuchi

Differential Revision: https://phabricator.services.mozilla.com/D73774
This commit is contained in:
Mark Striemer 2020-06-03 18:21:32 +00:00
parent 1fe5275822
commit 3e55277036
5 changed files with 83 additions and 8 deletions

View File

@ -984,6 +984,12 @@
value: true
mirror: always
# Open PDFs in Edge with the --app flag if it is the default.
- name: browser.pdf.launchDefaultEdgeAsApp
type: bool
value: true
mirror: always
# Force usage of in-memory (rather than file on disk) media cache for video streaming when private browsing
- name: browser.privatebrowsing.forceMediaMemoryCache
type: bool

View File

@ -14,6 +14,7 @@
#include "nsCURILoader.h"
#include "nsCExternalHandlerService.h"
#include "nsIExternalProtocolService.h"
#include "nsMimeTypes.h"
#include "mozilla/StaticPtr.h"
static bool sInitializedOurData = false;
@ -257,6 +258,25 @@ nsMIMEInfoBase::SetAlwaysAskBeforeHandling(bool aAlwaysAsk) {
return NS_OK;
}
NS_IMETHODIMP
nsMIMEInfoBase::IsPdf(bool* isPdf) {
if (mSchemeOrType == APPLICATION_PDF) {
*isPdf = true;
return NS_OK;
}
nsAutoCString fileExt;
nsresult rv = GetPrimaryExtension(fileExt);
if (NS_FAILED(rv)) {
return rv;
}
*isPdf = fileExt.LowerCaseEqualsASCII("pdf") ||
fileExt.LowerCaseEqualsASCII(".pdf");
return NS_OK;
}
/* static */
nsresult nsMIMEInfoBase::GetLocalFileFromURI(nsIURI* aURI, nsIFile** aFile) {
nsresult rv;
@ -403,6 +423,18 @@ nsresult nsMIMEInfoBase::LaunchWithIProcess(nsIFile* aApp,
return process->Runw(false, &string, 1);
}
/* static */
nsresult nsMIMEInfoBase::LaunchWithIProcess(nsIFile* aApp, const int aArgc,
const char16_t** aArgv) {
nsresult rv;
nsCOMPtr<nsIProcess> process = InitProcess(aApp, &rv);
if (NS_FAILED(rv)) {
return rv;
}
return process->Runw(false, aArgv, aArgc);
}
// nsMIMEInfoImpl implementation
NS_IMETHODIMP
nsMIMEInfoImpl::GetDefaultDescription(nsAString& aDefaultDescription) {

View File

@ -66,6 +66,7 @@ class nsMIMEInfoBase : public nsIMIMEInfo {
bool* aAlwaysAskBeforeHandling) override;
NS_IMETHOD SetAlwaysAskBeforeHandling(bool aAlwaysAskBeforeHandling) override;
NS_IMETHOD GetPossibleLocalHandlers(nsIArray** _retval) override;
NS_IMETHOD IsPdf(bool* isPdf);
enum HandlerClass { eMIMEInfo, eProtocolInfo };
@ -129,6 +130,8 @@ class nsMIMEInfoBase : public nsIMIMEInfo {
*/
static nsresult LaunchWithIProcess(nsIFile* aApp, const nsCString& aArg);
static nsresult LaunchWithIProcess(nsIFile* aApp, const nsString& aArg);
static nsresult LaunchWithIProcess(nsIFile* aApp, const int aArgc,
const char16_t** aArgv);
/**
* Given a file: nsIURI, return the associated nsIFile

View File

@ -19,8 +19,9 @@
#include "nsUnicharUtils.h"
#include "nsITextToSubURI.h"
#include "nsVariant.h"
#include "mozilla/AssembleCmdLine.h"
#include "mozilla/CmdLineAndEnvUtils.h"
#include "mozilla/ShellHeaderOnlyUtils.h"
#include "mozilla/StaticPrefs_browser.h"
#include "mozilla/UrlmonHeaderOnlyUtils.h"
#include "mozilla/UniquePtrExtensions.h"
@ -39,10 +40,12 @@ nsresult nsMIMEInfoWin::LaunchDefaultWithFile(nsIFile* aFile) {
return aFile->Launch();
}
nsresult nsMIMEInfoWin::ShellExecuteWithIFile(nsIFile* aExecutable,
const nsString& aArgs) {
nsresult nsMIMEInfoWin::ShellExecuteWithIFile(nsIFile* aExecutable, int aArgc,
const wchar_t** aArgv) {
nsresult rv;
NS_ASSERTION(aArgc >= 1, "aArgc must be at least 1");
nsAutoString execPath;
rv = aExecutable->GetTarget(execPath);
if (NS_FAILED(rv) || execPath.IsEmpty()) {
@ -52,7 +55,7 @@ nsresult nsMIMEInfoWin::ShellExecuteWithIFile(nsIFile* aExecutable,
return rv;
}
auto assembledArgs = mozilla::assembleSingleArgument(aArgs);
auto assembledArgs = mozilla::MakeCommandLine(aArgc, aArgv);
if (!assembledArgs) {
return NS_ERROR_FILE_EXECUTION_FAILED;
}
@ -73,9 +76,10 @@ nsresult nsMIMEInfoWin::ShellExecuteWithIFile(nsIFile* aExecutable,
mozilla::LauncherVoidResult shellExecuteOk = mozilla::ShellExecuteByExplorer(
execPathBStr, assembledArgs.get(), verbDefault, workingDir, showCmd);
if (shellExecuteOk.isErr()) {
// No need to pass assembledArgs to LaunchWithIProcess. aArgs will be
// No need to pass assembledArgs to LaunchWithIProcess. aArgv will be
// processed in nsProcess::RunProcess.
return LaunchWithIProcess(aExecutable, aArgs);
return LaunchWithIProcess(aExecutable, aArgc,
reinterpret_cast<const char16_t**>(aArgv));
}
return NS_OK;
@ -90,6 +94,34 @@ nsMIMEInfoWin::LaunchWithFile(nsIFile* aFile) {
"nsMIMEInfoBase should have mClass == eMIMEInfo");
if (mPreferredAction == useSystemDefault) {
if (StaticPrefs::browser_pdf_launchDefaultEdgeAsApp()) {
// Since Edgium is the default PDF handler, if we're using the OS default
// and it's Edgium prefer it's app mode so it operates as a PDF viewer
// (without browser toolbars). Bug 1632277.
bool isPdf;
rv = IsPdf(&isPdf);
if (NS_SUCCEEDED(rv) && isPdf) {
nsAutoCString defaultAppExecutable;
rv = mDefaultApplication->GetNativeLeafName(defaultAppExecutable);
if (NS_SUCCEEDED(rv) &&
defaultAppExecutable.LowerCaseEqualsLiteral("msedge.exe")) {
nsAutoString path;
rv = aFile->GetPath(path);
if (NS_SUCCEEDED(rv)) {
// If the --app flag doesn't work we'll want to fallback to a
// regular path. Send two args so we call `msedge.exe --app={path}
// {path}`.
nsAutoString appArg;
appArg.AppendLiteral("--app=");
appArg.Append(path);
const wchar_t* argv[] = {appArg.get(), path.get()};
return ShellExecuteWithIFile(mDefaultApplication,
mozilla::ArrayLength(argv), argv);
}
}
}
}
return LaunchDefaultWithFile(aFile);
}
@ -170,7 +202,8 @@ nsMIMEInfoWin::LaunchWithFile(nsIFile* aFile) {
}
nsAutoString path;
aFile->GetPath(path);
return ShellExecuteWithIFile(executable, path);
const wchar_t* argv[] = {path.get()};
return ShellExecuteWithIFile(executable, mozilla::ArrayLength(argv), argv);
}
return NS_ERROR_INVALID_ARG;

View File

@ -65,7 +65,8 @@ class nsMIMEInfoWin : public nsMIMEInfoBase, public nsIPropertyBag {
const nsAString& appFilesystemCommand);
// Helper routine to call mozilla::ShellExecuteByExplorer
nsresult ShellExecuteWithIFile(nsIFile* aExecutable, const nsString& aArgs);
nsresult ShellExecuteWithIFile(nsIFile* aExecutable, int aArgc,
const wchar_t** aArgv);
};
#endif