Bug 516362 p1. Add a localized helper to prompt the user for approval to install the app. r=zbraniecki,fluent-reviewers,flod

Differential Revision: https://phabricator.services.mozilla.com/D122476
This commit is contained in:
Jonathan Watt 2021-08-30 21:15:04 +00:00
Родитель c9893d3d5f
Коммит ed7d14cf9a
3 изменённых файлов: 83 добавлений и 0 удалений

Просмотреть файл

@ -265,6 +265,9 @@ var whitelist = [
{ file: "chrome://browser/content/screenshots/menu-visible.svg" },
{ file: "resource://app/modules/SnapshotSelector.jsm" },
// toolkit/xre/MacRunFromDmgUtils.mm
{ file: "resource://gre/localization/en-US/toolkit/global/run-from-dmg.ftl" },
];
if (AppConstants.NIGHTLY_BUILD && AppConstants.platform != "win") {

Просмотреть файл

@ -0,0 +1,13 @@
# 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/.
## Strings for a dialog that may open on macOS before the app's main window
## opens. The dialog prompts the user to allow the app to install itself in an
## appropriate location before relaunching itself from that location if the
## user accepts.
prompt-to-install-title = Finish installing { -brand-short-name }?
prompt-to-install-message = Complete this one-step installation to help keep { -brand-short-name } up to date and prevent data loss. { -brand-short-name } will be added to your Applications folder and Dock.
prompt-to-install-yes-button = Install
prompt-to-install-no-button = Dont Install

Просмотреть файл

@ -3,6 +3,7 @@
* 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 <AppKit/AppKit.h>
#include <ApplicationServices/ApplicationServices.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
@ -13,6 +14,8 @@
#include "MacRunFromDmgUtils.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/intl/Localization.h"
#include "nsCocoaFeatures.h"
#include "nsCommandLine.h"
#include "nsCommandLineServiceMac.h"
@ -28,6 +31,70 @@
namespace mozilla {
namespace MacRunFromDmgUtils {
/**
* Opens a dialog to ask the user whether the app should be installed to their
* Applications folder. Returns true if the dialog is successfully opened and
* the user accept, otherwise returns false.
*/
static bool AskUserIfWeShouldInstall() {
NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
// Try to get the localized strings:
nsTArray<nsCString> resIds = {
"branding/brand.ftl"_ns,
"toolkit/global/run-from-dmg.ftl"_ns,
};
RefPtr<intl::Localization> l10n = intl::Localization::Create(resIds, true);
ErrorResult rv;
nsAutoCString mozTitle, mozMessage, mozInstall, mozDontInstall;
l10n->FormatValueSync("prompt-to-install-title"_ns, {}, mozTitle, rv);
if (rv.Failed()) {
return false;
}
l10n->FormatValueSync("prompt-to-install-message"_ns, {}, mozMessage, rv);
if (rv.Failed()) {
return false;
}
l10n->FormatValueSync("prompt-to-install-yes-button"_ns, {}, mozInstall, rv);
if (rv.Failed()) {
return false;
}
l10n->FormatValueSync("prompt-to-install-no-button"_ns, {}, mozDontInstall, rv);
if (rv.Failed()) {
return false;
}
NSString* title = [NSString stringWithUTF8String:reinterpret_cast<const char*>(mozTitle.get())];
NSString* message =
[NSString stringWithUTF8String:reinterpret_cast<const char*>(mozMessage.get())];
NSString* install =
[NSString stringWithUTF8String:reinterpret_cast<const char*>(mozInstall.get())];
NSString* dontInstall =
[NSString stringWithUTF8String:reinterpret_cast<const char*>(mozDontInstall.get())];
NSAlert* alert = [[[NSAlert alloc] init] autorelease];
// Note that we don't set an icon since the app icon is used by default.
[alert setAlertStyle:NSAlertStyleInformational];
[alert setMessageText:title];
[alert setInformativeText:message];
// Note that if the user hits 'Enter' the "Install" button is activated,
// whereas if they hit 'Space' the "Don't Install" button is activated.
// That's standard behavior so probably desirable.
[alert addButtonWithTitle:install];
NSButton* dontInstallButton = [alert addButtonWithTitle:dontInstall];
// Since the "Don't Install" button doesn't have the title "Cancel" we need
// to map the Escape key to it manually:
[dontInstallButton setKeyEquivalent:@"\e"];
NSInteger result = [alert runModal];
return result == NSAlertFirstButtonReturn;
NS_OBJC_END_TRY_BLOCK_RETURN(false);
}
bool IsAppRunningFromDmg() {
NS_OBJC_BEGIN_TRY_BLOCK_RETURN;