Commit c3a1a18c authored by Ken Rockot's avatar Ken Rockot Committed by Commit Bot

Move image annotation client into keyed service

The service (in //services/image_annotation) has been
hanging off of ProfileImpl since it was moved off of Service Manager
APIs. That breaks image annotation for OTR profiles.

This CL migrates the client code to the AccessibilityLabelsService that
hangs off of all Profiles.

This also opportunistically moves Data Decoder consumption off of
Service Manager APIs in favor of the new DataDecoder client API.

Fixed: 1020657
Change-Id: I62f77f2f1bcfc84225efb8b62a75d74f2d0c655c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1895963Reviewed-by: default avatarRobert Sesek <rsesek@chromium.org>
Reviewed-by: default avatarNektarios Paisios <nektar@chromium.org>
Reviewed-by: default avatarAndrew Moylan <amoylan@chromium.org>
Reviewed-by: default avatarStefan Kuhne <skuhne@chromium.org>
Reviewed-by: default avatarDominic Mazzoni <dmazzoni@chromium.org>
Commit-Queue: Ken Rockot <rockot@google.com>
Cr-Commit-Position: refs/heads/master@{#712657}
parent 244d65f3
include_rules = [
"+services/image_annotation",
]
...@@ -5,16 +5,22 @@ ...@@ -5,16 +5,22 @@
#include "chrome/browser/accessibility/accessibility_labels_service.h" #include "chrome/browser/accessibility/accessibility_labels_service.h"
#include "base/metrics/histogram_functions.h" #include "base/metrics/histogram_functions.h"
#include "base/no_destructor.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "chrome/browser/accessibility/accessibility_state_utils.h" #include "chrome/browser/accessibility/accessibility_state_utils.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
#include "chrome/common/channel_info.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "components/pref_registry/pref_registry_syncable.h" #include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "components/sync_preferences/pref_service_syncable.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/browser_accessibility_state.h"
#include "content/public/common/content_features.h" #include "content/public/common/content_features.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_action_data.h"
#include "ui/accessibility/ax_enums.mojom.h" #include "ui/accessibility/ax_enums.mojom.h"
...@@ -24,6 +30,41 @@ ...@@ -24,6 +30,41 @@
#include "chrome/browser/ui/browser_list.h" #include "chrome/browser/ui/browser_list.h"
#endif #endif
namespace {
// Returns the Chrome Google API key for the channel of this build.
std::string APIKeyForChannel() {
if (chrome::GetChannel() == version_info::Channel::STABLE)
return google_apis::GetAPIKey();
return google_apis::GetNonStableAPIKey();
}
AccessibilityLabelsService::ImageAnnotatorBinder&
GetImageAnnotatorBinderOverride() {
static base::NoDestructor<AccessibilityLabelsService::ImageAnnotatorBinder>
binder;
return *binder;
}
class ImageAnnotatorClient : public image_annotation::Annotator::Client {
public:
ImageAnnotatorClient() = default;
~ImageAnnotatorClient() override = default;
// image_annotation::Annotator::Client implementation:
void BindJsonParser(mojo::PendingReceiver<data_decoder::mojom::JsonParser>
receiver) override {
data_decoder_.GetService()->BindJsonParser(std::move(receiver));
}
private:
data_decoder::DataDecoder data_decoder_;
DISALLOW_COPY_AND_ASSIGN(ImageAnnotatorClient);
};
} // namespace
AccessibilityLabelsService::~AccessibilityLabelsService() {} AccessibilityLabelsService::~AccessibilityLabelsService() {}
// static // static
...@@ -109,6 +150,29 @@ void AccessibilityLabelsService::EnableLabelsServiceOnce() { ...@@ -109,6 +150,29 @@ void AccessibilityLabelsService::EnableLabelsServiceOnce() {
#endif #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 {
service_ = std::make_unique<image_annotation::ImageAnnotationService>(
std::move(service_receiver), APIKeyForChannel(),
profile_->GetURLLoaderFactory(),
std::make_unique<ImageAnnotatorClient>());
}
}
remote_service_->BindAnnotator(std::move(receiver));
}
void AccessibilityLabelsService::OverrideImageAnnotatorBinderForTesting(
ImageAnnotatorBinder binder) {
GetImageAnnotatorBinderOverride() = std::move(binder);
}
void AccessibilityLabelsService::OnImageLabelsEnabledChanged() { void AccessibilityLabelsService::OnImageLabelsEnabledChanged() {
// TODO(dmazzoni) Implement for Android, which doesn't support // TODO(dmazzoni) Implement for Android, which doesn't support
// AllTabContentses(). crbug.com/905419 // AllTabContentses(). crbug.com/905419
......
...@@ -8,10 +8,17 @@ ...@@ -8,10 +8,17 @@
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "components/keyed_service/core/keyed_service.h" #include "components/keyed_service/core/keyed_service.h"
#include "components/prefs/pref_change_registrar.h" #include "components/prefs/pref_change_registrar.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/image_annotation/public/mojom/image_annotation.mojom.h"
#include "ui/accessibility/ax_mode.h" #include "ui/accessibility/ax_mode.h"
class Profile; class Profile;
namespace image_annotation {
class ImageAnnotationService;
}
namespace user_prefs { namespace user_prefs {
class PrefRegistrySyncable; class PrefRegistrySyncable;
} }
...@@ -35,6 +42,17 @@ class AccessibilityLabelsService : public KeyedService { ...@@ -35,6 +42,17 @@ class AccessibilityLabelsService : public KeyedService {
void EnableLabelsServiceOnce(); void EnableLabelsServiceOnce();
// Routes an Annotator interface receiver to the Image Annotation service for
// binding.
void BindImageAnnotator(
mojo::PendingReceiver<image_annotation::mojom::Annotator> receiver);
// Allows tests to override how this object binds a connection to a remote
// ImageAnnotationService.
using ImageAnnotatorBinder = base::RepeatingCallback<void(
mojo::PendingReceiver<image_annotation::mojom::ImageAnnotationService>)>;
void OverrideImageAnnotatorBinderForTesting(ImageAnnotatorBinder binder);
private: private:
friend class AccessibilityLabelsServiceFactory; friend class AccessibilityLabelsServiceFactory;
...@@ -51,6 +69,10 @@ class AccessibilityLabelsService : public KeyedService { ...@@ -51,6 +69,10 @@ class AccessibilityLabelsService : public KeyedService {
PrefChangeRegistrar pref_change_registrar_; PrefChangeRegistrar pref_change_registrar_;
// Implementation of and remote connection to the Image Annotation service.
std::unique_ptr<image_annotation::ImageAnnotationService> service_;
mojo::Remote<image_annotation::mojom::ImageAnnotationService> remote_service_;
base::WeakPtrFactory<AccessibilityLabelsService> weak_factory_{this}; base::WeakPtrFactory<AccessibilityLabelsService> weak_factory_{this};
DISALLOW_COPY_AND_ASSIGN(AccessibilityLabelsService); DISALLOW_COPY_AND_ASSIGN(AccessibilityLabelsService);
......
...@@ -8,8 +8,9 @@ ...@@ -8,8 +8,9 @@
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "chrome/browser/accessibility/accessibility_labels_service.h"
#include "chrome/browser/accessibility/accessibility_labels_service_factory.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_impl.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h" #include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/in_process_browser_test.h"
...@@ -207,8 +208,9 @@ class ImageAnnotationBrowserTest : public InProcessBrowserTest { ...@@ -207,8 +208,9 @@ class ImageAnnotationBrowserTest : public InProcessBrowserTest {
content::WebContents* web_contents = content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents(); browser()->tab_strip_model()->GetActiveWebContents();
ProfileImpl::OverrideImageAnnotationServiceBinderForTesting( AccessibilityLabelsServiceFactory::GetForProfile(browser()->profile())
base::BindRepeating(&BindImageAnnotatorService)); ->OverrideImageAnnotatorBinderForTesting(
base::BindRepeating(&BindImageAnnotatorService));
ui::AXMode mode = ui::kAXModeComplete; ui::AXMode mode = ui::kAXModeComplete;
mode.set_mode(ui::AXMode::kLabelImages, true); mode.set_mode(ui::AXMode::kLabelImages, true);
...@@ -218,8 +220,8 @@ class ImageAnnotationBrowserTest : public InProcessBrowserTest { ...@@ -218,8 +220,8 @@ class ImageAnnotationBrowserTest : public InProcessBrowserTest {
} }
void TearDownOnMainThread() override { void TearDownOnMainThread() override {
ProfileImpl::OverrideImageAnnotationServiceBinderForTesting( AccessibilityLabelsServiceFactory::GetForProfile(browser()->profile())
base::NullCallback()); ->OverrideImageAnnotatorBinderForTesting(base::NullCallback());
InProcessBrowserTest::TearDownOnMainThread(); InProcessBrowserTest::TearDownOnMainThread();
} }
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#include "base/feature_list.h" #include "base/feature_list.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "chrome/browser/accessibility/accessibility_labels_service.h"
#include "chrome/browser/accessibility/accessibility_labels_service_factory.h"
#include "chrome/browser/content_settings/content_settings_manager_impl.h" #include "chrome/browser/content_settings/content_settings_manager_impl.h"
#include "chrome/browser/navigation_predictor/navigation_predictor.h" #include "chrome/browser/navigation_predictor/navigation_predictor.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
...@@ -36,13 +38,14 @@ ...@@ -36,13 +38,14 @@
namespace chrome { namespace chrome {
namespace internal { namespace internal {
// Forward image Annotator requests to the profile's ImageAnnotationService. // Forward image Annotator requests to the profile's AccessibilityLabelsService.
void BindImageAnnotator( void BindImageAnnotator(
content::RenderFrameHost* const frame_host, content::RenderFrameHost* const frame_host,
mojo::PendingReceiver<image_annotation::mojom::Annotator> receiver) { mojo::PendingReceiver<image_annotation::mojom::Annotator> receiver) {
Profile::FromBrowserContext(frame_host->GetProcess()->GetBrowserContext()) AccessibilityLabelsServiceFactory::GetForProfile(
->GetImageAnnotationService() Profile::FromBrowserContext(
->BindAnnotator(std::move(receiver)); frame_host->GetProcess()->GetBrowserContext()))
->BindImageAnnotator(std::move(receiver));
} }
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
......
...@@ -3,8 +3,5 @@ specific_include_rules = { ...@@ -3,8 +3,5 @@ specific_include_rules = {
# Profile embeds the Identity Service, but the dependence # Profile embeds the Identity Service, but the dependence
# should be used *only* for Identity Service creation. # should be used *only* for Identity Service creation.
"+services/identity/identity_service.h", "+services/identity/identity_service.h",
# Profile embeds the image_annotation service.
"+services/image_annotation/image_annotation_service.h",
], ],
} }
...@@ -293,11 +293,6 @@ identity::mojom::IdentityService* Profile::GetIdentityService() { ...@@ -293,11 +293,6 @@ identity::mojom::IdentityService* Profile::GetIdentityService() {
return nullptr; return nullptr;
} }
image_annotation::mojom::ImageAnnotationService*
Profile::GetImageAnnotationService() {
return nullptr;
}
bool Profile::IsNewProfile() { bool Profile::IsNewProfile() {
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
// The profile is new if the preference files has just been created, except on // The profile is new if the preference files has just been created, except on
......
...@@ -45,12 +45,6 @@ class IdentityService; ...@@ -45,12 +45,6 @@ class IdentityService;
} // namespace mojom } // namespace mojom
} // namespace identity } // namespace identity
namespace image_annotation {
namespace mojom {
class ImageAnnotationService;
}
} // namespace image_annotation
namespace policy { namespace policy {
class SchemaRegistryService; class SchemaRegistryService;
class ProfilePolicyConnector; class ProfilePolicyConnector;
...@@ -394,11 +388,6 @@ class Profile : public content::BrowserContext { ...@@ -394,11 +388,6 @@ class Profile : public content::BrowserContext {
// null if the profile does not have a corresponding service instance. // null if the profile does not have a corresponding service instance.
virtual identity::mojom::IdentityService* GetIdentityService(); virtual identity::mojom::IdentityService* GetIdentityService();
// Exposes access to the profile's Image Annotation service instance. This may
// return null if the profile does not have a corresponding service instance.
virtual image_annotation::mojom::ImageAnnotationService*
GetImageAnnotationService();
// Stop sending accessibility events until ResumeAccessibilityEvents(). // Stop sending accessibility events until ResumeAccessibilityEvents().
// Calls to Pause nest; no events will be sent until the number of // Calls to Pause nest; no events will be sent until the number of
// Resume calls matches the number of Pause calls received. // Resume calls matches the number of Pause calls received.
......
...@@ -155,14 +155,12 @@ ...@@ -155,14 +155,12 @@
#include "content/public/browser/url_data_source.h" #include "content/public/browser/url_data_source.h"
#include "content/public/common/content_constants.h" #include "content/public/common/content_constants.h"
#include "extensions/buildflags/buildflags.h" #include "extensions/buildflags/buildflags.h"
#include "google_apis/google_api_keys.h"
#include "mojo/public/cpp/bindings/pending_remote.h" #include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h" #include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "ppapi/buildflags/buildflags.h" #include "ppapi/buildflags/buildflags.h"
#include "printing/buildflags/buildflags.h" #include "printing/buildflags/buildflags.h"
#include "services/data_decoder/public/mojom/constants.mojom.h" #include "services/data_decoder/public/mojom/constants.mojom.h"
#include "services/identity/identity_service.h" #include "services/identity/identity_service.h"
#include "services/image_annotation/image_annotation_service.h"
#include "services/network/public/cpp/features.h" #include "services/network/public/cpp/features.h"
#include "services/preferences/public/cpp/in_process_service_factory.h" #include "services/preferences/public/cpp/in_process_service_factory.h"
#include "services/preferences/public/mojom/preferences.mojom.h" #include "services/preferences/public/mojom/preferences.mojom.h"
...@@ -257,13 +255,6 @@ const int kCreateSessionServiceDelayMS = 500; ...@@ -257,13 +255,6 @@ const int kCreateSessionServiceDelayMS = 500;
const char kPrefExitTypeCrashed[] = "Crashed"; const char kPrefExitTypeCrashed[] = "Crashed";
const char kPrefExitTypeSessionEnded[] = "SessionEnded"; const char kPrefExitTypeSessionEnded[] = "SessionEnded";
// Returns the Chrome Google API key for the channel of this build.
std::string APIKeyForChannel() {
if (chrome::GetChannel() == version_info::Channel::STABLE)
return google_apis::GetAPIKey();
return google_apis::GetNonStableAPIKey();
}
// Gets the creation time for |path|, returning base::Time::Now() on failure. // Gets the creation time for |path|, returning base::Time::Now() on failure.
base::Time GetCreationTimeForPath(const base::FilePath& path) { base::Time GetCreationTimeForPath(const base::FilePath& path) {
base::File::Info info; base::File::Info info;
...@@ -339,28 +330,6 @@ bool LocaleNotChanged(const std::string& pref_locale, ...@@ -339,28 +330,6 @@ bool LocaleNotChanged(const std::string& pref_locale,
} }
#endif // defined(OS_CHROMEOS) #endif // defined(OS_CHROMEOS)
class ImageAnnotatorClient : public image_annotation::Annotator::Client {
public:
ImageAnnotatorClient() = default;
~ImageAnnotatorClient() override = default;
// image_annotation::Annotator::Client implementation:
void BindJsonParser(mojo::PendingReceiver<data_decoder::mojom::JsonParser>
receiver) override {
content::GetSystemConnector()->Connect(data_decoder::mojom::kServiceName,
std::move(receiver));
}
private:
DISALLOW_COPY_AND_ASSIGN(ImageAnnotatorClient);
};
ProfileImpl::ImageAnnotationServiceBinder&
GetImageAnnotationServiceBinderOverride() {
static base::NoDestructor<ProfileImpl::ImageAnnotationServiceBinder> binder;
return *binder;
}
} // namespace } // namespace
// static // static
...@@ -1099,24 +1068,6 @@ identity::mojom::IdentityService* ProfileImpl::GetIdentityService() { ...@@ -1099,24 +1068,6 @@ identity::mojom::IdentityService* ProfileImpl::GetIdentityService() {
return remote_identity_service_.get(); return remote_identity_service_.get();
} }
image_annotation::mojom::ImageAnnotationService*
ProfileImpl::GetImageAnnotationService() {
if (!remote_image_annotation_service_) {
auto receiver =
remote_image_annotation_service_.BindNewPipeAndPassReceiver();
const auto& binder = GetImageAnnotationServiceBinderOverride();
if (binder) {
binder.Run(std::move(receiver));
} else {
image_annotation_service_impl_ =
std::make_unique<image_annotation::ImageAnnotationService>(
std::move(receiver), APIKeyForChannel(), GetURLLoaderFactory(),
std::make_unique<ImageAnnotatorClient>());
}
}
return remote_image_annotation_service_.get();
}
PrefService* ProfileImpl::GetPrefs() { PrefService* ProfileImpl::GetPrefs() {
return const_cast<PrefService*>( return const_cast<PrefService*>(
static_cast<const ProfileImpl*>(this)->GetPrefs()); static_cast<const ProfileImpl*>(this)->GetPrefs());
...@@ -1511,12 +1462,6 @@ void ProfileImpl::SetCreationTimeForTesting(base::Time creation_time) { ...@@ -1511,12 +1462,6 @@ void ProfileImpl::SetCreationTimeForTesting(base::Time creation_time) {
creation_time_ = creation_time; creation_time_ = creation_time;
} }
// static
void ProfileImpl::OverrideImageAnnotationServiceBinderForTesting(
ImageAnnotationServiceBinder binder) {
GetImageAnnotationServiceBinderOverride() = std::move(binder);
}
GURL ProfileImpl::GetHomePage() { GURL ProfileImpl::GetHomePage() {
// --homepage overrides any preferences. // --homepage overrides any preferences.
const base::CommandLine& command_line = const base::CommandLine& command_line =
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
#include "extensions/buildflags/buildflags.h" #include "extensions/buildflags/buildflags.h"
#include "mojo/public/cpp/bindings/remote.h" #include "mojo/public/cpp/bindings/remote.h"
#include "services/identity/public/mojom/identity_service.mojom.h" #include "services/identity/public/mojom/identity_service.mojom.h"
#include "services/image_annotation/public/mojom/image_annotation.mojom.h"
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
#include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h" #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h"
...@@ -53,10 +52,6 @@ namespace identity { ...@@ -53,10 +52,6 @@ namespace identity {
class IdentityService; class IdentityService;
} }
namespace image_annotation {
class ImageAnnotationService;
}
namespace policy { namespace policy {
class ConfigurationPolicyProvider; class ConfigurationPolicyProvider;
class ProfilePolicyConnector; class ProfilePolicyConnector;
...@@ -171,8 +166,6 @@ class ProfileImpl : public Profile { ...@@ -171,8 +166,6 @@ class ProfileImpl : public Profile {
bool ShouldRestoreOldSessionCookies() override; bool ShouldRestoreOldSessionCookies() override;
bool ShouldPersistSessionCookies() override; bool ShouldPersistSessionCookies() override;
identity::mojom::IdentityService* GetIdentityService() override; identity::mojom::IdentityService* GetIdentityService() override;
image_annotation::mojom::ImageAnnotationService* GetImageAnnotationService()
override;
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
void ChangeAppLocale(const std::string& locale, AppLocaleChangedVia) override; void ChangeAppLocale(const std::string& locale, AppLocaleChangedVia) override;
...@@ -182,13 +175,6 @@ class ProfileImpl : public Profile { ...@@ -182,13 +175,6 @@ class ProfileImpl : public Profile {
void SetCreationTimeForTesting(base::Time creation_time) override; void SetCreationTimeForTesting(base::Time creation_time) override;
// Overrides how the ImageAnnotationService service instance is bound in a
// testing environment. If |binder| is null, any existing override is removed.
using ImageAnnotationServiceBinder = base::RepeatingCallback<void(
mojo::PendingReceiver<image_annotation::mojom::ImageAnnotationService>)>;
static void OverrideImageAnnotationServiceBinderForTesting(
ImageAnnotationServiceBinder binder);
private: private:
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
friend class chromeos::KioskTest; friend class chromeos::KioskTest;
...@@ -261,12 +247,6 @@ class ProfileImpl : public Profile { ...@@ -261,12 +247,6 @@ class ProfileImpl : public Profile {
// |GetIdentityService()|. // |GetIdentityService()|.
mojo::Remote<identity::mojom::IdentityService> remote_identity_service_; mojo::Remote<identity::mojom::IdentityService> remote_identity_service_;
// The Image Annotation service instance for this profile.
std::unique_ptr<image_annotation::ImageAnnotationService>
image_annotation_service_impl_;
mojo::Remote<image_annotation::mojom::ImageAnnotationService>
remote_image_annotation_service_;
// !!! BIG HONKING WARNING !!! // !!! BIG HONKING WARNING !!!
// The order of the members below is important. Do not change it unless // The order of the members below is important. Do not change it unless
// you know what you're doing. Also, if adding a new member here make sure // you know what you're doing. Also, if adding a new member here make sure
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment