Chromium Code Reviews| 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 #include "components/translate/content/browser/content_translate_driver.h" | 5 #include "components/translate/content/browser/content_translate_driver_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "components/translate/content/common/translate_messages.h" | |
| 13 #include "components/translate/core/browser/translate_download_manager.h" | 12 #include "components/translate/core/browser/translate_download_manager.h" |
| 14 #include "components/translate/core/browser/translate_manager.h" | 13 #include "components/translate/core/browser/translate_manager.h" |
| 15 #include "content/public/browser/browser_context.h" | 14 #include "content/public/browser/browser_context.h" |
| 16 #include "content/public/browser/navigation_controller.h" | 15 #include "content/public/browser/navigation_controller.h" |
| 17 #include "content/public/browser/navigation_details.h" | 16 #include "content/public/browser/navigation_details.h" |
| 18 #include "content/public/browser/navigation_entry.h" | 17 #include "content/public/browser/navigation_entry.h" |
| 19 #include "content/public/browser/page_navigator.h" | 18 #include "content/public/browser/page_navigator.h" |
| 20 #include "content/public/browser/render_frame_host.h" | 19 #include "content/public/browser/render_frame_host.h" |
| 21 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 22 #include "content/public/common/referrer.h" | 21 #include "content/public/common/referrer.h" |
| 23 #include "net/http/http_status_code.h" | 22 #include "net/http/http_status_code.h" |
| 24 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 25 | 24 |
| 26 namespace { | 25 namespace { |
| 27 | 26 |
| 28 // The maximum number of attempts we'll do to see if the page has finshed | 27 // The maximum number of attempts we'll do to see if the page has finshed |
| 29 // loading before giving up the translation | 28 // loading before giving up the translation |
| 30 const int kMaxTranslateLoadCheckAttempts = 20; | 29 const int kMaxTranslateLoadCheckAttempts = 20; |
| 31 | 30 |
| 32 } // namespace | 31 } // namespace |
| 33 | 32 |
| 34 namespace translate { | 33 namespace translate { |
| 35 | 34 |
| 36 ContentTranslateDriver::ContentTranslateDriver( | 35 ContentTranslateDriverImpl::ContentTranslateDriverImpl( |
| 37 content::NavigationController* nav_controller) | 36 content::NavigationController* nav_controller) |
| 38 : content::WebContentsObserver(nav_controller->GetWebContents()), | 37 : content::WebContentsObserver(nav_controller->GetWebContents()), |
| 39 navigation_controller_(nav_controller), | 38 navigation_controller_(nav_controller), |
| 40 translate_manager_(NULL), | 39 translate_manager_(NULL), |
| 41 max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts), | 40 max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts), |
| 41 next_page_seq_no_(0), | |
| 42 weak_pointer_factory_(this) { | 42 weak_pointer_factory_(this) { |
| 43 DCHECK(navigation_controller_); | 43 DCHECK(navigation_controller_); |
| 44 } | 44 } |
| 45 | 45 |
| 46 ContentTranslateDriver::~ContentTranslateDriver() {} | 46 ContentTranslateDriverImpl::~ContentTranslateDriverImpl() {} |
| 47 | 47 |
| 48 void ContentTranslateDriver::AddObserver(Observer* observer) { | 48 void ContentTranslateDriverImpl::BindRequest( |
| 49 mojom::ContentTranslateDriverRequest request) { | |
| 50 bindings_.AddBinding(this, std::move(request)); | |
| 51 } | |
| 52 | |
| 53 void ContentTranslateDriverImpl::AddObserver(Observer* observer) { | |
| 49 observer_list_.AddObserver(observer); | 54 observer_list_.AddObserver(observer); |
| 50 } | 55 } |
| 51 | 56 |
| 52 void ContentTranslateDriver::RemoveObserver(Observer* observer) { | 57 void ContentTranslateDriverImpl::RemoveObserver(Observer* observer) { |
| 53 observer_list_.RemoveObserver(observer); | 58 observer_list_.RemoveObserver(observer); |
| 54 } | 59 } |
| 55 | 60 |
| 56 void ContentTranslateDriver::InitiateTranslation(const std::string& page_lang, | 61 void ContentTranslateDriverImpl::InitiateTranslation( |
| 57 int attempt) { | 62 const std::string& page_lang, |
| 63 int attempt) { | |
| 58 if (translate_manager_->GetLanguageState().translation_pending()) | 64 if (translate_manager_->GetLanguageState().translation_pending()) |
| 59 return; | 65 return; |
| 60 | 66 |
| 61 // During a reload we need web content to be available before the | 67 // During a reload we need web content to be available before the |
| 62 // translate script is executed. Otherwise we will run the translate script on | 68 // translate script is executed. Otherwise we will run the translate script on |
| 63 // an empty DOM which will fail. Therefore we wait a bit to see if the page | 69 // an empty DOM which will fail. Therefore we wait a bit to see if the page |
| 64 // has finished. | 70 // has finished. |
| 65 if (web_contents()->IsLoading() && attempt < max_reload_check_attempts_) { | 71 if (web_contents()->IsLoading() && attempt < max_reload_check_attempts_) { |
| 66 int backoff = attempt * kMaxTranslateLoadCheckAttempts; | 72 int backoff = attempt * kMaxTranslateLoadCheckAttempts; |
| 67 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 73 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 68 FROM_HERE, | 74 FROM_HERE, |
| 69 base::Bind(&ContentTranslateDriver::InitiateTranslation, | 75 base::Bind(&ContentTranslateDriverImpl::InitiateTranslation, |
| 70 weak_pointer_factory_.GetWeakPtr(), page_lang, attempt + 1), | 76 weak_pointer_factory_.GetWeakPtr(), page_lang, attempt + 1), |
| 71 base::TimeDelta::FromMilliseconds(backoff)); | 77 base::TimeDelta::FromMilliseconds(backoff)); |
| 72 return; | 78 return; |
| 73 } | 79 } |
| 74 | 80 |
| 75 translate_manager_->InitiateTranslation( | 81 translate_manager_->InitiateTranslation( |
| 76 translate::TranslateDownloadManager::GetLanguageCode(page_lang)); | 82 translate::TranslateDownloadManager::GetLanguageCode(page_lang)); |
| 77 } | 83 } |
| 78 | 84 |
| 79 // TranslateDriver methods | 85 // TranslateDriver methods |
| 80 | 86 |
| 81 bool ContentTranslateDriver::IsLinkNavigation() { | 87 bool ContentTranslateDriverImpl::IsLinkNavigation() { |
| 82 return navigation_controller_ && | 88 return navigation_controller_ && |
| 83 navigation_controller_->GetLastCommittedEntry() && | 89 navigation_controller_->GetLastCommittedEntry() && |
| 84 ui::PageTransitionCoreTypeIs( | 90 ui::PageTransitionCoreTypeIs( |
| 85 navigation_controller_->GetLastCommittedEntry() | 91 navigation_controller_->GetLastCommittedEntry() |
| 86 ->GetTransitionType(), | 92 ->GetTransitionType(), |
| 87 ui::PAGE_TRANSITION_LINK); | 93 ui::PAGE_TRANSITION_LINK); |
| 88 } | 94 } |
| 89 | 95 |
| 90 void ContentTranslateDriver::OnTranslateEnabledChanged() { | 96 void ContentTranslateDriverImpl::OnTranslateEnabledChanged() { |
| 91 content::WebContents* web_contents = navigation_controller_->GetWebContents(); | 97 content::WebContents* web_contents = navigation_controller_->GetWebContents(); |
| 92 FOR_EACH_OBSERVER( | 98 FOR_EACH_OBSERVER(Observer, observer_list_, |
| 93 Observer, observer_list_, OnTranslateEnabledChanged(web_contents)); | 99 OnTranslateEnabledChanged(web_contents)); |
| 94 } | 100 } |
| 95 | 101 |
| 96 void ContentTranslateDriver::OnIsPageTranslatedChanged() { | 102 void ContentTranslateDriverImpl::OnIsPageTranslatedChanged() { |
| 97 content::WebContents* web_contents = | 103 content::WebContents* web_contents = navigation_controller_->GetWebContents(); |
| 98 navigation_controller_->GetWebContents(); | 104 FOR_EACH_OBSERVER(Observer, observer_list_, |
| 99 FOR_EACH_OBSERVER( | 105 OnIsPageTranslatedChanged(web_contents)); |
| 100 Observer, observer_list_, OnIsPageTranslatedChanged(web_contents)); | |
| 101 } | 106 } |
| 102 | 107 |
| 103 void ContentTranslateDriver::TranslatePage(int page_seq_no, | 108 void ContentTranslateDriverImpl::TranslatePage( |
| 104 const std::string& translate_script, | 109 int page_seq_no, |
| 105 const std::string& source_lang, | 110 const std::string& translate_script, |
| 106 const std::string& target_lang) { | 111 const std::string& source_lang, |
| 107 content::WebContents* web_contents = navigation_controller_->GetWebContents(); | 112 const std::string& target_lang) { |
| 108 web_contents->GetMainFrame()->Send(new ChromeFrameMsg_TranslatePage( | 113 auto it = mojo_pages_.find(page_seq_no); |
| 109 web_contents->GetMainFrame()->GetRoutingID(), page_seq_no, | 114 if (it == mojo_pages_.end()) |
| 110 translate_script, source_lang, target_lang)); | 115 return; // This page has navigated away. |
| 116 | |
| 117 it->second->Translate( | |
| 118 translate_script, source_lang, target_lang, | |
| 119 base::Bind(&ContentTranslateDriverImpl::OnPageTranslated, | |
| 120 base::Unretained(this))); | |
| 111 } | 121 } |
| 112 | 122 |
| 113 void ContentTranslateDriver::RevertTranslation(int page_seq_no) { | 123 void ContentTranslateDriverImpl::RevertTranslation(int page_seq_no) { |
| 114 content::WebContents* web_contents = navigation_controller_->GetWebContents(); | 124 auto it = mojo_pages_.find(page_seq_no); |
| 115 web_contents->GetMainFrame()->Send(new ChromeFrameMsg_RevertTranslation( | 125 if (it == mojo_pages_.end()) |
| 116 web_contents->GetMainFrame()->GetRoutingID(), page_seq_no)); | 126 return; // This page has navigated away. |
| 127 | |
| 128 it->second->RevertTranslation(); | |
| 117 } | 129 } |
| 118 | 130 |
| 119 bool ContentTranslateDriver::IsOffTheRecord() { | 131 bool ContentTranslateDriverImpl::IsOffTheRecord() { |
| 120 return navigation_controller_->GetBrowserContext()->IsOffTheRecord(); | 132 return navigation_controller_->GetBrowserContext()->IsOffTheRecord(); |
| 121 } | 133 } |
| 122 | 134 |
| 123 const std::string& ContentTranslateDriver::GetContentsMimeType() { | 135 const std::string& ContentTranslateDriverImpl::GetContentsMimeType() { |
| 124 return navigation_controller_->GetWebContents()->GetContentsMimeType(); | 136 return navigation_controller_->GetWebContents()->GetContentsMimeType(); |
| 125 } | 137 } |
| 126 | 138 |
| 127 const GURL& ContentTranslateDriver::GetLastCommittedURL() { | 139 const GURL& ContentTranslateDriverImpl::GetLastCommittedURL() { |
| 128 return navigation_controller_->GetWebContents()->GetLastCommittedURL(); | 140 return navigation_controller_->GetWebContents()->GetLastCommittedURL(); |
| 129 } | 141 } |
| 130 | 142 |
| 131 const GURL& ContentTranslateDriver::GetVisibleURL() { | 143 const GURL& ContentTranslateDriverImpl::GetVisibleURL() { |
| 132 return navigation_controller_->GetWebContents()->GetVisibleURL(); | 144 return navigation_controller_->GetWebContents()->GetVisibleURL(); |
| 133 } | 145 } |
| 134 | 146 |
| 135 bool ContentTranslateDriver::HasCurrentPage() { | 147 bool ContentTranslateDriverImpl::HasCurrentPage() { |
| 136 return (navigation_controller_->GetLastCommittedEntry() != NULL); | 148 return (navigation_controller_->GetLastCommittedEntry() != NULL); |
| 137 } | 149 } |
| 138 | 150 |
| 139 void ContentTranslateDriver::OpenUrlInNewTab(const GURL& url) { | 151 void ContentTranslateDriverImpl::OpenUrlInNewTab(const GURL& url) { |
| 140 content::OpenURLParams params(url, | 152 content::OpenURLParams params(url, content::Referrer(), NEW_FOREGROUND_TAB, |
| 141 content::Referrer(), | 153 ui::PAGE_TRANSITION_LINK, false); |
| 142 NEW_FOREGROUND_TAB, | |
| 143 ui::PAGE_TRANSITION_LINK, | |
| 144 false); | |
| 145 navigation_controller_->GetWebContents()->OpenURL(params); | 154 navigation_controller_->GetWebContents()->OpenURL(params); |
| 146 } | 155 } |
| 147 | 156 |
| 148 // content::WebContentsObserver methods | 157 // content::WebContentsObserver methods |
| 149 | 158 |
| 150 void ContentTranslateDriver::NavigationEntryCommitted( | 159 void ContentTranslateDriverImpl::NavigationEntryCommitted( |
| 151 const content::LoadCommittedDetails& load_details) { | 160 const content::LoadCommittedDetails& load_details) { |
| 152 // Check whether this is a reload: When doing a page reload, the | 161 // Check whether this is a reload: When doing a page reload, the |
| 153 // TranslateLanguageDetermined IPC is not sent so the translation needs to be | 162 // TranslateLanguageDetermined IPC is not sent so the translation needs to be |
| 154 // explicitly initiated. | 163 // explicitly initiated. |
| 155 | 164 |
| 156 content::NavigationEntry* entry = | 165 content::NavigationEntry* entry = |
| 157 web_contents()->GetController().GetLastCommittedEntry(); | 166 web_contents()->GetController().GetLastCommittedEntry(); |
| 158 if (!entry) { | 167 if (!entry) { |
| 159 NOTREACHED(); | 168 NOTREACHED(); |
| 160 return; | 169 return; |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 184 | 193 |
| 185 if (!translate_manager_->GetLanguageState().page_needs_translation()) | 194 if (!translate_manager_->GetLanguageState().page_needs_translation()) |
| 186 return; | 195 return; |
| 187 | 196 |
| 188 // Note that we delay it as the ordering of the processing of this callback | 197 // Note that we delay it as the ordering of the processing of this callback |
| 189 // by WebContentsObservers is undefined and might result in the current | 198 // by WebContentsObservers is undefined and might result in the current |
| 190 // infobars being removed. Since the translation initiation process might add | 199 // infobars being removed. Since the translation initiation process might add |
| 191 // an infobar, it must be done after that. | 200 // an infobar, it must be done after that. |
| 192 base::ThreadTaskRunnerHandle::Get()->PostTask( | 201 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 193 FROM_HERE, | 202 FROM_HERE, |
| 194 base::Bind(&ContentTranslateDriver::InitiateTranslation, | 203 base::Bind(&ContentTranslateDriverImpl::InitiateTranslation, |
| 195 weak_pointer_factory_.GetWeakPtr(), | 204 weak_pointer_factory_.GetWeakPtr(), |
| 196 translate_manager_->GetLanguageState().original_language(), | 205 translate_manager_->GetLanguageState().original_language(), |
| 197 0)); | 206 0)); |
| 198 } | 207 } |
| 199 | 208 |
| 200 void ContentTranslateDriver::DidNavigateAnyFrame( | 209 void ContentTranslateDriverImpl::DidNavigateAnyFrame( |
| 201 content::RenderFrameHost* render_frame_host, | 210 content::RenderFrameHost* render_frame_host, |
| 202 const content::LoadCommittedDetails& details, | 211 const content::LoadCommittedDetails& details, |
| 203 const content::FrameNavigateParams& params) { | 212 const content::FrameNavigateParams& params) { |
| 204 // Let the LanguageState clear its state. | 213 // Let the LanguageState clear its state. |
| 205 const bool reload = | 214 const bool reload = |
| 206 ui::PageTransitionCoreTypeIs(details.entry->GetTransitionType(), | 215 ui::PageTransitionCoreTypeIs(details.entry->GetTransitionType(), |
| 207 ui::PAGE_TRANSITION_RELOAD) || | 216 ui::PAGE_TRANSITION_RELOAD) || |
| 208 details.type == content::NAVIGATION_TYPE_SAME_PAGE; | 217 details.type == content::NAVIGATION_TYPE_SAME_PAGE; |
| 209 translate_manager_->GetLanguageState().DidNavigate( | 218 translate_manager_->GetLanguageState().DidNavigate( |
| 210 details.is_in_page, details.is_main_frame, reload); | 219 details.is_in_page, details.is_main_frame, reload); |
| 211 } | 220 } |
| 212 | 221 |
| 213 bool ContentTranslateDriver::OnMessageReceived( | 222 void ContentTranslateDriverImpl::OnPageTranslated( |
|
groby-ooo-7-16
2016/07/11 22:34:43
Is there a reason this moved up here? (Keeping it
leonhsl(Using Gerrit)
2016/07/12 02:48:17
Ah, I don't remember why it's moved here. I'll mov
leonhsl(Using Gerrit)
2016/07/13 09:56:18
Done.
| |
| 214 const IPC::Message& message, | 223 bool cancelled, |
| 215 content::RenderFrameHost* render_frame_host) { | 224 mojo::String original_lang, |
| 216 bool handled = true; | 225 mojo::String translated_lang, |
| 217 IPC_BEGIN_MESSAGE_MAP(ContentTranslateDriver, message) | 226 TranslateErrors::Type error_type) { |
| 218 IPC_MESSAGE_HANDLER(ChromeFrameHostMsg_TranslateAssignedSequenceNumber, | 227 if (cancelled) |
| 219 OnTranslateAssignedSequenceNumber) | 228 return; |
| 220 IPC_MESSAGE_HANDLER(ChromeFrameHostMsg_TranslateLanguageDetermined, | 229 |
| 221 OnLanguageDetermined) | 230 translate_manager_->PageTranslated(original_lang, translated_lang, |
| 222 IPC_MESSAGE_HANDLER(ChromeFrameHostMsg_PageTranslated, OnPageTranslated) | 231 error_type); |
| 223 IPC_MESSAGE_UNHANDLED(handled = false) | 232 FOR_EACH_OBSERVER( |
| 224 IPC_END_MESSAGE_MAP() | 233 Observer, observer_list_, |
| 225 return handled; | 234 OnPageTranslated(original_lang, translated_lang, error_type)); |
| 226 } | 235 } |
| 227 | 236 |
| 228 void ContentTranslateDriver::OnTranslateAssignedSequenceNumber( | 237 void ContentTranslateDriverImpl::OnPageAway(int page_seq_no) { |
|
groby-ooo-7-16
2016/07/11 22:34:43
Do we actually still need the concept of page sequ
leonhsl(Using Gerrit)
2016/07/12 02:48:17
I think TranslateManager also serves IOSTranslateD
| |
| 229 int page_seq_no) { | 238 mojo_pages_.erase(page_seq_no); |
| 230 translate_manager_->set_current_seq_no(page_seq_no); | |
| 231 } | 239 } |
| 232 | 240 |
| 233 void ContentTranslateDriver::OnLanguageDetermined( | 241 // mojom::ContentTranslateDriver implementation. |
| 242 void ContentTranslateDriverImpl::NewPage( | |
|
groby-ooo-7-16
2016/07/11 22:34:43
I take it all functions modifying/reading |mojo_pa
leonhsl(Using Gerrit)
2016/07/12 02:48:17
Yeah, mojo is thread-hostile, all the mojo calls a
| |
| 243 mojom::PagePtr page, | |
| 234 const LanguageDetectionDetails& details, | 244 const LanguageDetectionDetails& details, |
| 235 bool page_needs_translation) { | 245 bool page_needs_translation) { |
| 246 mojo_pages_[++next_page_seq_no_] = std::move(page); | |
| 247 mojo_pages_[next_page_seq_no_].set_connection_error_handler( | |
|
groby-ooo-7-16
2016/07/11 22:34:43
I'm new to this mojo thing, so possibly odd questi
leonhsl(Using Gerrit)
2016/07/12 02:48:17
Yeah it's right. TranslateHelper will close the co
| |
| 248 base::Bind(&ContentTranslateDriverImpl::OnPageAway, | |
| 249 base::Unretained(this), next_page_seq_no_)); | |
|
groby-ooo-7-16
2016/07/11 22:34:43
Are we guaranteed that |this| will be alive until
leonhsl(Using Gerrit)
2016/07/12 02:48:17
Yeah this is guaranteed. Because destroying |this|
| |
| 250 translate_manager_->set_current_seq_no(next_page_seq_no_); | |
| 251 | |
| 236 translate_manager_->GetLanguageState().LanguageDetermined( | 252 translate_manager_->GetLanguageState().LanguageDetermined( |
| 237 details.adopted_language, page_needs_translation); | 253 details.adopted_language, page_needs_translation); |
| 238 | 254 |
| 239 if (web_contents()) | 255 if (web_contents()) |
| 240 translate_manager_->InitiateTranslation(details.adopted_language); | 256 translate_manager_->InitiateTranslation(details.adopted_language); |
| 241 | 257 |
| 242 FOR_EACH_OBSERVER(Observer, observer_list_, OnLanguageDetermined(details)); | 258 FOR_EACH_OBSERVER(Observer, observer_list_, OnLanguageDetermined(details)); |
| 243 } | 259 } |
| 244 | 260 |
| 245 void ContentTranslateDriver::OnPageTranslated( | |
| 246 const std::string& original_lang, | |
| 247 const std::string& translated_lang, | |
| 248 TranslateErrors::Type error_type) { | |
| 249 translate_manager_->PageTranslated( | |
| 250 original_lang, translated_lang, error_type); | |
| 251 FOR_EACH_OBSERVER( | |
| 252 Observer, | |
| 253 observer_list_, | |
| 254 OnPageTranslated(original_lang, translated_lang, error_type)); | |
| 255 } | |
| 256 | |
| 257 } // namespace translate | 261 } // namespace translate |
| OLD | NEW |