1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/translate/ios/browser/language_detection_controller.h"
#include <string>
#include "base/bind.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/time/time.h"
#include "components/prefs/pref_member.h"
#include "components/translate/core/common/translate_pref_names.h"
#include "components/translate/core/language_detection/language_detection_util.h"
#import "components/translate/ios/browser/js_language_detection_manager.h"
#include "ios/web/public/string_util.h"
#import "ios/web/public/url_scheme_util.h"
#include "ios/web/public/web_state/web_state.h"
namespace translate {
namespace {
// Name for the UMA metric used to track text extraction time.
const char kTranslateCaptureText[] = "Translate.CaptureText";
// Prefix for the language detection javascript commands. Must be kept in sync
// with language_detection.js.
const char kCommandPrefix[] = "languageDetection";
}
LanguageDetectionController::LanguageDetectionController(
web::WebState* web_state,
JsLanguageDetectionManager* manager,
PrefService* prefs)
: web::WebStateObserver(web_state),
js_manager_([manager retain]),
weak_method_factory_(this) {
DCHECK(web::WebStateObserver::web_state());
DCHECK(js_manager_);
translate_enabled_.Init(prefs::kEnableTranslate, prefs);
web_state->AddScriptCommandCallback(
base::Bind(&LanguageDetectionController::OnTextCaptured,
base::Unretained(this)),
kCommandPrefix);
}
LanguageDetectionController::~LanguageDetectionController() {
}
scoped_ptr<LanguageDetectionController::CallbackList::Subscription>
LanguageDetectionController::RegisterLanguageDetectionCallback(
const Callback& callback) {
return language_detection_callbacks_.Add(callback);
}
void LanguageDetectionController::StartLanguageDetection() {
if (!translate_enabled_.GetValue())
return; // Translate disabled in preferences.
DCHECK(web_state());
const GURL& url = web_state()->GetVisibleURL();
if (!web::UrlHasWebScheme(url) || !web_state()->ContentIsHTML())
return;
[js_manager_ inject];
[js_manager_ startLanguageDetection];
}
bool LanguageDetectionController::OnTextCaptured(
const base::DictionaryValue& command,
const GURL& url,
bool interacting) {
std::string textCapturedCommand;
if (!command.GetString("command", &textCapturedCommand) ||
textCapturedCommand != "languageDetection.textCaptured" ||
!command.HasKey("translationAllowed")) {
NOTREACHED();
return false;
}
bool translation_allowed = false;
command.GetBoolean("translationAllowed", &translation_allowed);
if (!translation_allowed) {
// Translation not allowed by the page. Done processing.
return true;
}
if (!command.HasKey("captureTextTime") || !command.HasKey("htmlLang") ||
!command.HasKey("httpContentLanguage")) {
NOTREACHED();
return false;
}
int capture_text_time = 0;
command.GetInteger("captureTextTime", &capture_text_time);
UMA_HISTOGRAM_TIMES(kTranslateCaptureText,
base::TimeDelta::FromMillisecondsD(capture_text_time));
std::string html_lang;
command.GetString("htmlLang", &html_lang);
std::string http_content_language;
command.GetString("httpContentLanguage", &http_content_language);
// If there is no language defined in httpEquiv, use the HTTP header.
if (http_content_language.empty())
http_content_language = web_state()->GetContentLanguageHeader();
[js_manager_ retrieveBufferedTextContent:
base::Bind(&LanguageDetectionController::OnTextRetrieved,
weak_method_factory_.GetWeakPtr(),
http_content_language, html_lang)];
return true;
}
void LanguageDetectionController::OnTextRetrieved(
const std::string& http_content_language,
const std::string& html_lang,
const base::string16& text_content) {
std::string language = translate::DeterminePageLanguage(
http_content_language, html_lang,
web::GetStringByClippingLastWord(text_content,
language_detection::kMaxIndexChars),
nullptr /* cld_language */, nullptr /* is_cld_reliable */);
if (language.empty())
return; // No language detected.
DetectionDetails details;
details.content_language = http_content_language;
details.html_root_language = html_lang;
details.adopted_language = language;
language_detection_callbacks_.Notify(details);
}
// web::WebStateObserver implementation:
void LanguageDetectionController::PageLoaded(
web::PageLoadCompletionStatus load_completion_status) {
if (load_completion_status == web::PageLoadCompletionStatus::SUCCESS)
StartLanguageDetection();
}
void LanguageDetectionController::UrlHashChanged() {
StartLanguageDetection();
}
void LanguageDetectionController::HistoryStateChanged() {
StartLanguageDetection();
}
void LanguageDetectionController::WebStateDestroyed() {
web_state()->RemoveScriptCommandCallback(kCommandPrefix);
}
} // namespace translate
|