diff --git a/chromium_src/BUILD.gn b/chromium_src/BUILD.gn index 7df57ee1bb..7d79f15968 100644 --- a/chromium_src/BUILD.gn +++ b/chromium_src/BUILD.gn @@ -12,6 +12,8 @@ import("//third_party/widevine/cdm/widevine.gni") static_library("chrome") { visibility = [ "//electron:electron_lib" ] sources = [ + "//chrome/browser/accessibility/accessibility_ui.cc", + "//chrome/browser/accessibility/accessibility_ui.h", "//chrome/browser/browser_process.cc", "//chrome/browser/browser_process.h", "//chrome/browser/crash_upload_list/crash_upload_list_crashpad.cc", @@ -66,6 +68,7 @@ static_library("chrome") { "//chrome/common", "//chrome/common:version_header", "//components/keyed_service/content", + "//components/paint_preview/buildflags", "//components/proxy_config", "//components/security_state/content", "//content/public/browser", diff --git a/electron_paks.gni b/electron_paks.gni index c373f0e466..1e938d786b 100644 --- a/electron_paks.gni +++ b/electron_paks.gni @@ -54,6 +54,7 @@ template("electron_extra_paks") { ]) output = "${invoker.output_dir}/resources.pak" sources = [ + "$root_gen_dir/chrome/dev_ui_browser_resources.pak", "$root_gen_dir/components/components_resources.pak", "$root_gen_dir/content/browser/resources/media/media_internals_resources.pak", "$root_gen_dir/content/browser/tracing/tracing_resources.pak", @@ -67,6 +68,7 @@ template("electron_extra_paks") { "$target_gen_dir/electron_resources.pak", ] deps = [ + "//chrome/browser:dev_ui_browser_resources", "//components/resources", "//content:content_resources", "//content:dev_ui_content_resources", diff --git a/filenames.gni b/filenames.gni index 33d19ed2a9..26de2d0600 100644 --- a/filenames.gni +++ b/filenames.gni @@ -398,6 +398,8 @@ filenames = { "shell/browser/ui/views/submenu_button.h", "shell/browser/ui/views/win_frame_view.cc", "shell/browser/ui/views/win_frame_view.h", + "shell/browser/ui/webui/accessibility_ui.cc", + "shell/browser/ui/webui/accessibility_ui.h", "shell/browser/ui/win/dialog_thread.cc", "shell/browser/ui/win/dialog_thread.h", "shell/browser/ui/win/electron_desktop_native_widget_aura.cc", diff --git a/patches/chromium/.patches b/patches/chromium/.patches index ba92a5ceeb..655e2b2648 100644 --- a/patches/chromium/.patches +++ b/patches/chromium/.patches @@ -95,3 +95,5 @@ allow_setting_secondary_label_via_simplemenumodel.patch fix_swap_global_proxies_before_initializing_the_windows_proxies.patch feat_add_streaming-protocol_registry_to_multibuffer_data_source.patch use_electron_resources_in_icon_reader_service.patch +fix_patch_out_profile_refs_in_accessibility_ui.patch +fix_accessibility_label_id_mismatch.patch diff --git a/patches/chromium/fix_accessibility_label_id_mismatch.patch b/patches/chromium/fix_accessibility_label_id_mismatch.patch new file mode 100644 index 0000000000..8124b03d26 --- /dev/null +++ b/patches/chromium/fix_accessibility_label_id_mismatch.patch @@ -0,0 +1,28 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Shelley Vohr +Date: Tue, 14 Jul 2020 09:54:17 -0700 +Subject: Fix accessibility label ID mismatch + +Refs https://chromium-review.googlesource.com/c/chromium/src/+/2231678. + +When the label was updated to use underscores, the associated jQuery +call in accessibility.js was not likewise updated, so it would error +with: + +"Uncaught TypeError: Cannot read property 'value' of null" + +Upstreamed at https://chromium-review.googlesource.com/c/chromium/src/+/2296883 + +diff --git a/chrome/browser/resources/accessibility/accessibility.js b/chrome/browser/resources/accessibility/accessibility.js +index d0b0b7f9b922f9c5af180e1bfb22b886b51f90f7..bfa746233310a1150382750030f565fa81e93361 100644 +--- a/chrome/browser/resources/accessibility/accessibility.js ++++ b/chrome/browser/resources/accessibility/accessibility.js +@@ -80,7 +80,7 @@ cr.define('accessibility', function() { + // function with the result. + const requestType = element.id.split(':')[1]; + if (data.type == 'browser') { +- const delay = $('native_ui_delay').value; ++ const delay = $('native-ui-delay').value; + setTimeout(() => { + chrome.send( + 'requestNativeUITree', [{ diff --git a/patches/chromium/fix_patch_out_profile_refs_in_accessibility_ui.patch b/patches/chromium/fix_patch_out_profile_refs_in_accessibility_ui.patch new file mode 100644 index 0000000000..e4a351eb8a --- /dev/null +++ b/patches/chromium/fix_patch_out_profile_refs_in_accessibility_ui.patch @@ -0,0 +1,150 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Shelley Vohr +Date: Mon, 6 Jul 2020 13:46:06 -0700 +Subject: fix: patch out Profile refs in accessibility_ui + +This tweaks Chrome's Accessibility support at chrome://accessibility +to make it usable from Electron by removing Profile references. + +diff --git a/chrome/browser/accessibility/accessibility_ui.cc b/chrome/browser/accessibility/accessibility_ui.cc +index acec47527e3f14e3c4b4f262da9995a95e6d3355..3a9434cbe7d86a0e99598c64ea756f07b241f74d 100644 +--- a/chrome/browser/accessibility/accessibility_ui.cc ++++ b/chrome/browser/accessibility/accessibility_ui.cc +@@ -19,7 +19,9 @@ + #include "base/strings/utf_string_conversions.h" + #include "base/values.h" + #include "build/build_config.h" ++#if 0 + #include "chrome/browser/profiles/profile.h" ++#endif + #include "chrome/common/chrome_features.h" + #include "chrome/common/pref_names.h" + #include "chrome/common/webui_url_constants.h" +@@ -44,7 +46,7 @@ + #include "ui/accessibility/platform/ax_platform_node_delegate.h" + #include "ui/base/webui/web_ui_util.h" + +-#if !defined(OS_ANDROID) ++#if 0 + #include "chrome/browser/ui/browser.h" + #include "chrome/browser/ui/browser_list.h" + #include "chrome/browser/ui/browser_window.h" +@@ -146,7 +148,7 @@ std::unique_ptr BuildTargetDescriptor( + accessibility_mode); + } + +-#if !defined(OS_ANDROID) ++#if 0 + std::unique_ptr BuildTargetDescriptor(Browser* browser) { + std::unique_ptr target_data( + new base::DictionaryValue()); +@@ -169,7 +171,9 @@ void HandleAccessibilityRequestCallback( + DCHECK(ShouldHandleAccessibilityRequestCallback(path)); + + base::DictionaryValue data; ++#if 0 + PrefService* pref = Profile::FromBrowserContext(current_context)->GetPrefs(); ++#endif + ui::AXMode mode = + content::BrowserAccessibilityState::GetInstance()->GetAccessibilityMode(); + bool is_native_enabled = content::BrowserAccessibilityState::GetInstance() +@@ -198,9 +202,7 @@ void HandleAccessibilityRequestCallback( + // The "labelImages" flag works only if "web" is enabled, the current profile + // has the kAccessibilityImageLabelsEnabled preference set and the appropriate + // command line switch has been used. +- bool are_accessibility_image_labels_enabled = +- is_web_enabled && +- pref->GetBoolean(prefs::kAccessibilityImageLabelsEnabled); ++ bool are_accessibility_image_labels_enabled = is_web_enabled; + bool label_images = mode.has_mode(ui::AXMode::kLabelImages); + data.SetString(kLabelImages, are_accessibility_image_labels_enabled + ? (label_images ? kOn : kOff) +@@ -209,7 +211,7 @@ void HandleAccessibilityRequestCallback( + // The "pdf" flag is independent of the others. + data.SetString(kPDF, pdf ? kOn : kOff); + +- bool show_internal = pref->GetBoolean(prefs::kShowInternalAccessibilityTree); ++ bool show_internal = true; + data.SetString(kInternal, show_internal ? kOn : kOff); + + std::unique_ptr rvh_list(new base::ListValue()); +@@ -246,11 +248,11 @@ void HandleAccessibilityRequestCallback( + data.Set(kPagesField, std::move(rvh_list)); + + std::unique_ptr browser_list(new base::ListValue()); +-#if !defined(OS_ANDROID) ++#if 0 + for (Browser* browser : *BrowserList::GetInstance()) { + browser_list->Append(BuildTargetDescriptor(browser)); + } +-#endif // !defined(OS_ANDROID) ++#endif // !defined(OS_ANDROID) + data.Set(kBrowsersField, std::move(browser_list)); + + std::string json_string; +@@ -477,8 +479,10 @@ void AccessibilityUIMessageHandler::SetGlobalFlag(const base::ListValue* args) { + + AllowJavascript(); + if (flag_name_str == kInternal) { ++#if 0 + PrefService* pref = Profile::FromWebUI(web_ui())->GetPrefs(); + pref->SetBoolean(prefs::kShowInternalAccessibilityTree, enabled); ++#endif + return; + } + +@@ -581,11 +585,12 @@ void AccessibilityUIMessageHandler::RequestWebContentsTree( + content::AccessibilityTreeFormatter::PropertyFilter::ALLOW_EMPTY); + AddPropertyFilters(property_filters, deny, + content::AccessibilityTreeFormatter::PropertyFilter::DENY); +- ++#if 0 + PrefService* pref = Profile::FromWebUI(web_ui())->GetPrefs(); + bool internal = pref->GetBoolean(prefs::kShowInternalAccessibilityTree); ++#endif + base::string16 accessibility_contents_utf16 = +- web_contents->DumpAccessibilityTree(internal, property_filters); ++ web_contents->DumpAccessibilityTree(true, property_filters); + result->SetString(kTreeField, + base::UTF16ToUTF8(accessibility_contents_utf16)); + CallJavascriptFunction(request_type, *(result.get())); +@@ -626,7 +631,8 @@ void AccessibilityUIMessageHandler::RequestNativeUITree( + content::AccessibilityTreeFormatter::PropertyFilter::ALLOW_EMPTY); + AddPropertyFilters(property_filters, deny, + content::AccessibilityTreeFormatter::PropertyFilter::DENY); +- ++#endif ++#if 0 + for (Browser* browser : *BrowserList::GetInstance()) { + if (browser->session_id().id() == session_id) { + std::unique_ptr result( +@@ -707,5 +713,7 @@ void AccessibilityUIMessageHandler::RequestAccessibilityEvents( + // static + void AccessibilityUIMessageHandler::RegisterProfilePrefs( + user_prefs::PrefRegistrySyncable* registry) { ++#if 0 + registry->RegisterBooleanPref(prefs::kShowInternalAccessibilityTree, false); ++#endif + } +diff --git a/chrome/browser/accessibility/accessibility_ui.h b/chrome/browser/accessibility/accessibility_ui.h +index 906b24117cdf584dc4935e81c61fe664c48d552b..7a542240c77877a19d0e80649605a1fee393c50f 100644 +--- a/chrome/browser/accessibility/accessibility_ui.h ++++ b/chrome/browser/accessibility/accessibility_ui.h +@@ -24,6 +24,8 @@ struct AXEventNotificationDetails; + class WebContents; + } // namespace content + ++class ElectronAccessibilityUIMessageHandler; ++ + namespace user_prefs { + class PrefRegistrySyncable; + } // namespace user_prefs +@@ -60,6 +62,8 @@ class AccessibilityUIMessageHandler : public content::WebUIMessageHandler { + static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); + + private: ++ friend class ElectronAccessibilityUIMessageHandler; ++ + std::vector event_logs_; + std::unique_ptr observer_; + diff --git a/shell/browser/electron_web_ui_controller_factory.cc b/shell/browser/electron_web_ui_controller_factory.cc index 385a1c0ef5..150c061b9e 100644 --- a/shell/browser/electron_web_ui_controller_factory.cc +++ b/shell/browser/electron_web_ui_controller_factory.cc @@ -10,6 +10,7 @@ #include "content/public/browser/web_contents.h" #include "electron/buildflags/buildflags.h" #include "shell/browser/ui/devtools_ui.h" +#include "shell/browser/ui/webui/accessibility_ui.h" namespace electron { @@ -25,7 +26,8 @@ ElectronWebUIControllerFactory::~ElectronWebUIControllerFactory() = default; content::WebUI::TypeID ElectronWebUIControllerFactory::GetWebUIType( content::BrowserContext* browser_context, const GURL& url) { - if (url.host() == chrome::kChromeUIDevToolsHost) { + if (url.host() == chrome::kChromeUIDevToolsHost || + url.host() == chrome::kChromeUIAccessibilityHost) { return const_cast(this); } @@ -51,7 +53,10 @@ ElectronWebUIControllerFactory::CreateWebUIControllerForURL( if (url.host() == chrome::kChromeUIDevToolsHost) { auto* browser_context = web_ui->GetWebContents()->GetBrowserContext(); return std::make_unique(browser_context, web_ui); + } else if (url.host() == chrome::kChromeUIAccessibilityHost) { + return std::make_unique(web_ui); } + return std::unique_ptr(); } diff --git a/shell/browser/native_window.cc b/shell/browser/native_window.cc index 760d3aba91..43ce30783d 100644 --- a/shell/browser/native_window.cc +++ b/shell/browser/native_window.cc @@ -49,6 +49,8 @@ gfx::Size GetExpandedWindowSize(const NativeWindow* window, gfx::Size size) { NativeWindow::NativeWindow(const gin_helper::Dictionary& options, NativeWindow* parent) : widget_(new views::Widget), parent_(parent), weak_factory_(this) { + ++next_id_; + options.Get(options::kFrame, &has_frame_); options.Get(options::kTransparent, &transparent_); options.Get(options::kEnableLargerThanScreen, &enable_larger_than_screen_); @@ -606,6 +608,9 @@ std::string NativeWindow::GetAccessibleTitle() { return base::UTF16ToUTF8(accessible_title_); } +// static +int32_t NativeWindow::next_id_ = 0; + // static void NativeWindowRelay::CreateForWebContents( content::WebContents* web_contents, diff --git a/shell/browser/native_window.h b/shell/browser/native_window.h index 67d13d2309..a520a1af42 100644 --- a/shell/browser/native_window.h +++ b/shell/browser/native_window.h @@ -308,6 +308,8 @@ class NativeWindow : public base::SupportsUserData, std::list browser_views() const { return browser_views_; } + int32_t window_id() const { return next_id_; } + protected: NativeWindow(const gin_helper::Dictionary& options, NativeWindow* parent); @@ -329,6 +331,8 @@ class NativeWindow : public base::SupportsUserData, private: std::unique_ptr widget_; + static int32_t next_id_; + // The content view, weak ref. views::View* content_view_ = nullptr; diff --git a/shell/browser/ui/webui/accessibility_ui.cc b/shell/browser/ui/webui/accessibility_ui.cc new file mode 100644 index 0000000000..fe3c1ca92b --- /dev/null +++ b/shell/browser/ui/webui/accessibility_ui.cc @@ -0,0 +1,427 @@ +// Copyright (c) 2020 Microsoft, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "shell/browser/ui/webui/accessibility_ui.h" + +#include + +#include "base/bind.h" +#include "base/bind_helpers.h" +#include "base/command_line.h" +#include "base/json/json_writer.h" +#include "base/optional.h" +#include "base/strings/pattern.h" +#include "base/strings/string_number_conversions.h" +#include "base/strings/utf_string_conversions.h" +#include "base/values.h" +#include "build/build_config.h" +#include "chrome/common/chrome_features.h" +#include "chrome/common/pref_names.h" +#include "chrome/common/webui_url_constants.h" +#include "chrome/grit/dev_ui_browser_resources.h" // nogncheck +#include "components/pref_registry/pref_registry_syncable.h" +#include "components/prefs/pref_service.h" +#include "content/public/browser/accessibility_tree_formatter.h" +#include "content/public/browser/ax_event_notification_details.h" +#include "content/public/browser/browser_accessibility_state.h" +#include "content/public/browser/browser_thread.h" +#include "content/public/browser/favicon_status.h" +#include "content/public/browser/navigation_entry.h" +#include "content/public/browser/render_process_host.h" +#include "content/public/browser/render_view_host.h" +#include "content/public/browser/render_widget_host.h" +#include "content/public/browser/render_widget_host_iterator.h" +#include "content/public/browser/web_contents.h" +#include "content/public/browser/web_contents_delegate.h" +#include "content/public/browser/web_ui_data_source.h" +#include "net/base/escape.h" +#include "shell/browser/native_window.h" +#include "shell/browser/window_list.h" +#include "ui/accessibility/platform/ax_platform_node.h" +#include "ui/accessibility/platform/ax_platform_node_delegate.h" +#include "ui/base/webui/web_ui_util.h" + +namespace { + +static const char kTargetsDataFile[] = "targets-data.json"; + +static const char kAccessibilityModeField[] = "a11yMode"; +static const char kBrowsersField[] = "browsers"; +static const char kErrorField[] = "error"; +static const char kFaviconUrlField[] = "faviconUrl"; +static const char kNameField[] = "name"; +static const char kPagesField[] = "pages"; +static const char kPidField[] = "pid"; +static const char kSessionIdField[] = "sessionId"; +static const char kProcessIdField[] = "processId"; +static const char kRequestTypeField[] = "requestType"; +static const char kRoutingIdField[] = "routingId"; +static const char kTypeField[] = "type"; +static const char kUrlField[] = "url"; +static const char kTreeField[] = "tree"; + +// Global flags +static const char kBrowser[] = "browser"; +static const char kCopyTree[] = "copyTree"; +static const char kHTML[] = "html"; +static const char kInternal[] = "internal"; +static const char kLabelImages[] = "labelImages"; +static const char kNative[] = "native"; +static const char kPage[] = "page"; +static const char kPDF[] = "pdf"; +static const char kScreenReader[] = "screenreader"; +static const char kShowOrRefreshTree[] = "showOrRefreshTree"; +static const char kText[] = "text"; +static const char kWeb[] = "web"; + +// Possible global flag values +static const char kDisabled[] = "disabled"; +static const char kOff[] = "off"; +static const char kOn[] = "on"; + +std::unique_ptr BuildTargetDescriptor( + const GURL& url, + const std::string& name, + const GURL& favicon_url, + int process_id, + int routing_id, + ui::AXMode accessibility_mode, + base::ProcessHandle handle = base::kNullProcessHandle) { + std::unique_ptr target_data( + new base::DictionaryValue()); + target_data->SetInteger(kProcessIdField, process_id); + target_data->SetInteger(kRoutingIdField, routing_id); + target_data->SetString(kUrlField, url.spec()); + target_data->SetString(kNameField, net::EscapeForHTML(name)); + target_data->SetInteger(kPidField, base::GetProcId(handle)); + target_data->SetString(kFaviconUrlField, favicon_url.spec()); + target_data->SetInteger(kAccessibilityModeField, accessibility_mode.mode()); + target_data->SetString(kTypeField, kPage); + return target_data; +} + +std::unique_ptr BuildTargetDescriptor( + content::RenderViewHost* rvh) { + content::WebContents* web_contents = + content::WebContents::FromRenderViewHost(rvh); + ui::AXMode accessibility_mode; + + std::string title; + GURL url; + GURL favicon_url; + if (web_contents) { + url = web_contents->GetURL(); + title = base::UTF16ToUTF8(web_contents->GetTitle()); + content::NavigationController& controller = web_contents->GetController(); + content::NavigationEntry* entry = controller.GetVisibleEntry(); + if (entry != nullptr && entry->GetURL().is_valid()) { + gfx::Image favicon_image = entry->GetFavicon().image; + if (!favicon_image.IsEmpty()) { + const SkBitmap* favicon_bitmap = favicon_image.ToSkBitmap(); + favicon_url = GURL(webui::GetBitmapDataUrl(*favicon_bitmap)); + } + } + accessibility_mode = web_contents->GetAccessibilityMode(); + } + + return BuildTargetDescriptor(url, title, favicon_url, + rvh->GetProcess()->GetID(), rvh->GetRoutingID(), + accessibility_mode); +} + +std::unique_ptr BuildTargetDescriptor( + electron::NativeWindow* window) { + std::unique_ptr target_data( + new base::DictionaryValue()); + target_data->SetInteger(kSessionIdField, window->window_id()); + target_data->SetString(kNameField, window->GetTitle()); + target_data->SetString(kTypeField, kBrowser); + return target_data; +} + +bool ShouldHandleAccessibilityRequestCallback(const std::string& path) { + return path == kTargetsDataFile; +} + +// Add property filters to the property_filters vector for the given property +// filter type. The attributes are passed in as a string with each attribute +// separated by a space. +void AddPropertyFilters( + std::vector& + property_filters, + const std::string& attributes, + content::AccessibilityTreeFormatter::PropertyFilter::Type type) { + for (const std::string& attribute : base::SplitString( + attributes, " ", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) { + property_filters.push_back( + content::AccessibilityTreeFormatter::PropertyFilter( + base::ASCIIToUTF16(attribute), type)); + } +} + +bool MatchesPropertyFilters( + const std::vector& + property_filters, + const base::string16& text) { + bool allow = false; + for (const auto& filter : property_filters) { + if (base::MatchPattern(text, filter.match_str)) { + switch (filter.type) { + case content::AccessibilityTreeFormatter::PropertyFilter::ALLOW_EMPTY: + allow = true; + break; + case content::AccessibilityTreeFormatter::PropertyFilter::ALLOW: + allow = (!base::MatchPattern(text, base::UTF8ToUTF16("*=''"))); + break; + case content::AccessibilityTreeFormatter::PropertyFilter::DENY: + allow = false; + break; + } + } + } + return allow; +} + +std::string RecursiveDumpAXPlatformNodeAsString( + ui::AXPlatformNode* node, + int indent, + const std::vector& + property_filters) { + if (!node) + return ""; + std::string str(2 * indent, '+'); + std::string line = node->GetDelegate()->GetData().ToString(); + std::vector attributes = base::SplitString( + line, " ", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); + for (std::string attribute : attributes) { + if (MatchesPropertyFilters(property_filters, + base::UTF8ToUTF16(attribute))) { + str += attribute + " "; + } + } + str += "\n"; + for (int i = 0; i < node->GetDelegate()->GetChildCount(); i++) { + gfx::NativeViewAccessible child = node->GetDelegate()->ChildAtIndex(i); + ui::AXPlatformNode* child_node = + ui::AXPlatformNode::FromNativeViewAccessible(child); + str += RecursiveDumpAXPlatformNodeAsString(child_node, indent + 1, + property_filters); + } + return str; +} + +bool IsValidJSValue(const std::string* str) { + return str && str->length() < 5000U; +} + +void HandleAccessibilityRequestCallback( + content::BrowserContext* current_context, + const std::string& path, + content::WebUIDataSource::GotDataCallback callback) { + DCHECK(ShouldHandleAccessibilityRequestCallback(path)); + + base::DictionaryValue data; + ui::AXMode mode = + content::BrowserAccessibilityState::GetInstance()->GetAccessibilityMode(); + bool is_native_enabled = content::BrowserAccessibilityState::GetInstance() + ->IsRendererAccessibilityEnabled(); + bool native = mode.has_mode(ui::AXMode::kNativeAPIs); + bool web = mode.has_mode(ui::AXMode::kWebContents); + bool text = mode.has_mode(ui::AXMode::kInlineTextBoxes); + bool screenreader = mode.has_mode(ui::AXMode::kScreenReader); + bool html = mode.has_mode(ui::AXMode::kHTML); + bool pdf = mode.has_mode(ui::AXMode::kPDF); + + // The "native" and "web" flags are disabled if + // --disable-renderer-accessibility is set. + data.SetString(kNative, + is_native_enabled ? (native ? kOn : kOff) : kDisabled); + data.SetString(kWeb, is_native_enabled ? (web ? kOn : kOff) : kDisabled); + + // The "text", "screenreader" and "html" flags are only + // meaningful if "web" is enabled. + bool is_web_enabled = is_native_enabled && web; + data.SetString(kText, is_web_enabled ? (text ? kOn : kOff) : kDisabled); + data.SetString(kScreenReader, + is_web_enabled ? (screenreader ? kOn : kOff) : kDisabled); + data.SetString(kHTML, is_web_enabled ? (html ? kOn : kOff) : kDisabled); + + // TODO(codebytere): enable use of this flag. + // + // The "labelImages" flag works only if "web" is enabled, the current profile + // has the kAccessibilityImageLabelsEnabled preference set and the appropriate + // command line switch has been used. Since this is so closely tied into user + // prefs and causes bugs, we're disabling it for now. + bool are_accessibility_image_labels_enabled = is_web_enabled; + data.SetString(kLabelImages, kDisabled); + + // The "pdf" flag is independent of the others. + data.SetString(kPDF, pdf ? kOn : kOff); + + // Always dump the Accessibility tree. + data.SetString(kInternal, kOn); + + std::unique_ptr rvh_list(new base::ListValue()); + std::unique_ptr widgets( + content::RenderWidgetHost::GetRenderWidgetHosts()); + + while (content::RenderWidgetHost* widget = widgets->GetNextHost()) { + // Ignore processes that don't have a connection, such as crashed tabs. + if (!widget->GetProcess()->IsInitializedAndNotDead()) + continue; + content::RenderViewHost* rvh = content::RenderViewHost::From(widget); + if (!rvh) + continue; + content::WebContents* web_contents = + content::WebContents::FromRenderViewHost(rvh); + content::WebContentsDelegate* delegate = web_contents->GetDelegate(); + if (!delegate) + continue; + // Ignore views that are never user-visible, like background pages. + if (delegate->IsNeverComposited(web_contents)) + continue; + content::BrowserContext* context = rvh->GetProcess()->GetBrowserContext(); + if (context != current_context) + continue; + + std::unique_ptr descriptor = + BuildTargetDescriptor(rvh); + descriptor->SetBoolean(kNative, is_native_enabled); + descriptor->SetBoolean(kWeb, is_web_enabled); + descriptor->SetBoolean(kLabelImages, + are_accessibility_image_labels_enabled); + rvh_list->Append(std::move(descriptor)); + } + + data.Set(kPagesField, std::move(rvh_list)); + + std::unique_ptr window_list(new base::ListValue()); + for (auto* window : electron::WindowList::GetWindows()) { + window_list->Append(BuildTargetDescriptor(window)); + } + + data.Set(kBrowsersField, std::move(window_list)); + + std::string json_string; + base::JSONWriter::Write(data, &json_string); + + std::move(callback).Run(base::RefCountedString::TakeString(&json_string)); +} + +} // namespace + +ElectronAccessibilityUI::ElectronAccessibilityUI(content::WebUI* web_ui) + : content::WebUIController(web_ui) { + // Set up the chrome://accessibility source. + content::WebUIDataSource* html_source = + content::WebUIDataSource::Create(chrome::kChromeUIAccessibilityHost); + + // Add required resources. + html_source->UseStringsJs(); + html_source->AddResourcePath("accessibility.css", IDR_ACCESSIBILITY_CSS); + html_source->AddResourcePath("accessibility.js", IDR_ACCESSIBILITY_JS); + html_source->SetDefaultResource(IDR_ACCESSIBILITY_HTML); + html_source->SetRequestFilter( + base::BindRepeating(&ShouldHandleAccessibilityRequestCallback), + base::BindRepeating(&HandleAccessibilityRequestCallback, + web_ui->GetWebContents()->GetBrowserContext())); + + content::BrowserContext* browser_context = + web_ui->GetWebContents()->GetBrowserContext(); + content::WebUIDataSource::Add(browser_context, html_source); + + web_ui->AddMessageHandler( + std::make_unique()); +} + +ElectronAccessibilityUI::~ElectronAccessibilityUI() = default; + +ElectronAccessibilityUIMessageHandler::ElectronAccessibilityUIMessageHandler() = + default; + +void ElectronAccessibilityUIMessageHandler::RequestNativeUITree( + const base::ListValue* args) { + const base::DictionaryValue* data; + CHECK(args->GetDictionary(0, &data)); + + int window_id = *data->FindIntPath(kSessionIdField); + const std::string* request_type_p = data->FindStringPath(kRequestTypeField); + CHECK(IsValidJSValue(request_type_p)); + std::string request_type = *request_type_p; + CHECK(request_type == kShowOrRefreshTree || request_type == kCopyTree); + request_type = "accessibility." + request_type; + + const std::string* allow_p = data->FindStringPath("filters.allow"); + CHECK(IsValidJSValue(allow_p)); + std::string allow = *allow_p; + const std::string* allow_empty_p = data->FindStringPath("filters.allowEmpty"); + CHECK(IsValidJSValue(allow_empty_p)); + std::string allow_empty = *allow_empty_p; + const std::string* deny_p = data->FindStringPath("filters.deny"); + CHECK(IsValidJSValue(deny_p)); + std::string deny = *deny_p; + + AllowJavascript(); + + std::vector + property_filters; + AddPropertyFilters( + property_filters, allow, + content::AccessibilityTreeFormatter::PropertyFilter::ALLOW); + AddPropertyFilters( + property_filters, allow_empty, + content::AccessibilityTreeFormatter::PropertyFilter::ALLOW_EMPTY); + AddPropertyFilters(property_filters, deny, + content::AccessibilityTreeFormatter::PropertyFilter::DENY); + + for (auto* window : electron::WindowList::GetWindows()) { + if (window->window_id() == window_id) { + std::unique_ptr result( + BuildTargetDescriptor(window)); + gfx::NativeWindow native_window = window->GetNativeWindow(); + ui::AXPlatformNode* node = + ui::AXPlatformNode::FromNativeWindow(native_window); + result->SetKey(kTreeField, + base::Value(RecursiveDumpAXPlatformNodeAsString( + node, 0, property_filters))); + CallJavascriptFunction(request_type, *(result.get())); + return; + } + } + + // No browser with the specified |id| was found. + std::unique_ptr result(new base::DictionaryValue()); + result->SetInteger(kSessionIdField, window_id); + result->SetString(kTypeField, kBrowser); + result->SetString(kErrorField, "Window no longer exists."); + CallJavascriptFunction(request_type, *(result.get())); +} + +void ElectronAccessibilityUIMessageHandler::RegisterMessages() { + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + + web_ui()->RegisterMessageCallback( + "toggleAccessibility", + base::BindRepeating(&AccessibilityUIMessageHandler::ToggleAccessibility, + base::Unretained(this))); + web_ui()->RegisterMessageCallback( + "setGlobalFlag", + base::BindRepeating(&AccessibilityUIMessageHandler::SetGlobalFlag, + base::Unretained(this))); + web_ui()->RegisterMessageCallback( + "requestWebContentsTree", + base::BindRepeating( + &AccessibilityUIMessageHandler::RequestWebContentsTree, + base::Unretained(this))); + web_ui()->RegisterMessageCallback( + "requestNativeUITree", + base::BindRepeating( + &ElectronAccessibilityUIMessageHandler::RequestNativeUITree, + base::Unretained(this))); + web_ui()->RegisterMessageCallback( + "requestAccessibilityEvents", + base::BindRepeating( + &AccessibilityUIMessageHandler::RequestAccessibilityEvents, + base::Unretained(this))); +} diff --git a/shell/browser/ui/webui/accessibility_ui.h b/shell/browser/ui/webui/accessibility_ui.h new file mode 100644 index 0000000000..c38c14e26e --- /dev/null +++ b/shell/browser/ui/webui/accessibility_ui.h @@ -0,0 +1,39 @@ +// Copyright (c) 2020 Microsoft, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef SHELL_BROWSER_UI_WEBUI_ACCESSIBILITY_UI_H_ +#define SHELL_BROWSER_UI_WEBUI_ACCESSIBILITY_UI_H_ + +#include +#include +#include + +#include "base/macros.h" +#include "chrome/browser/accessibility/accessibility_ui.h" +#include "content/public/browser/web_ui_controller.h" +#include "content/public/browser/web_ui_data_source.h" +#include "content/public/browser/web_ui_message_handler.h" + +// Controls the accessibility web UI page. +class ElectronAccessibilityUI : public content::WebUIController { + public: + explicit ElectronAccessibilityUI(content::WebUI* web_ui); + ~ElectronAccessibilityUI() override; +}; + +// Manages messages sent from accessibility.js via json. +class ElectronAccessibilityUIMessageHandler + : public AccessibilityUIMessageHandler { + public: + ElectronAccessibilityUIMessageHandler(); + + void RegisterMessages() final; + + private: + void RequestNativeUITree(const base::ListValue* args); + + DISALLOW_COPY_AND_ASSIGN(ElectronAccessibilityUIMessageHandler); +}; + +#endif // SHELL_BROWSER_UI_WEBUI_ACCESSIBILITY_UI_H_