summaryrefslogtreecommitdiffstats
path: root/components/translate/ios/browser/language_detection_controller.h
blob: ec98439f4f2809f8c95f62e5e510f0c0cdd0452e (plain)
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
// 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.

#ifndef COMPONENTS_TRANSLATE_IOS_BROWSER_LANGUAGE_DETECTION_CONTROLLER_H_
#define COMPONENTS_TRANSLATE_IOS_BROWSER_LANGUAGE_DETECTION_CONTROLLER_H_

#include <string>

#include "base/callback_list.h"
#include "base/gtest_prod_util.h"
#include "base/mac/scoped_nsobject.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/prefs/pref_member.h"
#include "base/strings/string16.h"
#include "ios/web/public/web_state/web_state_observer.h"

class GURL;
@class JsLanguageDetectionManager;
class PrefService;

namespace base {
class DictionaryValue;
}

namespace web {
class WebState;
}

namespace translate {

class LanguageDetectionController : public web::WebStateObserver {
 public:
  // Language detection details, passed to language detection callbacks.
  struct DetectionDetails {
    // The language detected by the content (Content-Language).
    std::string content_language;

    // The language written in the lang attribute of the html element.
    std::string html_root_language;

    // The adopted language.
    std::string adopted_language;
  };

  LanguageDetectionController(web::WebState* web_state,
                              JsLanguageDetectionManager* manager,
                              PrefService* prefs);
  ~LanguageDetectionController() override;

  // Callback types for language detection events.
  typedef base::Callback<void(const DetectionDetails&)> Callback;
  typedef base::CallbackList<void(const DetectionDetails&)> CallbackList;

  // Registers a callback for language detection events.
  scoped_ptr<CallbackList::Subscription> RegisterLanguageDetectionCallback(
      const Callback& callback);

 private:
  FRIEND_TEST_ALL_PREFIXES(LanguageDetectionControllerTest, OnTextCaptured);

  // Starts the page language detection and initiates the translation process.
  void StartLanguageDetection();

  // Handles the "languageDetection.textCaptured" javascript command.
  // |interacting| is true if the user is currently interacting with the page.
  bool OnTextCaptured(const base::DictionaryValue& value,
                      const GURL& url,
                      bool interacting);

  // Completion handler used to retrieve the text buffered by the
  // JsLanguageDetectionManager.
  void OnTextRetrieved(const std::string& http_content_language,
                       const std::string& html_lang,
                       const base::string16& text);

  // web::WebStateObserver implementation:
  void PageLoaded(
      web::PageLoadCompletionStatus load_completion_status) override;
  void UrlHashChanged() override;
  void HistoryStateChanged() override;
  void WebStateDestroyed() override;

  CallbackList language_detection_callbacks_;
  base::scoped_nsobject<JsLanguageDetectionManager> js_manager_;
  BooleanPrefMember translate_enabled_;
  base::WeakPtrFactory<LanguageDetectionController> weak_method_factory_;

  DISALLOW_COPY_AND_ASSIGN(LanguageDetectionController);
};

}  // namespace translate

#endif  // COMPONENTS_TRANSLATE_IOS_BROWSER_LANGUAGE_DETECTION_CONTROLLER_H_