1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/accessibility/accessibility_labels_service.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/no_destructor.h"
#include "base/strings/string_split.h"
#include "build/build_config.h"
#include "chrome/browser/language/url_language_histogram_factory.h"
#include "chrome/browser/manta/manta_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/channel_info.h"
#include "chrome/common/pref_names.h"
#include "components/language/core/browser/language_usage_metrics.h"
#include "components/language/core/browser/pref_names.h"
#include "components/language/core/browser/url_language_histogram.h"
#include "components/manta/manta_service.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "components/sync_preferences/pref_service_syncable.h"
#include "components/version_info/channel.h"
#include "content/public/browser/browser_accessibility_state.h"
#include "content/public/browser/scoped_accessibility_mode.h"
#include "content/public/browser/web_contents.h"
#include "google_apis/google_api_keys.h"
#include "services/image_annotation/image_annotation_service.h"
#include "ui/accessibility/ax_action_data.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/platform/ax_platform.h"
#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
#else
#include "base/android/jni_android.h"
#include "chrome/browser/image_descriptions/jni_headers/ImageDescriptionsController_jni.h"
#endif
using LanguageInfo = language::UrlLanguageHistogram::LanguageInfo;
namespace {
AccessibilityLabelsService::ImageAnnotatorBinder&
GetImageAnnotatorBinderOverride() {
static base::NoDestructor<AccessibilityLabelsService::ImageAnnotatorBinder>
binder;
return *binder;
}
class ImageAnnotatorClient : public image_annotation::Annotator::Client {
public:
explicit ImageAnnotatorClient(Profile* profile) : profile_(profile) {}
ImageAnnotatorClient(const ImageAnnotatorClient&) = delete;
ImageAnnotatorClient& operator=(const ImageAnnotatorClient&) = delete;
~ImageAnnotatorClient() override = default;
std::vector<std::string> GetAcceptLanguages() override {
std::vector<std::string> accept_languages;
const PrefService* pref_service = profile_->GetPrefs();
std::string accept_languages_pref =
pref_service->GetString(language::prefs::kAcceptLanguages);
for (const std::string& lang :
base::SplitString(accept_languages_pref, ",", base::TRIM_WHITESPACE,
base::SPLIT_WANT_NONEMPTY)) {
accept_languages.push_back(lang);
}
return accept_languages;
}
std::vector<std::string> GetTopLanguages() override {
// The UrlLanguageHistogram includes the frequency of all languages
// of pages the user has visited, and some of these might be rare or
// even mistakes. Set a minimum threshold so that we're only returning
// languages that account for a nontrivial amount of browsing time.
// The purpose of this list is to handle the case where users might
// not be setting their accept languages correctly, we want a way to
// detect the primary languages a user actually reads.
const float kMinTopLanguageFrequency = 0.1;
std::vector<std::string> top_languages;
language::UrlLanguageHistogram* url_language_histogram =
UrlLanguageHistogramFactory::GetForBrowserContext(profile_);
std::vector<LanguageInfo> language_infos =
url_language_histogram->GetTopLanguages();
for (const LanguageInfo& info : language_infos) {
if (info.frequency >= kMinTopLanguageFrequency) {
top_languages.push_back(info.language_code);
}
}
return top_languages;
}
void RecordLanguageMetrics(const std::string& page_language,
const std::string& requested_language) override {
base::UmaHistogramSparse(
"Accessibility.ImageLabels.PageLanguage",
language::LanguageUsageMetrics::ToLanguageCodeHash(page_language));
base::UmaHistogramSparse(
"Accessibility.ImageLabels.RequestLanguage",
language::LanguageUsageMetrics::ToLanguageCodeHash(requested_language));
}
private:
const raw_ptr<Profile> profile_;
};
} // namespace
AccessibilityLabelsService::AccessibilityLabelsService(Profile* profile)
: profile_(profile) {
#if BUILDFLAG(IS_ANDROID)
// On Android we must add/remove a NetworkChangeObserver during construction/
// destruction to provide the "Only on Wi-Fi" functionality.
net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
#endif
}
AccessibilityLabelsService::~AccessibilityLabelsService() {
#if BUILDFLAG(IS_ANDROID)
net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
#endif
}
// static
void AccessibilityLabelsService::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(
prefs::kAccessibilityImageLabelsEnabled, false,
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
registry->RegisterBooleanPref(
prefs::kAccessibilityImageLabelsOptInAccepted, false,
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
#if BUILDFLAG(IS_ANDROID)
registry->RegisterBooleanPref(
prefs::kAccessibilityImageLabelsEnabledAndroid, false,
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
registry->RegisterBooleanPref(
prefs::kAccessibilityImageLabelsOnlyOnWifi, true,
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
#endif
}
// static
void AccessibilityLabelsService::InitOffTheRecordPrefs(
Profile* off_the_record_profile) {
DCHECK(off_the_record_profile->IsOffTheRecord());
off_the_record_profile->GetPrefs()->SetBoolean(
prefs::kAccessibilityImageLabelsEnabled, false);
off_the_record_profile->GetPrefs()->SetBoolean(
prefs::kAccessibilityImageLabelsOptInAccepted, false);
}
void AccessibilityLabelsService::Init() {
pref_change_registrar_.Init(profile_->GetPrefs());
pref_change_registrar_.Add(
#if !BUILDFLAG(IS_ANDROID)
prefs::kAccessibilityImageLabelsEnabled,
#else
prefs::kAccessibilityImageLabelsEnabledAndroid,
#endif
base::BindRepeating(
&AccessibilityLabelsService::OnImageLabelsEnabledChanged,
weak_factory_.GetWeakPtr()));
// This ensures prefs refresh the label images AXMode on startup.
OnImageLabelsEnabledChanged();
}
bool AccessibilityLabelsService::IsEnabled() {
#if !BUILDFLAG(IS_ANDROID)
return profile_->GetPrefs()->GetBoolean(
prefs::kAccessibilityImageLabelsEnabled);
#else
return GetAndroidEnabledStatus();
#endif
}
void AccessibilityLabelsService::EnableLabelsServiceOnce(
content::WebContents* web_contents) {
if (!ui::AXPlatform::GetInstance().IsScreenReaderActive()) {
return;
}
// TODO(grt): Use ScopedAccessibilityMode here targeting the WC and remove
// the action.
// For Android, we call through the JNI (see below) and send the web contents
// directly, since Android does not support BrowserList::GetInstance.
#if !BUILDFLAG(IS_ANDROID)
// Fire an AXAction on the active tab to enable this feature once only.
// We only need to fire this event for the active page.
ui::AXActionData action_data;
action_data.action = ax::mojom::Action::kAnnotatePageImages;
web_contents->GetPrimaryMainFrame()->ForEachRenderFrameHost(
[&action_data](content::RenderFrameHost* render_frame_host) {
if (render_frame_host->IsRenderFrameLive()) {
render_frame_host->AccessibilityPerformAction(action_data);
}
});
#endif
}
void AccessibilityLabelsService::BindImageAnnotator(
mojo::PendingReceiver<image_annotation::mojom::Annotator> receiver) {
if (!remote_service_) {
auto service_receiver = remote_service_.BindNewPipeAndPassReceiver();
auto& binder = GetImageAnnotatorBinderOverride();
if (binder) {
binder.Run(std::move(service_receiver));
} else {
auto* manta_service = manta::MantaServiceFactory::GetForProfile(profile_);
CHECK(manta_service);
service_ = std::make_unique<image_annotation::ImageAnnotationService>(
std::move(service_receiver),
google_apis::GetAPIKey(chrome::GetChannel()),
profile_->GetURLLoaderFactory(),
manta_service->CreateAnchovyProvider(),
std::make_unique<ImageAnnotatorClient>(profile_));
}
}
remote_service_->BindAnnotator(std::move(receiver));
}
void AccessibilityLabelsService::OverrideImageAnnotatorBinderForTesting(
ImageAnnotatorBinder binder) {
GetImageAnnotatorBinderOverride() = std::move(binder);
}
void AccessibilityLabelsService::OnImageLabelsEnabledChanged() {
if (!IsEnabled()) {
scoped_accessibility_mode_.reset();
} else if (!scoped_accessibility_mode_) {
scoped_accessibility_mode_ =
content::BrowserAccessibilityState::GetInstance()
->CreateScopedModeForBrowserContext(profile_,
ui::AXMode::kLabelImages);
}
UpdateAccessibilityLabelsHistograms();
}
void AccessibilityLabelsService::UpdateAccessibilityLabelsHistograms() {
base::UmaHistogramBoolean("Accessibility.ImageLabels2",
profile_->GetPrefs()->GetBoolean(
prefs::kAccessibilityImageLabelsEnabled));
#if BUILDFLAG(IS_ANDROID)
// For Android we will track additional histograms.
base::UmaHistogramBoolean(
"Accessibility.ImageLabels.Android",
profile_->GetPrefs()->GetBoolean(
prefs::kAccessibilityImageLabelsEnabledAndroid));
base::UmaHistogramBoolean("Accessibility.ImageLabels.Android.OnlyOnWifi",
profile_->GetPrefs()->GetBoolean(
prefs::kAccessibilityImageLabelsOnlyOnWifi));
#endif
}
#if BUILDFLAG(IS_ANDROID)
void AccessibilityLabelsService::OnNetworkChanged(
net::NetworkChangeNotifier::ConnectionType type) {
// When the network status changes, we want to (potentially) update the
// AXMode of all web contents for the current profile.
OnImageLabelsEnabledChanged();
}
bool AccessibilityLabelsService::GetAndroidEnabledStatus() {
// On Android, user has an option to toggle "only on wifi", so also check
// the current connection type if necessary.
bool enabled = profile_->GetPrefs()->GetBoolean(
prefs::kAccessibilityImageLabelsEnabledAndroid);
bool only_on_wifi = profile_->GetPrefs()->GetBoolean(
prefs::kAccessibilityImageLabelsOnlyOnWifi);
if (enabled && only_on_wifi) {
net::NetworkChangeNotifier::ConnectionType current_connection_type =
net::NetworkChangeNotifier::GetConnectionType();
bool is_wifi =
(current_connection_type ==
net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI);
bool is_ethernet =
(current_connection_type ==
net::NetworkChangeNotifier::ConnectionType::CONNECTION_ETHERNET);
enabled = (is_wifi || is_ethernet);
}
return enabled;
}
void JNI_ImageDescriptionsController_GetImageDescriptionsOnce(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_web_contents) {
content::WebContents* web_contents =
content::WebContents::FromJavaWebContents(j_web_contents);
if (!web_contents) {
return;
}
// We only need to fire this event for the active page.
ui::AXActionData action_data;
action_data.action = ax::mojom::Action::kAnnotatePageImages;
web_contents->GetPrimaryMainFrame()->ForEachRenderFrameHost(
[&action_data](content::RenderFrameHost* render_frame_host) {
if (render_frame_host->IsRenderFrameLive()) {
render_frame_host->AccessibilityPerformAction(action_data);
}
});
}
#endif
|