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 #import "components/translate/ios/browser/translate_controller.h"
8 #include "base/bind_helpers.h"
9 #include "base/logging.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "base/values.h"
12 #import "components/translate/ios/browser/js_translate_manager.h"
13 #include "ios/web/public/web_state/web_state.h"
18 // Prefix for the translate javascript commands. Must be kept in sync with
20 const char kCommandPrefix[] = "translate";
23 TranslateController::TranslateController(web::WebState* web_state,
24 JsTranslateManager* manager)
25 : web::WebStateObserver(web_state),
27 js_manager_([manager retain]),
28 weak_method_factory_(this) {
30 DCHECK(web::WebStateObserver::web_state());
31 web_state->AddScriptCommandCallback(
32 base::Bind(&TranslateController::OnJavascriptCommandReceived,
33 base::Unretained(this)),
37 TranslateController::~TranslateController() {
40 void TranslateController::InjectTranslateScript(
41 const std::string& translate_script) {
42 [js_manager_ setScript:base::SysUTF8ToNSString(translate_script)];
44 [js_manager_ injectWaitUntilTranslateReadyScript];
47 void TranslateController::RevertTranslation() {
48 [js_manager_ revertTranslation];
51 void TranslateController::StartTranslation(const std::string& source_language,
52 const std::string& target_language) {
53 [js_manager_ startTranslationFrom:source_language to:target_language];
56 void TranslateController::CheckTranslateStatus() {
57 [js_manager_ injectTranslateStatusScript];
60 bool TranslateController::OnJavascriptCommandReceived(
61 const base::DictionaryValue& command,
64 const base::Value* value = nullptr;
65 command.Get("command", &value);
70 std::string out_string;
71 value->GetAsString(&out_string);
72 if (out_string == "translate.ready")
73 return OnTranslateReady(command);
74 else if (out_string == "translate.status")
75 return OnTranslateComplete(command);
81 bool TranslateController::OnTranslateReady(
82 const base::DictionaryValue& command) {
83 if (!command.HasKey("timeout")) {
89 double load_time = 0.;
90 double ready_time = 0.;
92 command.GetBoolean("timeout", &timeout);
94 if (!command.HasKey("loadTime") || !command.HasKey("readyTime")) {
98 command.GetDouble("loadTime", &load_time);
99 command.GetDouble("readyTime", &ready_time);
102 observer_->OnTranslateScriptReady(!timeout, load_time, ready_time);
106 bool TranslateController::OnTranslateComplete(
107 const base::DictionaryValue& command) {
108 if (!command.HasKey("success")) {
113 bool success = false;
114 std::string original_language;
115 double translation_time = 0.;
117 command.GetBoolean("success", &success);
119 if (!command.HasKey("originalPageLanguage") ||
120 !command.HasKey("translationTime")) {
124 command.GetString("originalPageLanguage", &original_language);
125 command.GetDouble("translationTime", &translation_time);
129 observer_->OnTranslateComplete(success, original_language,
134 // web::WebStateObserver implementation.
136 void TranslateController::WebStateDestroyed() {
137 web_state()->RemoveScriptCommandCallback(kCommandPrefix);
140 } // namespace translate