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 "chrome/browser/translate/translate_service.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/string_split.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/common/pref_names.h"
14 #include "components/translate/core/browser/translate_download_manager.h"
15 #include "components/translate/core/browser/translate_manager.h"
16 #include "content/public/common/url_constants.h"
19 #if defined(OS_CHROMEOS)
20 #include "chrome/browser/chromeos/file_manager/app_id.h"
21 #include "extensions/common/constants.h"
25 // The singleton instance of TranslateService.
26 TranslateService
* g_translate_service
= NULL
;
29 TranslateService::TranslateService() : use_infobar_(false) {
30 resource_request_allowed_notifier_
.Init(this);
33 TranslateService::~TranslateService() {}
36 void TranslateService::Initialize() {
37 if (g_translate_service
)
40 g_translate_service
= new TranslateService
;
41 // Initialize the allowed state for resource requests.
42 g_translate_service
->OnResourceRequestsAllowed();
43 TranslateDownloadManager
* download_manager
=
44 TranslateDownloadManager::GetInstance();
45 download_manager
->set_request_context(
46 g_browser_process
->system_request_context());
47 download_manager
->set_application_locale(
48 g_browser_process
->GetApplicationLocale());
52 void TranslateService::Shutdown(bool cleanup_pending_fetcher
) {
53 TranslateDownloadManager
* download_manager
=
54 TranslateDownloadManager::GetInstance();
55 if (cleanup_pending_fetcher
) {
56 download_manager
->Shutdown();
58 // This path is only used by browser tests.
59 download_manager
->set_request_context(NULL
);
64 void TranslateService::InitializeForTesting() {
65 if (!g_translate_service
) {
66 TranslateService::Initialize();
68 TranslateDownloadManager::GetInstance()->ResetForTesting();
69 g_translate_service
->OnResourceRequestsAllowed();
74 void TranslateService::ShutdownForTesting() {
75 TranslateDownloadManager::GetInstance()->Shutdown();
78 void TranslateService::OnResourceRequestsAllowed() {
79 TranslateLanguageList
* language_list
=
80 TranslateDownloadManager::GetInstance()->language_list();
86 language_list
->SetResourceRequestsAllowed(
87 resource_request_allowed_notifier_
.ResourceRequestsAllowed());
91 bool TranslateService::IsTranslateBubbleEnabled() {
94 return !g_translate_service
->use_infobar_
;
95 #elif defined(OS_MACOSX)
96 // The bubble UX is experimental on Mac OS X.
97 return CommandLine::ForCurrentProcess()->HasSwitch(
98 switches::kEnableTranslateNewUX
);
100 // The bubble UX is not implemented on the non-Aura platforms.
106 void TranslateService::SetUseInfobar(bool value
) {
108 g_translate_service
->use_infobar_
= value
;
112 std::string
TranslateService::GetTargetLanguage(PrefService
* prefs
) {
113 std::vector
<std::string
> accept_languages_list
;
114 base::SplitString(prefs
->GetString(prefs::kAcceptLanguages
), ',',
115 &accept_languages_list
);
116 return TranslateManager::GetTargetLanguage(accept_languages_list
);
120 bool TranslateService::IsTranslatableURL(const GURL
& url
) {
121 // A URLs is translatable unless it is one of the following:
122 // - empty (can happen for popups created with window.open(""))
123 // - an internal URL (chrome:// and others)
124 // - the devtools (which is considered UI)
125 // - Chrome OS file manager extension
126 // - an FTP page (as FTP pages tend to have long lists of filenames that may
128 return !url
.is_empty() && !url
.SchemeIs(content::kChromeUIScheme
) &&
129 !url
.SchemeIs(content::kChromeDevToolsScheme
) &&
130 #if defined(OS_CHROMEOS)
131 !(url
.SchemeIs(extensions::kExtensionScheme
) &&
132 url
.DomainIs(file_manager::kFileManagerAppId
)) &&
134 !url
.SchemeIs(content::kFtpScheme
);