Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / translate / translate_service.cc
blobe13f6f9e7f61b9088f90132de7d2bed7a2daf7f9
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/metrics/field_trial.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/string_util.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/pref_names.h"
16 #include "components/translate/core/browser/translate_download_manager.h"
17 #include "components/translate/core/browser/translate_manager.h"
18 #include "content/public/common/url_constants.h"
19 #include "url/gurl.h"
21 #if defined(OS_CHROMEOS)
22 #include "chrome/browser/chromeos/file_manager/app_id.h"
23 #include "extensions/common/constants.h"
24 #endif
26 namespace {
27 // The singleton instance of TranslateService.
28 TranslateService* g_translate_service = NULL;
31 TranslateService::TranslateService()
32 : resource_request_allowed_notifier_(
33 g_browser_process->local_state(),
34 switches::kDisableBackgroundNetworking) {
35 resource_request_allowed_notifier_.Init(this);
38 TranslateService::~TranslateService() {}
40 // static
41 void TranslateService::Initialize() {
42 if (g_translate_service)
43 return;
45 g_translate_service = new TranslateService;
46 // Initialize the allowed state for resource requests.
47 g_translate_service->OnResourceRequestsAllowed();
48 translate::TranslateDownloadManager* download_manager =
49 translate::TranslateDownloadManager::GetInstance();
50 download_manager->set_request_context(
51 g_browser_process->system_request_context());
52 download_manager->set_application_locale(
53 g_browser_process->GetApplicationLocale());
56 // static
57 void TranslateService::Shutdown(bool cleanup_pending_fetcher) {
58 translate::TranslateDownloadManager* download_manager =
59 translate::TranslateDownloadManager::GetInstance();
60 if (cleanup_pending_fetcher) {
61 download_manager->Shutdown();
62 } else {
63 // This path is only used by browser tests.
64 download_manager->set_request_context(NULL);
68 // static
69 void TranslateService::InitializeForTesting() {
70 if (!g_translate_service) {
71 TranslateService::Initialize();
72 translate::TranslateManager::SetIgnoreMissingKeyForTesting(true);
73 } else {
74 translate::TranslateDownloadManager::GetInstance()->ResetForTesting();
75 g_translate_service->OnResourceRequestsAllowed();
79 // static
80 void TranslateService::ShutdownForTesting() {
81 translate::TranslateDownloadManager::GetInstance()->Shutdown();
84 void TranslateService::OnResourceRequestsAllowed() {
85 translate::TranslateLanguageList* language_list =
86 translate::TranslateDownloadManager::GetInstance()->language_list();
87 if (!language_list) {
88 NOTREACHED();
89 return;
92 language_list->SetResourceRequestsAllowed(
93 resource_request_allowed_notifier_.ResourceRequestsAllowed());
96 // static
97 bool TranslateService::IsTranslateBubbleEnabled() {
98 #if defined(USE_AURA)
99 return true;
100 #elif defined(OS_MACOSX)
101 const std::string group_name =
102 base::FieldTrialList::FindFullName("TranslateNewUX");
103 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
104 switches::kDisableTranslateNewUX))
105 return false;
106 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
107 switches::kEnableTranslateNewUX))
108 return true;
109 return base::StartsWith(group_name, "Enabled", base::CompareCase::SENSITIVE);
110 #else
111 // The bubble UX is not implemented on other platforms.
112 return false;
113 #endif
116 // static
117 std::string TranslateService::GetTargetLanguage(PrefService* prefs) {
118 std::vector<std::string> accept_languages_list = base::SplitString(
119 prefs->GetString(prefs::kAcceptLanguages), ",",
120 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
121 return translate::TranslateManager::GetTargetLanguage(accept_languages_list);
124 // static
125 bool TranslateService::IsTranslatableURL(const GURL& url) {
126 // A URLs is translatable unless it is one of the following:
127 // - empty (can happen for popups created with window.open(""))
128 // - an internal URL (chrome:// and others)
129 // - the devtools (which is considered UI)
130 // - Chrome OS file manager extension
131 // - an FTP page (as FTP pages tend to have long lists of filenames that may
132 // confuse the CLD)
133 return !url.is_empty() && !url.SchemeIs(content::kChromeUIScheme) &&
134 !url.SchemeIs(content::kChromeDevToolsScheme) &&
135 #if defined(OS_CHROMEOS)
136 !(url.SchemeIs(extensions::kExtensionScheme) &&
137 url.DomainIs(file_manager::kFileManagerAppId)) &&
138 #endif
139 !url.SchemeIs(url::kFtpScheme);