Don't use NSAccessibilityRemoteUIElement for WebContentsViewMac

Using this API may be causing crashes, and it was only added for code
cleanliness. Remove it.

TBR=ccameron@chromium.org, ellyjones

(cherry picked from commit cff221605266947be84c7057c61989c44962e0f7)

Bug: 924955
Change-Id: Id25bab33958381bd16ce4887c842943ccae99a34
Reviewed-on: https://chromium-review.googlesource.com/c/1437395
Commit-Queue: ccameron <ccameron@chromium.org>
Reviewed-by: ccameron <ccameron@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#626202}
Reviewed-on: https://chromium-review.googlesource.com/c/1450397
Cr-Commit-Position: refs/branch-heads/3683@{#126}
Cr-Branched-From: e51029943e0a38dd794b73caaf6373d5496ae783-refs/heads/master@{#625896}
diff --git a/content/browser/web_contents/web_contents_ns_view_bridge.h b/content/browser/web_contents/web_contents_ns_view_bridge.h
index 1d1c5e1..09b460fe6 100644
--- a/content/browser/web_contents/web_contents_ns_view_bridge.h
+++ b/content/browser/web_contents/web_contents_ns_view_bridge.h
@@ -14,7 +14,6 @@
 #include "content/common/content_export.h"
 #include "content/public/common/web_contents_ns_view_bridge.mojom.h"
 #include "ui/base/cocoa/ns_view_ids.h"
-#include "ui/base/cocoa/remote_accessibility_api.h"
 
 @class WebContentsViewCocoa;
 
@@ -51,8 +50,6 @@
 
  private:
   base::scoped_nsobject<WebContentsViewCocoa> cocoa_view_;
-  base::scoped_nsobject<NSAccessibilityRemoteUIElement>
-      parent_accessibility_element_;
   mojom::WebContentsNSViewClientAssociatedPtr client_;
 
   std::unique_ptr<ui::ScopedNSViewIdMapping> view_id_;
diff --git a/content/browser/web_contents/web_contents_ns_view_bridge.mm b/content/browser/web_contents/web_contents_ns_view_bridge.mm
index 44204e2..af7ead0 100644
--- a/content/browser/web_contents/web_contents_ns_view_bridge.mm
+++ b/content/browser/web_contents/web_contents_ns_view_bridge.mm
@@ -46,15 +46,12 @@
   CHECK(parent_ns_view);
   [parent_ns_view addSubview:cocoa_view_];
 
-  parent_accessibility_element_ =
-      ui::RemoteAccessibility::GetRemoteElementFromToken(parent_token);
-  [cocoa_view_ setAccessibilityParentElement:parent_accessibility_element_];
+  // TODO(https://crbug.com/924955): Ignore the accessibility token because
+  // using it causes crashes.
 }
 
 void WebContentsNSViewBridge::ResetParentNSView() {
   [cocoa_view_ removeFromSuperview];
-  [cocoa_view_ setAccessibilityParentElement:nil];
-  parent_accessibility_element_.reset();
 }
 
 void WebContentsNSViewBridge::SetBounds(const gfx::Rect& bounds_in_window) {
diff --git a/content/browser/web_contents/web_contents_view_mac.mm b/content/browser/web_contents/web_contents_view_mac.mm
index d4f6a36..7f4308b 100644
--- a/content/browser/web_contents/web_contents_view_mac.mm
+++ b/content/browser/web_contents/web_contents_view_mac.mm
@@ -30,7 +30,6 @@
 #include "mojo/public/cpp/bindings/interface_request.h"
 #include "ui/base/cocoa/cocoa_base_utils.h"
 #include "ui/base/cocoa/ns_view_ids.h"
-#include "ui/base/cocoa/remote_accessibility_api.h"
 #include "ui/gfx/image/image_skia_util_mac.h"
 #include "ui/gfx/mac/coordinate_conversion.h"
 
@@ -533,8 +532,12 @@
 
 void WebContentsViewMac::ViewsHostableAttach(ViewsHostableView::Host* host) {
   views_host_ = host;
-  std::vector<uint8_t> token = ui::RemoteAccessibility::GetTokenForLocalElement(
-      views_host_->GetAccessibilityElement());
+  // TODO(https://crbug.com/924955): Using the remote accessibility to set
+  // the parent accessibility element here causes crashes, so just set it
+  // directly on the in-process WebContentsViewCocoa only.
+  std::vector<uint8_t> token;
+  [cocoa_view()
+      setAccessibilityParentElement:views_host_->GetAccessibilityElement()];
 
   // Create an NSView in the target process, if one exists.
   uint64_t factory_host_id = views_host_->GetViewsFactoryHostId();
@@ -578,6 +581,7 @@
     // Permit the in-process NSView to call back into |this| again.
     [cocoa_view() setClient:this];
   }
+  [cocoa_view() setAccessibilityParentElement:nil];
   ns_view_bridge_local_->SetVisible(false);
   ns_view_bridge_local_->ResetParentNSView();
   views_host_ = nullptr;
diff --git a/ui/base/cocoa/remote_accessibility_api.mm b/ui/base/cocoa/remote_accessibility_api.mm
index 8a6d9d1..b901d78 100644
--- a/ui/base/cocoa/remote_accessibility_api.mm
+++ b/ui/base/cocoa/remote_accessibility_api.mm
@@ -18,6 +18,8 @@
 base::scoped_nsobject<NSAccessibilityRemoteUIElement>
 RemoteAccessibility::GetRemoteElementFromToken(
     const std::vector<uint8_t>& token) {
+  if (token.empty())
+    return base::scoped_nsobject<NSAccessibilityRemoteUIElement>();
   base::scoped_nsobject<NSData> data(
       [[NSData alloc] initWithBytes:token.data() length:token.size()]);
   return base::scoped_nsobject<NSAccessibilityRemoteUIElement>(