| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #import "ios/web_view/internal/translate/web_view_translate_client.h" | 5 #import "ios/web_view/internal/translate/web_view_translate_client.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #import "base/mac/scoped_nsobject.h" | 10 #import "base/mac/scoped_nsobject.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "components/infobars/core/infobar.h" | 12 #include "components/infobars/core/infobar.h" |
| 13 #include "components/prefs/pref_service.h" | 13 #include "components/prefs/pref_service.h" |
| 14 #include "components/translate/core/browser/page_translated_details.h" | 14 #include "components/translate/core/browser/page_translated_details.h" |
| 15 #include "components/translate/core/browser/translate_accept_languages.h" | 15 #include "components/translate/core/browser/translate_accept_languages.h" |
| 16 #include "components/translate/core/browser/translate_infobar_delegate.h" | 16 #include "components/translate/core/browser/translate_infobar_delegate.h" |
| 17 #include "components/translate/core/browser/translate_manager.h" | 17 #include "components/translate/core/browser/translate_manager.h" |
| 18 #include "components/translate/core/browser/translate_prefs.h" | 18 #include "components/translate/core/browser/translate_prefs.h" |
| 19 #include "components/translate/core/browser/translate_step.h" | 19 #include "components/translate/core/browser/translate_step.h" |
| 20 #include "ios/web/public/browser_state.h" | 20 #include "ios/web/public/browser_state.h" |
| 21 #import "ios/web/public/web_state/web_state.h" | 21 #import "ios/web/public/web_state/web_state.h" |
| 22 #include "ios/web_view/internal/pref_names.h" | 22 #include "ios/web_view/internal/pref_names.h" |
| 23 #import "ios/web_view/internal/translate/cwv_translate_manager_impl.h" | 23 #import "ios/web_view/internal/translate/cwv_translate_manager_impl.h" |
| 24 #include "ios/web_view/internal/translate/web_view_translate_accept_languages_fa
ctory.h" | 24 #include "ios/web_view/internal/translate/web_view_translate_accept_languages_fa
ctory.h" |
| 25 #include "ios/web_view/internal/web_view_browser_state.h" | 25 #include "ios/web_view/internal/web_view_browser_state.h" |
| 26 #import "ios/web_view/public/cwv_translate_delegate.h" | 26 #import "ios/web_view/public/cwv_translate_delegate.h" |
| 27 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 28 | 28 |
| 29 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 30 #error "This file requires ARC support." |
| 31 #endif |
| 32 |
| 29 DEFINE_WEB_STATE_USER_DATA_KEY(ios_web_view::WebViewTranslateClient); | 33 DEFINE_WEB_STATE_USER_DATA_KEY(ios_web_view::WebViewTranslateClient); |
| 30 | 34 |
| 31 namespace ios_web_view { | 35 namespace ios_web_view { |
| 32 | 36 |
| 33 WebViewTranslateClient::WebViewTranslateClient(web::WebState* web_state) | 37 WebViewTranslateClient::WebViewTranslateClient(web::WebState* web_state) |
| 34 : web::WebStateObserver(web_state), | 38 : web::WebStateObserver(web_state), |
| 35 translate_manager_(base::MakeUnique<translate::TranslateManager>( | 39 translate_manager_(base::MakeUnique<translate::TranslateManager>( |
| 36 this, | 40 this, |
| 37 prefs::kAcceptLanguages)), | 41 prefs::kAcceptLanguages)), |
| 38 translate_driver_(web_state, | 42 translate_driver_(web_state, |
| 39 web_state->GetNavigationManager(), | 43 web_state->GetNavigationManager(), |
| 40 translate_manager_.get()) {} | 44 translate_manager_.get()) {} |
| 41 | 45 |
| 42 WebViewTranslateClient::~WebViewTranslateClient() = default; | 46 WebViewTranslateClient::~WebViewTranslateClient() = default; |
| 43 | 47 |
| 44 // TranslateClient implementation: | 48 // TranslateClient implementation: |
| 45 | 49 |
| 46 std::unique_ptr<infobars::InfoBar> WebViewTranslateClient::CreateInfoBar( | 50 std::unique_ptr<infobars::InfoBar> WebViewTranslateClient::CreateInfoBar( |
| 47 std::unique_ptr<translate::TranslateInfoBarDelegate> delegate) const { | 51 std::unique_ptr<translate::TranslateInfoBarDelegate> delegate) const { |
| 48 NOTREACHED(); | 52 NOTREACHED(); |
| 49 return nullptr; | 53 return nullptr; |
| 50 } | 54 } |
| 51 | 55 |
| 52 void WebViewTranslateClient::ShowTranslateUI( | 56 void WebViewTranslateClient::ShowTranslateUI( |
| 53 translate::TranslateStep step, | 57 translate::TranslateStep step, |
| 54 const std::string& source_language, | 58 const std::string& source_language, |
| 55 const std::string& target_language, | 59 const std::string& target_language, |
| 56 translate::TranslateErrors::Type error_type, | 60 translate::TranslateErrors::Type error_type, |
| 57 bool triggered_from_menu) { | 61 bool triggered_from_menu) { |
| 58 if (!delegate_) | 62 if (!delegate_.get()) |
| 59 return; | 63 return; |
| 60 | 64 |
| 61 if (error_type != translate::TranslateErrors::NONE) | 65 if (error_type != translate::TranslateErrors::NONE) |
| 62 step = translate::TRANSLATE_STEP_TRANSLATE_ERROR; | 66 step = translate::TRANSLATE_STEP_TRANSLATE_ERROR; |
| 63 | 67 |
| 64 translate_manager_->GetLanguageState().SetTranslateEnabled(true); | 68 translate_manager_->GetLanguageState().SetTranslateEnabled(true); |
| 65 | 69 |
| 66 if (step == translate::TRANSLATE_STEP_BEFORE_TRANSLATE && | 70 if (step == translate::TRANSLATE_STEP_BEFORE_TRANSLATE && |
| 67 !translate_manager_->GetLanguageState().HasLanguageChanged()) { | 71 !translate_manager_->GetLanguageState().HasLanguageChanged()) { |
| 68 return; | 72 return; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 } | 142 } |
| 139 | 143 |
| 140 void WebViewTranslateClient::WebStateDestroyed() { | 144 void WebViewTranslateClient::WebStateDestroyed() { |
| 141 // Translation process can be interrupted. | 145 // Translation process can be interrupted. |
| 142 // Destroying the TranslateManager now guarantees that it never has to deal | 146 // Destroying the TranslateManager now guarantees that it never has to deal |
| 143 // with nullptr WebState. | 147 // with nullptr WebState. |
| 144 translate_manager_.reset(); | 148 translate_manager_.reset(); |
| 145 } | 149 } |
| 146 | 150 |
| 147 } // namespace ios_web_view | 151 } // namespace ios_web_view |
| OLD | NEW |