droger | bfba8a4 | 2014-12-16 21:58:13 | [diff] [blame^] | 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 COMPONENTS_TRANSLATE_IOS_BROWSER_TRANSLATE_CONTROLLER_H_ |
| 6 | #define COMPONENTS_TRANSLATE_IOS_BROWSER_TRANSLATE_CONTROLLER_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/gtest_prod_util.h" |
| 11 | #include "base/mac/scoped_nsobject.h" |
| 12 | #include "base/macros.h" |
| 13 | #include "base/memory/weak_ptr.h" |
| 14 | #include "ios/web/public/web_state/web_state_observer.h" |
| 15 | |
| 16 | @class JsTranslateManager; |
| 17 | class GURL; |
| 18 | |
| 19 | namespace base { |
| 20 | class DictionaryValue; |
| 21 | } |
| 22 | |
| 23 | namespace web { |
| 24 | class WebState; |
| 25 | } |
| 26 | |
| 27 | namespace translate { |
| 28 | |
| 29 | // TranslateController controls the translation of the page, by injecting the |
| 30 | // translate scripts and monitoring the status. |
| 31 | class TranslateController : public web::WebStateObserver { |
| 32 | public: |
| 33 | // Observer class to monitor the progress of the translation. |
| 34 | class Observer { |
| 35 | public: |
| 36 | // Called when the translate script is ready. |
| 37 | // In case of timeout, |success| is false. |
| 38 | virtual void OnTranslateScriptReady(bool success, |
| 39 | double load_time, |
| 40 | double ready_time) = 0; |
| 41 | |
| 42 | // Called when the translation is complete. |
| 43 | virtual void OnTranslateComplete(bool success, |
| 44 | const std::string& original_language, |
| 45 | double translation_time) = 0; |
| 46 | }; |
| 47 | |
| 48 | TranslateController(web::WebState* web_state, JsTranslateManager* manager); |
| 49 | ~TranslateController() override; |
| 50 | |
| 51 | // Sets the observer. |
| 52 | void set_observer(Observer* observer) { observer_ = observer; } |
| 53 | |
| 54 | // Injects the translate script. |
| 55 | void InjectTranslateScript(const std::string& translate_script); |
| 56 | |
| 57 | // Reverts the translation. |
| 58 | void RevertTranslation(); |
| 59 | |
| 60 | // Starts the translation. Must be called when the translation script is |
| 61 | // ready. |
| 62 | void StartTranslation(const std::string& source_language, |
| 63 | const std::string& target_language); |
| 64 | |
| 65 | // Checks the translation status and calls the observer when it is done. |
| 66 | // This method must be called after StartTranslation(). |
| 67 | void CheckTranslateStatus(); |
| 68 | |
| 69 | // Changes the JsTranslateManager used by this TranslateController. |
| 70 | // Only used for testing. |
| 71 | void SetJsTranslateManagerForTesting(JsTranslateManager* manager) { |
| 72 | js_manager_.reset([manager retain]); |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | FRIEND_TEST_ALL_PREFIXES(TranslateControllerTest, |
| 77 | OnJavascriptCommandReceived); |
| 78 | FRIEND_TEST_ALL_PREFIXES(TranslateControllerTest, |
| 79 | OnTranslateScriptReadyTimeoutCalled); |
| 80 | FRIEND_TEST_ALL_PREFIXES(TranslateControllerTest, |
| 81 | OnTranslateScriptReadyCalled); |
| 82 | FRIEND_TEST_ALL_PREFIXES(TranslateControllerTest, TranslationSuccess); |
| 83 | FRIEND_TEST_ALL_PREFIXES(TranslateControllerTest, TranslationFailure); |
| 84 | |
| 85 | // Called when a JavaScript command is received. |
| 86 | bool OnJavascriptCommandReceived(const base::DictionaryValue& command, |
| 87 | const GURL& url, |
| 88 | bool interacting); |
| 89 | // Methods to handle specific JavaScript commands. |
| 90 | // Return false if the command is invalid. |
| 91 | bool OnTranslateReady(const base::DictionaryValue& command); |
| 92 | bool OnTranslateComplete(const base::DictionaryValue& command); |
| 93 | |
| 94 | // web::WebStateObserver implementation: |
| 95 | void WebStateDestroyed() override; |
| 96 | |
| 97 | Observer* observer_; |
| 98 | base::scoped_nsobject<JsTranslateManager> js_manager_; |
| 99 | base::WeakPtrFactory<TranslateController> weak_method_factory_; |
| 100 | |
| 101 | DISALLOW_COPY_AND_ASSIGN(TranslateController); |
| 102 | }; |
| 103 | |
| 104 | } // namespace translate |
| 105 | |
| 106 | #endif // COMPONENTS_TRANSLATE_IOS_BROWSER_TRANSLATE_CONTROLLER_H_ |