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()
30 : resource_request_allowed_notifier_(
31 g_browser_process
->local_state(),
32 switches::kDisableBackgroundNetworking
) {
33 resource_request_allowed_notifier_
.Init(this);
36 TranslateService::~TranslateService() {}
39 void TranslateService::Initialize() {
40 if (g_translate_service
)
43 g_translate_service
= new TranslateService
;
44 // Initialize the allowed state for resource requests.
45 g_translate_service
->OnResourceRequestsAllowed();
46 translate::TranslateDownloadManager
* download_manager
=
47 translate::TranslateDownloadManager::GetInstance();
48 download_manager
->set_request_context(
49 g_browser_process
->system_request_context());
50 download_manager
->set_application_locale(
51 g_browser_process
->GetApplicationLocale());
55 void TranslateService::Shutdown(bool cleanup_pending_fetcher
) {
56 translate::TranslateDownloadManager
* download_manager
=
57 translate::TranslateDownloadManager::GetInstance();
58 if (cleanup_pending_fetcher
) {
59 download_manager
->Shutdown();
61 // This path is only used by browser tests.
62 download_manager
->set_request_context(NULL
);
67 void TranslateService::InitializeForTesting() {
68 if (!g_translate_service
) {
69 TranslateService::Initialize();
70 translate::TranslateManager::SetIgnoreMissingKeyForTesting(true);
72 translate::TranslateDownloadManager::GetInstance()->ResetForTesting();
73 g_translate_service
->OnResourceRequestsAllowed();
78 void TranslateService::ShutdownForTesting() {
79 translate::TranslateDownloadManager::GetInstance()->Shutdown();
82 void TranslateService::OnResourceRequestsAllowed() {
83 translate::TranslateLanguageList
* language_list
=
84 translate::TranslateDownloadManager::GetInstance()->language_list();
90 language_list
->SetResourceRequestsAllowed(
91 resource_request_allowed_notifier_
.ResourceRequestsAllowed());
95 bool TranslateService::IsTranslateBubbleEnabled() {
98 #elif defined(OS_MACOSX)
99 // The bubble UX is experimental on Mac OS X.
100 return base::CommandLine::ForCurrentProcess()->HasSwitch(
101 switches::kEnableTranslateNewUX
);
103 // The bubble UX is not implemented on other platforms.
109 std::string
TranslateService::GetTargetLanguage(PrefService
* prefs
) {
110 std::vector
<std::string
> accept_languages_list
= base::SplitString(
111 prefs
->GetString(prefs::kAcceptLanguages
), ",",
112 base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
113 return translate::TranslateManager::GetTargetLanguage(accept_languages_list
);
117 bool TranslateService::IsTranslatableURL(const GURL
& url
) {
118 // A URLs is translatable unless it is one of the following:
119 // - empty (can happen for popups created with window.open(""))
120 // - an internal URL (chrome:// and others)
121 // - the devtools (which is considered UI)
122 // - Chrome OS file manager extension
123 // - an FTP page (as FTP pages tend to have long lists of filenames that may
125 return !url
.is_empty() && !url
.SchemeIs(content::kChromeUIScheme
) &&
126 !url
.SchemeIs(content::kChromeDevToolsScheme
) &&
127 #if defined(OS_CHROMEOS)
128 !(url
.SchemeIs(extensions::kExtensionScheme
) &&
129 url
.DomainIs(file_manager::kFileManagerAppId
)) &&
131 !url
.SchemeIs(url::kFtpScheme
);