blob: 460d4a0706ef2ad0017f5cd7dc4676a1aa2b99a7 [file] [log] [blame]
drogerbfba8a42014-12-16 21:58:131// 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
John Z Wu31d0dd62018-11-02 00:59:298#include <iterator>
John Z Wudfaa4292018-09-07 01:10:269#include <memory>
John Z Wu31d0dd62018-11-02 00:59:2910#include <set>
drogerbfba8a42014-12-16 21:58:1311#include <string>
12
13#include "base/gtest_prod_util.h"
stkhapugin2393ab22017-01-20 15:34:4514#include "base/mac/scoped_nsobject.h"
drogerbfba8a42014-12-16 21:58:1315#include "base/macros.h"
16#include "base/memory/weak_ptr.h"
John Z Wuae98b4a2018-08-27 22:32:2317#include "components/translate/core/common/translate_errors.h"
drogerbfba8a42014-12-16 21:58:1318#include "ios/web/public/web_state/web_state_observer.h"
John Z Wudfaa4292018-09-07 01:10:2619#include "services/network/public/cpp/simple_url_loader.h"
drogerbfba8a42014-12-16 21:58:1320
21@class JsTranslateManager;
22class GURL;
23
24namespace base {
25class DictionaryValue;
John Z Wudfaa4292018-09-07 01:10:2626} // namespace base
drogerbfba8a42014-12-16 21:58:1327
28namespace web {
John Z Wudfaa4292018-09-07 01:10:2629class NavigationContext;
drogerbfba8a42014-12-16 21:58:1330class WebState;
John Z Wudfaa4292018-09-07 01:10:2631} // namespace web
drogerbfba8a42014-12-16 21:58:1332
33namespace translate {
34
35// TranslateController controls the translation of the page, by injecting the
36// translate scripts and monitoring the status.
37class TranslateController : public web::WebStateObserver {
38 public:
39 // Observer class to monitor the progress of the translation.
40 class Observer {
41 public:
42 // Called when the translate script is ready.
John Z Wuae98b4a2018-08-27 22:32:2343 // |error_type| Indicates error code.
44 virtual void OnTranslateScriptReady(TranslateErrors::Type error_type,
drogerbfba8a42014-12-16 21:58:1345 double load_time,
46 double ready_time) = 0;
47
48 // Called when the translation is complete.
John Z Wuae98b4a2018-08-27 22:32:2349 // |error_type| Indicates error code.
50 virtual void OnTranslateComplete(TranslateErrors::Type error_type,
drogerbfba8a42014-12-16 21:58:1351 const std::string& original_language,
52 double translation_time) = 0;
53 };
54
55 TranslateController(web::WebState* web_state, JsTranslateManager* manager);
56 ~TranslateController() override;
57
58 // Sets the observer.
59 void set_observer(Observer* observer) { observer_ = observer; }
60
61 // Injects the translate script.
62 void InjectTranslateScript(const std::string& translate_script);
63
64 // Reverts the translation.
65 void RevertTranslation();
66
67 // Starts the translation. Must be called when the translation script is
68 // ready.
69 void StartTranslation(const std::string& source_language,
70 const std::string& target_language);
71
drogerbfba8a42014-12-16 21:58:1372 // Changes the JsTranslateManager used by this TranslateController.
73 // Only used for testing.
stkhapugin2393ab22017-01-20 15:34:4574 void SetJsTranslateManagerForTesting(JsTranslateManager* manager);
drogerbfba8a42014-12-16 21:58:1375
76 private:
77 FRIEND_TEST_ALL_PREFIXES(TranslateControllerTest,
78 OnJavascriptCommandReceived);
79 FRIEND_TEST_ALL_PREFIXES(TranslateControllerTest,
Olivier Robin7b03f402018-06-21 13:17:0480 OnIFrameJavascriptCommandReceived);
81 FRIEND_TEST_ALL_PREFIXES(TranslateControllerTest,
drogerbfba8a42014-12-16 21:58:1382 OnTranslateScriptReadyTimeoutCalled);
83 FRIEND_TEST_ALL_PREFIXES(TranslateControllerTest,
84 OnTranslateScriptReadyCalled);
85 FRIEND_TEST_ALL_PREFIXES(TranslateControllerTest, TranslationSuccess);
86 FRIEND_TEST_ALL_PREFIXES(TranslateControllerTest, TranslationFailure);
John Z Wu7a1c7f12018-09-12 16:00:0987 FRIEND_TEST_ALL_PREFIXES(TranslateControllerTest, OnTranslateLoadJavascript);
John Z Wu31d0dd62018-11-02 00:59:2988 FRIEND_TEST_ALL_PREFIXES(TranslateControllerTest, OnTranslateSendRequest);
drogerbfba8a42014-12-16 21:58:1389
90 // Called when a JavaScript command is received.
91 bool OnJavascriptCommandReceived(const base::DictionaryValue& command,
92 const GURL& url,
Olivier Robin7b03f402018-06-21 13:17:0493 bool interacting,
Olivier Robin25206252018-09-11 09:09:4194 bool is_main_frame,
95 web::WebFrame* sender_frame);
drogerbfba8a42014-12-16 21:58:1396 // Methods to handle specific JavaScript commands.
97 // Return false if the command is invalid.
98 bool OnTranslateReady(const base::DictionaryValue& command);
99 bool OnTranslateComplete(const base::DictionaryValue& command);
John Z Wudfaa4292018-09-07 01:10:26100 bool OnTranslateLoadJavaScript(const base::DictionaryValue& command);
John Z Wu31d0dd62018-11-02 00:59:29101 bool OnTranslateSendRequest(const base::DictionaryValue& command);
John Z Wudfaa4292018-09-07 01:10:26102
John Z Wudfaa4292018-09-07 01:10:26103 // The callback when the script is fetched or a server error occurred.
104 void OnScriptFetchComplete(std::unique_ptr<std::string> response_body);
John Z Wu31d0dd62018-11-02 00:59:29105 // The callback when translate requests have completed.
106 void OnRequestFetchComplete(
107 std::set<std::unique_ptr<network::SimpleURLLoader>>::iterator it,
108 std::string url,
109 int request_id,
110 std::unique_ptr<std::string> response_body);
drogerbfba8a42014-12-16 21:58:13111
112 // web::WebStateObserver implementation:
Sylvain Defresne3212e2a2017-10-18 21:32:49113 void WebStateDestroyed(web::WebState* web_state) override;
John Z Wudfaa4292018-09-07 01:10:26114 void DidStartNavigation(web::WebState* web_state,
115 web::NavigationContext* navigation_context) override;
drogerbfba8a42014-12-16 21:58:13116
Sylvain Defresne4051ebd2017-11-13 18:12:01117 // The WebState this instance is observing. Will be null after
118 // WebStateDestroyed has been called.
119 web::WebState* web_state_ = nullptr;
120
John Z Wu31d0dd62018-11-02 00:59:29121 // Used to fetch translate requests. There may be multiple requests in flight.
122 std::set<std::unique_ptr<network::SimpleURLLoader>> request_fetchers_;
John Z Wudfaa4292018-09-07 01:10:26123 // Used to fetch additional scripts needed for translate.
124 std::unique_ptr<network::SimpleURLLoader> script_fetcher_;
125
drogerbfba8a42014-12-16 21:58:13126 Observer* observer_;
stkhapugin2393ab22017-01-20 15:34:45127 base::scoped_nsobject<JsTranslateManager> js_manager_;
drogerbfba8a42014-12-16 21:58:13128 base::WeakPtrFactory<TranslateController> weak_method_factory_;
129
130 DISALLOW_COPY_AND_ASSIGN(TranslateController);
131};
132
133} // namespace translate
134
135#endif // COMPONENTS_TRANSLATE_IOS_BROWSER_TRANSLATE_CONTROLLER_H_