• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/translate/translate_service.h"
6 
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"
18 
19 #if defined(OS_CHROMEOS)
20 #include "chrome/browser/chromeos/file_manager/app_id.h"
21 #include "extensions/common/constants.h"
22 #endif
23 
24 namespace {
25 // The singleton instance of TranslateService.
26 TranslateService* g_translate_service = NULL;
27 }
28 
TranslateService()29 TranslateService::TranslateService() {
30   resource_request_allowed_notifier_.Init(this);
31 }
32 
~TranslateService()33 TranslateService::~TranslateService() {}
34 
35 // static
Initialize()36 void TranslateService::Initialize() {
37   if (g_translate_service)
38     return;
39 
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());
49 }
50 
51 // static
Shutdown(bool cleanup_pending_fetcher)52 void TranslateService::Shutdown(bool cleanup_pending_fetcher) {
53   TranslateDownloadManager* download_manager =
54       TranslateDownloadManager::GetInstance();
55   if (cleanup_pending_fetcher) {
56     download_manager->Shutdown();
57   } else {
58     // This path is only used by browser tests.
59     download_manager->set_request_context(NULL);
60   }
61 }
62 
63 // static
InitializeForTesting()64 void TranslateService::InitializeForTesting() {
65   if (!g_translate_service) {
66     TranslateService::Initialize();
67   } else {
68     TranslateDownloadManager::GetInstance()->ResetForTesting();
69     g_translate_service->OnResourceRequestsAllowed();
70   }
71 }
72 
73 // static
ShutdownForTesting()74 void TranslateService::ShutdownForTesting() {
75   TranslateDownloadManager::GetInstance()->Shutdown();
76 }
77 
OnResourceRequestsAllowed()78 void TranslateService::OnResourceRequestsAllowed() {
79   TranslateLanguageList* language_list =
80       TranslateDownloadManager::GetInstance()->language_list();
81   if (!language_list) {
82     NOTREACHED();
83     return;
84   }
85 
86   language_list->SetResourceRequestsAllowed(
87       resource_request_allowed_notifier_.ResourceRequestsAllowed());
88 }
89 
90 // static
IsTranslateBubbleEnabled()91 bool TranslateService::IsTranslateBubbleEnabled() {
92 #if defined(USE_AURA)
93   return true;
94 #elif defined(OS_MACOSX)
95   // The bubble UX is experimental on Mac OS X.
96   return CommandLine::ForCurrentProcess()->HasSwitch(
97       switches::kEnableTranslateNewUX);
98 #else
99   // The bubble UX is not implemented on other platforms.
100   return false;
101 #endif
102 }
103 
104 // static
GetTargetLanguage(PrefService * prefs)105 std::string TranslateService::GetTargetLanguage(PrefService* prefs) {
106   std::vector<std::string> accept_languages_list;
107   base::SplitString(prefs->GetString(prefs::kAcceptLanguages), ',',
108                     &accept_languages_list);
109   return TranslateManager::GetTargetLanguage(accept_languages_list);
110 }
111 
112 // static
IsTranslatableURL(const GURL & url)113 bool TranslateService::IsTranslatableURL(const GURL& url) {
114   // A URLs is translatable unless it is one of the following:
115   // - empty (can happen for popups created with window.open(""))
116   // - an internal URL (chrome:// and others)
117   // - the devtools (which is considered UI)
118   // - Chrome OS file manager extension
119   // - an FTP page (as FTP pages tend to have long lists of filenames that may
120   //   confuse the CLD)
121   return !url.is_empty() && !url.SchemeIs(content::kChromeUIScheme) &&
122          !url.SchemeIs(content::kChromeDevToolsScheme) &&
123 #if defined(OS_CHROMEOS)
124          !(url.SchemeIs(extensions::kExtensionScheme) &&
125            url.DomainIs(file_manager::kFileManagerAppId)) &&
126 #endif
127          !url.SchemeIs(url::kFtpScheme);
128 }
129