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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
54 | 54 |
55 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeTranslateClient); | 55 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeTranslateClient); |
56 | 56 |
57 ChromeTranslateClient::ChromeTranslateClient(content::WebContents* web_contents) | 57 ChromeTranslateClient::ChromeTranslateClient(content::WebContents* web_contents) |
58 : content::WebContentsObserver(web_contents), | 58 : content::WebContentsObserver(web_contents), |
59 translate_driver_(&web_contents->GetController()), | 59 translate_driver_(&web_contents->GetController()), |
60 translate_manager_( | 60 translate_manager_( |
61 new translate::TranslateManager(this, prefs::kAcceptLanguages)), | 61 new translate::TranslateManager(this, prefs::kAcceptLanguages)), |
62 cld_data_provider_( | 62 cld_data_provider_( |
63 translate::CreateBrowserCldDataProviderFor(web_contents)) { | 63 translate::CreateBrowserCldDataProviderFor(web_contents)) { |
64 translate_driver_.AddObserver(this); | |
64 translate_driver_.set_translate_manager(translate_manager_.get()); | 65 translate_driver_.set_translate_manager(translate_manager_.get()); |
65 // Customization: for the standalone data source, we configure the path to | 66 // Customization: for the standalone data source, we configure the path to |
66 // CLD data immediately on startup. | 67 // CLD data immediately on startup. |
67 if (translate::CldDataSource::ShouldUseStandaloneDataFile() && | 68 if (translate::CldDataSource::ShouldUseStandaloneDataFile() && |
68 !g_cld_file_path_initialized_) { | 69 !g_cld_file_path_initialized_) { |
69 VLOG(1) << "Initializing CLD file path for the first time."; | 70 VLOG(1) << "Initializing CLD file path for the first time."; |
70 base::FilePath path; | 71 base::FilePath path; |
71 if (!PathService::Get(chrome::DIR_USER_DATA, &path)) { | 72 if (!PathService::Get(chrome::DIR_USER_DATA, &path)) { |
72 // Chrome isn't properly installed | 73 // Chrome isn't properly installed |
73 LOG(WARNING) << "Unable to locate user data directory"; | 74 LOG(WARNING) << "Unable to locate user data directory"; |
74 } else { | 75 } else { |
75 g_cld_file_path_initialized_ = true; | 76 g_cld_file_path_initialized_ = true; |
76 path = path.Append(kCldDataFileName); | 77 path = path.Append(kCldDataFileName); |
77 VLOG(1) << "Setting CLD data file path: " << path.value(); | 78 VLOG(1) << "Setting CLD data file path: " << path.value(); |
78 translate::SetCldDataFilePath(path); | 79 translate::SetCldDataFilePath(path); |
79 } | 80 } |
80 } | 81 } |
81 } | 82 } |
82 | 83 |
83 ChromeTranslateClient::~ChromeTranslateClient() { | 84 ChromeTranslateClient::~ChromeTranslateClient() { |
85 translate_driver_.RemoveObserver(this); | |
84 } | 86 } |
85 | 87 |
86 translate::LanguageState& ChromeTranslateClient::GetLanguageState() { | 88 translate::LanguageState& ChromeTranslateClient::GetLanguageState() { |
87 return translate_manager_->GetLanguageState(); | 89 return translate_manager_->GetLanguageState(); |
88 } | 90 } |
89 | 91 |
90 // static | 92 // static |
91 scoped_ptr<translate::TranslatePrefs> | 93 scoped_ptr<translate::TranslatePrefs> |
92 ChromeTranslateClient::CreateTranslatePrefs(PrefService* prefs) { | 94 ChromeTranslateClient::CreateTranslatePrefs(PrefService* prefs) { |
93 #if defined(OS_CHROMEOS) | 95 #if defined(OS_CHROMEOS) |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 NOTREACHED(); | 258 NOTREACHED(); |
257 return; | 259 return; |
258 } | 260 } |
259 | 261 |
260 chrome::AddSelectedTabWithURL( | 262 chrome::AddSelectedTabWithURL( |
261 browser, report_url, content::PAGE_TRANSITION_AUTO_BOOKMARK); | 263 browser, report_url, content::PAGE_TRANSITION_AUTO_BOOKMARK); |
262 #endif // defined(OS_ANDROID) | 264 #endif // defined(OS_ANDROID) |
263 } | 265 } |
264 | 266 |
265 bool ChromeTranslateClient::OnMessageReceived(const IPC::Message& message) { | 267 bool ChromeTranslateClient::OnMessageReceived(const IPC::Message& message) { |
266 bool handled = true; | 268 return cld_data_provider_->OnMessageReceived(message); |
267 IPC_BEGIN_MESSAGE_MAP(ChromeTranslateClient, message) | |
268 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_TranslateAssignedSequenceNumber, | |
269 OnTranslateAssignedSequenceNumber) | |
270 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_TranslateLanguageDetermined, | |
271 OnLanguageDetermined) | |
272 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated, OnPageTranslated) | |
273 IPC_MESSAGE_UNHANDLED(handled = false) | |
274 IPC_END_MESSAGE_MAP() | |
275 | |
276 if (!handled) { | |
277 handled = cld_data_provider_->OnMessageReceived(message); | |
278 } | |
279 return handled; | |
280 } | 269 } |
281 | 270 |
282 void ChromeTranslateClient::WebContentsDestroyed() { | 271 void ChromeTranslateClient::WebContentsDestroyed() { |
283 // Translation process can be interrupted. | 272 // Translation process can be interrupted. |
284 // Destroying the TranslateManager now guarantees that it never has to deal | 273 // Destroying the TranslateManager now guarantees that it never has to deal |
285 // with NULL WebContents. | 274 // with NULL WebContents. |
286 translate_manager_.reset(); | 275 translate_manager_.reset(); |
287 } | 276 } |
288 | 277 |
289 void ChromeTranslateClient::OnTranslateAssignedSequenceNumber(int page_seq_no) { | 278 // ContentTranslateDriver::Observer implementation. |
290 translate_manager_->set_current_seq_no(page_seq_no); | |
291 } | |
292 | 279 |
293 void ChromeTranslateClient::OnLanguageDetermined( | 280 void ChromeTranslateClient::OnLanguageDetermined( |
294 const translate::LanguageDetectionDetails& details, | 281 const translate::LanguageDetectionDetails& details) { |
295 bool page_needs_translation) { | 282 // TODO: Remove translate notifications and have the clients be |
296 GetLanguageState().LanguageDetermined(details.adopted_language, | 283 // TranslateDriver::Observer directly instead. |
Takashi Toyoshima
2014/09/16 06:32:47
I guess this means ContentTranslateDriver::Observe
droger
2014/09/16 10:57:13
Correct. Done.
| |
297 page_needs_translation); | |
298 | |
299 if (web_contents()) | |
300 translate_manager_->InitiateTranslation(details.adopted_language); | |
301 | |
302 content::NotificationService::current()->Notify( | 284 content::NotificationService::current()->Notify( |
303 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 285 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
304 content::Source<content::WebContents>(web_contents()), | 286 content::Source<content::WebContents>(web_contents()), |
305 content::Details<const translate::LanguageDetectionDetails>(&details)); | 287 content::Details<const translate::LanguageDetectionDetails>(&details)); |
306 } | 288 } |
307 | 289 |
308 void ChromeTranslateClient::OnPageTranslated( | 290 void ChromeTranslateClient::OnPageTranslated( |
309 const std::string& original_lang, | 291 const std::string& original_lang, |
310 const std::string& translated_lang, | 292 const std::string& translated_lang, |
311 translate::TranslateErrors::Type error_type) { | 293 translate::TranslateErrors::Type error_type) { |
294 // TODO: Remove translate notifications and have the clients be | |
Takashi Toyoshima
2014/09/16 06:32:47
ditto
| |
295 // TranslateDriver::Observer directly instead. | |
312 DCHECK(web_contents()); | 296 DCHECK(web_contents()); |
313 translate_manager_->PageTranslated( | |
314 original_lang, translated_lang, error_type); | |
315 | |
316 translate::PageTranslatedDetails details; | 297 translate::PageTranslatedDetails details; |
317 details.source_language = original_lang; | 298 details.source_language = original_lang; |
318 details.target_language = translated_lang; | 299 details.target_language = translated_lang; |
319 details.error_type = error_type; | 300 details.error_type = error_type; |
320 content::NotificationService::current()->Notify( | 301 content::NotificationService::current()->Notify( |
321 chrome::NOTIFICATION_PAGE_TRANSLATED, | 302 chrome::NOTIFICATION_PAGE_TRANSLATED, |
322 content::Source<content::WebContents>(web_contents()), | 303 content::Source<content::WebContents>(web_contents()), |
323 content::Details<translate::PageTranslatedDetails>(&details)); | 304 content::Details<translate::PageTranslatedDetails>(&details)); |
324 } | 305 } |
325 | 306 |
(...skipping 30 matching lines...) Expand all Loading... | |
356 if (GetLanguageState().InTranslateNavigation()) | 337 if (GetLanguageState().InTranslateNavigation()) |
357 return; | 338 return; |
358 } | 339 } |
359 | 340 |
360 TranslateBubbleFactory::Show( | 341 TranslateBubbleFactory::Show( |
361 browser->window(), web_contents(), step, error_type); | 342 browser->window(), web_contents(), step, error_type); |
362 #else | 343 #else |
363 NOTREACHED(); | 344 NOTREACHED(); |
364 #endif | 345 #endif |
365 } | 346 } |
OLD | NEW |