Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / components / translate / ios / browser / translate_controller.mm
blob6035ba5e9e7339d3ae7cf38825195e0a144780fc
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"
7 #include "base/bind.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"
15 namespace translate {
17 namespace {
18 // Prefix for the translate javascript commands. Must be kept in sync with
19 // translate_ios.js.
20 const char kCommandPrefix[] = "translate";
23 TranslateController::TranslateController(web::WebState* web_state,
24                                          JsTranslateManager* manager)
25     : web::WebStateObserver(web_state),
26       observer_(nullptr),
27       js_manager_([manager retain]),
28       weak_method_factory_(this) {
29   DCHECK(js_manager_);
30   DCHECK(web::WebStateObserver::web_state());
31   web_state->AddScriptCommandCallback(
32       base::Bind(&TranslateController::OnJavascriptCommandReceived,
33                  base::Unretained(this)),
34       kCommandPrefix);
37 TranslateController::~TranslateController() {
40 void TranslateController::InjectTranslateScript(
41     const std::string& translate_script) {
42   [js_manager_ setScript:base::SysUTF8ToNSString(translate_script)];
43   [js_manager_ inject];
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,
62     const GURL& url,
63     bool interacting) {
64   const base::Value* value = nullptr;
65   command.Get("command", &value);
66   if (!value) {
67     return false;
68   }
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);
77   NOTREACHED();
78   return false;
81 bool TranslateController::OnTranslateReady(
82     const base::DictionaryValue& command) {
83   if (!command.HasKey("timeout")) {
84     NOTREACHED();
85     return false;
86   }
88   bool timeout = false;
89   double load_time = 0.;
90   double ready_time = 0.;
92   command.GetBoolean("timeout", &timeout);
93   if (!timeout) {
94     if (!command.HasKey("loadTime") || !command.HasKey("readyTime")) {
95       NOTREACHED();
96       return false;
97     }
98     command.GetDouble("loadTime", &load_time);
99     command.GetDouble("readyTime", &ready_time);
100   }
101   if (observer_)
102     observer_->OnTranslateScriptReady(!timeout, load_time, ready_time);
103   return true;
106 bool TranslateController::OnTranslateComplete(
107     const base::DictionaryValue& command) {
108   if (!command.HasKey("success")) {
109     NOTREACHED();
110     return false;
111   }
113   bool success = false;
114   std::string original_language;
115   double translation_time = 0.;
117   command.GetBoolean("success", &success);
118   if (success) {
119     if (!command.HasKey("originalPageLanguage") ||
120         !command.HasKey("translationTime")) {
121       NOTREACHED();
122       return false;
123     }
124     command.GetString("originalPageLanguage", &original_language);
125     command.GetDouble("translationTime", &translation_time);
126   }
128   if (observer_)
129     observer_->OnTranslateComplete(success, original_language,
130                                    translation_time);
131   return true;
134 // web::WebStateObserver implementation.
136 void TranslateController::WebStateDestroyed() {
137   web_state()->RemoveScriptCommandCallback(kCommandPrefix);
140 }  // namespace translate