From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Cheng Zhao <zcbenz@gmail.com>
Date: Thu, 20 Sep 2018 17:48:49 -0700
Subject: mas_disable_remote_accessibility.patch

Disable remote accessibility APIs (NSAccessibilityRemoteUIElement) for MAS
build.

diff --git a/components/remote_cocoa/app_shim/bridge_factory_impl.mm b/components/remote_cocoa/app_shim/bridge_factory_impl.mm
index 5ed3c60b5f4f502b4171271c4e38c722bddd9d14..20346425bfbb6bea148df8ca1d3e61cdb11be20e 100644
--- a/components/remote_cocoa/app_shim/bridge_factory_impl.mm
+++ b/components/remote_cocoa/app_shim/bridge_factory_impl.mm
@@ -43,6 +43,7 @@ class Bridge : public BridgedNativeWidgetHostHelper {
 
   // BridgedNativeWidgetHostHelper:
   id GetNativeViewAccessible() override {
+#ifndef MAS_BUILD
     if (!remote_accessibility_element_) {
       int64_t browser_pid = 0;
       std::vector<uint8_t> element_token;
@@ -53,6 +54,9 @@ class Bridge : public BridgedNativeWidgetHostHelper {
           ui::RemoteAccessibility::GetRemoteElementFromToken(element_token);
     }
     return remote_accessibility_element_.get();
+#else
+    return nil;
+#endif
   }
   void DispatchKeyEvent(ui::KeyEvent* event) override {
     bool event_handled = false;
@@ -90,8 +94,10 @@ class Bridge : public BridgedNativeWidgetHostHelper {
   mojom::TextInputHostAssociatedPtr text_input_host_ptr_;
 
   std::unique_ptr<BridgedNativeWidgetImpl> bridge_impl_;
+#ifndef MAS_BUILD
   base::scoped_nsobject<NSAccessibilityRemoteUIElement>
       remote_accessibility_element_;
+#endif
 };
 
 }  // namespace
diff --git a/components/remote_cocoa/app_shim/bridged_native_widget_impl.mm b/components/remote_cocoa/app_shim/bridged_native_widget_impl.mm
index a9f2b8af7deeda5c5c05c6354ce83063c7261afb..402b6099126e0ac8dd3b6be5c0ffb1a1946dc139 100644
--- a/components/remote_cocoa/app_shim/bridged_native_widget_impl.mm
+++ b/components/remote_cocoa/app_shim/bridged_native_widget_impl.mm
@@ -543,10 +543,12 @@ void BridgedNativeWidgetImpl::CreateContentView(uint64_t ns_view_id,
   // this should be treated as an error and caught early.
   CHECK(bridged_view_);
 
+#ifndef MAS_BUILD
   // Send the accessibility tokens for the NSView now that it exists.
   host_->SetRemoteAccessibilityTokens(
       ui::RemoteAccessibility::GetTokenForLocalElement(window_),
       ui::RemoteAccessibility::GetTokenForLocalElement(bridged_view_));
+#endif
 
   // Beware: This view was briefly removed (in favor of a bare CALayer) in
   // crrev/c/1236675. The ordering of unassociated layers relative to NSView
diff --git a/content/app_shim_remote_cocoa/ns_view_bridge_factory_impl.mm b/content/app_shim_remote_cocoa/ns_view_bridge_factory_impl.mm
index 98b4efbae0082f912f0df3315724db567811d4a1..cc0b1c5b2e7af5cef916c6bb581d33663bc577c4 100644
--- a/content/app_shim_remote_cocoa/ns_view_bridge_factory_impl.mm
+++ b/content/app_shim_remote_cocoa/ns_view_bridge_factory_impl.mm
@@ -60,8 +60,10 @@ class RenderWidgetHostNSViewBridgeOwner
     return nil;
   }
   void SetAccessibilityWindow(NSWindow* window) override {
+#ifndef MAS_BUILD
     client_->SetRemoteAccessibilityWindowToken(
         ui::RemoteAccessibility::GetTokenForLocalElement(window));
+#endif
   }
 
   void ForwardKeyboardEvent(const NativeWebKeyboardEvent& key_event,
@@ -122,8 +124,10 @@ class RenderWidgetHostNSViewBridgeOwner
 
   mojom::RenderWidgetHostNSViewClientAssociatedPtr client_;
   std::unique_ptr<RenderWidgetHostNSViewBridgeLocal> bridge_;
+#ifndef MAS_BUILD
   base::scoped_nsobject<NSAccessibilityRemoteUIElement>
       remote_accessibility_element_;
+#endif
 
   DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostNSViewBridgeOwner);
 };
diff --git a/content/browser/renderer_host/render_widget_host_view_mac.h b/content/browser/renderer_host/render_widget_host_view_mac.h
index 9194753f4719183efbea0afcac85e4dfcd3e6036..69ca0507b18edc31afc08d198450c01254cc8110 100644
--- a/content/browser/renderer_host/render_widget_host_view_mac.h
+++ b/content/browser/renderer_host/render_widget_host_view_mac.h
@@ -44,7 +44,9 @@ class ScopedPasswordInputEnabler;
 
 @protocol RenderWidgetHostViewMacDelegate;
 
+#ifndef MAS_BUILD
 @class NSAccessibilityRemoteUIElement;
+#endif
 @class RenderWidgetHostViewCocoa;
 
 namespace content {
@@ -642,10 +644,12 @@ class CONTENT_EXPORT RenderWidgetHostViewMac
   // EnsureSurfaceSynchronizedForWebTest().
   uint32_t latest_capture_sequence_number_ = 0u;
 
+#ifndef MAS_BUILD
   // Remote accessibility objects corresponding to the NSWindow that this is
   // displayed to the user in.
   base::scoped_nsobject<NSAccessibilityRemoteUIElement>
       remote_window_accessible_;
+#endif
 
   // Used to force the NSApplication's focused accessibility element to be the
   // content::BrowserAccessibilityCocoa accessibility tree when the NSView for
diff --git a/content/browser/renderer_host/render_widget_host_view_mac.mm b/content/browser/renderer_host/render_widget_host_view_mac.mm
index a1138408c0985efc1334c9a92f7e875680b69a4a..2b10c5b8f6d4a7a091ef3dd9d8ff324e5663f768 100644
--- a/content/browser/renderer_host/render_widget_host_view_mac.mm
+++ b/content/browser/renderer_host/render_widget_host_view_mac.mm
@@ -244,8 +244,10 @@ RenderWidgetHostViewMac::~RenderWidgetHostViewMac() {
 void RenderWidgetHostViewMac::MigrateNSViewBridge(
     remote_cocoa::mojom::BridgeFactory* remote_cocoa_application,
     uint64_t parent_ns_view_id) {
+#ifndef MAS_BUILD
   // Destroy the previous remote accessibility element.
   remote_window_accessible_.reset();
+#endif
 
   // Disconnect from the previous bridge (this will have the effect of
   // destroying the associated bridge), and close the binding (to allow it
@@ -1390,8 +1392,10 @@ RenderWidgetHostViewMac::AccessibilityGetNativeViewAccessible() {
 
 gfx::NativeViewAccessible
 RenderWidgetHostViewMac::AccessibilityGetNativeViewAccessibleForWindow() {
+#ifndef MAS_BUILD
   if (remote_window_accessible_)
     return remote_window_accessible_.get();
+#endif
   return [cocoa_view() window];
 }
 
@@ -1431,9 +1435,11 @@ id RenderWidgetHostViewMac::GetFocusedBrowserAccessibilityElement() {
 }
 
 void RenderWidgetHostViewMac::SetAccessibilityWindow(NSWindow* window) {
+#ifndef MAS_BUILD
   // When running in-process, just use the NSView's NSWindow as its own
   // accessibility element.
   remote_window_accessible_.reset();
+#endif
 }
 
 bool RenderWidgetHostViewMac::SyncIsWidgetForMainFrame(
@@ -1914,12 +1920,14 @@ void RenderWidgetHostViewMac::StopSpeaking() {
 
 void RenderWidgetHostViewMac::SetRemoteAccessibilityWindowToken(
     const std::vector<uint8_t>& window_token) {
+#ifndef MAS_BUILD
   if (window_token.empty()) {
     remote_window_accessible_.reset();
   } else {
     remote_window_accessible_ =
         ui::RemoteAccessibility::GetRemoteElementFromToken(window_token);
   }
+#endif
 }
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/ui/base/BUILD.gn b/ui/base/BUILD.gn
index 20371e7cd4eb2b01d6d4411733428fa2ecf25a15..a039b947be9f458c1af3988ac9a2ac5c0a03edec 100644
--- a/ui/base/BUILD.gn
+++ b/ui/base/BUILD.gn
@@ -289,6 +289,13 @@ jumbo_component("base") {
     "window_open_disposition.h",
   ]
 
+  if (is_mas_build) {
+    sources -= [
+      "cocoa/remote_accessibility_api.h",
+      "cocoa/remote_accessibility_api.mm",
+    ]
+  }
+
   if (is_posix) {
     sources += [ "l10n/l10n_util_posix.cc" ]
   }
diff --git a/ui/base/cocoa/remote_accessibility_api.h b/ui/base/cocoa/remote_accessibility_api.h
index 2a58aebabb23443a2c11364af4988c573f3909ba..3424b6011e80e9c995519b6a8d652abd843d926b 100644
--- a/ui/base/cocoa/remote_accessibility_api.h
+++ b/ui/base/cocoa/remote_accessibility_api.h
@@ -11,6 +11,8 @@
 #include "base/mac/scoped_nsobject.h"
 #include "ui/base/ui_base_export.h"
 
+#ifndef MAS_BUILD
+
 @interface NSAccessibilityRemoteUIElement : NSObject
 + (void)registerRemoteUIProcessIdentifier:(int)pid;
 + (NSData*)remoteTokenForLocalUIElement:(id)element;
@@ -32,4 +34,6 @@ class UI_BASE_EXPORT RemoteAccessibility {
 
 }  // namespace ui
 
+#endif  // MAS_BUILD
+
 #endif  // UI_BASE_COCOA_REMOTE_ACCESSIBILITY_API_H_
diff --git a/ui/views/cocoa/bridged_native_widget_host_impl.h b/ui/views/cocoa/bridged_native_widget_host_impl.h
index eb874f0d4bcd6e08ddecfab5a0f9b18b562d6d92..ede669bc842f39062fe9e7af4605cbd285ece58e 100644
--- a/ui/views/cocoa/bridged_native_widget_host_impl.h
+++ b/ui/views/cocoa/bridged_native_widget_host_impl.h
@@ -28,7 +28,9 @@
 #include "ui/views/window/dialog_observer.h"
 
 @class NativeWidgetMacNSWindow;
+#ifndef MAS_BUILD
 @class NSAccessibilityRemoteUIElement;
+#endif
 @class NSView;
 
 namespace ui {
@@ -397,11 +399,13 @@ class VIEWS_EXPORT BridgedNativeWidgetHostImpl
   // process.
   remote_cocoa::mojom::BridgedNativeWidgetAssociatedPtr bridge_ptr_;
 
+#ifndef MAS_BUILD
   // Remote accessibility objects corresponding to the NSWindow and its root
   // NSView.
   base::scoped_nsobject<NSAccessibilityRemoteUIElement>
       remote_window_accessible_;
   base::scoped_nsobject<NSAccessibilityRemoteUIElement> remote_view_accessible_;
+#endif
 
   // Used to force the NSApplication's focused accessibility element to be the
   // views::Views accessibility tree when the NSView for this is focused.
diff --git a/ui/views/cocoa/bridged_native_widget_host_impl.mm b/ui/views/cocoa/bridged_native_widget_host_impl.mm
index dd3365aa841dc7d4d9c998ff097ffc8f21c811eb..f46c10998f29a15bedc246fd23b53d1b68653ff8 100644
--- a/ui/views/cocoa/bridged_native_widget_host_impl.mm
+++ b/ui/views/cocoa/bridged_native_widget_host_impl.mm
@@ -348,14 +348,22 @@ gfx::NativeViewAccessible
 BridgedNativeWidgetHostImpl::GetNativeViewAccessibleForNSView() const {
   if (bridge_impl_)
     return bridge_impl_->ns_view();
+#ifndef MAS_BUILD
   return remote_view_accessible_.get();
+#else
+  return nullptr;
+#endif
 }
 
 gfx::NativeViewAccessible
 BridgedNativeWidgetHostImpl::GetNativeViewAccessibleForNSWindow() const {
   if (bridge_impl_)
     return bridge_impl_->ns_window();
+#ifndef MAS_BUILD
   return remote_window_accessible_.get();
+#else
+  return nullptr;
+#endif
 }
 
 remote_cocoa::mojom::BridgedNativeWidget* BridgedNativeWidgetHostImpl::bridge()
@@ -1172,6 +1180,7 @@ void BridgedNativeWidgetHostImpl::OnFocusWindowToolbar() {
 void BridgedNativeWidgetHostImpl::SetRemoteAccessibilityTokens(
     const std::vector<uint8_t>& window_token,
     const std::vector<uint8_t>& view_token) {
+#ifndef MAS_BUILD
   remote_window_accessible_ =
       ui::RemoteAccessibility::GetRemoteElementFromToken(window_token);
   remote_view_accessible_ =
@@ -1179,14 +1188,17 @@ void BridgedNativeWidgetHostImpl::SetRemoteAccessibilityTokens(
   [remote_view_accessible_ setWindowUIElement:remote_window_accessible_.get()];
   [remote_view_accessible_
       setTopLevelUIElement:remote_window_accessible_.get()];
+#endif
 }
 
 bool BridgedNativeWidgetHostImpl::GetRootViewAccessibilityToken(
     int64_t* pid,
     std::vector<uint8_t>* token) {
+#ifndef MAS_BUILD
   *pid = getpid();
   id element_id = GetNativeViewAccessible();
   *token = ui::RemoteAccessibility::GetTokenForLocalElement(element_id);
+#endif
   return true;
 }
 
