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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_TRANSLATE_CHROME_TRANSLATE_CLIENT_H_ 6 #define CHROME_BROWSER_TRANSLATE_CHROME_TRANSLATE_CLIENT_H_ 7 8 #include <string> 9 10 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/weak_ptr.h" 12 #include "chrome/browser/ui/translate/translate_bubble_model.h" 13 #include "components/translate/content/browser/content_translate_driver.h" 14 #include "components/translate/core/browser/translate_client.h" 15 #include "components/translate/core/browser/translate_step.h" 16 #include "components/translate/core/common/translate_errors.h" 17 #include "content/public/browser/web_contents_observer.h" 18 #include "content/public/browser/web_contents_user_data.h" 19 20 #if defined(CLD2_DYNAMIC_MODE) 21 #include "base/basictypes.h" 22 #include "base/lazy_instance.h" 23 #include "base/synchronization/lock.h" 24 #include "base/task_runner.h" 25 #endif 26 27 namespace base { 28 class File; 29 } // namespace base 30 31 namespace content { 32 class BrowserContext; 33 class WebContents; 34 } // namespace content 35 36 namespace test { 37 class ScopedCLDDynamicDataHarness; 38 } // namespace test 39 40 struct LanguageDetectionDetails; 41 class LanguageState; 42 class PrefService; 43 class TranslateAcceptLanguages; 44 class TranslatePrefs; 45 class TranslateManager; 46 47 class ChromeTranslateClient 48 : public TranslateClient, 49 public content::WebContentsObserver, 50 public content::WebContentsUserData<ChromeTranslateClient> { 51 public: 52 virtual ~ChromeTranslateClient(); 53 54 // Gets the LanguageState associated with the page. 55 LanguageState& GetLanguageState(); 56 57 // Returns the ContentTranslateDriver instance associated with this 58 // WebContents. translate_driver()59 ContentTranslateDriver& translate_driver() { return translate_driver_; } 60 61 // Helper method to return a new TranslatePrefs instance. 62 static scoped_ptr<TranslatePrefs> CreateTranslatePrefs(PrefService* prefs); 63 64 // Helper method to return the TranslateAcceptLanguages instance associated 65 // with |browser_context|. 66 static TranslateAcceptLanguages* GetTranslateAcceptLanguages( 67 content::BrowserContext* browser_context); 68 69 // Helper method to return the TranslateManager instance associated with 70 // |web_contents|, or NULL if there is no such associated instance. 71 static TranslateManager* GetManagerFromWebContents( 72 content::WebContents* web_contents); 73 74 // Gets |source| and |target| language for translation. 75 static void GetTranslateLanguages(content::WebContents* web_contents, 76 std::string* source, 77 std::string* target); 78 79 // Gets the associated TranslateManager. 80 TranslateManager* GetTranslateManager(); 81 82 // Gets the associated WebContents. Returns NULL if the WebContents is being 83 // destroyed. 84 content::WebContents* GetWebContents(); 85 86 // Number of attempts before waiting for a page to be fully reloaded. set_translate_max_reload_attempts(int attempts)87 void set_translate_max_reload_attempts(int attempts) { 88 max_reload_check_attempts_ = attempts; 89 } 90 91 // TranslateClient implementation. 92 virtual TranslateDriver* GetTranslateDriver() OVERRIDE; 93 virtual PrefService* GetPrefs() OVERRIDE; 94 virtual scoped_ptr<TranslatePrefs> GetTranslatePrefs() OVERRIDE; 95 virtual TranslateAcceptLanguages* GetTranslateAcceptLanguages() OVERRIDE; 96 virtual int GetInfobarIconID() const OVERRIDE; 97 virtual scoped_ptr<infobars::InfoBar> CreateInfoBar( 98 scoped_ptr<TranslateInfoBarDelegate> delegate) const OVERRIDE; 99 virtual void ShowTranslateUI(translate::TranslateStep step, 100 const std::string source_language, 101 const std::string target_language, 102 TranslateErrors::Type error_type, 103 bool triggered_from_menu) OVERRIDE; 104 virtual bool IsTranslatableURL(const GURL& url) OVERRIDE; 105 virtual void ShowReportLanguageDetectionErrorUI( 106 const GURL& report_url) OVERRIDE; 107 108 private: 109 explicit ChromeTranslateClient(content::WebContents* web_contents); 110 friend class content::WebContentsUserData<ChromeTranslateClient>; 111 friend class test::ScopedCLDDynamicDataHarness; // For cleaning static state. 112 113 // content::WebContentsObserver implementation. 114 virtual void NavigationEntryCommitted( 115 const content::LoadCommittedDetails& load_details) OVERRIDE; 116 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 117 virtual void DidNavigateAnyFrame( 118 const content::LoadCommittedDetails& details, 119 const content::FrameNavigateParams& params) OVERRIDE; 120 virtual void WebContentsDestroyed() OVERRIDE; 121 122 // Initiates translation once the page is finished loading. 123 void InitiateTranslation(const std::string& page_lang, int attempt); 124 void OnLanguageDetermined(const LanguageDetectionDetails& details, 125 bool page_needs_translation); 126 void OnPageTranslated(int32 page_id, 127 const std::string& original_lang, 128 const std::string& translated_lang, 129 TranslateErrors::Type error_type); 130 131 #if defined(CLD2_DYNAMIC_MODE) 132 // Called when we receive ChromeViewHostMsg_NeedCLDData from a renderer. 133 // If we have already cached the data, responds immediately; else, enqueues 134 // a HandleCLDDataRequest on the blocking pool to cache the data. 135 // Acquires and releases s_file_lock_ in a non-blocking manner; queries 136 // handled while the file is being cached will gracefully and immediately 137 // fail. 138 // It is up to the originator of the message to poll again later if required; 139 // no "negative response" will be generated. 140 void OnCLDDataRequested(); 141 142 // Invoked on the blocking pool in order to cache the data. When successful, 143 // immediately responds to the request that initiated OnCLDDataRequested. 144 // Holds s_file_lock_ while the file is being cached. 145 static void HandleCLDDataRequest(); 146 147 // If the CLD data is ready, send it to the renderer. Briefly checks the lock. 148 void MaybeSendCLDDataAvailable(); 149 150 // Sends the renderer a response containing the data file handle. No locking. 151 void SendCLDDataAvailable(const base::File* handle, 152 const uint64 data_offset, 153 const uint64 data_length); 154 155 // The data file, cached as long as the process stays alive. 156 // We also track the offset at which the data starts, and its length. 157 static base::File* s_cached_file_; // guarded by file_lock_ 158 static uint64 s_cached_data_offset_; // guarded by file_lock_ 159 static uint64 s_cached_data_length_; // guarded by file_lock_ 160 161 // Guards s_cached_file_ 162 static base::LazyInstance<base::Lock> s_file_lock_; 163 164 #endif 165 166 // Shows the translate bubble. 167 void ShowBubble(translate::TranslateStep step, 168 TranslateErrors::Type error_type); 169 170 // Max number of attempts before checking if a page has been reloaded. 171 int max_reload_check_attempts_; 172 173 ContentTranslateDriver translate_driver_; 174 scoped_ptr<TranslateManager> translate_manager_; 175 176 // Necessary for binding the callback to HandleCLDDataRequest on the blocking 177 // pool and for delaying translation initialization until the page has 178 // finished loading on a reload. 179 base::WeakPtrFactory<ChromeTranslateClient> weak_pointer_factory_; 180 181 DISALLOW_COPY_AND_ASSIGN(ChromeTranslateClient); 182 }; 183 184 #endif // CHROME_BROWSER_TRANSLATE_CHROME_TRANSLATE_CLIENT_H_ 185