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

Side by Side Diff: components/translate/content/browser/content_translate_driver.cc

Issue 558663003: Move navigation handling code from TranslateClient to TranslateDriver (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add missing initialization Created 6 years, 3 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
OLDNEW
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.h"
6 6
7 #include "base/bind.h"
7 #include "base/logging.h" 8 #include "base/logging.h"
8 #include "components/translate/content/common/translate_messages.h" 9 #include "components/translate/content/common/translate_messages.h"
10 #include "components/translate/core/browser/translate_download_manager.h"
11 #include "components/translate/core/browser/translate_manager.h"
9 #include "content/public/browser/browser_context.h" 12 #include "content/public/browser/browser_context.h"
10 #include "content/public/browser/navigation_controller.h" 13 #include "content/public/browser/navigation_controller.h"
14 #include "content/public/browser/navigation_details.h"
11 #include "content/public/browser/navigation_entry.h" 15 #include "content/public/browser/navigation_entry.h"
12 #include "content/public/browser/page_navigator.h" 16 #include "content/public/browser/page_navigator.h"
13 #include "content/public/browser/render_view_host.h" 17 #include "content/public/browser/render_view_host.h"
14 #include "content/public/browser/web_contents.h" 18 #include "content/public/browser/web_contents.h"
15 #include "content/public/common/referrer.h" 19 #include "content/public/common/referrer.h"
20 #include "net/http/http_status_code.h"
16 #include "url/gurl.h" 21 #include "url/gurl.h"
17 22
23 namespace {
24
25 // The maximum number of attempts we'll do to see if the page has finshed
26 // loading before giving up the translation
27 const int kMaxTranslateLoadCheckAttempts = 20;
28
29 } // namespace
30
18 namespace translate { 31 namespace translate {
19 32
20 ContentTranslateDriver::ContentTranslateDriver( 33 ContentTranslateDriver::ContentTranslateDriver(
21 content::NavigationController* nav_controller) 34 content::NavigationController* nav_controller)
22 : navigation_controller_(nav_controller), 35 : content::WebContentsObserver(nav_controller->GetWebContents()),
23 observer_(NULL) { 36 navigation_controller_(nav_controller),
37 translate_manager_(NULL),
38 observer_(NULL),
39 max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts),
40 weak_pointer_factory_(this) {
24 DCHECK(navigation_controller_); 41 DCHECK(navigation_controller_);
25 } 42 }
26 43
27 ContentTranslateDriver::~ContentTranslateDriver() {} 44 ContentTranslateDriver::~ContentTranslateDriver() {}
28 45
46 void ContentTranslateDriver::InitiateTranslation(const std::string& page_lang,
47 int attempt) {
48 if (translate_manager_->GetLanguageState().translation_pending())
49 return;
50
51 // During a reload we need web content to be available before the
52 // translate script is executed. Otherwise we will run the translate script on
53 // an empty DOM which will fail. Therefore we wait a bit to see if the page
54 // has finished.
55 if (web_contents()->IsLoading() && attempt < max_reload_check_attempts_) {
56 int backoff = attempt * kMaxTranslateLoadCheckAttempts;
57 base::MessageLoop::current()->PostDelayedTask(
58 FROM_HERE,
59 base::Bind(&ContentTranslateDriver::InitiateTranslation,
60 weak_pointer_factory_.GetWeakPtr(),
61 page_lang,
62 attempt + 1),
63 base::TimeDelta::FromMilliseconds(backoff));
64 return;
65 }
66
67 translate_manager_->InitiateTranslation(
68 translate::TranslateDownloadManager::GetLanguageCode(page_lang));
69 }
70
29 // TranslateDriver methods 71 // TranslateDriver methods
30 72
31 bool ContentTranslateDriver::IsLinkNavigation() { 73 bool ContentTranslateDriver::IsLinkNavigation() {
32 return navigation_controller_ && navigation_controller_->GetActiveEntry() && 74 return navigation_controller_ && navigation_controller_->GetActiveEntry() &&
33 navigation_controller_->GetActiveEntry()->GetTransitionType() == 75 navigation_controller_->GetActiveEntry()->GetTransitionType() ==
34 content::PAGE_TRANSITION_LINK; 76 content::PAGE_TRANSITION_LINK;
35 } 77 }
36 78
37 void ContentTranslateDriver::OnTranslateEnabledChanged() { 79 void ContentTranslateDriver::OnTranslateEnabledChanged() {
38 if (observer_) { 80 if (observer_) {
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 143
102 void ContentTranslateDriver::OpenUrlInNewTab(const GURL& url) { 144 void ContentTranslateDriver::OpenUrlInNewTab(const GURL& url) {
103 content::OpenURLParams params(url, 145 content::OpenURLParams params(url,
104 content::Referrer(), 146 content::Referrer(),
105 NEW_FOREGROUND_TAB, 147 NEW_FOREGROUND_TAB,
106 content::PAGE_TRANSITION_LINK, 148 content::PAGE_TRANSITION_LINK,
107 false); 149 false);
108 navigation_controller_->GetWebContents()->OpenURL(params); 150 navigation_controller_->GetWebContents()->OpenURL(params);
109 } 151 }
110 152
153 // content::WebContentsObserver methods
154
155 void ContentTranslateDriver::NavigationEntryCommitted(
156 const content::LoadCommittedDetails& load_details) {
157 // Check whether this is a reload: When doing a page reload, the
158 // TranslateLanguageDetermined IPC is not sent so the translation needs to be
159 // explicitly initiated.
160
161 content::NavigationEntry* entry =
162 web_contents()->GetController().GetActiveEntry();
163 if (!entry) {
164 NOTREACHED();
165 return;
166 }
167
168 // If the navigation happened while offline don't show the translate
169 // bar since there will be nothing to translate.
170 if (load_details.http_status_code == 0 ||
171 load_details.http_status_code == net::HTTP_INTERNAL_SERVER_ERROR) {
172 return;
173 }
174
175 if (!load_details.is_main_frame &&
176 translate_manager_->GetLanguageState().translation_declined()) {
177 // Some sites (such as Google map) may trigger sub-frame navigations
178 // when the user interacts with the page. We don't want to show a new
179 // infobar if the user already dismissed one in that case.
180 return;
181 }
182
183 // If not a reload, return.
184 if (entry->GetTransitionType() != content::PAGE_TRANSITION_RELOAD &&
185 load_details.type != content::NAVIGATION_TYPE_SAME_PAGE) {
186 return;
187 }
188
189 if (!translate_manager_->GetLanguageState().page_needs_translation())
190 return;
191
192 // Note that we delay it as the ordering of the processing of this callback
193 // by WebContentsObservers is undefined and might result in the current
194 // infobars being removed. Since the translation initiation process might add
195 // an infobar, it must be done after that.
196 base::MessageLoop::current()->PostTask(
197 FROM_HERE,
198 base::Bind(&ContentTranslateDriver::InitiateTranslation,
199 weak_pointer_factory_.GetWeakPtr(),
200 translate_manager_->GetLanguageState().original_language(),
201 0));
202 }
203
204 void ContentTranslateDriver::DidNavigateAnyFrame(
205 const content::LoadCommittedDetails& details,
206 const content::FrameNavigateParams& params) {
207 // Let the LanguageState clear its state.
208 const bool reload =
209 details.entry->GetTransitionType() == content::PAGE_TRANSITION_RELOAD ||
210 details.type == content::NAVIGATION_TYPE_SAME_PAGE;
211 translate_manager_->GetLanguageState().DidNavigate(
212 details.is_in_page, details.is_main_frame, reload);
213 }
214
111 } // namespace translate 215 } // namespace translate
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698