Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ios / chrome / browser / translate / translate_service_ios.cc
blob3f2964f8fc63287b7bab512f2694edaa80890795
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ios/chrome/browser/translate/translate_service_ios.h"
7 #include "base/logging.h"
8 #include "components/translate/core/browser/translate_download_manager.h"
9 #include "components/translate/core/browser/translate_manager.h"
10 #include "ios/chrome/browser/application_context.h"
11 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
12 #include "url/gurl.h"
14 namespace {
15 // The singleton instance of TranslateServiceIOS.
16 TranslateServiceIOS* g_translate_service = nullptr;
19 TranslateServiceIOS::TranslateServiceIOS()
20 : resource_request_allowed_notifier_(
21 GetApplicationContext()->GetLocalState(),
22 nullptr) {
23 resource_request_allowed_notifier_.Init(this);
26 TranslateServiceIOS::~TranslateServiceIOS() {
29 // static
30 void TranslateServiceIOS::Initialize() {
31 if (g_translate_service)
32 return;
34 g_translate_service = new TranslateServiceIOS;
35 // Initialize the allowed state for resource requests.
36 g_translate_service->OnResourceRequestsAllowed();
37 translate::TranslateDownloadManager* download_manager =
38 translate::TranslateDownloadManager::GetInstance();
39 download_manager->set_request_context(
40 GetApplicationContext()->GetSystemURLRequestContext());
41 download_manager->set_application_locale(
42 GetApplicationContext()->GetApplicationLocale());
45 // static
46 void TranslateServiceIOS::Shutdown() {
47 translate::TranslateDownloadManager* download_manager =
48 translate::TranslateDownloadManager::GetInstance();
49 download_manager->Shutdown();
52 void TranslateServiceIOS::OnResourceRequestsAllowed() {
53 translate::TranslateLanguageList* language_list =
54 translate::TranslateDownloadManager::GetInstance()->language_list();
55 if (!language_list) {
56 NOTREACHED();
57 return;
60 language_list->SetResourceRequestsAllowed(
61 resource_request_allowed_notifier_.ResourceRequestsAllowed());
64 // static
65 bool TranslateServiceIOS::IsTranslatableURL(const GURL& url) {
66 // A URL is translatable unless it is one of the following:
67 // - empty (can happen for popups created with window.open(""))
68 // - an internal URL
69 return !url.is_empty() &&
70 !url.SchemeIs(ios::GetChromeBrowserProvider()->GetChromeUIScheme());