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_LANGUAGE_DETECTION_CONTROLLER_H_
6 #define COMPONENTS_TRANSLATE_IOS_BROWSER_LANGUAGE_DETECTION_CONTROLLER_H_
10 #include "base/callback_list.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/mac/scoped_nsobject.h"
13 #include "base/macros.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/prefs/pref_member.h"
16 #include "base/strings/string16.h"
17 #include "ios/web/public/web_state/web_state_observer.h"
20 @
class JsLanguageDetectionManager
;
24 class DictionaryValue
;
33 class LanguageDetectionController
: public web::WebStateObserver
{
35 // Language detection details, passed to language detection callbacks.
36 struct DetectionDetails
{
37 // The language detected by the content (Content-Language).
38 std::string content_language
;
40 // The language written in the lang attribute of the html element.
41 std::string html_root_language
;
43 // The adopted language.
44 std::string adopted_language
;
47 LanguageDetectionController(web::WebState
* web_state
,
48 JsLanguageDetectionManager
* manager
,
50 ~LanguageDetectionController() override
;
52 // Callback types for language detection events.
53 typedef base::Callback
<void(const DetectionDetails
&)> Callback
;
54 typedef base::CallbackList
<void(const DetectionDetails
&)> CallbackList
;
56 // Registers a callback for language detection events.
57 scoped_ptr
<CallbackList::Subscription
> RegisterLanguageDetectionCallback(
58 const Callback
& callback
);
61 FRIEND_TEST_ALL_PREFIXES(LanguageDetectionControllerTest
, OnTextCaptured
);
63 // Starts the page language detection and initiates the translation process.
64 void StartLanguageDetection();
66 // Handles the "languageDetection.textCaptured" javascript command.
67 // |interacting| is true if the user is currently interacting with the page.
68 bool OnTextCaptured(const base::DictionaryValue
& value
,
72 // Completion handler used to retrieve the text buffered by the
73 // JsLanguageDetectionManager.
74 void OnTextRetrieved(const std::string
& http_content_language
,
75 const std::string
& html_lang
,
76 const base::string16
& text
);
78 // web::WebStateObserver implementation:
80 web::PageLoadCompletionStatus load_completion_status
) override
;
81 void UrlHashChanged() override
;
82 void HistoryStateChanged() override
;
83 void WebStateDestroyed() override
;
85 CallbackList language_detection_callbacks_
;
86 base::scoped_nsobject
<JsLanguageDetectionManager
> js_manager_
;
87 BooleanPrefMember translate_enabled_
;
88 base::WeakPtrFactory
<LanguageDetectionController
> weak_method_factory_
;
90 DISALLOW_COPY_AND_ASSIGN(LanguageDetectionController
);
93 } // namespace translate
95 #endif // COMPONENTS_TRANSLATE_IOS_BROWSER_LANGUAGE_DETECTION_CONTROLLER_H_