Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / autofill / popup_controller_common.h
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.
4
5 #ifndef CHROME_BROWSER_UI_AUTOFILL_POPUP_CONTROLLER_COMMON_H_
6 #define CHROME_BROWSER_UI_AUTOFILL_POPUP_CONTROLLER_COMMON_H_
7
8 #include "content/public/browser/render_widget_host.h"
9 #include "ui/gfx/native_widget_types.h"
10 #include "ui/gfx/rect.h"
11 #include "ui/gfx/rect_f.h"
12
13 namespace content {
14 struct NativeWebKeyboardEvent;
15 class RenderViewHost;
16 class WebContents;
17 }
18
19 namespace gfx {
20 class Display;
21 }
22
23 namespace autofill {
24
25 // Class that controls common functionality for  Autofill style popups. Can
26 // determine the correct location of a popup of a desired size and can register
27 // a handler to key press events.
28 class PopupControllerCommon {
29  public:
30   PopupControllerCommon(const gfx::RectF& element_bounds,
31                         gfx::NativeView container_view,
32                         content::WebContents* web_contents);
33   virtual ~PopupControllerCommon();
34
35   const gfx::RectF& element_bounds() const { return element_bounds_; }
36   gfx::NativeView container_view() { return container_view_; }
37   content::WebContents* web_contents() { return web_contents_; }
38
39   // Returns the enclosing rectangle for |element_bounds_|.
40   const gfx::Rect RoundedElementBounds() const;
41
42   // Returns the bounds that the popup should be placed at, given the desired
43   // width and height. By default this places the popup below |element_bounds|
44   // but it will be placed above if there isn't enough space.
45   gfx::Rect GetPopupBounds(int desired_width, int desired_height) const;
46
47   // Callback used to register with RenderViewHost. This can only be set once,
48   // or else a callback may be registered that will not be removed
49   // (crbug.com/338070). Call will crash if callback is already set.
50   void SetKeyPressCallback(content::RenderWidgetHost::KeyPressEventCallback);
51
52   // Register listener for key press events with the current RenderViewHost
53   // associated with |web_contents_|. If callback has already been registered,
54   // this has no effect.
55   void RegisterKeyPressCallback();
56
57   // Remove previously registered callback, assuming that the current
58   // RenderViewHost is the same as when it was originally registered. Safe to
59   // call even if the callback is not currently registered.
60   void RemoveKeyPressCallback();
61
62  protected:
63   // A helper function to get the display closest to the given point (virtual
64   // for testing).
65   virtual gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const;
66
67  private:
68   // Calculates the width of the popup and the x position of it. These values
69   // will stay on the screen.
70   std::pair<int, int> CalculatePopupXAndWidth(
71       const gfx::Display& left_display,
72       const gfx::Display& right_display,
73       int popup_required_width) const;
74
75   // Calculates the height of the popup and the y position of it. These values
76   // will stay on the screen.
77   std::pair<int, int> CalculatePopupYAndHeight(
78       const gfx::Display& top_display,
79       const gfx::Display& bottom_display,
80       int popup_required_height) const;
81
82   // The bounds of the text element that is the focus of the popup.
83   // These coordinates are in screen space.
84   gfx::RectF element_bounds_;
85
86   // Weak reference
87   gfx::NativeView container_view_;
88
89   // The WebContents in which this object should listen for keyboard events
90   // while showing the popup. Can be NULL, in which case this object will not
91   // listen for keyboard events.
92   content::WebContents* web_contents_;
93
94   // The RenderViewHost that this object has registered its keyboard press
95   // callback with.
96   content::RenderViewHost* key_press_event_target_;
97
98   content::RenderWidgetHost::KeyPressEventCallback key_press_event_callback_;
99
100   DISALLOW_COPY_AND_ASSIGN(PopupControllerCommon);
101 };
102
103 }  // namespace autofill
104
105 #endif  // CHROME_BROWSER_UI_AUTOFILL_POPUP_CONTROLLER_COMMON_H_