Files
palemoon27/browser/components/shell/nsMacShellService.cpp
T
roytam1 7abb13fe93 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1234012 - Do not try to copy files from the non-existing default profile. r=bsmedberg (d30314c902)
- Bug 1192831 - Remove PlayPreview API. r=jet, r=peterv (bb33dc609d)
- Bug 1098064 part B - remove nsIPluginHost.isPluginOOP, r=jimm (39643555da)
- Bug 1239913 - Convert toolkit/ to use asyncOpen2 where loadingPrincipal is SystemPrincipal (r=sicking) (b7d663d0e6)
- minor (ac122dbb68)
- Bug 1216972 - OS.File AsyncShutdown for content processes. r=froydnj (53550f1050)
- Bug 1204834 - OS.File.writeAtomic should have the path argument in its closure. r=yoric (8dcf508961)
- Bug 1201904 - Add test for force windowless mode. r=aklotz (ae4e591342)
- Bug 1131368 - test plugin.allowed_types pref; r=josh (6a61ae8592)
- Bug 1129040 - Query the chrome process blocklist service prior to instantiating plugins in the content process. r=billm (398664884f)
- Bug 1225293 - Add Microsoft Silverlight to Win64 Plugin Whitelist; r=bsmedberg (eeecf59efc)
- Bug 1165981 - Mochitests and test plugin for Win64 plugin disabling; r=bsmedberg (82211ff568)
- Bug 1213710 part 1 - Convert all html/xul files in dom/plugins/test/mochitest to unix format. r=bsmedberg (8f89d2ac1d)
- Bug 1213710 part 2 - Rename dom/plugins/test/mochitest/utils.js to plugin-utils.js. r=bsmedberg (e74d035ffc)
- Bug 1225293 - Mochitests for Win64 Silverlight Whitelisting; r=bsmedberg (079ed3a442)
- minor bits left out (5fa7b83b68)
- Bug 1214516 - Sync check PBackgroundChild existence to initiate indexed DB open/delete request right away if it exists. r=khuey (8a2c5c7f8f)
- Bug 1200004 - Fix IDB permission handling around navigated windows. r=baku (cea1d31ed9)
- Bug 1239751 - FieldInfoHash does not need a post barrier; r=jonco (70b443bc23)
- Bug 891107 - Part 5: Show function name in this and callee type error messages in js-ctypes. r=jorendorff (4358085f8f)
- Bug 891107 - Part 6: Show information about range and value in array index error messages in js-ctypes. r=jorendorff (dfa926dc95)
- Bug 891107 - Part 7: Show information about value, type, function, and argument number in function related error messages in js-ctypes. r=jorendorff (4b71ee5304)
- Bug 891107 - Part 8: Show information about field name and type in struct field related error messages in js-ctypes. r=jorendorff (3a128382a6)
- Bug 891107 - Part 9: Report construction error as TypeError in js-ctypes. r=jorendorff (4199e59c7b)
- Bug 891107 - Part 10: Show information about value in pointer related error messages in js-ctypes. r=jorendorff (ad7e5fe53d)
- Bug 891107 - Part 11: Show information about type in cast error messages in js-ctypes. r=jorendorff (74fe412bb9)
- Bug 1243918 - Don't clobber safeseh in msvcc.sh -clang-cl; r=glandium (f014a945c9)
- default to -O3 again (0d9261962c)
- Bug 1239369 - Restore oomTests's check that an exception is thrown on failure, except where it isn't r=terrence (9f00c22106)
- Bug 1203595 - move tests into manual-tests. r=waldo (444b7c10aa)
- Bug 1079844 - Rename the shell builtin 'neuter' function to 'detachArrayBuffer', consistent with the spec name for the operation. r=till (48cfe8ff3a)
- Bug 1234428 - findPath wrap cross-compartment objects. r=jonco (2ca70c86b0)
- minor (c32fe44e31)
- Add a semicolon to avoid relying on ASI in one place in String.js. No bug, r=trivial (5f005bf2af)
- minor (2604e4d608)
- Bug 1132630 - Prefix bound function names. r=till (3b30a82402)
- Bug 1246157 - avoid memory leak when bindings->init fails. r=jcoppeard (e1f4513225)
- Bug 1241116: Fix a leak in js_StartPref; r=nbp (8d258dcb05)
- Bug 1232015 - Update pdf.js to version 1.3.72. r=bdahl (20cb998eff)
- Bug 1232196 - Update pdf.js to version 1.3.76. r=bdahl (a324208154)
- Bug 1233801 - Update pdf.js to version 1.3.110. r=bdahl (071620492b)
- Bug 1235724 - Update pdf.js to version 1.3.142. r=bdahl (0f1f4703e4)
- Bug 1167224 - Remove getMostRecentBrowserWindow() from nsIBrowserGlue and use RecentWindow in nsBrowserGlue.js. r=mak (bcfa5b5eac)
- remove duplicate (9978d0a5d8)
- Bug 1194903 - Increase the timeout of browser_pdfjs_zoom.js (1103f801ae)
- Bug 1170339 - Increase the timeout of browser_pdfjs_navigation.js (e4c9517f36)
- Bug 1237815 - Update pdf.js to version 1.3.161. r=bdahl (6c4c566eab)
- Bug 1198459 - Add some AppConstant use. r=felipe (69400b0cef)
- Bug 1191242 - Instrument the default browser prompt. r=dolske p=bsmedberg (85f43f1b7d)
- Bug 1198459 - Modify existing a11y prompting to support disabling a11y in content until a restart. r=felipe (aaeac171b1)
- bit of Bug 1241993 (25de6f8509)
- Bug 1221050 - part 1: set up migration infrastructure, r=ttaubert (22347f42a2)
- Bug 1225648 - Remove duplicate skipDefaultBrowser code and fix double-counting error when tracking prompt counts. r=Gijs (da4603369b)
- Bug 1193204 - Remove browser.devedition.theme.enabled check when unapplying theme in ui migration;r=Gijs (148d6d599d)
- Bug 1205038 - Add missing BROWSER_SET_DEFAULT_ALWAYS_CHECK probe to Histograms.json, and fix BROWSER_SET_DEFAULT_TIME_TO_COMPLETION_SECONDS by fixing callback. r=dolske,Gijs (880b22e029)
- Bug 1238679 - Mark the Bool32x4 functions as inlinable. r=bbouvier (f0ea70894e)
- Bug 992472 - Remove self-hosting macros ARRAY_PUSH and ARRAY_SLICE. r=efaust (60ae1d29a9)
- missing bit of 1040390 (dea7f70cf1)
- Bug 1246136: Check for dead code in assertion in EmitIfElse; r=luke (03d6f690da)
- Bug 1241233 - Odin: don't report symbolicLinks memory usage twice (r=bbouvier) (f2344bc392)
- Bug 1241581 - Put back null filename checks (r=bbouvier) (3c54ac7cf9)
- Bug 1249787 - BaldrMonkey: Fix wasm string hex escape parsing endianness. r=luke (bf3aeb7ddb)
- Bug 1229855: Fix miscompilation of uint8_t enum class with gcc4.8.2; r=luke (a86039958b)
- Bug 1241454: Fix enum class storage of ExprType; r=bustage (9ee3368aed)
- Bug 1248502 - Remove obsolete variants and configure flags, r=terrence (5dbcd14884)
2023-12-12 14:29:52 +08:00

479 lines
15 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsDirectoryServiceDefs.h"
#include "nsIDOMElement.h"
#include "nsIDOMHTMLImageElement.h"
#include "nsIImageLoadingContent.h"
#include "nsIDocument.h"
#include "nsIContent.h"
#include "nsILocalFileMac.h"
#include "nsIObserverService.h"
#include "nsIPrefService.h"
#include "nsIServiceManager.h"
#include "nsIStringBundle.h"
#include "nsIURL.h"
#include "nsIWebBrowserPersist.h"
#include "nsMacShellService.h"
#include "nsIProperties.h"
#include "nsServiceManagerUtils.h"
#include "nsShellService.h"
#include "nsStringAPI.h"
#include "nsIDocShell.h"
#include "nsILoadContext.h"
#include <CoreFoundation/CoreFoundation.h>
#include <ApplicationServices/ApplicationServices.h>
#define NETWORK_PREFPANE NS_LITERAL_CSTRING("/System/Library/PreferencePanes/Network.prefPane")
#define DESKTOP_PREFPANE NS_LITERAL_CSTRING("/System/Library/PreferencePanes/DesktopScreenEffectsPref.prefPane")
#define SAFARI_BUNDLE_IDENTIFIER "com.apple.Safari"
NS_IMPL_ISUPPORTS(nsMacShellService, nsIMacShellService, nsIShellService, nsIWebProgressListener)
NS_IMETHODIMP
nsMacShellService::IsDefaultBrowser(bool aStartupCheck,
bool aForAllTypes,
bool* aIsDefaultBrowser)
{
*aIsDefaultBrowser = false;
CFStringRef firefoxID = ::CFBundleGetIdentifier(::CFBundleGetMainBundle());
if (!firefoxID) {
// CFBundleGetIdentifier is expected to return nullptr only if the specified
// bundle doesn't have a bundle identifier in its plist. In this case, that
// means a failure, since our bundle does have an identifier.
return NS_ERROR_FAILURE;
}
// Get the default http handler's bundle ID (or nullptr if it has not been
// explicitly set)
CFStringRef defaultBrowserID = ::LSCopyDefaultHandlerForURLScheme(CFSTR("http"));
if (defaultBrowserID) {
*aIsDefaultBrowser = ::CFStringCompare(firefoxID, defaultBrowserID, 0) == kCFCompareEqualTo;
::CFRelease(defaultBrowserID);
}
// If this is the first browser window, maintain internal state that we've
// checked this session (so that subsequent window opens don't show the
// default browser dialog).
if (aStartupCheck)
mCheckedThisSession = true;
return NS_OK;
}
NS_IMETHODIMP
nsMacShellService::SetDefaultBrowser(bool aClaimAllTypes, bool aForAllUsers)
{
// Note: We don't support aForAllUsers on Mac OS X.
CFStringRef firefoxID = ::CFBundleGetIdentifier(::CFBundleGetMainBundle());
if (!firefoxID) {
return NS_ERROR_FAILURE;
}
if (::LSSetDefaultHandlerForURLScheme(CFSTR("http"), firefoxID) != noErr) {
return NS_ERROR_FAILURE;
}
if (::LSSetDefaultHandlerForURLScheme(CFSTR("https"), firefoxID) != noErr) {
return NS_ERROR_FAILURE;
}
if (aClaimAllTypes) {
if (::LSSetDefaultHandlerForURLScheme(CFSTR("ftp"), firefoxID) != noErr) {
return NS_ERROR_FAILURE;
}
if (::LSSetDefaultRoleHandlerForContentType(kUTTypeHTML, kLSRolesAll, firefoxID) != noErr) {
return NS_ERROR_FAILURE;
}
}
nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
if (prefs) {
(void) prefs->SetBoolPref(PREF_CHECKDEFAULTBROWSER, true);
// Reset the number of times the dialog should be shown
// before it is silenced.
(void) prefs->SetIntPref(PREF_DEFAULTBROWSERCHECKCOUNT, 0);
}
return NS_OK;
}
NS_IMETHODIMP
nsMacShellService::GetShouldCheckDefaultBrowser(bool* aResult)
{
// If we've already checked, the browser has been started and this is a
// new window open, and we don't want to check again.
if (mCheckedThisSession) {
*aResult = false;
return NS_OK;
}
nsresult rv;
nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
if (NS_FAILED(rv)) {
return rv;
}
return prefs->GetBoolPref(PREF_CHECKDEFAULTBROWSER, aResult);
}
NS_IMETHODIMP
nsMacShellService::SetShouldCheckDefaultBrowser(bool aShouldCheck)
{
nsresult rv;
nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
if (NS_FAILED(rv)) {
return rv;
}
return prefs->SetBoolPref(PREF_CHECKDEFAULTBROWSER, aShouldCheck);
}
NS_IMETHODIMP
nsMacShellService::GetCanSetDesktopBackground(bool* aResult)
{
*aResult = true;
return NS_OK;
}
NS_IMETHODIMP
nsMacShellService::SetDesktopBackground(nsIDOMElement* aElement,
int32_t aPosition)
{
// Note: We don't support aPosition on OS X.
// Get the image URI:
nsresult rv;
nsCOMPtr<nsIImageLoadingContent> imageContent = do_QueryInterface(aElement,
&rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIURI> imageURI;
rv = imageContent->GetCurrentURI(getter_AddRefs(imageURI));
NS_ENSURE_SUCCESS(rv, rv);
// We need the referer URI for nsIWebBrowserPersist::saveURI
nsCOMPtr<nsIContent> content = do_QueryInterface(aElement, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsIURI *docURI = content->OwnerDoc()->GetDocumentURI();
if (!docURI)
return NS_ERROR_FAILURE;
// Get the desired image file name
nsCOMPtr<nsIURL> imageURL(do_QueryInterface(imageURI));
if (!imageURL) {
// XXXmano (bug 300293): Non-URL images (e.g. the data: protocol) are not
// yet supported. What filename should we take here?
return NS_ERROR_NOT_IMPLEMENTED;
}
nsAutoCString fileName;
imageURL->GetFileName(fileName);
nsCOMPtr<nsIProperties> fileLocator
(do_GetService("@mozilla.org/file/directory_service;1", &rv));
NS_ENSURE_SUCCESS(rv, rv);
// Get the current user's "Pictures" folder (That's ~/Pictures):
fileLocator->Get(NS_OSX_PICTURE_DOCUMENTS_DIR, NS_GET_IID(nsIFile),
getter_AddRefs(mBackgroundFile));
if (!mBackgroundFile)
return NS_ERROR_OUT_OF_MEMORY;
nsAutoString fileNameUnicode;
CopyUTF8toUTF16(fileName, fileNameUnicode);
// and add the imgage file name itself:
mBackgroundFile->Append(fileNameUnicode);
// Download the image; the desktop background will be set in OnStateChange()
nsCOMPtr<nsIWebBrowserPersist> wbp
(do_CreateInstance("@mozilla.org/embedding/browser/nsWebBrowserPersist;1", &rv));
NS_ENSURE_SUCCESS(rv, rv);
uint32_t flags = nsIWebBrowserPersist::PERSIST_FLAGS_NO_CONVERSION |
nsIWebBrowserPersist::PERSIST_FLAGS_REPLACE_EXISTING_FILES |
nsIWebBrowserPersist::PERSIST_FLAGS_FROM_CACHE;
wbp->SetPersistFlags(flags);
wbp->SetProgressListener(this);
nsCOMPtr<nsILoadContext> loadContext;
nsCOMPtr<nsISupports> container = content->OwnerDoc()->GetContainer();
nsCOMPtr<nsIDocShell> docShell = do_QueryInterface(container);
if (docShell) {
loadContext = do_QueryInterface(docShell);
}
return wbp->SaveURI(imageURI, nullptr,
docURI, content->OwnerDoc()->GetReferrerPolicy(),
nullptr, nullptr,
mBackgroundFile, loadContext);
}
NS_IMETHODIMP
nsMacShellService::OnProgressChange(nsIWebProgress* aWebProgress,
nsIRequest* aRequest,
int32_t aCurSelfProgress,
int32_t aMaxSelfProgress,
int32_t aCurTotalProgress,
int32_t aMaxTotalProgress)
{
return NS_OK;
}
NS_IMETHODIMP
nsMacShellService::OnLocationChange(nsIWebProgress* aWebProgress,
nsIRequest* aRequest,
nsIURI* aLocation,
uint32_t aFlags)
{
return NS_OK;
}
NS_IMETHODIMP
nsMacShellService::OnStatusChange(nsIWebProgress* aWebProgress,
nsIRequest* aRequest,
nsresult aStatus,
const char16_t* aMessage)
{
return NS_OK;
}
NS_IMETHODIMP
nsMacShellService::OnSecurityChange(nsIWebProgress* aWebProgress,
nsIRequest* aRequest,
uint32_t aState)
{
return NS_OK;
}
NS_IMETHODIMP
nsMacShellService::OnStateChange(nsIWebProgress* aWebProgress,
nsIRequest* aRequest,
uint32_t aStateFlags,
nsresult aStatus)
{
if (aStateFlags & STATE_STOP) {
nsCOMPtr<nsIObserverService> os(do_GetService("@mozilla.org/observer-service;1"));
if (os)
os->NotifyObservers(nullptr, "shell:desktop-background-changed", nullptr);
bool exists = false;
mBackgroundFile->Exists(&exists);
if (!exists)
return NS_OK;
nsAutoCString nativePath;
mBackgroundFile->GetNativePath(nativePath);
AEDesc tAEDesc = { typeNull, nil };
OSErr err = noErr;
AliasHandle aliasHandle = nil;
FSRef pictureRef;
OSStatus status;
// Convert the path into a FSRef
status = ::FSPathMakeRef((const UInt8*)nativePath.get(), &pictureRef,
nullptr);
if (status == noErr) {
err = ::FSNewAlias(nil, &pictureRef, &aliasHandle);
if (err == noErr && aliasHandle == nil)
err = paramErr;
if (err == noErr) {
// We need the descriptor (based on the picture file reference)
// for the 'Set Desktop Picture' apple event.
char handleState = ::HGetState((Handle)aliasHandle);
::HLock((Handle)aliasHandle);
err = ::AECreateDesc(typeAlias, *aliasHandle,
GetHandleSize((Handle)aliasHandle), &tAEDesc);
// unlock the alias handler
::HSetState((Handle)aliasHandle, handleState);
::DisposeHandle((Handle)aliasHandle);
}
if (err == noErr) {
AppleEvent tAppleEvent;
OSType sig = 'MACS';
AEBuildError tAEBuildError;
// Create a 'Set Desktop Pictue' Apple Event
err = ::AEBuildAppleEvent(kAECoreSuite, kAESetData, typeApplSignature,
&sig, sizeof(OSType), kAutoGenerateReturnID,
kAnyTransactionID, &tAppleEvent, &tAEBuildError,
"'----':'obj '{want:type (prop),form:prop" \
",seld:type('dpic'),from:'null'()},data:(@)",
&tAEDesc);
if (err == noErr) {
AppleEvent reply = { typeNull, nil };
// Sent the event we built, the reply event isn't necessary
err = ::AESend(&tAppleEvent, &reply, kAENoReply, kAENormalPriority,
kNoTimeOut, nil, nil);
::AEDisposeDesc(&tAppleEvent);
}
}
}
}
return NS_OK;
}
NS_IMETHODIMP
nsMacShellService::OpenApplication(int32_t aApplication)
{
nsresult rv = NS_OK;
CFURLRef appURL = nil;
OSStatus err = noErr;
switch (aApplication) {
case nsIShellService::APPLICATION_MAIL:
{
CFURLRef tempURL = ::CFURLCreateWithString(kCFAllocatorDefault,
CFSTR("mailto:"), nullptr);
err = ::LSGetApplicationForURL(tempURL, kLSRolesAll, nullptr, &appURL);
::CFRelease(tempURL);
}
break;
case nsIShellService::APPLICATION_NEWS:
{
CFURLRef tempURL = ::CFURLCreateWithString(kCFAllocatorDefault,
CFSTR("news:"), nullptr);
err = ::LSGetApplicationForURL(tempURL, kLSRolesAll, nullptr, &appURL);
::CFRelease(tempURL);
}
break;
case nsIMacShellService::APPLICATION_KEYCHAIN_ACCESS:
err = ::LSGetApplicationForInfo('APPL', 'kcmr', nullptr, kLSRolesAll,
nullptr, &appURL);
break;
case nsIMacShellService::APPLICATION_NETWORK:
{
nsCOMPtr<nsIFile> lf;
rv = NS_NewNativeLocalFile(NETWORK_PREFPANE, true, getter_AddRefs(lf));
NS_ENSURE_SUCCESS(rv, rv);
bool exists;
lf->Exists(&exists);
if (!exists)
return NS_ERROR_FILE_NOT_FOUND;
return lf->Launch();
}
case nsIMacShellService::APPLICATION_DESKTOP:
{
nsCOMPtr<nsIFile> lf;
rv = NS_NewNativeLocalFile(DESKTOP_PREFPANE, true, getter_AddRefs(lf));
NS_ENSURE_SUCCESS(rv, rv);
bool exists;
lf->Exists(&exists);
if (!exists)
return NS_ERROR_FILE_NOT_FOUND;
return lf->Launch();
}
}
if (appURL && err == noErr) {
err = ::LSOpenCFURLRef(appURL, nullptr);
rv = err != noErr ? NS_ERROR_FAILURE : NS_OK;
::CFRelease(appURL);
}
return rv;
}
NS_IMETHODIMP
nsMacShellService::GetDesktopBackgroundColor(uint32_t *aColor)
{
// This method and |SetDesktopBackgroundColor| has no meaning on Mac OS X.
// The mac desktop preferences UI uses pictures for the few solid colors it
// supports.
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsMacShellService::SetDesktopBackgroundColor(uint32_t aColor)
{
// This method and |GetDesktopBackgroundColor| has no meaning on Mac OS X.
// The mac desktop preferences UI uses pictures for the few solid colors it
// supports.
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsMacShellService::OpenApplicationWithURI(nsIFile* aApplication, const nsACString& aURI)
{
nsCOMPtr<nsILocalFileMac> lfm(do_QueryInterface(aApplication));
CFURLRef appURL;
nsresult rv = lfm->GetCFURL(&appURL);
if (NS_FAILED(rv))
return rv;
const nsCString spec(aURI);
const UInt8* uriString = (const UInt8*)spec.get();
CFURLRef uri = ::CFURLCreateWithBytes(nullptr, uriString, aURI.Length(),
kCFStringEncodingUTF8, nullptr);
if (!uri)
return NS_ERROR_OUT_OF_MEMORY;
CFArrayRef uris = ::CFArrayCreate(nullptr, (const void**)&uri, 1, nullptr);
if (!uris) {
::CFRelease(uri);
return NS_ERROR_OUT_OF_MEMORY;
}
LSLaunchURLSpec launchSpec;
launchSpec.appURL = appURL;
launchSpec.itemURLs = uris;
launchSpec.passThruParams = nullptr;
launchSpec.launchFlags = kLSLaunchDefaults;
launchSpec.asyncRefCon = nullptr;
OSErr err = ::LSOpenFromURLSpec(&launchSpec, nullptr);
::CFRelease(uris);
::CFRelease(uri);
return err != noErr ? NS_ERROR_FAILURE : NS_OK;
}
NS_IMETHODIMP
nsMacShellService::GetDefaultFeedReader(nsIFile** _retval)
{
nsresult rv = NS_ERROR_FAILURE;
*_retval = nullptr;
CFStringRef defaultHandlerID = ::LSCopyDefaultHandlerForURLScheme(CFSTR("feed"));
if (!defaultHandlerID) {
defaultHandlerID = ::CFStringCreateWithCString(kCFAllocatorDefault,
SAFARI_BUNDLE_IDENTIFIER,
kCFStringEncodingASCII);
}
CFURLRef defaultHandlerURL = nullptr;
OSStatus status = ::LSFindApplicationForInfo(kLSUnknownCreator,
defaultHandlerID,
nullptr, // inName
nullptr, // outAppRef
&defaultHandlerURL);
if (status == noErr && defaultHandlerURL) {
nsCOMPtr<nsILocalFileMac> defaultReader =
do_CreateInstance("@mozilla.org/file/local;1", &rv);
if (NS_SUCCEEDED(rv)) {
rv = defaultReader->InitWithCFURL(defaultHandlerURL);
if (NS_SUCCEEDED(rv)) {
NS_ADDREF(*_retval = defaultReader);
rv = NS_OK;
}
}
::CFRelease(defaultHandlerURL);
}
::CFRelease(defaultHandlerID);
return rv;
}