Disable TabDragController tests that fail with a real compositor.
[chromium-blink-merge.git] / chrome / browser / ui / gtk / web_contents_modal_dialog_manager_gtk.cc
blob5eba5a969da012fe9db2092d8429fea2d697a640
1 // Copyright (c) 2013 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.
5 #include "chrome/browser/ui/gtk/gtk_util.h"
6 #include "chrome/browser/ui/gtk/tab_contents/chrome_web_contents_view_delegate_gtk.h"
7 #include "components/web_modal/native_web_contents_modal_dialog_manager.h"
8 #include "components/web_modal/web_contents_modal_dialog_manager.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "ui/base/gtk/focus_store_gtk.h"
12 using web_modal::NativeWebContentsModalDialog;
13 using web_modal::NativeWebContentsModalDialogManagerDelegate;
15 namespace {
17 // Web contents modal dialog manager implementation for the GTK port. Unlike the
18 // Win32 system, ConstrainedWindowGtk doesn't draw draggable fake windows and
19 // instead just centers the dialog. It is thus an order of magnitude simpler.
20 class NativeWebContentsModalDialogManagerGtk
21 : public web_modal::NativeWebContentsModalDialogManager {
22 public:
23 NativeWebContentsModalDialogManagerGtk(
24 NativeWebContentsModalDialogManagerDelegate* native_delegate)
25 : native_delegate_(native_delegate),
26 shown_widget_(NULL) {
29 virtual ~NativeWebContentsModalDialogManagerGtk() {
32 // NativeWebContentsModalDialogManager overrides
33 virtual void ManageDialog(NativeWebContentsModalDialog dialog) OVERRIDE {
34 DCHECK(g_object_is_floating(GetGtkWidget(dialog)));
35 g_object_ref_sink(GetGtkWidget(dialog));
37 g_signal_connect(GetGtkWidget(dialog), "hierarchy-changed",
38 G_CALLBACK(OnHierarchyChangedThunk), this);
39 g_signal_connect(GetGtkWidget(dialog),
40 "destroy",
41 G_CALLBACK(OnDestroyThunk),
42 this);
45 virtual void ShowDialog(NativeWebContentsModalDialog dialog) OVERRIDE {
46 GtkWidget* widget = GetGtkWidget(dialog);
48 // Any previously-shown widget should be destroyed before showing a new
49 // widget.
50 DCHECK(shown_widget_ == widget || shown_widget_ == NULL);
51 gtk_widget_show_all(widget);
53 if (!shown_widget_) {
54 // We collaborate with WebContentsView and stick ourselves in the
55 // WebContentsView's floating container.
56 ContainingView()->AttachWebContentsModalDialog(widget);
59 shown_widget_ = widget;
62 virtual void HideDialog(NativeWebContentsModalDialog dialog) OVERRIDE {
63 gtk_widget_hide(GetGtkWidget(dialog));
66 virtual void CloseDialog(NativeWebContentsModalDialog dialog) OVERRIDE {
67 gtk_widget_destroy(GetGtkWidget(dialog));
70 virtual void FocusDialog(NativeWebContentsModalDialog dialog) OVERRIDE {
71 GtkWidget* focus_widget =
72 reinterpret_cast<GtkWidget*>(
73 g_object_get_data(G_OBJECT(GetGtkWidget(dialog)), "focus_widget"));
75 if (!focus_widget)
76 return;
78 // The user may have focused another tab. In this case do not grab focus
79 // until this tab is refocused.
80 if (gtk_util::IsWidgetAncestryVisible(focus_widget))
81 gtk_widget_grab_focus(focus_widget);
82 else
83 ContainingView()->focus_store()->SetWidget(focus_widget);
86 virtual void PulseDialog(NativeWebContentsModalDialog dialog) OVERRIDE {
89 virtual void HostChanged(
90 web_modal::WebContentsModalDialogHost* new_host) OVERRIDE {
93 private:
94 typedef ChromeWebContentsViewDelegateGtk TabContentsViewType;
96 GtkWidget* GetGtkWidget(NativeWebContentsModalDialog dialog) {
97 return GTK_WIDGET(dialog);
100 // Returns the View that we collaborate with to position ourselves.
101 TabContentsViewType* ContainingView() const {
102 // WebContents may be destroyed already on tab shutdown.
103 content::WebContents* web_contents = native_delegate_->GetWebContents();
104 return web_contents ?
105 ChromeWebContentsViewDelegateGtk::GetFor(web_contents) : NULL;
108 CHROMEGTK_CALLBACK_1(NativeWebContentsModalDialogManagerGtk,
109 void,
110 OnHierarchyChanged,
111 GtkWidget*);
112 CHROMEGTK_CALLBACK_0(NativeWebContentsModalDialogManagerGtk, void, OnDestroy);
114 NativeWebContentsModalDialogManagerDelegate* native_delegate_;
116 // The widget currently being shown.
117 GtkWidget* shown_widget_;
119 DISALLOW_COPY_AND_ASSIGN(NativeWebContentsModalDialogManagerGtk);
122 void NativeWebContentsModalDialogManagerGtk::OnHierarchyChanged(
123 GtkWidget* sender,
124 GtkWidget* previous_toplevel) {
125 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
127 if (!gtk_widget_is_toplevel(gtk_widget_get_toplevel(sender)))
128 return;
130 FocusDialog(sender);
133 void NativeWebContentsModalDialogManagerGtk::OnDestroy(
134 GtkWidget* sender) {
135 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
137 if (shown_widget_ == sender) {
138 // The containing view may already be destroyed on tab shutdown.
139 if (ContainingView())
140 ContainingView()->RemoveWebContentsModalDialog(sender);
142 shown_widget_ = NULL;
145 native_delegate_->WillClose(sender);
147 g_object_unref(sender);
150 } // namespace
152 namespace web_modal {
154 NativeWebContentsModalDialogManager*
155 WebContentsModalDialogManager::CreateNativeManager(
156 NativeWebContentsModalDialogManagerDelegate* native_delegate) {
157 return new NativeWebContentsModalDialogManagerGtk(native_delegate);
160 } // namespace web_modal