Commit ff27adf2 authored by Matthew Noorenberghe's avatar Matthew Noorenberghe
Browse files

Bug 1615134 - Shell service API to open macOS Security & Privacy preferences panes. r=spohl

Example: Cc["@mozilla.org/browser/shell-service;1"].getService(Ci.nsIMacShellService).showSecurityPreferences("Privacy_AllFiles")

Differential Revision: https://phabricator.services.mozilla.com/D62690

--HG--
extra : moz-landing-system : lando
parent 48f45a3d
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ LOCAL_INCLUDES += [

BROWSER_CHROME_MANIFESTS += ['test/browser.ini']
MOCHITEST_CHROME_MANIFESTS += ['test/chrome.ini']
XPCSHELL_TESTS_MANIFESTS += ['test/unit/xpcshell.ini']

JAR_MANIFESTS += ['jar.mn']

@@ -26,6 +27,11 @@ if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'cocoa':
    SOURCES += [
        'nsMacShellService.cpp',
    ]

    LOCAL_INCLUDES += [
        # For CocoaFileUtils
        '/xpcom/io'
    ]
elif CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gtk':
    XPIDL_SOURCES += [
        'nsIGNOMEShellService.idl',
+12 −0
Original line number Diff line number Diff line
@@ -12,4 +12,16 @@ interface nsIMacShellService : nsIShellService
   * Opens the desktop preferences, e.g. for after setting the background.
   */
  void showDesktopPreferences();

  /**
   * @param aPaneID used by macOS to identify the pane to open.
   *                Example arguments:
   *                  * "" - use the default Security and Privacy pane.
   *                  * General
   *                  * Privacy
   *                  * Privacy_AllFiles
   *                  * Privacy_Camera
   *                  * Privacy_Microphone
   */
  void showSecurityPreferences(in ACString aPaneID);
};
+30 −0
Original line number Diff line number Diff line
@@ -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 "CocoaFileUtils.h"
#include "nsDirectoryServiceDefs.h"
#include "nsIImageLoadingContent.h"
#include "mozilla/dom/Document.h"
@@ -288,3 +289,32 @@ nsMacShellService::SetDesktopBackgroundColor(uint32_t aColor) {
  // supports.
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
nsMacShellService::ShowSecurityPreferences(const nsACString& aPaneID) {
  nsresult rv = NS_ERROR_NOT_AVAILABLE;

  CFStringRef paneID = ::CFStringCreateWithBytes(
      kCFAllocatorDefault, (const UInt8*)PromiseFlatCString(aPaneID).get(),
      aPaneID.Length(), kCFStringEncodingUTF8, false);

  if (paneID) {
    CFStringRef format =
        CFSTR("x-apple.systempreferences:com.apple.preference.security?%@");
    if (format) {
      CFStringRef urlStr =
          CFStringCreateWithFormat(kCFAllocatorDefault, NULL, format, paneID);
      if (urlStr) {
        CFURLRef url = ::CFURLCreateWithString(NULL, urlStr, NULL);
        rv = CocoaFileUtils::OpenURL(url);

        ::CFRelease(urlStr);
      }

      ::CFRelease(format);
    }

    ::CFRelease(paneID);
  }
  return rv;
}
+30 −0
Original line number Diff line number Diff line
/* Any copyright is dedicated to the Public Domain.
 * https://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Test the macOS ShowSecurityPreferences shell service method.
 */

function killSystemPreferences() {
  let killallFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
  killallFile.initWithPath("/usr/bin/killall");
  let sysPrefsArg = ["System Preferences"];
  let process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
  process.init(killallFile);
  process.run(true, sysPrefsArg, 1);
  return process.exitValue;
}

add_task(async function setup() {
  info("Ensure System Preferences isn't already running");
  killSystemPreferences();
});

add_task(async function test_prefsOpen() {
  let shellSvc = Cc["@mozilla.org/browser/shell-service;1"].getService(
    Ci.nsIMacShellService
  );
  shellSvc.showSecurityPreferences("Privacy_AllFiles");

  equal(killSystemPreferences(), 0, "Ensure System Preferences was started");
});
+5 −0
Original line number Diff line number Diff line
[DEFAULT]
firefox-appdir = browser

[test_macOS_showSecurityPreferences.js]
skip-if = toolkit != "cocoa"