15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef CHROME_BROWSER_UI_AUTOFILL_POPUP_CONTROLLER_COMMON_H_
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define CHROME_BROWSER_UI_AUTOFILL_POPUP_CONTROLLER_COMMON_H_
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/public/browser/render_widget_host.h"
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/native_widget_types.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/rect.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/rect_f.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace content {
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)struct NativeWebKeyboardEvent;
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class RenderViewHost;
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class WebContents;
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace gfx {
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class Display;
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace autofill {
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Class that controls common functionality for  Autofill style popups. Can
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// determine the correct location of a popup of a desired size and can register
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// a handler to key press events.
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class PopupControllerCommon {
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PopupControllerCommon(const gfx::RectF& element_bounds,
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        gfx::NativeView container_view,
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        content::WebContents* web_contents);
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~PopupControllerCommon();
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const gfx::RectF& element_bounds() const { return element_bounds_; }
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::NativeView container_view() { return container_view_; }
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  content::WebContents* web_contents() { return web_contents_; }
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns the enclosing rectangle for |element_bounds_|.
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const gfx::Rect RoundedElementBounds() const;
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns the bounds that the popup should be placed at, given the desired
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // width and height. By default this places the popup below |element_bounds|
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // but it will be placed above if there isn't enough space.
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  gfx::Rect GetPopupBounds(int desired_width, int desired_height) const;
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Callback used to register with RenderViewHost. This can only be set once,
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // or else a callback may be registered that will not be removed
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // (crbug.com/338070). Call will crash if callback is already set.
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SetKeyPressCallback(content::RenderWidgetHost::KeyPressEventCallback);
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Register listener for key press events with the current RenderViewHost
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // associated with |web_contents_|. If callback has already been registered,
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // this has no effect.
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void RegisterKeyPressCallback();
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Remove previously registered callback, assuming that the current
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // RenderViewHost is the same as when it was originally registered. Safe to
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // call even if the callback is not currently registered.
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void RemoveKeyPressCallback();
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) protected:
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // A helper function to get the display closest to the given point (virtual
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // for testing).
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const;
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Calculates the width of the popup and the x position of it. These values
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // will stay on the screen.
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::pair<int, int> CalculatePopupXAndWidth(
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const gfx::Display& left_display,
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const gfx::Display& right_display,
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      int popup_required_width) const;
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Calculates the height of the popup and the y position of it. These values
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // will stay on the screen.
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::pair<int, int> CalculatePopupYAndHeight(
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const gfx::Display& top_display,
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const gfx::Display& bottom_display,
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      int popup_required_height) const;
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The bounds of the text element that is the focus of the popup.
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // These coordinates are in screen space.
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::RectF element_bounds_;
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Weak reference
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  gfx::NativeView container_view_;
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The WebContents in which this object should listen for keyboard events
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // while showing the popup. Can be NULL, in which case this object will not
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // listen for keyboard events.
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  content::WebContents* web_contents_;
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The RenderViewHost that this object has registered its keyboard press
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // callback with.
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  content::RenderViewHost* key_press_event_target_;
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  content::RenderWidgetHost::KeyPressEventCallback key_press_event_callback_;
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(PopupControllerCommon);
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace autofill
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // CHROME_BROWSER_UI_AUTOFILL_POPUP_CONTROLLER_COMMON_H_
106