Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(187)

Side by Side Diff: chrome/browser/translate/translate_tab_helper.cc

Issue 227363005: Move reload processing from TranslateManager to TranslateTabHelper. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Response to review Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/translate/translate_tab_helper.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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" 8 #include "base/prefs/pref_service.h"
9 #include "base/strings/string_split.h" 9 #include "base/strings/string_split.h"
10 #include "chrome/browser/chrome_notification_types.h" 10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/translate/translate_accept_languages_factory.h" 12 #include "chrome/browser/translate/translate_accept_languages_factory.h"
13 #include "chrome/browser/translate/translate_infobar_delegate.h" 13 #include "chrome/browser/translate/translate_infobar_delegate.h"
14 #include "chrome/browser/translate/translate_manager.h" 14 #include "chrome/browser/translate/translate_manager.h"
15 #include "chrome/browser/translate/translate_service.h" 15 #include "chrome/browser/translate/translate_service.h"
16 #include "chrome/browser/ui/browser.h" 16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_finder.h" 17 #include "chrome/browser/ui/browser_finder.h"
18 #include "chrome/browser/ui/browser_window.h" 18 #include "chrome/browser/ui/browser_window.h"
19 #include "chrome/browser/ui/tabs/tab_strip_model.h" 19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "chrome/browser/ui/translate/translate_bubble_factory.h" 20 #include "chrome/browser/ui/translate/translate_bubble_factory.h"
21 #include "chrome/common/pref_names.h" 21 #include "chrome/common/pref_names.h"
22 #include "components/translate/content/common/translate_messages.h" 22 #include "components/translate/content/common/translate_messages.h"
23 #include "components/translate/core/browser/page_translated_details.h" 23 #include "components/translate/core/browser/page_translated_details.h"
24 #include "components/translate/core/browser/translate_accept_languages.h" 24 #include "components/translate/core/browser/translate_accept_languages.h"
25 #include "components/translate/core/browser/translate_download_manager.h"
25 #include "components/translate/core/browser/translate_prefs.h" 26 #include "components/translate/core/browser/translate_prefs.h"
26 #include "components/translate/core/common/language_detection_details.h" 27 #include "components/translate/core/common/language_detection_details.h"
28 #include "content/public/browser/navigation_details.h"
29 #include "content/public/browser/navigation_entry.h"
27 #include "content/public/browser/notification_service.h" 30 #include "content/public/browser/notification_service.h"
28 #include "content/public/browser/web_contents.h" 31 #include "content/public/browser/web_contents.h"
32 #include "net/http/http_status_code.h"
29 33
30 #if defined(CLD2_DYNAMIC_MODE) 34 #if defined(CLD2_DYNAMIC_MODE)
31 #include "base/files/file.h" 35 #include "base/files/file.h"
32 #include "base/path_service.h" 36 #include "base/path_service.h"
33 #include "chrome/common/chrome_paths.h" 37 #include "chrome/common/chrome_paths.h"
34 #include "content/public/browser/browser_thread.h" 38 #include "content/public/browser/browser_thread.h"
35 #include "content/public/browser/render_process_host.h" 39 #include "content/public/browser/render_process_host.h"
36 #include "content/public/browser/render_view_host.h" 40 #include "content/public/browser/render_view_host.h"
37 #endif 41 #endif
38 42
43 namespace {
44
45 // The maximum number of attempts we'll do to see if the page has finshed
46 // loading before giving up the translation
47 const int kMaxTranslateLoadCheckAttempts = 20;
48
49 } // namespace
50
39 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TranslateTabHelper); 51 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TranslateTabHelper);
40 52
41 #if defined(CLD2_DYNAMIC_MODE) 53 #if defined(CLD2_DYNAMIC_MODE)
42 // Statics defined in the .h file: 54 // Statics defined in the .h file:
43 base::File* TranslateTabHelper::s_cached_file_ = NULL; 55 base::File* TranslateTabHelper::s_cached_file_ = NULL;
44 uint64 TranslateTabHelper::s_cached_data_offset_ = 0; 56 uint64 TranslateTabHelper::s_cached_data_offset_ = 0;
45 uint64 TranslateTabHelper::s_cached_data_length_ = 0; 57 uint64 TranslateTabHelper::s_cached_data_length_ = 0;
46 base::LazyInstance<base::Lock> TranslateTabHelper::s_file_lock_ = 58 base::LazyInstance<base::Lock> TranslateTabHelper::s_file_lock_ =
47 LAZY_INSTANCE_INITIALIZER; 59 LAZY_INSTANCE_INITIALIZER;
48 #endif 60 #endif
49 61
50 TranslateTabHelper::TranslateTabHelper(content::WebContents* web_contents) 62 TranslateTabHelper::TranslateTabHelper(content::WebContents* web_contents)
51 : content::WebContentsObserver(web_contents), 63 : content::WebContentsObserver(web_contents),
52 #if defined(CLD2_DYNAMIC_MODE) 64 max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts),
53 weak_pointer_factory_(this),
54 #endif
55 translate_driver_(&web_contents->GetController()), 65 translate_driver_(&web_contents->GetController()),
56 translate_manager_(new TranslateManager(this, prefs::kAcceptLanguages)) {} 66 translate_manager_(new TranslateManager(this, prefs::kAcceptLanguages)),
67 weak_pointer_factory_(this) {}
57 68
58 TranslateTabHelper::~TranslateTabHelper() { 69 TranslateTabHelper::~TranslateTabHelper() {
59 } 70 }
60 71
61 LanguageState& TranslateTabHelper::GetLanguageState() { 72 LanguageState& TranslateTabHelper::GetLanguageState() {
62 return translate_driver_.GetLanguageState(); 73 return translate_driver_.GetLanguageState();
63 } 74 }
64 75
65 // static 76 // static
66 scoped_ptr<TranslatePrefs> TranslateTabHelper::CreateTranslatePrefs( 77 scoped_ptr<TranslatePrefs> TranslateTabHelper::CreateTranslatePrefs(
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated, OnPageTranslated) 208 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated, OnPageTranslated)
198 #if defined(CLD2_DYNAMIC_MODE) 209 #if defined(CLD2_DYNAMIC_MODE)
199 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_NeedCLDData, OnCLDDataRequested) 210 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_NeedCLDData, OnCLDDataRequested)
200 #endif 211 #endif
201 IPC_MESSAGE_UNHANDLED(handled = false) 212 IPC_MESSAGE_UNHANDLED(handled = false)
202 IPC_END_MESSAGE_MAP() 213 IPC_END_MESSAGE_MAP()
203 214
204 return handled; 215 return handled;
205 } 216 }
206 217
218 void TranslateTabHelper::NavigationEntryCommitted(
219 const content::LoadCommittedDetails& load_details) {
220 // Check whether this is a reload: When doing a page reload, the
221 // TranslateLanguageDetermined IPC is not sent so the translation needs to be
222 // explicitly initiated.
223
224 content::NavigationEntry* entry =
225 web_contents()->GetController().GetActiveEntry();
226 if (!entry) {
227 NOTREACHED();
228 return;
229 }
230
231 // If the navigation happened while offline don't show the translate
232 // bar since there will be nothing to translate.
233 if (load_details.http_status_code == 0 ||
234 load_details.http_status_code == net::HTTP_INTERNAL_SERVER_ERROR) {
235 return;
236 }
237
238 if (!load_details.is_main_frame &&
239 translate_driver_.GetLanguageState().translation_declined()) {
240 // Some sites (such as Google map) may trigger sub-frame navigations
241 // when the user interacts with the page. We don't want to show a new
242 // infobar if the user already dismissed one in that case.
243 return;
244 }
245
246 // If not a reload, return.
247 if (entry->GetTransitionType() != content::PAGE_TRANSITION_RELOAD &&
248 load_details.type != content::NAVIGATION_TYPE_SAME_PAGE) {
249 return;
250 }
251
252 if (!translate_driver_.GetLanguageState().page_needs_translation())
253 return;
254
255 // Note that we delay it as the ordering of the processing of this callback
256 // by WebContentsObservers is undefined and might result in the current
257 // infobars being removed. Since the translation initiation process might add
258 // an infobar, it must be done after that.
259 base::MessageLoop::current()->PostTask(
260 FROM_HERE,
261 base::Bind(&TranslateTabHelper::InitiateTranslation,
262 weak_pointer_factory_.GetWeakPtr(),
263 translate_driver_.GetLanguageState().original_language(),
264 0));
265 }
266
207 void TranslateTabHelper::DidNavigateAnyFrame( 267 void TranslateTabHelper::DidNavigateAnyFrame(
208 const content::LoadCommittedDetails& details, 268 const content::LoadCommittedDetails& details,
209 const content::FrameNavigateParams& params) { 269 const content::FrameNavigateParams& params) {
210 // Let the LanguageState clear its state. 270 // Let the LanguageState clear its state.
211 translate_driver_.DidNavigate(details); 271 translate_driver_.DidNavigate(details);
212 } 272 }
213 273
214 void TranslateTabHelper::WebContentsDestroyed( 274 void TranslateTabHelper::WebContentsDestroyed(
215 content::WebContents* web_contents) { 275 content::WebContents* web_contents) {
216 // Translation process can be interrupted. 276 // Translation process can be interrupted.
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 } else { 399 } else {
340 // Else, this request has taken care of it all. Cache all info. 400 // Else, this request has taken care of it all. Cache all info.
341 s_cached_file_ = file.release(); 401 s_cached_file_ = file.release();
342 s_cached_data_offset_ = data_offset; 402 s_cached_data_offset_ = data_offset;
343 s_cached_data_length_ = data_length; 403 s_cached_data_length_ = data_length;
344 } 404 }
345 } 405 }
346 } 406 }
347 #endif // defined(CLD2_DYNAMIC_MODE) 407 #endif // defined(CLD2_DYNAMIC_MODE)
348 408
409 void TranslateTabHelper::InitiateTranslation(const std::string& page_lang,
410 int attempt) {
411 if (translate_driver_.GetLanguageState().translation_pending())
412 return;
413
414 // During a reload we need web content to be available before the
415 // translate script is executed. Otherwise we will run the translate script on
416 // an empty DOM which will fail. Therefore we wait a bit to see if the page
417 // has finished.
418 if (web_contents()->IsLoading() && attempt < max_reload_check_attempts_) {
419 int backoff = attempt * kMaxTranslateLoadCheckAttempts;
420 base::MessageLoop::current()->PostDelayedTask(
421 FROM_HERE,
422 base::Bind(&TranslateTabHelper::InitiateTranslation,
423 weak_pointer_factory_.GetWeakPtr(),
424 page_lang,
425 ++attempt),
426 base::TimeDelta::FromMilliseconds(backoff));
427 return;
428 }
429
430 translate_manager_->InitiateTranslation(
431 TranslateDownloadManager::GetLanguageCode(page_lang));
432 }
433
349 void TranslateTabHelper::OnLanguageDetermined( 434 void TranslateTabHelper::OnLanguageDetermined(
350 const LanguageDetectionDetails& details, 435 const LanguageDetectionDetails& details,
351 bool page_needs_translation) { 436 bool page_needs_translation) {
352 translate_driver_.GetLanguageState().LanguageDetermined( 437 translate_driver_.GetLanguageState().LanguageDetermined(
353 details.adopted_language, page_needs_translation); 438 details.adopted_language, page_needs_translation);
354 439
355 if (web_contents()) 440 if (web_contents())
356 translate_manager_->InitiateTranslation(details.adopted_language); 441 translate_manager_->InitiateTranslation(details.adopted_language);
357 442
358 content::NotificationService::current()->Notify( 443 content::NotificationService::current()->Notify(
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 if (GetLanguageState().InTranslateNavigation()) 496 if (GetLanguageState().InTranslateNavigation())
412 return; 497 return;
413 } 498 }
414 499
415 TranslateBubbleFactory::Show( 500 TranslateBubbleFactory::Show(
416 browser->window(), web_contents(), step, error_type); 501 browser->window(), web_contents(), step, error_type);
417 #else 502 #else
418 NOTREACHED(); 503 NOTREACHED();
419 #endif 504 #endif
420 } 505 }
OLDNEW
« no previous file with comments | « chrome/browser/translate/translate_tab_helper.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698