From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Cheng Zhao <zcbenz@gmail.com>
Date: Tue, 9 Oct 2018 10:36:20 -0700
Subject: mas: avoid some private APIs

Guard usages in blink of private Mac APIs by MAS_BUILD, so they can be
excluded for people who want to submit their apps to the Mac App store.

diff --git a/base/process/process_info_mac.cc b/base/process/process_info_mac.cc
index 368405f29313d51a6eee16517b634c6d0ea95281..2553a7fbf1e8b4dea796dec3b3e906d265d3ad76 100644
--- a/base/process/process_info_mac.cc
+++ b/base/process/process_info_mac.cc
@@ -5,18 +5,22 @@
 #include <stdlib.h>
 #include <unistd.h>
 
+#ifndef MAS_BUILD
 extern "C" {
 pid_t responsibility_get_pid_responsible_for_pid(pid_t)
     API_AVAILABLE(macosx(10.12));
 }
+#endif
 
 namespace base {
 
 bool IsProcessSelfResponsible() {
+#ifndef MAS_BUILD
   if (__builtin_available(macOS 10.14, *)) {
     const pid_t pid = getpid();
     return responsibility_get_pid_responsible_for_pid(pid) == pid;
   }
+#endif
   return true;
 }
 
diff --git a/content/common/pseudonymization_salt.cc b/content/common/pseudonymization_salt.cc
index 28e003bef910abff022def659fe18d4cd0549f8a..530bcbdb5d350f6486dc1e8536f7b279be69e241 100644
--- a/content/common/pseudonymization_salt.cc
+++ b/content/common/pseudonymization_salt.cc
@@ -41,11 +41,13 @@ uint32_t GetPseudonymizationSalt() {
   uint32_t salt = g_salt.load();
 
   if (salt == 0) {
+#ifndef MAS_BUILD
 #if DCHECK_IS_ON()
     // Only the Browser process needs to initialize the `salt` on demand.
     // Other processes (identified via the IsProcessSandboxed heuristic) should
     // receive the salt from their parent processes.
     DCHECK(!sandbox::policy::Sandbox::IsProcessSandboxed());
+#endif
 #endif
     salt = InitializeSalt();
   }
diff --git a/content/renderer/renderer_main_platform_delegate_mac.mm b/content/renderer/renderer_main_platform_delegate_mac.mm
index 894ac47e596c1c96a7e0659be80ed8a5629d0304..eca797a24df79b8502b9698e6ed8830ad1c5cb59 100644
--- a/content/renderer/renderer_main_platform_delegate_mac.mm
+++ b/content/renderer/renderer_main_platform_delegate_mac.mm
@@ -10,9 +10,11 @@
 #include "sandbox/mac/seatbelt.h"
 #include "sandbox/mac/system_services.h"
 
+#ifndef MAS_BUILD
 extern "C" {
 CGError CGSSetDenyWindowServerConnections(bool);
 }
+#endif
 
 namespace content {
 
@@ -22,6 +24,7 @@
 // verifies there are no existing open connections), and then indicates that
 // Chrome should continue execution without access to launchservicesd.
 void DisableSystemServices() {
+#ifndef MAS_BUILD
   // Tell the WindowServer that we don't want to make any future connections.
   // This will return Success as long as there are no open connections, which
   // is what we want.
@@ -30,6 +33,7 @@ void DisableSystemServices() {
 
   sandbox::DisableLaunchServices();
   sandbox::DisableCoreServicesCheckFix();
+#endif
 }
 
 }  // namespace
diff --git a/content/renderer/theme_helper_mac.mm b/content/renderer/theme_helper_mac.mm
index c15f3a631292b538698625328fb429ee3c9964f5..37e038753ecf1b82ec92c06b2c0729b5328781c9 100644
--- a/content/renderer/theme_helper_mac.mm
+++ b/content/renderer/theme_helper_mac.mm
@@ -7,11 +7,11 @@
 #include <Cocoa/Cocoa.h>
 
 #include "base/strings/sys_string_conversions.h"
-
+#if !defined(MAS_BUILD)
 extern "C" {
 bool CGFontRenderingGetFontSmoothingDisabled(void) API_AVAILABLE(macos(10.14));
 }
-
+#endif
 namespace content {
 
 void SystemColorsDidChange(int aqua_color_variant,
@@ -59,8 +59,19 @@ void SystemColorsDidChange(int aqua_color_variant,
 bool IsSubpixelAntialiasingAvailable() {
   if (__builtin_available(macOS 10.14, *)) {
     // See https://trac.webkit.org/changeset/239306/webkit for more info.
+#if !defined(MAS_BUILD)
     return !CGFontRenderingGetFontSmoothingDisabled();
+#else
+    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
+    NSString *default_key = @"CGFontRenderingGetFontSmoothingDisabled";
+    // Check that key exists since boolForKey defaults to NO when the
+    // key is missing and this key in fact defaults to YES;
+    if ([defaults objectForKey:default_key] == nil)
+      return false;
+    return ![defaults boolForKey:default_key];
+#endif
   }
+
   return true;
 }
 
diff --git a/device/bluetooth/bluetooth_adapter_mac.mm b/device/bluetooth/bluetooth_adapter_mac.mm
index 3f7dce0281f7b5a540d7b9377ef14a8a6aa9a2fa..11d8419791f3e45d5242081422d452d4fc703833 100644
--- a/device/bluetooth/bluetooth_adapter_mac.mm
+++ b/device/bluetooth/bluetooth_adapter_mac.mm
@@ -42,6 +42,7 @@
 #include "device/bluetooth/bluetooth_socket_mac.h"
 #include "device/bluetooth/public/cpp/bluetooth_address.h"
 
+#ifndef MAS_BUILD
 extern "C" {
 // Undocumented IOBluetooth Preference API [1]. Used by `blueutil` [2] and
 // `Karabiner` [3] to programmatically control the Bluetooth state. Calling the
@@ -55,6 +56,7 @@
 // [4] https://support.apple.com/kb/PH25091
 void IOBluetoothPreferenceSetControllerPowerState(int state);
 }
+#endif
 
 namespace {
 
@@ -123,8 +125,10 @@ CBCentralManagerState GetCBManagerState(CBCentralManager* manager) {
       controller_state_function_(
           base::BindRepeating(&BluetoothAdapterMac::GetHostControllerState,
                               base::Unretained(this))),
+#ifndef MAS_BUILD
       power_state_function_(
           base::BindRepeating(IOBluetoothPreferenceSetControllerPowerState)),
+#endif
       classic_discovery_manager_(
           BluetoothDiscoveryManagerMac::CreateClassic(this)),
       low_energy_discovery_manager_(
@@ -365,8 +369,12 @@ CBCentralManagerState GetCBManagerState(CBCentralManager* manager) {
 }
 
 bool BluetoothAdapterMac::SetPoweredImpl(bool powered) {
+#ifndef MAS_BUILD
   power_state_function_.Run(base::strict_cast<int>(powered));
   return true;
+#else
+  return false;
+#endif
 }
 
 void BluetoothAdapterMac::RemovePairingDelegateInternal(
diff --git a/media/audio/BUILD.gn b/media/audio/BUILD.gn
index ebe37172d254e8441fe2b8c290bd5a59af38d754..6a131f5c41f3e43a1467efeec2ce63f6903691b7 100644
--- a/media/audio/BUILD.gn
+++ b/media/audio/BUILD.gn
@@ -173,6 +173,12 @@ source_set("audio") {
       "mac/scoped_audio_unit.cc",
       "mac/scoped_audio_unit.h",
     ]
+    if (is_mas_build) {
+      sources -= [
+        "mac/coreaudio_dispatch_override.cc",
+        "mac/coreaudio_dispatch_override.h",
+      ]
+    }
     frameworks = [
       "AudioToolbox.framework",
       "AudioUnit.framework",
diff --git a/media/audio/mac/audio_manager_mac.cc b/media/audio/mac/audio_manager_mac.cc
index b8805f9174818ac086a5d6542c9962050f00aee8..5491b62a2e180b2f6e48e243e2ac78c3b1a16892 100644
--- a/media/audio/mac/audio_manager_mac.cc
+++ b/media/audio/mac/audio_manager_mac.cc
@@ -887,7 +887,7 @@ AudioParameters AudioManagerMac::GetPreferredOutputStreamParameters(
 
 void AudioManagerMac::InitializeOnAudioThread() {
   DCHECK(GetTaskRunner()->BelongsToCurrentThread());
-  InitializeCoreAudioDispatchOverride();
+  // InitializeCoreAudioDispatchOverride();
   power_observer_ = std::make_unique<AudioPowerObserver>();
 }
 
diff --git a/net/dns/dns_config_service_posix.cc b/net/dns/dns_config_service_posix.cc
index 3cce5f20af78f4456466df64fe0d040b5dba5fa8..1814ebbca91007b242a4e4ef359896594c23616a 100644
--- a/net/dns/dns_config_service_posix.cc
+++ b/net/dns/dns_config_service_posix.cc
@@ -129,8 +129,8 @@ class DnsConfigServicePosix::Watcher : public DnsConfigService::Watcher {
 
   bool Watch() override {
     CheckOnCorrectSequence();
-
     bool success = true;
+#ifndef MAS_BUILD
     if (!config_watcher_.Watch(base::BindRepeating(&Watcher::OnConfigChanged,
                                                    base::Unretained(this)))) {
       LOG(ERROR) << "DNS config watch failed to start.";
@@ -147,6 +147,7 @@ class DnsConfigServicePosix::Watcher : public DnsConfigService::Watcher {
       success = false;
     }
 #endif  // !BUILDFLAG(IS_IOS)
+#endif
     return success;
   }
 
diff --git a/sandbox/mac/sandbox_compiler.cc b/sandbox/mac/sandbox_compiler.cc
index e524aa7b851022abed1edac39e18d8d92e5349b4..718d3f963da5c1a15a1bdb0e6043f89bc0f940f8 100644
--- a/sandbox/mac/sandbox_compiler.cc
+++ b/sandbox/mac/sandbox_compiler.cc
@@ -28,6 +28,7 @@ bool SandboxCompiler::InsertStringParam(const std::string& key,
 }
 
 bool SandboxCompiler::CompileAndApplyProfile(std::string* error) {
+#ifndef MAS_BUILD
   char* error_internal = nullptr;
   std::vector<const char*> params;
 
@@ -44,6 +45,7 @@ bool SandboxCompiler::CompileAndApplyProfile(std::string* error) {
     sandbox::Seatbelt::FreeError(error_internal);
     return false;
   }
+#endif
   return true;
 }
 
diff --git a/sandbox/mac/seatbelt.cc b/sandbox/mac/seatbelt.cc
index e0c31170acd13c9997c6b1d04c6de1420feaf422..0e561f97b33380bfbe52e64c2c4a6988095e8074 100644
--- a/sandbox/mac/seatbelt.cc
+++ b/sandbox/mac/seatbelt.cc
@@ -64,7 +64,11 @@ void Seatbelt::FreeError(char* errorbuf) {
 
 // static
 bool Seatbelt::IsSandboxed() {
+#ifndef MAS_BUILD
   return ::sandbox_check(getpid(), NULL, 0);
+#else
+  return true;
+#endif
 }
 
 }  // namespace sandbox
diff --git a/sandbox/mac/seatbelt_extension.cc b/sandbox/mac/seatbelt_extension.cc
index d59a16112d27e2696437163483c44eca414c225c..1ccd20fe7efa3cbae48f99d0660b025283dd370e 100644
--- a/sandbox/mac/seatbelt_extension.cc
+++ b/sandbox/mac/seatbelt_extension.cc
@@ -11,6 +11,7 @@
 #include "base/notreached.h"
 #include "sandbox/mac/seatbelt_extension_token.h"
 
+#ifndef MAS_BUILD
 // libsandbox private API.
 extern "C" {
 extern const char* APP_SANDBOX_READ;
@@ -22,6 +23,7 @@ char* sandbox_extension_issue_file(const char* type,
                                    const char* path,
                                    uint32_t flags);
 }
+#endif
 
 namespace sandbox {
 
@@ -50,7 +52,11 @@ std::unique_ptr<SeatbeltExtension> SeatbeltExtension::FromToken(
 
 bool SeatbeltExtension::Consume() {
   DCHECK(!token_.empty());
+#ifndef MAS_BUILD
   handle_ = sandbox_extension_consume(token_.c_str());
+#else
+  handle_ = -1;
+#endif
   return handle_ > 0;
 }
 
@@ -62,7 +68,11 @@ bool SeatbeltExtension::ConsumePermanently() {
 }
 
 bool SeatbeltExtension::Revoke() {
+#ifndef MAS_BUILD
   int rv = sandbox_extension_release(handle_);
+#else
+  int rv = -1;
+#endif
   handle_ = 0;
   token_.clear();
   return rv == 0;
@@ -80,12 +90,14 @@ SeatbeltExtension::SeatbeltExtension(const std::string& token)
 char* SeatbeltExtension::IssueToken(SeatbeltExtension::Type type,
                                     const std::string& resource) {
   switch (type) {
+#ifndef MAS_BUILD
     case FILE_READ:
       return sandbox_extension_issue_file(APP_SANDBOX_READ, resource.c_str(),
                                           0);
     case FILE_READ_WRITE:
       return sandbox_extension_issue_file(APP_SANDBOX_READ_WRITE,
                                           resource.c_str(), 0);
+#endif
     default:
       NOTREACHED();
       return nullptr;
diff --git a/ui/accessibility/platform/inspect/ax_transform_mac.mm b/ui/accessibility/platform/inspect/ax_transform_mac.mm
index 7cb34e119cd30353fe56e7c71ed5e1d417896888..dbb6cc8e37eff9b30269687f29808ec3ca46b243 100644
--- a/ui/accessibility/platform/inspect/ax_transform_mac.mm
+++ b/ui/accessibility/platform/inspect/ax_transform_mac.mm
@@ -86,6 +86,7 @@
     }
   }
 
+#ifndef MAS_BUILD
   // AXTextMarker
   if (IsAXTextMarker(value)) {
     return AXTextMarkerToBaseValue(value, indexer);
@@ -94,6 +95,7 @@
   // AXTextMarkerRange
   if (IsAXTextMarkerRange(value))
     return AXTextMarkerRangeToBaseValue(value, indexer);
+#endif
 
   // Accessible object
   if (IsNSAccessibilityElement(value) || IsAXUIElement(value)) {
