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.
5 #ifndef COMPONENTS_TRANSLATE_IOS_BROWSER_TRANSLATE_CONTROLLER_H_
6 #define COMPONENTS_TRANSLATE_IOS_BROWSER_TRANSLATE_CONTROLLER_H_
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"
16 @
class JsTranslateManager
;
20 class DictionaryValue
;
29 // TranslateController controls the translation of the page, by injecting the
30 // translate scripts and monitoring the status.
31 class TranslateController
: public web::WebStateObserver
{
33 // Observer class to monitor the progress of the translation.
36 // Called when the translate script is ready.
37 // In case of timeout, |success| is false.
38 virtual void OnTranslateScriptReady(bool success
,
40 double ready_time
) = 0;
42 // Called when the translation is complete.
43 virtual void OnTranslateComplete(bool success
,
44 const std::string
& original_language
,
45 double translation_time
) = 0;
48 TranslateController(web::WebState
* web_state
, JsTranslateManager
* manager
);
49 ~TranslateController() override
;
52 void set_observer(Observer
* observer
) { observer_
= observer
; }
54 // Injects the translate script.
55 void InjectTranslateScript(const std::string
& translate_script
);
57 // Reverts the translation.
58 void RevertTranslation();
60 // Starts the translation. Must be called when the translation script is
62 void StartTranslation(const std::string
& source_language
,
63 const std::string
& target_language
);
65 // Checks the translation status and calls the observer when it is done.
66 // This method must be called after StartTranslation().
67 void CheckTranslateStatus();
69 // Changes the JsTranslateManager used by this TranslateController.
70 // Only used for testing.
71 void SetJsTranslateManagerForTesting(JsTranslateManager
* manager
) {
72 js_manager_
.reset([manager retain
]);
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
);
85 // Called when a JavaScript command is received.
86 bool OnJavascriptCommandReceived(const base::DictionaryValue
& command
,
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
);
94 // web::WebStateObserver implementation:
95 void WebStateDestroyed() override
;
98 base::scoped_nsobject
<JsTranslateManager
> js_manager_
;
99 base::WeakPtrFactory
<TranslateController
> weak_method_factory_
;
101 DISALLOW_COPY_AND_ASSIGN(TranslateController
);
104 } // namespace translate
106 #endif // COMPONENTS_TRANSLATE_IOS_BROWSER_TRANSLATE_CONTROLLER_H_