Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2011 The Chromium Authors. All rights reserved. | 1 // Copyright 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/translate/translate_tab_helper.h" | 5 #include "chrome/browser/translate/translate_tab_helper.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/prefs/pref_service.h" | |
| 9 #include "base/strings/string_split.h" | |
| 8 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/translate/translate_accept_languages_factory.h" | 12 #include "chrome/browser/translate/translate_accept_languages_factory.h" |
| 11 #include "chrome/browser/translate/translate_infobar_delegate.h" | 13 #include "chrome/browser/translate/translate_infobar_delegate.h" |
| 12 #include "chrome/browser/translate/translate_manager.h" | 14 #include "chrome/browser/translate/translate_manager.h" |
| 13 #include "chrome/browser/translate/translate_service.h" | 15 #include "chrome/browser/translate/translate_service.h" |
| 14 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/browser_finder.h" | 17 #include "chrome/browser/ui/browser_finder.h" |
| 16 #include "chrome/browser/ui/browser_window.h" | 18 #include "chrome/browser/ui/browser_window.h" |
| 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 59 | 61 |
| 60 // static | 62 // static |
| 61 TranslateManager* TranslateTabHelper::GetManagerFromWebContents( | 63 TranslateManager* TranslateTabHelper::GetManagerFromWebContents( |
| 62 content::WebContents* web_contents) { | 64 content::WebContents* web_contents) { |
| 63 TranslateTabHelper* translate_tab_helper = FromWebContents(web_contents); | 65 TranslateTabHelper* translate_tab_helper = FromWebContents(web_contents); |
| 64 if (!translate_tab_helper) | 66 if (!translate_tab_helper) |
| 65 return NULL; | 67 return NULL; |
| 66 return translate_tab_helper->GetTranslateManager(); | 68 return translate_tab_helper->GetTranslateManager(); |
| 67 } | 69 } |
| 68 | 70 |
| 71 // static | |
| 72 void TranslateTabHelper::GetTranslateLanguages( | |
| 73 content::WebContents* web_contents, | |
| 74 std::string* source, | |
| 75 std::string* target) { | |
| 76 DCHECK(source != NULL); | |
| 77 DCHECK(target != NULL); | |
| 78 | |
| 79 TranslateTabHelper* translate_tab_helper = FromWebContents(web_contents); | |
|
Takashi Toyoshima
2014/04/02 03:57:30
Did you confirm that there is no race and translat
hajimehoshi
2014/04/02 09:16:18
Yes as long as this is called on UI thread.
| |
| 80 *source = translate_tab_helper->GetLanguageState().original_language(); | |
| 81 | |
| 82 Profile* profile = | |
| 83 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
| 84 Profile* original_profile = profile->GetOriginalProfile(); | |
| 85 PrefService* prefs = original_profile->GetPrefs(); | |
| 86 if (!web_contents->GetBrowserContext()->IsOffTheRecord()) { | |
| 87 std::string auto_translate_language = | |
| 88 TranslateManager::GetAutoTargetLanguage(*source, prefs); | |
| 89 if (!auto_translate_language.empty()) { | |
| 90 *target = auto_translate_language; | |
| 91 return; | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 std::string accept_languages_str = prefs->GetString(prefs::kAcceptLanguages); | |
| 96 std::vector<std::string> accept_languages_list; | |
| 97 base::SplitString(accept_languages_str, ',', &accept_languages_list); | |
| 98 *target = TranslateManager::GetTargetLanguage(accept_languages_list); | |
| 99 } | |
| 100 | |
| 69 TranslateManager* TranslateTabHelper::GetTranslateManager() { | 101 TranslateManager* TranslateTabHelper::GetTranslateManager() { |
| 70 return translate_manager_.get(); | 102 return translate_manager_.get(); |
| 71 } | 103 } |
| 72 | 104 |
| 73 content::WebContents* TranslateTabHelper::GetWebContents() { | 105 content::WebContents* TranslateTabHelper::GetWebContents() { |
| 74 return web_contents(); | 106 return web_contents(); |
| 75 } | 107 } |
| 76 | 108 |
| 77 void TranslateTabHelper::ShowTranslateUI(TranslateTabHelper::TranslateStep step, | 109 void TranslateTabHelper::ShowTranslateUI(TranslateTabHelper::TranslateStep step, |
| 78 const std::string source_language, | 110 const std::string source_language, |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 220 if (GetLanguageState().InTranslateNavigation()) | 252 if (GetLanguageState().InTranslateNavigation()) |
| 221 return; | 253 return; |
| 222 } | 254 } |
| 223 | 255 |
| 224 TranslateBubbleFactory::Show( | 256 TranslateBubbleFactory::Show( |
| 225 browser->window(), web_contents(), step, error_type); | 257 browser->window(), web_contents(), step, error_type); |
| 226 #else | 258 #else |
| 227 NOTREACHED(); | 259 NOTREACHED(); |
| 228 #endif | 260 #endif |
| 229 } | 261 } |
| OLD | NEW |