chromium/chrome/browser/accessibility/accessibility_labels_service.cc

// 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/accessibility/accessibility_state_utils.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/data_decoder/public/cpp/data_decoder.h"
#include "services/image_annotation/image_annotation_service.h"
#include "ui/accessibility/ax_action_data.h"
#include "ui/accessibility/ax_enums.mojom.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

LanguageInfo;

namespace {

AccessibilityLabelsService::ImageAnnotatorBinder&
GetImageAnnotatorBinderOverride() {}

class ImageAnnotatorClient : public image_annotation::Annotator::Client {};

}  // namespace

AccessibilityLabelsService::AccessibilityLabelsService(Profile* profile)
    :{}

AccessibilityLabelsService::~AccessibilityLabelsService() {}

// static
void AccessibilityLabelsService::RegisterProfilePrefs(
    user_prefs::PrefRegistrySyncable* registry) {}

// static
void AccessibilityLabelsService::InitOffTheRecordPrefs(
    Profile* off_the_record_profile) {}

void AccessibilityLabelsService::Init() {}

bool AccessibilityLabelsService::IsEnabled() {}

void AccessibilityLabelsService::EnableLabelsServiceOnce(
    content::WebContents* web_contents) {}

void AccessibilityLabelsService::BindImageAnnotator(
    mojo::PendingReceiver<image_annotation::mojom::Annotator> receiver) {}

void AccessibilityLabelsService::OverrideImageAnnotatorBinderForTesting(
    ImageAnnotatorBinder binder) {}

void AccessibilityLabelsService::OnImageLabelsEnabledChanged() {}

void AccessibilityLabelsService::UpdateAccessibilityLabelsHistograms() {}

#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) {
    enabled = net::NetworkChangeNotifier::GetConnectionType() ==
              net::NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI;
  }

  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