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

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

Issue 424123003: Replace the use of CLD_DATA_FROM* and CLD-related ifdefs with runtime checks. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 6 years, 4 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
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 "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/prefs/pref_service.h" 11 #include "base/prefs/pref_service.h"
11 #include "base/strings/string_split.h" 12 #include "base/strings/string_split.h"
12 #include "chrome/browser/chrome_notification_types.h" 13 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/infobars/infobar_service.h" 14 #include "chrome/browser/infobars/infobar_service.h"
14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/translate/translate_accept_languages_factory.h" 16 #include "chrome/browser/translate/translate_accept_languages_factory.h"
16 #include "chrome/browser/translate/translate_service.h" 17 #include "chrome/browser/translate/translate_service.h"
17 #include "chrome/browser/ui/browser.h" 18 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_finder.h" 19 #include "chrome/browser/ui/browser_finder.h"
19 #include "chrome/browser/ui/browser_tabstrip.h" 20 #include "chrome/browser/ui/browser_tabstrip.h"
20 #include "chrome/browser/ui/browser_window.h" 21 #include "chrome/browser/ui/browser_window.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h" 22 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/browser/ui/translate/translate_bubble_factory.h" 23 #include "chrome/browser/ui/translate/translate_bubble_factory.h"
24 #include "chrome/common/chrome_paths.h"
23 #include "chrome/common/pref_names.h" 25 #include "chrome/common/pref_names.h"
24 #include "components/infobars/core/infobar.h" 26 #include "components/infobars/core/infobar.h"
27 #include "components/translate/content/common/cld_data_source.h"
25 #include "components/translate/content/common/translate_messages.h" 28 #include "components/translate/content/common/translate_messages.h"
26 #include "components/translate/core/browser/language_state.h" 29 #include "components/translate/core/browser/language_state.h"
27 #include "components/translate/core/browser/page_translated_details.h" 30 #include "components/translate/core/browser/page_translated_details.h"
28 #include "components/translate/core/browser/translate_accept_languages.h" 31 #include "components/translate/core/browser/translate_accept_languages.h"
29 #include "components/translate/core/browser/translate_download_manager.h" 32 #include "components/translate/core/browser/translate_download_manager.h"
30 #include "components/translate/core/browser/translate_infobar_delegate.h" 33 #include "components/translate/core/browser/translate_infobar_delegate.h"
31 #include "components/translate/core/browser/translate_manager.h" 34 #include "components/translate/core/browser/translate_manager.h"
32 #include "components/translate/core/browser/translate_prefs.h" 35 #include "components/translate/core/browser/translate_prefs.h"
33 #include "components/translate/core/common/language_detection_details.h" 36 #include "components/translate/core/common/language_detection_details.h"
34 #include "content/public/browser/navigation_details.h" 37 #include "content/public/browser/navigation_details.h"
35 #include "content/public/browser/navigation_entry.h" 38 #include "content/public/browser/navigation_entry.h"
36 #include "content/public/browser/notification_service.h" 39 #include "content/public/browser/notification_service.h"
37 #include "content/public/browser/render_view_host.h" 40 #include "content/public/browser/render_view_host.h"
38 #include "content/public/browser/web_contents.h" 41 #include "content/public/browser/web_contents.h"
39 #include "grit/theme_resources.h" 42 #include "grit/theme_resources.h"
40 #include "net/http/http_status_code.h" 43 #include "net/http/http_status_code.h"
41 #include "url/gurl.h" 44 #include "url/gurl.h"
42 45
43 #if defined(CLD_DATA_FROM_STANDALONE)
44 #include "base/path_service.h"
45 #include "chrome/common/chrome_paths.h"
46 #include "components/translate/content/browser/data_file_browser_cld_data_provid er.h"
47 #endif
48
49 namespace { 46 namespace {
50 47
51 // The maximum number of attempts we'll do to see if the page has finshed 48 // The maximum number of attempts we'll do to see if the page has finshed
52 // loading before giving up the translation 49 // loading before giving up the translation
53 const int kMaxTranslateLoadCheckAttempts = 20; 50 const int kMaxTranslateLoadCheckAttempts = 20;
54 51
55 #if defined(CLD_DATA_FROM_STANDALONE)
56 // This build uses a standalone CLD2 data file.
57 // TODO(andrewhayden): Make the data file path into a gyp/gn define 52 // TODO(andrewhayden): Make the data file path into a gyp/gn define
58 // If you change this, also update standalone_cld_data_harness.cc 53 // If you change this, also update standalone_cld_data_harness.cc
59 // accordingly! 54 // accordingly!
60 const base::FilePath::CharType kCldDataFileName[] = 55 const base::FilePath::CharType kCldDataFileName[] =
61 FILE_PATH_LITERAL("cld2_data.bin"); 56 FILE_PATH_LITERAL("cld2_data.bin");
62 57
63 bool g_cld_file_path_initialized_ = false; 58 bool g_cld_file_path_initialized_ = false;
64 59
65 void InitCldFilePath() {
66 VLOG(1) << "Initializing CLD file path for the first time.";
67 base::FilePath path;
68 if (!PathService::Get(chrome::DIR_USER_DATA, &path)) {
69 LOG(WARNING) << "Unable to locate user data directory";
70 return; // Chrome isn't properly installed
71 }
72 g_cld_file_path_initialized_ = true;
73 path = path.Append(kCldDataFileName);
74 VLOG(1) << "Setting CLD data file path: " << path.value();
75 translate::SetCldDataFilePath(path);
76 }
77 #endif
78
79 } // namespace 60 } // namespace
80 61
81 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeTranslateClient); 62 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromeTranslateClient);
82 63
83 ChromeTranslateClient::ChromeTranslateClient(content::WebContents* web_contents) 64 ChromeTranslateClient::ChromeTranslateClient(content::WebContents* web_contents)
84 : content::WebContentsObserver(web_contents), 65 : content::WebContentsObserver(web_contents),
85 max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts), 66 max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts),
86 translate_driver_(&web_contents->GetController()), 67 translate_driver_(&web_contents->GetController()),
87 translate_manager_( 68 translate_manager_(
88 new translate::TranslateManager(this, prefs::kAcceptLanguages)), 69 new translate::TranslateManager(this, prefs::kAcceptLanguages)),
89 cld_data_provider_( 70 cld_data_provider_(
90 translate::CreateBrowserCldDataProviderFor(web_contents)), 71 translate::CreateBrowserCldDataProviderFor(web_contents)),
91 weak_pointer_factory_(this) { 72 weak_pointer_factory_(this) {
92 #if defined(CLD_DATA_FROM_STANDALONE) 73 // Customization: for the standalone data source, we configure the path to
93 if (!g_cld_file_path_initialized_) 74 // CLD data immediately on startup.
94 InitCldFilePath(); 75 if (translate::CldDataSource::ShouldUseStandaloneDataFile() &&
95 #endif 76 !g_cld_file_path_initialized_) {
77 VLOG(1) << "Initializing CLD file path for the first time.";
78 base::FilePath path;
79 if (!PathService::Get(chrome::DIR_USER_DATA, &path)) {
80 // Chrome isn't properly installed
81 LOG(WARNING) << "Unable to locate user data directory";
82 } else {
83 g_cld_file_path_initialized_ = true;
84 path = path.Append(kCldDataFileName);
85 VLOG(1) << "Setting CLD data file path: " << path.value();
86 translate::SetCldDataFilePath(path);
87 }
88 }
96 } 89 }
97 90
98 ChromeTranslateClient::~ChromeTranslateClient() { 91 ChromeTranslateClient::~ChromeTranslateClient() {
99 } 92 }
100 93
101 translate::LanguageState& ChromeTranslateClient::GetLanguageState() { 94 translate::LanguageState& ChromeTranslateClient::GetLanguageState() {
102 return translate_manager_->GetLanguageState(); 95 return translate_manager_->GetLanguageState();
103 } 96 }
104 97
105 // static 98 // static
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 if (GetLanguageState().InTranslateNavigation()) 453 if (GetLanguageState().InTranslateNavigation())
461 return; 454 return;
462 } 455 }
463 456
464 TranslateBubbleFactory::Show( 457 TranslateBubbleFactory::Show(
465 browser->window(), web_contents(), step, error_type); 458 browser->window(), web_contents(), step, error_type);
466 #else 459 #else
467 NOTREACHED(); 460 NOTREACHED();
468 #endif 461 #endif
469 } 462 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698