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

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: 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 "chrome/browser/chrome_notification_types.h" 8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/profiles/profile.h" 9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/translate/translate_accept_languages_factory.h" 10 #include "chrome/browser/translate/translate_accept_languages_factory.h"
11 #include "chrome/browser/translate/translate_infobar_delegate.h" 11 #include "chrome/browser/translate/translate_infobar_delegate.h"
12 #include "chrome/browser/translate/translate_manager.h" 12 #include "chrome/browser/translate/translate_manager.h"
13 #include "chrome/browser/translate/translate_service.h" 13 #include "chrome/browser/translate/translate_service.h"
14 #include "chrome/browser/ui/browser.h" 14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_finder.h" 15 #include "chrome/browser/ui/browser_finder.h"
16 #include "chrome/browser/ui/browser_window.h" 16 #include "chrome/browser/ui/browser_window.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h" 17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/browser/ui/translate/translate_bubble_factory.h" 18 #include "chrome/browser/ui/translate/translate_bubble_factory.h"
19 #include "chrome/common/pref_names.h" 19 #include "chrome/common/pref_names.h"
20 #include "components/translate/content/common/translate_messages.h" 20 #include "components/translate/content/common/translate_messages.h"
21 #include "components/translate/core/browser/page_translated_details.h" 21 #include "components/translate/core/browser/page_translated_details.h"
22 #include "components/translate/core/browser/translate_accept_languages.h" 22 #include "components/translate/core/browser/translate_accept_languages.h"
23 #include "components/translate/core/browser/translate_download_manager.h"
23 #include "components/translate/core/browser/translate_prefs.h" 24 #include "components/translate/core/browser/translate_prefs.h"
24 #include "components/translate/core/common/language_detection_details.h" 25 #include "components/translate/core/common/language_detection_details.h"
26 #include "content/public/browser/navigation_details.h"
27 #include "content/public/browser/navigation_entry.h"
25 #include "content/public/browser/notification_service.h" 28 #include "content/public/browser/notification_service.h"
26 #include "content/public/browser/web_contents.h" 29 #include "content/public/browser/web_contents.h"
30 #include "net/http/http_status_code.h"
27 31
28 #if defined(CLD2_DYNAMIC_MODE) 32 #if defined(CLD2_DYNAMIC_MODE)
29 #include "base/files/file.h" 33 #include "base/files/file.h"
30 #include "base/path_service.h" 34 #include "base/path_service.h"
31 #include "chrome/common/chrome_paths.h" 35 #include "chrome/common/chrome_paths.h"
32 #include "content/public/browser/browser_thread.h" 36 #include "content/public/browser/browser_thread.h"
33 #include "content/public/browser/render_process_host.h" 37 #include "content/public/browser/render_process_host.h"
34 #include "content/public/browser/render_view_host.h" 38 #include "content/public/browser/render_view_host.h"
35 #endif 39 #endif
36 40
41 namespace {
42
43 // The maximum number of attempts we'll do to see if the page has finshed
44 // loading before giving up the translation
45 const int kMaxTranslateLoadCheckAttempts = 20;
46
47 } // namespace
48
37 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TranslateTabHelper); 49 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TranslateTabHelper);
38 50
39 #if defined(CLD2_DYNAMIC_MODE) 51 #if defined(CLD2_DYNAMIC_MODE)
40 // Statics defined in the .h file: 52 // Statics defined in the .h file:
41 base::File* TranslateTabHelper::s_cached_file_ = NULL; 53 base::File* TranslateTabHelper::s_cached_file_ = NULL;
42 uint64 TranslateTabHelper::s_cached_data_offset_ = 0; 54 uint64 TranslateTabHelper::s_cached_data_offset_ = 0;
43 uint64 TranslateTabHelper::s_cached_data_length_ = 0; 55 uint64 TranslateTabHelper::s_cached_data_length_ = 0;
44 base::LazyInstance<base::Lock> TranslateTabHelper::s_file_lock_ = 56 base::LazyInstance<base::Lock> TranslateTabHelper::s_file_lock_ =
45 LAZY_INSTANCE_INITIALIZER; 57 LAZY_INSTANCE_INITIALIZER;
46 #endif 58 #endif
47 59
48 TranslateTabHelper::TranslateTabHelper(content::WebContents* web_contents) 60 TranslateTabHelper::TranslateTabHelper(content::WebContents* web_contents)
49 : content::WebContentsObserver(web_contents), 61 : content::WebContentsObserver(web_contents),
50 #if defined(CLD2_DYNAMIC_MODE) 62 max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts),
51 weak_pointer_factory_(this),
52 #endif
53 translate_driver_(&web_contents->GetController()), 63 translate_driver_(&web_contents->GetController()),
54 translate_manager_(new TranslateManager(this, prefs::kAcceptLanguages)) {} 64 translate_manager_(new TranslateManager(this, prefs::kAcceptLanguages)),
65 weak_pointer_factory_(this) {}
55 66
56 TranslateTabHelper::~TranslateTabHelper() { 67 TranslateTabHelper::~TranslateTabHelper() {
57 } 68 }
58 69
59 LanguageState& TranslateTabHelper::GetLanguageState() { 70 LanguageState& TranslateTabHelper::GetLanguageState() {
60 return translate_driver_.GetLanguageState(); 71 return translate_driver_.GetLanguageState();
61 } 72 }
62 73
63 // static 74 // static
64 scoped_ptr<TranslatePrefs> TranslateTabHelper::CreateTranslatePrefs( 75 scoped_ptr<TranslatePrefs> TranslateTabHelper::CreateTranslatePrefs(
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated, OnPageTranslated) 172 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated, OnPageTranslated)
162 #if defined(CLD2_DYNAMIC_MODE) 173 #if defined(CLD2_DYNAMIC_MODE)
163 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_NeedCLDData, OnCLDDataRequested) 174 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_NeedCLDData, OnCLDDataRequested)
164 #endif 175 #endif
165 IPC_MESSAGE_UNHANDLED(handled = false) 176 IPC_MESSAGE_UNHANDLED(handled = false)
166 IPC_END_MESSAGE_MAP() 177 IPC_END_MESSAGE_MAP()
167 178
168 return handled; 179 return handled;
169 } 180 }
170 181
182 void TranslateTabHelper::NavigationEntryCommitted(
183 const content::LoadCommittedDetails& load_details) {
184 // Check whether this is a reload: When doing a page reload, the
blundell 2014/04/07 08:03:01 A couple notes here: - The WCO method NavigationE
185 // TranslateLanguageDetermined IPC is not sent
186 // so the translation needs to be explicitly initiated.
187
188 content::NavigationEntry* entry =
189 web_contents()->GetController().GetActiveEntry();
190 if (!entry) {
blundell 2014/04/07 08:03:01 I don't really know why there are all these checks
191 NOTREACHED();
192 return;
193 }
194
195 // If the navigation happened while offline don't show the translate
196 // bar since there will be nothing to translate.
197 if (load_details.http_status_code == 0 ||
198 load_details.http_status_code == net::HTTP_INTERNAL_SERVER_ERROR) {
199 return;
200 }
201
202 if (!load_details.is_main_frame &&
203 translate_driver_.GetLanguageState().translation_declined()) {
204 // Some sites (such as Google map) may trigger sub-frame navigations
205 // when the user interacts with the page. We don't want to show a new
206 // infobar if the user already dismissed one in that case.
207 return;
208 }
209
210 // If not a reload, return.
211 if (entry->GetTransitionType() != content::PAGE_TRANSITION_RELOAD &&
blundell 2014/04/07 08:03:01 I actually think it would be clearest if this chec
212 load_details.type != content::NAVIGATION_TYPE_SAME_PAGE) {
213 return;
214 }
215
216 if (!translate_driver_.GetLanguageState().page_needs_translation())
217 return;
218
219 // Note that we delay it as the ordering of the processing of this callback
220 // by WebContentsObservers is undefined and might result in the current
221 // infobars being removed. Since the translation initiation process might add
222 // an
223 // infobar, it must
droger 2014/04/07 11:14:41 Nit: format.
224 // be done after that.
225 base::MessageLoop::current()->PostTask(
226 FROM_HERE,
227 base::Bind(&TranslateTabHelper::InitiateTranslation,
228 weak_pointer_factory_.GetWeakPtr(),
229 translate_driver_.GetLanguageState().original_language(),
230 0));
231 }
232
171 void TranslateTabHelper::DidNavigateAnyFrame( 233 void TranslateTabHelper::DidNavigateAnyFrame(
172 const content::LoadCommittedDetails& details, 234 const content::LoadCommittedDetails& details,
173 const content::FrameNavigateParams& params) { 235 const content::FrameNavigateParams& params) {
174 // Let the LanguageState clear its state. 236 // Let the LanguageState clear its state.
175 translate_driver_.DidNavigate(details); 237 translate_driver_.DidNavigate(details);
176 } 238 }
177 239
178 void TranslateTabHelper::WebContentsDestroyed( 240 void TranslateTabHelper::WebContentsDestroyed(
179 content::WebContents* web_contents) { 241 content::WebContents* web_contents) {
180 // Translation process can be interrupted. 242 // Translation process can be interrupted.
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 } else { 365 } else {
304 // Else, this request has taken care of it all. Cache all info. 366 // Else, this request has taken care of it all. Cache all info.
305 s_cached_file_ = file.release(); 367 s_cached_file_ = file.release();
306 s_cached_data_offset_ = data_offset; 368 s_cached_data_offset_ = data_offset;
307 s_cached_data_length_ = data_length; 369 s_cached_data_length_ = data_length;
308 } 370 }
309 } 371 }
310 } 372 }
311 #endif // defined(CLD2_DYNAMIC_MODE) 373 #endif // defined(CLD2_DYNAMIC_MODE)
312 374
375 void TranslateTabHelper::InitiateTranslation(const std::string& page_lang,
376 int attempt) {
377 if (translate_driver_.GetLanguageState().translation_pending())
378 return;
379
380 // During a reload we need web content to be available before the
381 // translate script is executed. Otherwise we will run the translate script on
382 // an empty DOM which will fail. Therefore we wait a bit to see if the page
383 // has finished.
384 if (web_contents()->IsLoading() && attempt < max_reload_check_attempts_) {
385 int backoff = attempt * kMaxTranslateLoadCheckAttempts;
386 base::MessageLoop::current()->PostDelayedTask(
387 FROM_HERE,
388 base::Bind(&TranslateTabHelper::InitiateTranslation,
389 weak_pointer_factory_.GetWeakPtr(),
390 page_lang,
391 ++attempt),
392 base::TimeDelta::FromMilliseconds(backoff));
393 return;
394 }
395
396 translate_manager_->InitiateTranslation(
397 TranslateDownloadManager::GetLanguageCode(page_lang));
398 }
399
313 void TranslateTabHelper::OnLanguageDetermined( 400 void TranslateTabHelper::OnLanguageDetermined(
314 const LanguageDetectionDetails& details, 401 const LanguageDetectionDetails& details,
315 bool page_needs_translation) { 402 bool page_needs_translation) {
316 translate_driver_.GetLanguageState().LanguageDetermined( 403 translate_driver_.GetLanguageState().LanguageDetermined(
317 details.adopted_language, page_needs_translation); 404 details.adopted_language, page_needs_translation);
318 405
319 if (web_contents()) 406 if (web_contents())
320 translate_manager_->InitiateTranslation(details.adopted_language); 407 translate_manager_->InitiateTranslation(details.adopted_language);
321 408
322 content::NotificationService::current()->Notify( 409 content::NotificationService::current()->Notify(
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 if (GetLanguageState().InTranslateNavigation()) 462 if (GetLanguageState().InTranslateNavigation())
376 return; 463 return;
377 } 464 }
378 465
379 TranslateBubbleFactory::Show( 466 TranslateBubbleFactory::Show(
380 browser->window(), web_contents(), step, error_type); 467 browser->window(), web_contents(), step, error_type);
381 #else 468 #else
382 NOTREACHED(); 469 NOTREACHED();
383 #endif 470 #endif
384 } 471 }
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