File: language_detection_observer.h

package info (click to toggle)
chromium 140.0.7339.127-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,192,880 kB
  • sloc: cpp: 35,093,808; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (81 lines) | stat: -rw-r--r-- 3,026 bytes parent folder | download | duplicates (4)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_PERMISSIONS_PREDICTION_SERVICE_LANGUAGE_DETECTION_OBSERVER_H_
#define CHROME_BROWSER_PERMISSIONS_PREDICTION_SERVICE_LANGUAGE_DETECTION_OBSERVER_H_

#include "base/memory/weak_ptr.h"
#include "base/timer/timer.h"
#include "components/translate/core/browser/translate_driver.h"
#include "content/public/browser/web_contents.h"

class ChromeTranslateClient;

namespace permissions {

// An observer with timeout that waits for the page's language to be determined.
//
// This class checks if the language of the provided webcontents page is
// already known to be English. If so, it synchronously invokes the success
// callback. If the language is known and is not English, it invokes the
// fallback callback.
//
// If the language is not yet determined, it registers itself as a
// LanguageDetectionObserver using ChromeTranslateDriver class and will
// unregister itself as an observer after either a successful detection, a
// non-English detection, or a timeout. It then waits for language
// determination, invoking the appropriate callback based on whether the
// detected language is English.
//
// A timeout is used to limit the wait time for language detection. If
// the language is not determined within the timeout period, the fallback
// callback is invoked and the observer is deregistered.
class LanguageDetectionObserver
    : public translate::TranslateDriver::LanguageDetectionObserver {
 public:
  LanguageDetectionObserver();
  ~LanguageDetectionObserver() override;

  // The timeout for the language detection in seconds. If the language
  // detection takes longer than this, the fallback callback will be invoked.
  static const int kLanguageDetectionTimeout = 1;

  // Virtual for testing.
  virtual void Init(content::WebContents* web_contents,
                    base::OnceCallback<void()> on_english_detected,
                    base::OnceCallback<void()> on_fallback);

  void OnLanguageDetermined(
      const translate::LanguageDetectionDetails& details) override;

  void Reset();

  bool WaitingForLanguageDetection();

 protected:
  void OnTimeout();

  // Virtual for testing.
  virtual void RemoveAsObserver();

  ChromeTranslateClient* chrome_translate_client();

  raw_ptr<content::WebContents> web_contents_;

  // Called when English is determined to be the language of the current
  // webcontents.
  base::OnceCallback<void()> on_english_detected_callback_;

  // Called when language detection takes longer than the timeout or when the
  // detected language is not English.
  base::OnceCallback<void()> fallback_callback_;

  base::OneShotTimer timeout_timer_;

  // Used for the timer to bind OnTimeout as a callback.
  base::WeakPtrFactory<LanguageDetectionObserver> weak_ptr_factory_{this};
};
}  // namespace permissions

#endif  // CHROME_BROWSER_PERMISSIONS_PREDICTION_SERVICE_LANGUAGE_DETECTION_OBSERVER_H_