Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / translate / translate_service.cc
blob8d360cc29625ba5aa8426e0fb93a252bc9367a89
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 translate::TranslateManager::SetIgnoreMissingKeyForTesting(true);
71 } else {
72 translate::TranslateDownloadManager::GetInstance()->ResetForTesting();
73 g_translate_service->OnResourceRequestsAllowed();
77 // static
78 void TranslateService::ShutdownForTesting() {
79 translate::TranslateDownloadManager::GetInstance()->Shutdown();
82 void TranslateService::OnResourceRequestsAllowed() {
83 translate::TranslateLanguageList* language_list =
84 translate::TranslateDownloadManager::GetInstance()->language_list();
85 if (!language_list) {
86 NOTREACHED();
87 return;
90 language_list->SetResourceRequestsAllowed(
91 resource_request_allowed_notifier_.ResourceRequestsAllowed());
94 // static
95 bool TranslateService::IsTranslateBubbleEnabled() {
96 #if defined(USE_AURA)
97 return true;
98 #elif defined(OS_MACOSX)
99 // The bubble UX is experimental on Mac OS X.
100 return base::CommandLine::ForCurrentProcess()->HasSwitch(
101 switches::kEnableTranslateNewUX);
102 #else
103 // The bubble UX is not implemented on other platforms.
104 return false;
105 #endif
108 // static
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);
116 // static
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
124 // confuse the CLD)
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)) &&
130 #endif
131 !url.SchemeIs(url::kFtpScheme);