From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 20 Sep 2018 17:48:49 -0700 Subject: mas: avoid usage of NSAccessibilityRemoteUIElement According to ccameron: NSAccessibilityRemoteUIElement is unnecessary for Electron's use-case. We use it for progressive web apps (where the AXTree is in the browser process, but macOS needs to think it's coming from the PWA process). I think it can just be chopped out -- if there are any side-effects, we should be able to work around them. diff --git a/components/remote_cocoa/app_shim/application_bridge.mm b/components/remote_cocoa/app_shim/application_bridge.mm index 447bc6bfb54484b30686db7fd9df0454f48316c8..a1b3140858ae9a456f1c07fbe60cf9ca55e78bb0 100644 --- a/components/remote_cocoa/app_shim/application_bridge.mm +++ b/components/remote_cocoa/app_shim/application_bridge.mm @@ -51,6 +51,7 @@ // NativeWidgetNSWindowHostHelper: id GetNativeViewAccessible() override { +#if !IS_MAS_BUILD() if (!remote_accessibility_element_) { base::ProcessId browser_pid = base::kNullProcessId; std::vector element_token; @@ -61,6 +62,9 @@ id GetNativeViewAccessible() override { ui::RemoteAccessibility::GetRemoteElementFromToken(element_token); } return remote_accessibility_element_.get(); +#else + return nil; +#endif } void DispatchKeyEvent(ui::KeyEvent* event) override { bool event_handled = false; @@ -99,8 +103,10 @@ void GetWordAt(const gfx::Point& location_in_content, mojo::AssociatedRemote text_input_host_remote_; std::unique_ptr bridge_; +#if !IS_MAS_BUILD() base::scoped_nsobject remote_accessibility_element_; +#endif }; } // namespace diff --git a/components/remote_cocoa/app_shim/native_widget_ns_window_bridge.mm b/components/remote_cocoa/app_shim/native_widget_ns_window_bridge.mm index bb0bb33745c9fa7c2c9b7ba0b03e832ad51f0829..de419f3226b586b122cb9b90d105d85a957c02d7 100644 --- a/components/remote_cocoa/app_shim/native_widget_ns_window_bridge.mm +++ b/components/remote_cocoa/app_shim/native_widget_ns_window_bridge.mm @@ -567,10 +567,12 @@ NSUInteger CountBridgedWindows(NSArray* child_windows) { // this should be treated as an error and caught early. CHECK(bridged_view_); +#if !IS_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 5fbbb7a1d56640a2bf540ecb148f358ef88c4ad2..7eb3433b6561bba527d9c69b4819e7c26388eb2d 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 @@ -77,6 +77,7 @@ explicit RenderWidgetHostNSViewBridgeOwner( // RenderWidgetHostNSViewHostHelper implementation. id GetAccessibilityElement() override { +#if !IS_MAS_BUILD() if (!remote_accessibility_element_) { base::ProcessId browser_pid = base::kNullProcessId; std::vector element_token; @@ -87,6 +88,9 @@ id GetAccessibilityElement() override { ui::RemoteAccessibility::GetRemoteElementFromToken(element_token); } return remote_accessibility_element_.get(); +#else + return nil; +#endif } id GetRootBrowserAccessibilityElement() override { @@ -100,8 +104,10 @@ id GetFocusedBrowserAccessibilityElement() override { return nil; } void SetAccessibilityWindow(NSWindow* window) override { +#if !IS_MAS_BUILD() host_->SetRemoteAccessibilityWindowToken( ui::RemoteAccessibility::GetTokenForLocalElement(window)); +#endif } void ForwardKeyboardEvent(const content::NativeWebKeyboardEvent& key_event, @@ -163,8 +169,10 @@ void SmartMagnify(const blink::WebGestureEvent& web_event) override { mojo::AssociatedRemote host_; std::unique_ptr bridge_; +#if !IS_MAS_BUILD() base::scoped_nsobject remote_accessibility_element_; +#endif }; } diff --git a/content/browser/accessibility/browser_accessibility_manager_mac.mm b/content/browser/accessibility/browser_accessibility_manager_mac.mm index 22e8250d7f87d1c34854aa07f96b229caa807e17..5575c479ac6aaa92b6a637781c1cdb472b54f95c 100644 --- a/content/browser/accessibility/browser_accessibility_manager_mac.mm +++ b/content/browser/accessibility/browser_accessibility_manager_mac.mm @@ -22,7 +22,9 @@ #include "ui/accelerated_widget_mac/accelerated_widget_mac.h" #include "ui/accessibility/ax_role_properties.h" #include "ui/accessibility/platform/ax_private_webkit_constants_mac.h" +#if !IS_MAS_BUILD() #include "ui/base/cocoa/remote_accessibility_api.h" +#endif namespace { @@ -617,6 +619,7 @@ void PostAnnouncementNotification(NSString* announcement) { if ([NSApp isActive]) return window == [NSApp accessibilityFocusedWindow]; +#if !IS_MAS_BUILD() // TODO(accessibility): We need a solution to the problem described below. // If the window is NSAccessibilityRemoteUIElement, there are some challenges: // 1. NSApp is the browser which spawned the PWA, and what it considers the @@ -644,6 +647,7 @@ void PostAnnouncementNotification(NSString* announcement) { // from within the app shim content. if ([window isKindOfClass:[NSAccessibilityRemoteUIElement class]]) return true; +#endif return false; } 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 a58b06e3fa5e9008e6013565c2a58fad906c8b79..8e679d886ca90636f63b1752296fc76405f562e5 100644 --- a/content/browser/renderer_host/render_widget_host_view_mac.h +++ b/content/browser/renderer_host/render_widget_host_view_mac.h @@ -52,7 +52,9 @@ class ScopedPasswordInputEnabler; @protocol RenderWidgetHostViewMacDelegate; +#if !IS_MAS_BUILD() @class NSAccessibilityRemoteUIElement; +#endif @class RenderWidgetHostViewCocoa; namespace content { @@ -670,10 +672,12 @@ class CONTENT_EXPORT RenderWidgetHostViewMac // EnsureSurfaceSynchronizedForWebTest(). uint32_t latest_capture_sequence_number_ = 0u; +#if !IS_MAS_BUILD() // Remote accessibility objects corresponding to the NSWindow that this is // displayed to the user in. base::scoped_nsobject 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 ab7e3a7428a2ea2ac9eb3f0e97cd4bce9c72f0ba..a557cc2b06e68e54817f154f2d1e6a7635958d0e 100644 --- a/content/browser/renderer_host/render_widget_host_view_mac.mm +++ b/content/browser/renderer_host/render_widget_host_view_mac.mm @@ -262,8 +262,10 @@ void RenderWidgetHostViewMac::MigrateNSViewBridge( remote_cocoa::mojom::Application* remote_cocoa_application, uint64_t parent_ns_view_id) { +#if !IS_MAS_BUILD() // Destroy the previous remote accessibility element. remote_window_accessible_.reset(); +#endif // Reset `ns_view_` before resetting `remote_ns_view_` to avoid dangling // pointers. `ns_view_` gets reinitialized later in this method. @@ -1580,8 +1582,10 @@ void CombineTextNodesAndMakeCallback(SpeechCallback callback, gfx::NativeViewAccessible RenderWidgetHostViewMac::AccessibilityGetNativeViewAccessibleForWindow() { +#if !IS_MAS_BUILD() if (remote_window_accessible_) return remote_window_accessible_.get(); +#endif return [GetInProcessNSView() window]; } @@ -1629,9 +1633,11 @@ void CombineTextNodesAndMakeCallback(SpeechCallback callback, } void RenderWidgetHostViewMac::SetAccessibilityWindow(NSWindow* window) { +#if !IS_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( @@ -2127,20 +2133,26 @@ void CombineTextNodesAndMakeCallback(SpeechCallback callback, void RenderWidgetHostViewMac::GetRenderWidgetAccessibilityToken( GetRenderWidgetAccessibilityTokenCallback callback) { base::ProcessId pid = getpid(); +#if !IS_MAS_BUILD() id element_id = GetNativeViewAccessible(); std::vector token = ui::RemoteAccessibility::GetTokenForLocalElement(element_id); +#else + std::vector token; +#endif std::move(callback).Run(pid, token); } void RenderWidgetHostViewMac::SetRemoteAccessibilityWindowToken( const std::vector& window_token) { +#if !IS_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 acd06385277ac91670e5aa3ad386ccb6740a5a52..8ed2cd062b2e9531742835c61f015975c7d59d40 100644 --- a/ui/base/BUILD.gn +++ b/ui/base/BUILD.gn @@ -354,6 +354,13 @@ component("base") { sources += [ "resource/resource_bundle_lacros.cc" ] } + if (is_mas_build) { + sources -= [ + "cocoa/remote_accessibility_api.h", + "cocoa/remote_accessibility_api.mm", + ] + } + if (is_ios) { sources += [ "device_form_factor_ios.mm", diff --git a/ui/base/cocoa/remote_accessibility_api.h b/ui/base/cocoa/remote_accessibility_api.h index 4d47115d3f72da17b2ada8866770ac24717c29da..506d7847d904478793d992dbe548a61177644d09 100644 --- a/ui/base/cocoa/remote_accessibility_api.h +++ b/ui/base/cocoa/remote_accessibility_api.h @@ -11,6 +11,8 @@ #include "base/component_export.h" #include "base/mac/scoped_nsobject.h" +#if !IS_MAS_BUILD() + @interface NSAccessibilityRemoteUIElement : NSObject + (void)setRemoteUIApp:(BOOL)flag; + (void)registerRemoteUIProcessIdentifier:(int)pid; @@ -33,4 +35,6 @@ class COMPONENT_EXPORT(UI_BASE) RemoteAccessibility { } // namespace ui +#endif // MAS_BUILD + #endif // UI_BASE_COCOA_REMOTE_ACCESSIBILITY_API_H_ diff --git a/ui/views/cocoa/native_widget_mac_ns_window_host.h b/ui/views/cocoa/native_widget_mac_ns_window_host.h index 1c30148ec8f091e6e35a9f849cc1935cd10f2691..3ca2b624c03ca09b75b966d63f3559d0966e5d51 100644 --- a/ui/views/cocoa/native_widget_mac_ns_window_host.h +++ b/ui/views/cocoa/native_widget_mac_ns_window_host.h @@ -32,7 +32,9 @@ #include "ui/views/window/dialog_observer.h" @class NativeWidgetMacNSWindow; +#if !IS_MAS_BUILD() @class NSAccessibilityRemoteUIElement; +#endif @class NSView; namespace remote_cocoa { @@ -464,11 +466,13 @@ class VIEWS_EXPORT NativeWidgetMacNSWindowHost mojo::AssociatedRemote remote_ns_window_remote_; +#if !IS_MAS_BUILD() // Remote accessibility objects corresponding to the NSWindow and its root // NSView. base::scoped_nsobject remote_window_accessible_; base::scoped_nsobject 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/native_widget_mac_ns_window_host.mm b/ui/views/cocoa/native_widget_mac_ns_window_host.mm index 0cb9bea6f8d29ccacac658ccb419e3549cdc6af5..14fcb3e2daf451a7db8111aad550300235bcd168 100644 --- a/ui/views/cocoa/native_widget_mac_ns_window_host.mm +++ b/ui/views/cocoa/native_widget_mac_ns_window_host.mm @@ -337,14 +337,22 @@ void HandleAccelerator(const ui::Accelerator& accelerator, NativeWidgetMacNSWindowHost::GetNativeViewAccessibleForNSView() const { if (in_process_ns_window_bridge_) return in_process_ns_window_bridge_->ns_view(); +#if !IS_MAS_BUILD() return remote_view_accessible_.get(); +#else + return nullptr; +#endif } gfx::NativeViewAccessible NativeWidgetMacNSWindowHost::GetNativeViewAccessibleForNSWindow() const { if (in_process_ns_window_bridge_) return in_process_ns_window_bridge_->ns_window(); +#if !IS_MAS_BUILD() return remote_window_accessible_.get(); +#else + return nullptr; +#endif } remote_cocoa::mojom::NativeWidgetNSWindow* @@ -1350,6 +1358,7 @@ void HandleAccelerator(const ui::Accelerator& accelerator, void NativeWidgetMacNSWindowHost::SetRemoteAccessibilityTokens( const std::vector& window_token, const std::vector& view_token) { +#if !IS_MAS_BUILD() remote_window_accessible_ = ui::RemoteAccessibility::GetRemoteElementFromToken(window_token); remote_view_accessible_ = @@ -1358,14 +1367,17 @@ void HandleAccelerator(const ui::Accelerator& accelerator, [remote_view_accessible_ setTopLevelUIElement:remote_window_accessible_.get()]; [NSAccessibilityRemoteUIElement setRemoteUIApp:YES]; +#endif } bool NativeWidgetMacNSWindowHost::GetRootViewAccessibilityToken( base::ProcessId* pid, std::vector* token) { +#if !IS_MAS_BUILD() *pid = getpid(); id element_id = GetNativeViewAccessible(); *token = ui::RemoteAccessibility::GetTokenForLocalElement(element_id); +#endif return true; }