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 "chrome/browser/translate/chrome_translate_client.h" | 5 #include "chrome/browser/translate/chrome_translate_client.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 #include "components/translate/content/common/cld_data_source.h" | 27 #include "components/translate/content/common/cld_data_source.h" |
28 #include "components/translate/content/common/translate_messages.h" | 28 #include "components/translate/content/common/translate_messages.h" |
29 #include "components/translate/core/browser/language_state.h" | 29 #include "components/translate/core/browser/language_state.h" |
30 #include "components/translate/core/browser/page_translated_details.h" | 30 #include "components/translate/core/browser/page_translated_details.h" |
31 #include "components/translate/core/browser/translate_accept_languages.h" | 31 #include "components/translate/core/browser/translate_accept_languages.h" |
32 #include "components/translate/core/browser/translate_download_manager.h" | 32 #include "components/translate/core/browser/translate_download_manager.h" |
33 #include "components/translate/core/browser/translate_infobar_delegate.h" | 33 #include "components/translate/core/browser/translate_infobar_delegate.h" |
34 #include "components/translate/core/browser/translate_manager.h" | 34 #include "components/translate/core/browser/translate_manager.h" |
35 #include "components/translate/core/browser/translate_prefs.h" | 35 #include "components/translate/core/browser/translate_prefs.h" |
36 #include "components/translate/core/common/language_detection_details.h" | 36 #include "components/translate/core/common/language_detection_details.h" |
37 #include "content/public/browser/navigation_details.h" | |
38 #include "content/public/browser/navigation_entry.h" | |
39 #include "content/public/browser/notification_service.h" | 37 #include "content/public/browser/notification_service.h" |
40 #include "content/public/browser/render_view_host.h" | 38 #include "content/public/browser/render_view_host.h" |
41 #include "content/public/browser/web_contents.h" | 39 #include "content/public/browser/web_contents.h" |
42 #include "grit/theme_resources.h" | 40 #include "grit/theme_resources.h" |
43 #include "net/http/http_status_code.h" | |
44 #include "url/gurl.h" | 41 #include "url/gurl.h" |
45 | 42 |
46 namespace { | 43 namespace { |
47 | 44 |
48 // The maximum number of attempts we'll do to see if the page has finshed | |
49 // loading before giving up the translation | |
50 const int kMaxTranslateLoadCheckAttempts = 20; | |
51 | |
52 // TODO(andrewhayden): Make the data file path into a gyp/gn define | 45 // TODO(andrewhayden): Make the data file path into a gyp/gn define |
53 // If you change this, also update standalone_cld_data_harness.cc | 46 // If you change this, also update standalone_cld_data_harness.cc |
54 // accordingly! | 47 // accordingly! |
55 const base::FilePath::CharType kCldDataFileName[] = | 48 const base::FilePath::CharType kCldDataFileName[] = |
56 FILE_PATH_LITERAL("cld2_data.bin"); | 49 FILE_PATH_LITERAL("cld2_data.bin"); |
57 | 50 |
58 bool g_cld_file_path_initialized_ = false; | 51 bool g_cld_file_path_initialized_ = false; |
59 | 52 |
60 } // namespace | 53 } // namespace |
61 | 54 |
62 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeTranslateClient); | 55 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeTranslateClient); |
63 | 56 |
64 ChromeTranslateClient::ChromeTranslateClient(content::WebContents* web_contents) | 57 ChromeTranslateClient::ChromeTranslateClient(content::WebContents* web_contents) |
65 : content::WebContentsObserver(web_contents), | 58 : content::WebContentsObserver(web_contents), |
66 max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts), | |
67 translate_driver_(&web_contents->GetController()), | 59 translate_driver_(&web_contents->GetController()), |
68 translate_manager_( | 60 translate_manager_( |
69 new translate::TranslateManager(this, prefs::kAcceptLanguages)), | 61 new translate::TranslateManager(this, prefs::kAcceptLanguages)), |
70 cld_data_provider_( | 62 cld_data_provider_( |
71 translate::CreateBrowserCldDataProviderFor(web_contents)), | 63 translate::CreateBrowserCldDataProviderFor(web_contents)) { |
72 weak_pointer_factory_(this) { | 64 translate_driver_.set_translate_manager(translate_manager_.get()); |
73 // Customization: for the standalone data source, we configure the path to | 65 // Customization: for the standalone data source, we configure the path to |
74 // CLD data immediately on startup. | 66 // CLD data immediately on startup. |
75 if (translate::CldDataSource::ShouldUseStandaloneDataFile() && | 67 if (translate::CldDataSource::ShouldUseStandaloneDataFile() && |
76 !g_cld_file_path_initialized_) { | 68 !g_cld_file_path_initialized_) { |
77 VLOG(1) << "Initializing CLD file path for the first time."; | 69 VLOG(1) << "Initializing CLD file path for the first time."; |
78 base::FilePath path; | 70 base::FilePath path; |
79 if (!PathService::Get(chrome::DIR_USER_DATA, &path)) { | 71 if (!PathService::Get(chrome::DIR_USER_DATA, &path)) { |
80 // Chrome isn't properly installed | 72 // Chrome isn't properly installed |
81 LOG(WARNING) << "Unable to locate user data directory"; | 73 LOG(WARNING) << "Unable to locate user data directory"; |
82 } else { | 74 } else { |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated, OnPageTranslated) | 272 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated, OnPageTranslated) |
281 IPC_MESSAGE_UNHANDLED(handled = false) | 273 IPC_MESSAGE_UNHANDLED(handled = false) |
282 IPC_END_MESSAGE_MAP() | 274 IPC_END_MESSAGE_MAP() |
283 | 275 |
284 if (!handled) { | 276 if (!handled) { |
285 handled = cld_data_provider_->OnMessageReceived(message); | 277 handled = cld_data_provider_->OnMessageReceived(message); |
286 } | 278 } |
287 return handled; | 279 return handled; |
288 } | 280 } |
289 | 281 |
290 void ChromeTranslateClient::NavigationEntryCommitted( | |
291 const content::LoadCommittedDetails& load_details) { | |
292 // Check whether this is a reload: When doing a page reload, the | |
293 // TranslateLanguageDetermined IPC is not sent so the translation needs to be | |
294 // explicitly initiated. | |
295 | |
296 content::NavigationEntry* entry = | |
297 web_contents()->GetController().GetActiveEntry(); | |
298 if (!entry) { | |
299 NOTREACHED(); | |
300 return; | |
301 } | |
302 | |
303 // If the navigation happened while offline don't show the translate | |
304 // bar since there will be nothing to translate. | |
305 if (load_details.http_status_code == 0 || | |
306 load_details.http_status_code == net::HTTP_INTERNAL_SERVER_ERROR) { | |
307 return; | |
308 } | |
309 | |
310 if (!load_details.is_main_frame && | |
311 GetLanguageState().translation_declined()) { | |
312 // Some sites (such as Google map) may trigger sub-frame navigations | |
313 // when the user interacts with the page. We don't want to show a new | |
314 // infobar if the user already dismissed one in that case. | |
315 return; | |
316 } | |
317 | |
318 // If not a reload, return. | |
319 if (entry->GetTransitionType() != content::PAGE_TRANSITION_RELOAD && | |
320 load_details.type != content::NAVIGATION_TYPE_SAME_PAGE) { | |
321 return; | |
322 } | |
323 | |
324 if (!GetLanguageState().page_needs_translation()) | |
325 return; | |
326 | |
327 // Note that we delay it as the ordering of the processing of this callback | |
328 // by WebContentsObservers is undefined and might result in the current | |
329 // infobars being removed. Since the translation initiation process might add | |
330 // an infobar, it must be done after that. | |
331 base::MessageLoop::current()->PostTask( | |
332 FROM_HERE, | |
333 base::Bind(&ChromeTranslateClient::InitiateTranslation, | |
334 weak_pointer_factory_.GetWeakPtr(), | |
335 GetLanguageState().original_language(), | |
336 0)); | |
337 } | |
338 | |
339 void ChromeTranslateClient::DidNavigateAnyFrame( | |
340 const content::LoadCommittedDetails& details, | |
341 const content::FrameNavigateParams& params) { | |
342 // Let the LanguageState clear its state. | |
343 const bool reload = | |
344 details.entry->GetTransitionType() == content::PAGE_TRANSITION_RELOAD || | |
345 details.type == content::NAVIGATION_TYPE_SAME_PAGE; | |
346 GetLanguageState().DidNavigate( | |
347 details.is_in_page, details.is_main_frame, reload); | |
348 } | |
349 | |
350 void ChromeTranslateClient::WebContentsDestroyed() { | 282 void ChromeTranslateClient::WebContentsDestroyed() { |
351 // Translation process can be interrupted. | 283 // Translation process can be interrupted. |
352 // Destroying the TranslateManager now guarantees that it never has to deal | 284 // Destroying the TranslateManager now guarantees that it never has to deal |
353 // with NULL WebContents. | 285 // with NULL WebContents. |
354 translate_manager_.reset(); | 286 translate_manager_.reset(); |
355 } | 287 } |
356 | 288 |
357 void ChromeTranslateClient::InitiateTranslation(const std::string& page_lang, | |
358 int attempt) { | |
359 if (GetLanguageState().translation_pending()) | |
360 return; | |
361 | |
362 // During a reload we need web content to be available before the | |
363 // translate script is executed. Otherwise we will run the translate script on | |
364 // an empty DOM which will fail. Therefore we wait a bit to see if the page | |
365 // has finished. | |
366 if (web_contents()->IsLoading() && attempt < max_reload_check_attempts_) { | |
367 int backoff = attempt * kMaxTranslateLoadCheckAttempts; | |
368 base::MessageLoop::current()->PostDelayedTask( | |
369 FROM_HERE, | |
370 base::Bind(&ChromeTranslateClient::InitiateTranslation, | |
371 weak_pointer_factory_.GetWeakPtr(), | |
372 page_lang, | |
373 attempt + 1), | |
374 base::TimeDelta::FromMilliseconds(backoff)); | |
375 return; | |
376 } | |
377 | |
378 translate_manager_->InitiateTranslation( | |
379 translate::TranslateDownloadManager::GetLanguageCode(page_lang)); | |
380 } | |
381 | |
382 void ChromeTranslateClient::OnTranslateAssignedSequenceNumber(int page_seq_no) { | 289 void ChromeTranslateClient::OnTranslateAssignedSequenceNumber(int page_seq_no) { |
383 translate_manager_->set_current_seq_no(page_seq_no); | 290 translate_manager_->set_current_seq_no(page_seq_no); |
384 } | 291 } |
385 | 292 |
386 void ChromeTranslateClient::OnLanguageDetermined( | 293 void ChromeTranslateClient::OnLanguageDetermined( |
387 const translate::LanguageDetectionDetails& details, | 294 const translate::LanguageDetectionDetails& details, |
388 bool page_needs_translation) { | 295 bool page_needs_translation) { |
389 GetLanguageState().LanguageDetermined(details.adopted_language, | 296 GetLanguageState().LanguageDetermined(details.adopted_language, |
390 page_needs_translation); | 297 page_needs_translation); |
391 | 298 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 if (GetLanguageState().InTranslateNavigation()) | 356 if (GetLanguageState().InTranslateNavigation()) |
450 return; | 357 return; |
451 } | 358 } |
452 | 359 |
453 TranslateBubbleFactory::Show( | 360 TranslateBubbleFactory::Show( |
454 browser->window(), web_contents(), step, error_type); | 361 browser->window(), web_contents(), step, error_type); |
455 #else | 362 #else |
456 NOTREACHED(); | 363 NOTREACHED(); |
457 #endif | 364 #endif |
458 } | 365 } |
OLD | NEW |