File: translate_bubble_controller.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (125 lines) | stat: -rw-r--r-- 5,585 bytes parent folder | download | duplicates (5)
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
// Copyright 2022 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_UI_VIEWS_TRANSLATE_TRANSLATE_BUBBLE_CONTROLLER_H_
#define CHROME_BROWSER_UI_VIEWS_TRANSLATE_TRANSLATE_BUBBLE_CONTROLLER_H_

#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/views/translate/partial_translate_bubble_view.h"
#include "chrome/browser/ui/views/translate/translate_bubble_view.h"
#include "content/public/browser/web_contents.h"

// Controls both TranslateBubbleView and PartialTranslateBubbleView shown for
// a given browser. This controller ensures only one of the two are shown at
// a time, and is responsible for creating/hiding the bubbles.
class TranslateBubbleController : public PartialTranslateBubbleModel::Observer {
 public:
  // `root_action_item` is used to retrieve the correct Translate ActionItem.
  explicit TranslateBubbleController(actions::ActionItem* root_action_item);
  ~TranslateBubbleController() override;
  TranslateBubbleController(const TranslateBubbleController&) = delete;
  TranslateBubbleController& operator=(const TranslateBubbleController&) =
      delete;

  // Shows the Full Page Translate bubble. Returns the newly created bubble's
  // Widget or nullptr in cases when the bubble already exists or when the
  // bubble is not created.
  views::Widget* ShowTranslateBubble(
      content::WebContents* web_contents,
      views::View* anchor_view,
      views::Button* highlighted_button,
      translate::TranslateStep step,
      const std::string& source_language,
      const std::string& target_language,
      translate::TranslateErrors error_type,
      LocationBarBubbleDelegateView::DisplayReason reason);

  // Initiates the Partial Translate request, showing the bubble after a delay
  // dependent on the Partial Translate response.
  void StartPartialTranslate(content::WebContents* web_contents,
                             views::View* anchor_view,
                             views::Button* highlighted_button,
                             const std::string& source_language,
                             const std::string& target_language,
                             const std::u16string& text_selection);

  // Closes the current Partial or Full Page Translate bubble, if either exists.
  // At most one of these bubbles should be non-null at any given time.
  void CloseBubble();

  // Returns the currently shown Full Page Translate bubble view. Returns
  // nullptr if the bubble is not currently shown.
  TranslateBubbleView* GetTranslateBubble() const;

  // Returns the currently shown Partial Translate bubble view. Returns nullptr
  // if the bubble is not currently shown.
  PartialTranslateBubbleView* GetPartialTranslateBubble() const;

  void SetTranslateBubbleModelFactory(
      base::RepeatingCallback<std::unique_ptr<TranslateBubbleModel>()>
          callback);
  void SetPartialTranslateBubbleModelFactory(
      base::RepeatingCallback<std::unique_ptr<PartialTranslateBubbleModel>()>
          callback);

  base::OnceClosure GetOnTranslateBubbleClosedCallback();
  base::OnceClosure GetOnPartialTranslateBubbleClosedCallback();

 private:
  // Weak references for the two possible Translate bubble views. These will be
  // nullptr if no bubble is currently shown. At most one of these pointers
  // should be non-null at any given time.
  raw_ptr<TranslateBubbleView> translate_bubble_view_ = nullptr;
  raw_ptr<PartialTranslateBubbleView> partial_translate_bubble_view_ = nullptr;

  // Factories used to construct models for the two different Translate bubbles.
  // If the factory is null, the standard implementations -
  // TranslateBubbleModelImpl and PartialTranslateBubbleModelImpl - will be
  // used.
  base::RepeatingCallback<std::unique_ptr<TranslateBubbleModel>()>
      model_factory_callback_;
  base::RepeatingCallback<std::unique_ptr<PartialTranslateBubbleModel>()>
      partial_model_factory_callback_;

  // Creates the Partial Translate bubble or updates the bubble if it already
  // exists.
  void CreatePartialTranslateBubble(
      content::WebContents* web_contents,
      views::View* anchor_view,
      views::Button* highlighted_button,
      PartialTranslateBubbleModel::ViewState view_state,
      const std::string& source_language,
      const std::string& target_language,
      const std::u16string& source_text,
      const std::u16string& target_text,
      translate::TranslateErrors error_type);

  // Handlers for when Translate bubbles are closed.
  void OnTranslateBubbleClosed();
  void OnPartialTranslateBubbleClosed();

  // Called when the initial wait for the Partial Translate response expires.
  // Shows the waitspin view.
  void OnPartialTranslateWaitExpired();

  // PartialTranslateBubbleModel::Observer impl.
  void OnPartialTranslateComplete() override;

  // Timer used for handling the delay before showing Partial Translate bubble.
  base::OneShotTimer partial_translate_timer_;

  friend class TranslateBubbleControllerTest;
  WEB_CONTENTS_USER_DATA_KEY_DECL();

  // The action item associated with showing a Translate UI.
  // The bubbles use this to appropriately configure its "IsBubbleShowing"
  // property.
  const base::WeakPtr<actions::ActionItem> action_item_;

  base::WeakPtrFactory<TranslateBubbleController> weak_ptr_factory_{this};
};

#endif  // CHROME_BROWSER_UI_VIEWS_TRANSLATE_TRANSLATE_BUBBLE_CONTROLLER_H_