blob: 8d2f78b1fbce42e31ff9f44cc20193814a8608a2 [file] [log] [blame]
// Copyright 2017 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 "ios/web_view/internal/translate/web_view_translate_service.h"
#include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "base/no_destructor.h"
#include "components/translate/core/browser/translate_download_manager.h"
#include "ios/web_view/internal/app/application_context.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace ios_web_view {
WebViewTranslateService::TranslateRequestsAllowedListener::
TranslateRequestsAllowedListener()
: resource_request_allowed_notifier_(
ios_web_view::ApplicationContext::GetInstance()->GetLocalState(),
/*disable_network_switch=*/nullptr,
base::BindOnce(&ApplicationContext::GetNetworkConnectionTracker,
base::Unretained(ApplicationContext::GetInstance()))) {
resource_request_allowed_notifier_.Init(this, /*leaky=*/false);
}
WebViewTranslateService::TranslateRequestsAllowedListener::
~TranslateRequestsAllowedListener() {}
void WebViewTranslateService::TranslateRequestsAllowedListener::
OnResourceRequestsAllowed() {
translate::TranslateLanguageList* language_list =
translate::TranslateDownloadManager::GetInstance()->language_list();
DCHECK(language_list);
language_list->SetResourceRequestsAllowed(
resource_request_allowed_notifier_.ResourceRequestsAllowed());
}
WebViewTranslateService* WebViewTranslateService::GetInstance() {
static base::NoDestructor<WebViewTranslateService> instance;
return instance.get();
}
WebViewTranslateService::WebViewTranslateService() {}
WebViewTranslateService::~WebViewTranslateService() = default;
void WebViewTranslateService::Initialize() {
// Initialize the allowed state for resource requests.
translate_requests_allowed_listener_.OnResourceRequestsAllowed();
// Initialize translate.
translate::TranslateDownloadManager* download_manager =
translate::TranslateDownloadManager::GetInstance();
download_manager->set_url_loader_factory(
ios_web_view::ApplicationContext::GetInstance()
->GetSharedURLLoaderFactory()
.get());
download_manager->set_application_locale(
ios_web_view::ApplicationContext::GetInstance()->GetApplicationLocale());
}
void WebViewTranslateService::Shutdown() {
translate::TranslateDownloadManager* download_manager =
translate::TranslateDownloadManager::GetInstance();
download_manager->Shutdown();
}
} // namespace ios_web_view