Fire an error if a pref used in the UI is missing once all prefs are fetched.
[chromium-blink-merge.git] / chrome / browser / translate / translate_service.cc
blob03ac7a719c1bc21109b51b9972ff124af8cbba4c
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"
17 #include "url/gurl.h"
19 #if defined(OS_CHROMEOS)
20 #include "chrome/browser/chromeos/file_manager/app_id.h"
21 #include "extensions/common/constants.h"
22 #endif
24 namespace {
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() {}
38 // static
39 void TranslateService::Initialize() {
40 if (g_translate_service)
41 return;
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());
54 // static
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();
60 } else {
61 // This path is only used by browser tests.
62 download_manager->set_request_context(NULL);
66 // static
67 void TranslateService::InitializeForTesting() {
68 if (!g_translate_service) {
69 TranslateService::Initialize();
70 } else {
71 translate::TranslateDownloadManager::GetInstance()->ResetForTesting();
72 g_translate_service->OnResourceRequestsAllowed();
76 // static
77 void TranslateService::ShutdownForTesting() {
78 translate::TranslateDownloadManager::GetInstance()->Shutdown();
81 void TranslateService::OnResourceRequestsAllowed() {
82 translate::TranslateLanguageList* language_list =
83 translate::TranslateDownloadManager::GetInstance()->language_list();
84 if (!language_list) {
85 NOTREACHED();
86 return;
89 language_list->SetResourceRequestsAllowed(
90 resource_request_allowed_notifier_.ResourceRequestsAllowed());
93 // static
94 bool TranslateService::IsTranslateBubbleEnabled() {
95 #if defined(USE_AURA)
96 return true;
97 #elif defined(OS_MACOSX)
98 // The bubble UX is experimental on Mac OS X.
99 return base::CommandLine::ForCurrentProcess()->HasSwitch(
100 switches::kEnableTranslateNewUX);
101 #else
102 // The bubble UX is not implemented on other platforms.
103 return false;
104 #endif
107 // static
108 std::string TranslateService::GetTargetLanguage(PrefService* prefs) {
109 std::vector<std::string> accept_languages_list;
110 base::SplitString(prefs->GetString(prefs::kAcceptLanguages), ',',
111 &accept_languages_list);
112 return translate::TranslateManager::GetTargetLanguage(accept_languages_list);
115 // static
116 bool TranslateService::IsTranslatableURL(const GURL& url) {
117 // A URLs is translatable unless it is one of the following:
118 // - empty (can happen for popups created with window.open(""))
119 // - an internal URL (chrome:// and others)
120 // - the devtools (which is considered UI)
121 // - Chrome OS file manager extension
122 // - an FTP page (as FTP pages tend to have long lists of filenames that may
123 // confuse the CLD)
124 return !url.is_empty() && !url.SchemeIs(content::kChromeUIScheme) &&
125 !url.SchemeIs(content::kChromeDevToolsScheme) &&
126 #if defined(OS_CHROMEOS)
127 !(url.SchemeIs(extensions::kExtensionScheme) &&
128 url.DomainIs(file_manager::kFileManagerAppId)) &&
129 #endif
130 !url.SchemeIs(url::kFtpScheme);