Commit ffac4483 authored by Michael Crouse's avatar Michael Crouse Committed by Chromium LUCI CQ

[LanguageDetection] SimpleKeyedService and optimization guide.


This change converts the translate model service to a
SimpleKeyedService and moves it to components which will enable
easier support beyond just browser use cases.

This change also registers the translate model service to
the optimization guide for provided necessary model files.

A future change will add loading and service the model files
to consumers of the translate model service.

Bug: 1151407
Change-Id: I4e903115c82a30757d46d75ed8d733874726d830
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2580339
Commit-Queue: Michael Crouse <mcrouse@chromium.org>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Reviewed-by: default avatarTrevor  Perrier <perrier@chromium.org>
Reviewed-by: default avatarScott Little <sclittle@chromium.org>
Reviewed-by: default avatarTarun Bansal <tbansal@chromium.org>
Reviewed-by: default avatarSophie Chang <sophiechang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#836656}
parent 2095c33c
...@@ -1842,8 +1842,6 @@ static_library("browser") { ...@@ -1842,8 +1842,6 @@ static_library("browser") {
"translate/translate_accept_languages_factory.h", "translate/translate_accept_languages_factory.h",
"translate/translate_model_service_factory.cc", "translate/translate_model_service_factory.cc",
"translate/translate_model_service_factory.h", "translate/translate_model_service_factory.h",
"translate/translate_model_service_impl.cc",
"translate/translate_model_service_impl.h",
"translate/translate_ranker_factory.cc", "translate/translate_ranker_factory.cc",
"translate/translate_ranker_factory.h", "translate/translate_ranker_factory.h",
"translate/translate_ranker_metrics_provider.cc", "translate/translate_ranker_metrics_provider.cc",
......
...@@ -7,11 +7,13 @@ ...@@ -7,11 +7,13 @@
#include "base/test/metrics/histogram_tester.h" #include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_key.h"
#include "chrome/browser/translate/translate_model_service_factory.h" #include "chrome/browser/translate/translate_model_service_factory.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h" #include "chrome/test/base/ui_test_utils.h"
#include "components/metrics/content/subprocess_metrics_provider.h" #include "components/metrics/content/subprocess_metrics_provider.h"
#include "components/optimization_guide/optimization_guide_features.h"
#include "components/translate/core/common/translate_util.h" #include "components/translate/core/common/translate_util.h"
#include "content/public/test/browser_test.h" #include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h" #include "content/public/test/browser_test_utils.h"
...@@ -59,8 +61,31 @@ using TranslateModelServiceDisabledBrowserTest = InProcessBrowserTest; ...@@ -59,8 +61,31 @@ using TranslateModelServiceDisabledBrowserTest = InProcessBrowserTest;
IN_PROC_BROWSER_TEST_F(TranslateModelServiceDisabledBrowserTest, IN_PROC_BROWSER_TEST_F(TranslateModelServiceDisabledBrowserTest,
TranslateModelServiceDisabled) { TranslateModelServiceDisabled) {
EXPECT_FALSE( EXPECT_FALSE(TranslateModelServiceFactory::GetOrBuildForKey(
TranslateModelServiceFactory::GetForProfile(browser()->profile())); browser()->profile()->GetProfileKey()));
}
class TranslateModelServiceWithoutOptimizationGuideBrowserTest
: public TranslateModelServiceDisabledBrowserTest {
public:
TranslateModelServiceWithoutOptimizationGuideBrowserTest() {
scoped_feature_list_.InitWithFeatures(
{translate::kTFLiteLanguageDetectionEnabled}, {});
}
~TranslateModelServiceWithoutOptimizationGuideBrowserTest() override =
default;
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
// This test confirms the translate model service is not available if
// the optimization guide does not exist.
IN_PROC_BROWSER_TEST_F(TranslateModelServiceWithoutOptimizationGuideBrowserTest,
TranslateModelServiceEnabled) {
EXPECT_FALSE(TranslateModelServiceFactory::GetOrBuildForKey(
browser()->profile()->GetProfileKey()));
} }
IN_PROC_BROWSER_TEST_F(TranslateModelServiceDisabledBrowserTest, IN_PROC_BROWSER_TEST_F(TranslateModelServiceDisabledBrowserTest,
...@@ -77,8 +102,11 @@ class TranslateModelServiceBrowserTest ...@@ -77,8 +102,11 @@ class TranslateModelServiceBrowserTest
: public TranslateModelServiceDisabledBrowserTest { : public TranslateModelServiceDisabledBrowserTest {
public: public:
TranslateModelServiceBrowserTest() { TranslateModelServiceBrowserTest() {
scoped_feature_list_.InitAndEnableFeature( scoped_feature_list_.InitWithFeatures(
translate::kTFLiteLanguageDetectionEnabled); {translate::kTFLiteLanguageDetectionEnabled,
optimization_guide::features::kOptimizationHints,
optimization_guide::features::kRemoteOptimizationGuideFetching},
{});
} }
~TranslateModelServiceBrowserTest() override = default; ~TranslateModelServiceBrowserTest() override = default;
...@@ -89,14 +117,14 @@ class TranslateModelServiceBrowserTest ...@@ -89,14 +117,14 @@ class TranslateModelServiceBrowserTest
IN_PROC_BROWSER_TEST_F(TranslateModelServiceBrowserTest, IN_PROC_BROWSER_TEST_F(TranslateModelServiceBrowserTest,
TranslateModelServiceEnabled) { TranslateModelServiceEnabled) {
EXPECT_TRUE( EXPECT_TRUE(TranslateModelServiceFactory::GetOrBuildForKey(
TranslateModelServiceFactory::GetForProfile(browser()->profile())); browser()->profile()->GetProfileKey()));
} }
IN_PROC_BROWSER_TEST_F(TranslateModelServiceBrowserTest, IN_PROC_BROWSER_TEST_F(TranslateModelServiceBrowserTest,
TranslateModelServiceEnabled_OffTheRecord) { TranslateModelServiceEnabled_OffTheRecord) {
EXPECT_TRUE(TranslateModelServiceFactory::GetForProfile( EXPECT_TRUE(TranslateModelServiceFactory::GetOrBuildForKey(
browser()->profile()->GetPrimaryOTRProfile())); browser()->profile()->GetPrimaryOTRProfile()->GetProfileKey()));
} }
IN_PROC_BROWSER_TEST_F(TranslateModelServiceBrowserTest, IN_PROC_BROWSER_TEST_F(TranslateModelServiceBrowserTest,
......
...@@ -4,21 +4,21 @@ ...@@ -4,21 +4,21 @@
#include "chrome/browser/translate/translate_model_service_factory.h" #include "chrome/browser/translate/translate_model_service_factory.h"
#include "chrome/browser/profiles/incognito_helpers.h" #include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/translate/translate_model_service_impl.h" #include "chrome/browser/profiles/profile_key.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h" #include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/keyed_service/core/simple_dependency_manager.h"
#include "components/translate/content/browser/translate_model_service.h"
#include "components/translate/core/common/translate_util.h" #include "components/translate/core/common/translate_util.h"
#include "content/public/browser/browser_context.h"
// static // static
TranslateModelServiceImpl* TranslateModelServiceFactory::GetForProfile( translate::TranslateModelService*
Profile* profile) { TranslateModelServiceFactory::GetOrBuildForKey(SimpleFactoryKey* key) {
if (translate::IsTFLiteLanguageDetectionEnabled()) { return static_cast<translate::TranslateModelService*>(
return static_cast<TranslateModelServiceImpl*>( GetInstance()->GetServiceForKey(key, true));
GetInstance()->GetServiceForBrowserContext(profile, true));
}
return nullptr;
} }
// static // static
...@@ -28,19 +28,31 @@ TranslateModelServiceFactory* TranslateModelServiceFactory::GetInstance() { ...@@ -28,19 +28,31 @@ TranslateModelServiceFactory* TranslateModelServiceFactory::GetInstance() {
} }
TranslateModelServiceFactory::TranslateModelServiceFactory() TranslateModelServiceFactory::TranslateModelServiceFactory()
: BrowserContextKeyedServiceFactory( : SimpleKeyedServiceFactory("TranslateModelService",
"TranslateModelService", SimpleDependencyManager::GetInstance()) {}
BrowserContextDependencyManager::GetInstance()) {}
TranslateModelServiceFactory::~TranslateModelServiceFactory() = default; TranslateModelServiceFactory::~TranslateModelServiceFactory() = default;
KeyedService* TranslateModelServiceFactory::BuildServiceInstanceFor( std::unique_ptr<KeyedService>
content::BrowserContext* context) const { TranslateModelServiceFactory::BuildServiceInstanceFor(
return new TranslateModelServiceImpl(); SimpleFactoryKey* key) const {
if (!translate::IsTFLiteLanguageDetectionEnabled())
return nullptr;
// The optimization guide service must be available for the translate model
// service to be created.
auto* opt_guide = OptimizationGuideKeyedServiceFactory::GetForProfile(
ProfileManager::GetProfileFromProfileKey(
ProfileKey::FromSimpleFactoryKey(key)));
if (opt_guide)
return std::make_unique<translate::TranslateModelService>(opt_guide);
return nullptr;
} }
content::BrowserContext* TranslateModelServiceFactory::GetBrowserContextToUse( SimpleFactoryKey* TranslateModelServiceFactory::GetKeyToUse(
content::BrowserContext* context) const { SimpleFactoryKey* key) const {
// Use the original profile's TranslateModelService, even in Incognito mode. // The translate model service should be able to
return chrome::GetBrowserContextRedirectedInIncognito(context); // operate in off the record sessions if the model is available from the
// optimization guide.
ProfileKey* profile_key = ProfileKey::FromSimpleFactoryKey(key);
return profile_key->GetOriginalKey();
} }
...@@ -7,24 +7,22 @@ ...@@ -7,24 +7,22 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/no_destructor.h" #include "base/no_destructor.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h" #include "components/keyed_service/core/simple_keyed_service_factory.h"
#include "components/translate/content/browser/translate_model_service.h"
namespace content { class SimpleFactoryKey;
class BrowserContext;
} // namespace content
class TranslateModelServiceImpl;
class Profile;
// LazyInstance that owns all TranslateModelService(s) and associates // LazyInstance that owns all TranslateModelService(s) and associates
// them with Profiles. // them with Profiles.
class TranslateModelServiceFactory : public BrowserContextKeyedServiceFactory { class TranslateModelServiceFactory : public SimpleKeyedServiceFactory {
public: public:
// Gets the TranslateModelService for the profile. // Gets the TranslateModelService for the profile.
// //
// Returns null if the features that allow for this to provide useful // Returns null if the features that allow for this to provide useful
// information are disabled. // information are disabled. Importantly, only available when the
static TranslateModelServiceImpl* GetForProfile(Profile* profile); // optimization guide service is.
static translate::TranslateModelService* GetOrBuildForKey(
SimpleFactoryKey* key);
// Gets the LazyInstance that owns all TranslateModelService(s). // Gets the LazyInstance that owns all TranslateModelService(s).
static TranslateModelServiceFactory* GetInstance(); static TranslateModelServiceFactory* GetInstance();
...@@ -35,11 +33,10 @@ class TranslateModelServiceFactory : public BrowserContextKeyedServiceFactory { ...@@ -35,11 +33,10 @@ class TranslateModelServiceFactory : public BrowserContextKeyedServiceFactory {
TranslateModelServiceFactory(); TranslateModelServiceFactory();
~TranslateModelServiceFactory() override; ~TranslateModelServiceFactory() override;
// BrowserContextKeyedServiceFactory: // SimpleKeyedServiceFactory overrides:
KeyedService* BuildServiceInstanceFor( std::unique_ptr<KeyedService> BuildServiceInstanceFor(
content::BrowserContext* context) const override; SimpleFactoryKey* key) const override;
content::BrowserContext* GetBrowserContextToUse( SimpleFactoryKey* GetKeyToUse(SimpleFactoryKey* key) const override;
content::BrowserContext* context) const override;
}; };
#endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MODEL_SERVICE_FACTORY_H_ #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MODEL_SERVICE_FACTORY_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/translate/translate_model_service_impl.h"
#include "base/files/file.h"
#include "base/optional.h"
TranslateModelServiceImpl::TranslateModelServiceImpl() {
// TODO(crbug.com/1151407): Register with the Optimiziation Guide for the
// language detection model.
}
TranslateModelServiceImpl::~TranslateModelServiceImpl() = default;
base::Optional<base::File>
TranslateModelServiceImpl::GetLanguageDetectionModelFile() {
// TODO(crbug.com/1151406): Implement loading the model on a background thread
// and return it for use by translate.
return base::nullopt;
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_MODEL_SERVICE_IMPL_H_
#define CHROME_BROWSER_TRANSLATE_TRANSLATE_MODEL_SERVICE_IMPL_H_
#include "components/keyed_service/core/keyed_service.h"
#include "components/translate/core/browser/translate_model_service.h"
// Service that manages models required to support translation in the browser.
class TranslateModelServiceImpl : public KeyedService,
public translate::TranslateModelService {
public:
TranslateModelServiceImpl();
~TranslateModelServiceImpl() override;
// translate::TranslateModelService:
base::Optional<base::File> GetLanguageDetectionModelFile() override;
};
#endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_MODEL_SERVICE_IMPL_H_
...@@ -14,6 +14,8 @@ static_library("browser") { ...@@ -14,6 +14,8 @@ static_library("browser") {
"content_translate_util.h", "content_translate_util.h",
"per_frame_content_translate_driver.cc", "per_frame_content_translate_driver.cc",
"per_frame_content_translate_driver.h", "per_frame_content_translate_driver.h",
"translate_model_service.cc",
"translate_model_service.h",
] ]
public_deps = [ public_deps = [
...@@ -24,7 +26,10 @@ static_library("browser") { ...@@ -24,7 +26,10 @@ static_library("browser") {
"//content/public/browser", "//content/public/browser",
] ]
deps = [ deps = [
"//components/keyed_service/core",
"//components/language/core/browser", "//components/language/core/browser",
"//components/optimization_guide",
"//components/optimization_guide/proto:optimization_guide_proto",
"//components/search_engines:search_engines", "//components/search_engines:search_engines",
"//components/services/language_detection/public/cpp", "//components/services/language_detection/public/cpp",
"//components/services/language_detection/public/mojom", "//components/services/language_detection/public/mojom",
......
include_rules = [ include_rules = [
"+content/public/browser", "+content/public/browser",
"+components/google/core/common", "+components/google/core/common",
"+components/keyed_service/core",
"+components/optimization_guide",
"+components/services/language_detection/public/cpp", "+components/services/language_detection/public/cpp",
"+components/services/language_detection/public/mojom", "+components/services/language_detection/public/mojom",
"+components/translate/core/language_detection", "+components/translate/core/language_detection",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/translate/content/browser/translate_model_service.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "components/optimization_guide/optimization_guide_decider.h"
#include "components/optimization_guide/proto/models.pb.h"
namespace translate {
TranslateModelService::TranslateModelService(
optimization_guide::OptimizationGuideDecider* opt_guide)
: opt_guide_(opt_guide) {
opt_guide_->AddObserverForOptimizationTargetModel(
optimization_guide::proto::OPTIMIZATION_TARGET_LANGUAGE_DETECTION, this);
}
TranslateModelService::~TranslateModelService() = default;
void TranslateModelService::Shutdown() {
// This and the optimization guide are keyed services, currently optimization
// guide is a BrowserContextKeyedService, it will be cleaned first so removing
// the observer should not be performed.
}
void TranslateModelService::OnModelFileUpdated(
optimization_guide::proto::OptimizationTarget optimization_target,
const base::FilePath& file_path) {
if (optimization_target !=
optimization_guide::proto::OPTIMIZATION_TARGET_LANGUAGE_DETECTION) {
return;
}
// TODO(crbug.com/1151406): Implement loading the model on a background thread
// and return it for use by translate.
}
base::Optional<base::File>
TranslateModelService::GetLanguageDetectionModelFile() {
// TODO(crbug.com/1151406): Implement loading the model on a background thread
// and return it for use by translate.
return base::nullopt;
}
} // namespace translate
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_TRANSLATE_CONTENT_BROWSER_TRANSLATE_MODEL_SERVICE_H_
#define COMPONENTS_TRANSLATE_CONTENT_BROWSER_TRANSLATE_MODEL_SERVICE_H_
#include "base/optional.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/optimization_guide/optimization_target_model_observer.h"
namespace base {
class File;
class FilePath;
} // namespace base
namespace optimization_guide {
class OptimizationGuideDecider;
} // namespace optimization_guide
namespace translate {
// Service that manages models required to support translation in the browser.
// Currently, the service should only be used in the browser as it relies on
// the Optimization Guide.
class TranslateModelService
: public KeyedService,
public optimization_guide::OptimizationTargetModelObserver {
public:
explicit TranslateModelService(
optimization_guide::OptimizationGuideDecider* opt_guide);
~TranslateModelService() override;
// KeyedService implementation:
void Shutdown() override;
// optimization_guide::OptimizationTargetModelObserver implementation:
void OnModelFileUpdated(
optimization_guide::proto::OptimizationTarget optimization_target,
const base::FilePath& file_path) override;
// Returns a loaded file containing the TFLite model capable of detecting the
// language of a web page's text.
base::Optional<base::File> GetLanguageDetectionModelFile();
private:
// Optimization Guide Service that provides model files for this
// service. Optimization Guide Service is a
// BrowserContextKeyedServiceFactory and should not
// be used after ShutDown.
optimization_guide::OptimizationGuideDecider* opt_guide_;
};
} // namespace translate
#endif // COMPONENTS_TRANSLATE_CONTENT_BROWSER_TRANSLATE_MODEL_SERVICE_H_
...@@ -30,7 +30,6 @@ static_library("browser") { ...@@ -30,7 +30,6 @@ static_library("browser") {
"translate_metrics_logger.h", "translate_metrics_logger.h",
"translate_metrics_logger_impl.cc", "translate_metrics_logger_impl.cc",
"translate_metrics_logger_impl.h", "translate_metrics_logger_impl.h",
"translate_model_service.h",
"translate_prefs.cc", "translate_prefs.cc",
"translate_prefs.h", "translate_prefs.h",
"translate_ranker.h", "translate_ranker.h",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_MODEL_SERVICE_H_
#define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_MODEL_SERVICE_H_
#include "base/optional.h"
namespace base {
class File;
} // namespace base
namespace translate {
// Service that manages models required to support translation in the browser.
class TranslateModelService {
public:
TranslateModelService() = default;
// Returns a loaded file containing the TFLite model capable of detecting the
// language of a web page's text.
virtual base::Optional<base::File> GetLanguageDetectionModelFile() = 0;
protected:
virtual ~TranslateModelService() = default;
};
} // namespace translate
#endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_MODEL_SERVICE_H_
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