1 // Copyright (c) 2011 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_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ 6 #define CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ 7 8 #include <string> 9 10 #include "base/gtest_prod_util.h" 11 #include "base/memory/weak_ptr.h" 12 #include "base/time/time.h" 13 #include "chrome/common/translate/translate_errors.h" 14 #include "content/public/renderer/render_view_observer.h" 15 16 namespace blink { 17 class WebDocument; 18 class WebFrame; 19 } 20 21 // This class deals with page translation. 22 // There is one TranslateHelper per RenderView. 23 24 class TranslateHelper : public content::RenderViewObserver { 25 public: 26 explicit TranslateHelper(content::RenderView* render_view); 27 virtual ~TranslateHelper(); 28 29 // Informs us that the page's text has been extracted. 30 void PageCaptured(int page_id, const base::string16& contents); 31 32 protected: 33 // The following methods are protected so they can be overridden in 34 // unit-tests. 35 void OnTranslatePage(int page_id, 36 const std::string& translate_script, 37 const std::string& source_lang, 38 const std::string& target_lang); 39 void OnRevertTranslation(int page_id); 40 41 // Returns true if the translate library is available, meaning the JavaScript 42 // has already been injected in that page. 43 virtual bool IsTranslateLibAvailable(); 44 45 // Returns true if the translate library has been initialized successfully. 46 virtual bool IsTranslateLibReady(); 47 48 // Returns true if the translation script has finished translating the page. 49 virtual bool HasTranslationFinished(); 50 51 // Returns true if the translation script has reported an error performing the 52 // translation. 53 virtual bool HasTranslationFailed(); 54 55 // Starts the translation by calling the translate library. This method 56 // should only be called when the translate script has been injected in the 57 // page. Returns false if the call failed immediately. 58 virtual bool StartTranslation(); 59 60 // Asks the Translate element in the page what the language of the page is. 61 // Can only be called if a translation has happened and was successful. 62 // Returns the language code on success, an empty string on failure. 63 virtual std::string GetOriginalPageLanguage(); 64 65 // Adjusts a delay time for a posted task. This is used in tests to do tasks 66 // immediately by returning 0. 67 virtual base::TimeDelta AdjustDelay(int delayInMs); 68 69 // Executes the JavaScript code in |script| in the main frame of RenderView. 70 virtual void ExecuteScript(const std::string& script); 71 72 // Executes the JavaScript code in |script| in the main frame of RenderView, 73 // and returns the boolean returned by the script evaluation if the script was 74 // run successfully. Otherwise, returns |fallback| value. 75 virtual bool ExecuteScriptAndGetBoolResult(const std::string& script, 76 bool fallback); 77 78 // Executes the JavaScript code in |script| in the main frame of RenderView, 79 // and returns the string returned by the script evaluation if the script was 80 // run successfully. Otherwise, returns empty string. 81 virtual std::string ExecuteScriptAndGetStringResult( 82 const std::string& script); 83 84 // Executes the JavaScript code in |script| in the main frame of RenderView. 85 // and returns the number returned by the script evaluation if the script was 86 // run successfully. Otherwise, returns 0.0. 87 virtual double ExecuteScriptAndGetDoubleResult(const std::string& script); 88 89 private: 90 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, AdoptHtmlLang); 91 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, 92 CLDAgreeWithLanguageCodeHavingCountryCode); 93 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, 94 CLDDisagreeWithWrongLanguageCode); 95 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, 96 InvalidLanguageMetaTagProviding); 97 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, LanguageCodeTypoCorrection); 98 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, LanguageCodeSynonyms); 99 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, ResetInvalidLanguageCode); 100 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, SimilarLanguageCode); 101 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, WellKnownWrongConfiguration); 102 103 // Converts language code to the one used in server supporting list. 104 static void ConvertLanguageCodeSynonym(std::string* code); 105 106 // Returns whether the page associated with |document| is a candidate for 107 // translation. Some pages can explictly specify (via a meta-tag) that they 108 // should not be translated. 109 static bool IsTranslationAllowed(blink::WebDocument* document); 110 111 // RenderViewObserver implementation. 112 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 113 114 // Cancels any translation that is currently being performed. This does not 115 // revert existing translations. 116 void CancelPendingTranslation(); 117 118 // Checks if the current running page translation is finished or errored and 119 // notifies the browser accordingly. If the translation has not terminated, 120 // posts a task to check again later. 121 void CheckTranslateStatus(); 122 123 // Called by TranslatePage to do the actual translation. |count| is used to 124 // limit the number of retries. 125 void TranslatePageImpl(int count); 126 127 // Sends a message to the browser to notify it that the translation failed 128 // with |error|. 129 void NotifyBrowserTranslationFailed(TranslateErrors::Type error); 130 131 // Convenience method to access the main frame. Can return NULL, typically 132 // if the page is being closed. 133 blink::WebFrame* GetMainFrame(); 134 135 // ID to represent a page which TranslateHelper captured and determined a 136 // content language. 137 int page_id_; 138 139 // The states associated with the current translation. 140 bool translation_pending_; 141 std::string source_lang_; 142 std::string target_lang_; 143 144 // Time when a page langauge is determined. This is used to know a duration 145 // time from showing infobar to requesting translation. 146 base::TimeTicks language_determined_time_; 147 148 // Method factory used to make calls to TranslatePageImpl. 149 base::WeakPtrFactory<TranslateHelper> weak_method_factory_; 150 151 DISALLOW_COPY_AND_ASSIGN(TranslateHelper); 152 }; 153 154 #endif // CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ 155