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

Create TranslateModelService and Factory

Implement the service that will handle loading TFLite models
needed for the translate service in Chrome.

Bug: 1151404
Change-Id: Ibcf242d7d4d54e032fd980fd244247bc1196fc69
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2553526Reviewed-by: default avatarScott Little <sclittle@chromium.org>
Reviewed-by: default avatarSophie Chang <sophiechang@chromium.org>
Reviewed-by: default avatarTarun Bansal <tbansal@chromium.org>
Commit-Queue: Michael Crouse <mcrouse@chromium.org>
Cr-Commit-Position: refs/heads/master@{#833569}
parent fe638118
...@@ -1839,6 +1839,10 @@ static_library("browser") { ...@@ -1839,6 +1839,10 @@ static_library("browser") {
"translate/chrome_translate_client.h", "translate/chrome_translate_client.h",
"translate/translate_accept_languages_factory.cc", "translate/translate_accept_languages_factory.cc",
"translate/translate_accept_languages_factory.h", "translate/translate_accept_languages_factory.h",
"translate/translate_model_service_factory.cc",
"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",
......
// 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 "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/translate/translate_model_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/translate/core/common/translate_util.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"
using TranslateModelServiceDisabledBrowserTest = InProcessBrowserTest;
IN_PROC_BROWSER_TEST_F(TranslateModelServiceDisabledBrowserTest,
TranslateModelServiceDisabled) {
EXPECT_FALSE(
TranslateModelServiceFactory::GetForProfile(browser()->profile()));
}
class TranslateModelServiceBrowserTest
: public TranslateModelServiceDisabledBrowserTest {
public:
TranslateModelServiceBrowserTest() {
scoped_feature_list_.InitAndEnableFeature(
translate::kTFLiteLanguageDetectionEnabled);
}
~TranslateModelServiceBrowserTest() override = default;
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_F(TranslateModelServiceBrowserTest,
TranslateModelServiceEnabled) {
EXPECT_TRUE(
TranslateModelServiceFactory::GetForProfile(browser()->profile()));
}
IN_PROC_BROWSER_TEST_F(TranslateModelServiceBrowserTest,
TranslateModelServiceEnabled_OffTheRecord) {
EXPECT_TRUE(TranslateModelServiceFactory::GetForProfile(
browser()->profile()->GetPrimaryOTRProfile()));
}
// 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_factory.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/translate/translate_model_service_impl.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/translate/core/common/translate_util.h"
#include "content/public/browser/browser_context.h"
// static
TranslateModelServiceImpl* TranslateModelServiceFactory::GetForProfile(
Profile* profile) {
if (translate::IsTFLiteLanguageDetectionEnabled()) {
return static_cast<TranslateModelServiceImpl*>(
GetInstance()->GetServiceForBrowserContext(profile, true));
}
return nullptr;
}
// static
TranslateModelServiceFactory* TranslateModelServiceFactory::GetInstance() {
static base::NoDestructor<TranslateModelServiceFactory> factory;
return factory.get();
}
TranslateModelServiceFactory::TranslateModelServiceFactory()
: BrowserContextKeyedServiceFactory(
"TranslateModelService",
BrowserContextDependencyManager::GetInstance()) {}
TranslateModelServiceFactory::~TranslateModelServiceFactory() = default;
KeyedService* TranslateModelServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
return new TranslateModelServiceImpl();
}
content::BrowserContext* TranslateModelServiceFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
// Use the original profile's TranslateModelService, even in Incognito mode.
return chrome::GetBrowserContextRedirectedInIncognito(context);
}
// 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_FACTORY_H_
#define CHROME_BROWSER_TRANSLATE_TRANSLATE_MODEL_SERVICE_FACTORY_H_
#include "base/macros.h"
#include "base/no_destructor.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
namespace content {
class BrowserContext;
} // namespace content
class TranslateModelServiceImpl;
class Profile;
// LazyInstance that owns all TranslateModelService(s) and associates
// them with Profiles.
class TranslateModelServiceFactory : public BrowserContextKeyedServiceFactory {
public:
// Gets the TranslateModelService for the profile.
//
// Returns null if the features that allow for this to provide useful
// information are disabled.
static TranslateModelServiceImpl* GetForProfile(Profile* profile);
// Gets the LazyInstance that owns all TranslateModelService(s).
static TranslateModelServiceFactory* GetInstance();
private:
friend base::NoDestructor<TranslateModelServiceFactory>;
TranslateModelServiceFactory();
~TranslateModelServiceFactory() override;
// BrowserContextKeyedServiceFactory:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const override;
};
#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_
...@@ -1382,6 +1382,7 @@ if (!is_android) { ...@@ -1382,6 +1382,7 @@ if (!is_android) {
"../browser/tracing/chrome_tracing_delegate_browsertest.cc", "../browser/tracing/chrome_tracing_delegate_browsertest.cc",
"../browser/translate/language_detection_service_browsertest.cc", "../browser/translate/language_detection_service_browsertest.cc",
"../browser/translate/translate_manager_browsertest.cc", "../browser/translate/translate_manager_browsertest.cc",
"../browser/translate/translate_model_service_browsertest.cc",
"../browser/ui/ask_google_for_suggestions_dialog_browsertest.cc", "../browser/ui/ask_google_for_suggestions_dialog_browsertest.cc",
"../browser/ui/autofill/payments/card_unmask_prompt_view_browsertest.cc", "../browser/ui/autofill/payments/card_unmask_prompt_view_browsertest.cc",
"../browser/ui/autofill/payments/card_unmask_prompt_view_tester.h", "../browser/ui/autofill/payments/card_unmask_prompt_view_tester.h",
......
...@@ -29,6 +29,7 @@ static_library("browser") { ...@@ -29,6 +29,7 @@ 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_
...@@ -25,6 +25,9 @@ const char kSecurityOrigin[] = "https://translate.googleapis.com/"; ...@@ -25,6 +25,9 @@ const char kSecurityOrigin[] = "https://translate.googleapis.com/";
const base::Feature kTranslateSubFrames{"TranslateSubFrames", const base::Feature kTranslateSubFrames{"TranslateSubFrames",
base::FEATURE_DISABLED_BY_DEFAULT}; base::FEATURE_DISABLED_BY_DEFAULT};
const base::Feature kTFLiteLanguageDetectionEnabled{
"TFLiteLanguageDetectionEnabled", base::FEATURE_DISABLED_BY_DEFAULT};
GURL GetTranslateSecurityOrigin() { GURL GetTranslateSecurityOrigin() {
std::string security_origin(kSecurityOrigin); std::string security_origin(kSecurityOrigin);
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
...@@ -45,4 +48,8 @@ bool IsSubFrameLanguageDetectionEnabled() { ...@@ -45,4 +48,8 @@ bool IsSubFrameLanguageDetectionEnabled() {
kTranslateSubFrames, kDetectLanguageInSubFrames, true); kTranslateSubFrames, kDetectLanguageInSubFrames, true);
} }
bool IsTFLiteLanguageDetectionEnabled() {
return base::FeatureList::IsEnabled(kTFLiteLanguageDetectionEnabled);
}
} // namespace translate } // namespace translate
...@@ -14,6 +14,9 @@ namespace translate { ...@@ -14,6 +14,9 @@ namespace translate {
// main frame. // main frame.
extern const base::Feature kTranslateSubFrames; extern const base::Feature kTranslateSubFrames;
// Controls whether the TFLite-based language detection is enabled.
extern const base::Feature kTFLiteLanguageDetectionEnabled;
// Isolated world sets following security-origin by default. // Isolated world sets following security-origin by default.
extern const char kSecurityOrigin[]; extern const char kSecurityOrigin[];
...@@ -27,6 +30,9 @@ bool IsSubFrameTranslationEnabled(); ...@@ -27,6 +30,9 @@ bool IsSubFrameTranslationEnabled();
// Return whether sub frame language detection is enabled. // Return whether sub frame language detection is enabled.
bool IsSubFrameLanguageDetectionEnabled(); bool IsSubFrameLanguageDetectionEnabled();
// Return whether TFLIte-based language detection is enabled.
bool IsTFLiteLanguageDetectionEnabled();
} // namespace translate } // namespace translate
#endif // COMPONENTS_TRANSLATE_CORE_COMMON_TRANSLATE_UTIL_H_ #endif // COMPONENTS_TRANSLATE_CORE_COMMON_TRANSLATE_UTIL_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