Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ios / chrome / browser / translate / chrome_ios_translate_client.mm
blob9eefa03b96f2f35620da60ab9041bb68fcbf3e24
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 "ios/chrome/browser/translate/chrome_ios_translate_client.h"
7 #include <vector>
9 #include "base/logging.h"
10 #include "base/prefs/pref_service.h"
11 #include "components/infobars/core/infobar.h"
12 #include "components/translate/core/browser/page_translated_details.h"
13 #include "components/translate/core/browser/translate_accept_languages.h"
14 #include "components/translate/core/browser/translate_infobar_delegate.h"
15 #include "components/translate/core/browser/translate_manager.h"
16 #include "components/translate/core/browser/translate_prefs.h"
17 #include "components/translate/core/browser/translate_step.h"
18 #include "ios/chrome/browser/infobars/infobar.h"
19 #include "ios/chrome/browser/infobars/infobar_controller.h"
20 #include "ios/chrome/browser/infobars/infobar_manager_impl.h"
21 #include "ios/chrome/browser/pref_names.h"
22 #import "ios/chrome/browser/translate/after_translate_infobar_controller.h"
23 #import "ios/chrome/browser/translate/before_translate_infobar_controller.h"
24 #import "ios/chrome/browser/translate/never_translate_infobar_controller.h"
25 #include "ios/chrome/browser/translate/translate_accept_languages_factory.h"
26 #import "ios/chrome/browser/translate/translate_message_infobar_controller.h"
27 #include "ios/chrome/browser/translate/translate_service_ios.h"
28 #include "ios/chrome/grit/ios_theme_resources.h"
29 #include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.h"
30 #include "ios/web/public/browser_state.h"
31 #include "ios/web/public/web_state/web_state.h"
32 #include "url/gurl.h"
34 DEFINE_WEB_STATE_USER_DATA_KEY(ChromeIOSTranslateClient);
36 ChromeIOSTranslateClient::ChromeIOSTranslateClient(web::WebState* web_state)
37     : web::WebStateObserver(web_state),
38       translate_manager_(
39           new translate::TranslateManager(this, ios::prefs::kAcceptLanguages)),
40       translate_driver_(web_state,
41                         web_state->GetNavigationManager(),
42                         translate_manager_.get()) {
45 ChromeIOSTranslateClient::~ChromeIOSTranslateClient() {
48 // static
49 scoped_ptr<translate::TranslatePrefs>
50 ChromeIOSTranslateClient::CreateTranslatePrefs(PrefService* prefs) {
51   return scoped_ptr<translate::TranslatePrefs>(new translate::TranslatePrefs(
52       prefs, ios::prefs::kAcceptLanguages, nullptr));
55 translate::TranslateManager* ChromeIOSTranslateClient::GetTranslateManager() {
56   return translate_manager_.get();
59 // TranslateClient implementation:
61 scoped_ptr<infobars::InfoBar> ChromeIOSTranslateClient::CreateInfoBar(
62     scoped_ptr<translate::TranslateInfoBarDelegate> delegate) const {
63   translate::TranslateStep step = delegate->translate_step();
65   scoped_ptr<InfoBarIOS> infobar(new InfoBarIOS(delegate.Pass()));
66   base::scoped_nsobject<InfoBarController> controller;
67   switch (step) {
68     case translate::TRANSLATE_STEP_AFTER_TRANSLATE:
69       controller.reset([[AfterTranslateInfoBarController alloc]
70           initWithDelegate:infobar.get()]);
71       break;
72     case translate::TRANSLATE_STEP_BEFORE_TRANSLATE:
73       controller.reset([[BeforeTranslateInfoBarController alloc]
74           initWithDelegate:infobar.get()]);
75       break;
76     case translate::TRANSLATE_STEP_NEVER_TRANSLATE:
77       controller.reset([[NeverTranslateInfoBarController alloc]
78           initWithDelegate:infobar.get()]);
79       break;
80     case translate::TRANSLATE_STEP_TRANSLATING:
81     case translate::TRANSLATE_STEP_TRANSLATE_ERROR:
82       controller.reset([[TranslateMessageInfoBarController alloc]
83           initWithDelegate:infobar.get()]);
84       break;
85     default:
86       NOTREACHED();
87   }
88   infobar->SetController(controller);
89   return infobar.Pass();
92 void ChromeIOSTranslateClient::ShowTranslateUI(
93     translate::TranslateStep step,
94     const std::string source_language,
95     const std::string target_language,
96     translate::TranslateErrors::Type error_type,
97     bool triggered_from_menu) {
98   DCHECK(web_state());
99   if (error_type != translate::TranslateErrors::NONE)
100     step = translate::TRANSLATE_STEP_TRANSLATE_ERROR;
102   // Infobar UI.
103   translate::TranslateInfoBarDelegate::Create(
104       step != translate::TRANSLATE_STEP_BEFORE_TRANSLATE,
105       translate_manager_->GetWeakPtr(),
106       InfoBarManagerImpl::FromWebState(web_state()),
107       web_state()->GetBrowserState()->IsOffTheRecord(), step, source_language,
108       target_language, error_type, triggered_from_menu);
111 translate::TranslateDriver* ChromeIOSTranslateClient::GetTranslateDriver() {
112   return &translate_driver_;
115 PrefService* ChromeIOSTranslateClient::GetPrefs() {
116   DCHECK(web_state());
117   ios::ChromeBrowserState* chrome_browser_state =
118       ios::ChromeBrowserState::FromBrowserState(web_state()->GetBrowserState());
119   return chrome_browser_state->GetOriginalChromeBrowserState()->GetPrefs();
122 scoped_ptr<translate::TranslatePrefs>
123 ChromeIOSTranslateClient::GetTranslatePrefs() {
124   DCHECK(web_state());
125   ios::ChromeBrowserState* chrome_browser_state =
126       ios::ChromeBrowserState::FromBrowserState(web_state()->GetBrowserState());
127   return CreateTranslatePrefs(chrome_browser_state->GetPrefs());
130 translate::TranslateAcceptLanguages*
131 ChromeIOSTranslateClient::GetTranslateAcceptLanguages() {
132   DCHECK(web_state());
133   return TranslateAcceptLanguagesFactory::GetForBrowserState(
134       ios::ChromeBrowserState::FromBrowserState(
135           web_state()->GetBrowserState()));
138 int ChromeIOSTranslateClient::GetInfobarIconID() const {
139   return IDR_INFOBAR_TRANSLATE_IOS;
142 bool ChromeIOSTranslateClient::IsTranslatableURL(const GURL& url) {
143   return TranslateServiceIOS::IsTranslatableURL(url);
146 void ChromeIOSTranslateClient::ShowReportLanguageDetectionErrorUI(
147     const GURL& report_url) {
148   NOTREACHED();
151 void ChromeIOSTranslateClient::WebStateDestroyed() {
152   // Translation process can be interrupted.
153   // Destroying the TranslateManager now guarantees that it never has to deal
154   // with nullptr WebState.
155   translate_manager_.reset();