OLD | NEW |
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 <vector> |
| 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
9 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
10 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
11 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/translate/translate_accept_languages_factory.h" | 14 #include "chrome/browser/translate/translate_accept_languages_factory.h" |
13 #include "chrome/browser/translate/translate_infobar_delegate.h" | 15 #include "chrome/browser/translate/translate_infobar_delegate.h" |
14 #include "chrome/browser/translate/translate_service.h" | 16 #include "chrome/browser/translate/translate_service.h" |
15 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/browser_finder.h" | 18 #include "chrome/browser/ui/browser_finder.h" |
(...skipping 17 matching lines...) Expand all Loading... |
34 #include "net/http/http_status_code.h" | 36 #include "net/http/http_status_code.h" |
35 #include "url/gurl.h" | 37 #include "url/gurl.h" |
36 | 38 |
37 #if defined(CLD2_DYNAMIC_MODE) | 39 #if defined(CLD2_DYNAMIC_MODE) |
38 #include "base/files/file.h" | 40 #include "base/files/file.h" |
39 #include "base/path_service.h" | 41 #include "base/path_service.h" |
40 #include "chrome/common/chrome_constants.h" | 42 #include "chrome/common/chrome_constants.h" |
41 #include "chrome/common/chrome_paths.h" | 43 #include "chrome/common/chrome_paths.h" |
42 #include "content/public/browser/browser_thread.h" | 44 #include "content/public/browser/browser_thread.h" |
43 #include "content/public/browser/render_process_host.h" | 45 #include "content/public/browser/render_process_host.h" |
44 #include "content/public/browser/render_view_host.h" | |
45 #endif | 46 #endif |
46 | 47 |
47 #if defined(CLD2_IS_COMPONENT) | 48 #if defined(CLD2_IS_COMPONENT) |
48 #include "chrome/browser/component_updater/cld_component_installer.h" | 49 #include "chrome/browser/component_updater/cld_component_installer.h" |
49 #endif | 50 #endif |
50 | 51 |
51 namespace { | 52 namespace { |
52 | 53 |
53 // The maximum number of attempts we'll do to see if the page has finshed | 54 // The maximum number of attempts we'll do to see if the page has finshed |
54 // loading before giving up the translation | 55 // loading before giving up the translation |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 const std::string target_language, | 156 const std::string target_language, |
156 TranslateErrors::Type error_type, | 157 TranslateErrors::Type error_type, |
157 bool triggered_from_menu) { | 158 bool triggered_from_menu) { |
158 DCHECK(web_contents()); | 159 DCHECK(web_contents()); |
159 if (error_type != TranslateErrors::NONE) | 160 if (error_type != TranslateErrors::NONE) |
160 step = translate::TRANSLATE_STEP_TRANSLATE_ERROR; | 161 step = translate::TRANSLATE_STEP_TRANSLATE_ERROR; |
161 | 162 |
162 if (TranslateService::IsTranslateBubbleEnabled()) { | 163 if (TranslateService::IsTranslateBubbleEnabled()) { |
163 // Bubble UI. | 164 // Bubble UI. |
164 if (step == translate::TRANSLATE_STEP_BEFORE_TRANSLATE) { | 165 if (step == translate::TRANSLATE_STEP_BEFORE_TRANSLATE) { |
165 // TODO: Move this logic out of UI code. | 166 // TODO(droger): Move this logic out of UI code. |
166 GetLanguageState().SetTranslateEnabled(true); | 167 GetLanguageState().SetTranslateEnabled(true); |
167 if (!GetLanguageState().HasLanguageChanged()) | 168 if (!GetLanguageState().HasLanguageChanged()) |
168 return; | 169 return; |
169 } | 170 } |
170 ShowBubble(step, error_type); | 171 ShowBubble(step, error_type); |
171 return; | 172 return; |
172 } | 173 } |
173 | 174 |
174 // Infobar UI. | 175 // Infobar UI. |
175 Profile* profile = | 176 Profile* profile = |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 | 381 |
381 void TranslateTabHelper::HandleCLDDataRequest() { | 382 void TranslateTabHelper::HandleCLDDataRequest() { |
382 // Because this function involves arbitrary file system access, it must run | 383 // Because this function involves arbitrary file system access, it must run |
383 // on the blocking pool. | 384 // on the blocking pool. |
384 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 385 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
385 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 386 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
386 | 387 |
387 { | 388 { |
388 base::AutoLock lock(s_file_lock_.Get()); | 389 base::AutoLock lock(s_file_lock_.Get()); |
389 if (s_cached_file_) | 390 if (s_cached_file_) |
390 return; // Already done, duplicate request | 391 return; // Already done, duplicate request |
391 } | 392 } |
392 | 393 |
| 394 #if defined(CLD2_IS_COMPONENT) |
| 395 base::FilePath path = component_updater::GetLatestCldDataFile(); |
| 396 if (path.empty()) |
| 397 return; |
| 398 #else // CLD2 data is at a well-known file path |
393 base::FilePath path; | 399 base::FilePath path; |
394 #if defined(CLD2_IS_COMPONENT) | |
395 if (!component_updater::GetLatestCldDataFile(&path)) | |
396 return; | |
397 #else | |
398 if (!PathService::Get(chrome::DIR_USER_DATA, &path)) { | 400 if (!PathService::Get(chrome::DIR_USER_DATA, &path)) { |
399 LOG(WARNING) << "Unable to locate user data directory"; | 401 LOG(WARNING) << "Unable to locate user data directory"; |
400 return; // Chrome isn't properly installed. | 402 return; // Chrome isn't properly installed. |
401 } | 403 } |
| 404 path = path.Append(chrome::kCLDDataFilename); |
| 405 #endif |
| 406 |
402 // If the file exists, we can send an IPC-safe construct back to the | 407 // If the file exists, we can send an IPC-safe construct back to the |
403 // renderer process immediately. | 408 // renderer process immediately; otherwise, nothing to do here. |
404 path = path.Append(chrome::kCLDDataFilename); | |
405 if (!base::PathExists(path)) | 409 if (!base::PathExists(path)) |
406 return; | 410 return; |
407 #endif | |
408 | 411 |
409 // Attempt to open the file for reading. | 412 // Attempt to open the file for reading. |
410 scoped_ptr<base::File> file( | 413 scoped_ptr<base::File> file( |
411 new base::File(path, base::File::FLAG_OPEN | base::File::FLAG_READ)); | 414 new base::File(path, base::File::FLAG_OPEN | base::File::FLAG_READ)); |
412 if (!file->IsValid()) { | 415 if (!file->IsValid()) { |
413 LOG(WARNING) << "CLD data file exists but cannot be opened"; | 416 LOG(WARNING) << "CLD data file exists but cannot be opened"; |
414 return; | 417 return; |
415 } | 418 } |
416 | 419 |
417 base::File::Info file_info; | 420 base::File::Info file_info; |
(...skipping 13 matching lines...) Expand all Loading... |
431 if (s_cached_file_) { | 434 if (s_cached_file_) { |
432 // Idempotence: Racing another request on the blocking pool, abort. | 435 // Idempotence: Racing another request on the blocking pool, abort. |
433 } else { | 436 } else { |
434 // Else, this request has taken care of it all. Cache all info. | 437 // Else, this request has taken care of it all. Cache all info. |
435 s_cached_file_ = file.release(); | 438 s_cached_file_ = file.release(); |
436 s_cached_data_offset_ = data_offset; | 439 s_cached_data_offset_ = data_offset; |
437 s_cached_data_length_ = data_length; | 440 s_cached_data_length_ = data_length; |
438 } | 441 } |
439 } | 442 } |
440 } | 443 } |
441 #endif // defined(CLD2_DYNAMIC_MODE) | 444 #endif // defined(CLD2_DYNAMIC_MODE) |
442 | 445 |
443 void TranslateTabHelper::InitiateTranslation(const std::string& page_lang, | 446 void TranslateTabHelper::InitiateTranslation(const std::string& page_lang, |
444 int attempt) { | 447 int attempt) { |
445 if (translate_driver_.GetLanguageState().translation_pending()) | 448 if (translate_driver_.GetLanguageState().translation_pending()) |
446 return; | 449 return; |
447 | 450 |
448 // During a reload we need web content to be available before the | 451 // During a reload we need web content to be available before the |
449 // translate script is executed. Otherwise we will run the translate script on | 452 // translate script is executed. Otherwise we will run the translate script on |
450 // an empty DOM which will fail. Therefore we wait a bit to see if the page | 453 // an empty DOM which will fail. Therefore we wait a bit to see if the page |
451 // has finished. | 454 // has finished. |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 if (GetLanguageState().InTranslateNavigation()) | 537 if (GetLanguageState().InTranslateNavigation()) |
535 return; | 538 return; |
536 } | 539 } |
537 | 540 |
538 TranslateBubbleFactory::Show( | 541 TranslateBubbleFactory::Show( |
539 browser->window(), web_contents(), step, error_type); | 542 browser->window(), web_contents(), step, error_type); |
540 #else | 543 #else |
541 NOTREACHED(); | 544 NOTREACHED(); |
542 #endif | 545 #endif |
543 } | 546 } |
OLD | NEW |