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.
5 #include "athena/extensions/athena_constrained_window_views_client.h"
7 #include "athena/activity/public/activity.h"
8 #include "athena/activity/public/activity_manager.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/observer_list.h"
11 #include "components/constrained_window/constrained_window_views.h"
12 #include "components/constrained_window/constrained_window_views_client.h"
13 #include "components/web_modal/web_contents_modal_dialog_host.h"
14 #include "extensions/browser/guest_view/guest_view_base.h"
15 #include "ui/aura/window.h"
16 #include "ui/aura/window_observer.h"
17 #include "ui/aura/window_property.h"
19 DECLARE_WINDOW_PROPERTY_TYPE(web_modal::ModalDialogHost
*);
24 // Provides the host environment for web modal dialogs. See
25 // web_modal::WebContentsModalDialogHost, and ModalDialogHost for more
27 class ModalDialogHostImpl
: public web_modal::WebContentsModalDialogHost
,
28 public aura::WindowObserver
{
30 // Returns a modal dialog host for |window|. If it doesn't exist it creates
31 // one and stores it as owned property.
32 static ModalDialogHost
* Get(aura::Window
* window
);
35 explicit ModalDialogHostImpl(aura::Window
* host_window
)
36 : host_window_(host_window
) {
37 host_window_
->AddObserver(this);
39 ~ModalDialogHostImpl() override
{}
41 // web_modal::ModalDialogHost:
42 gfx::NativeView
GetHostView() const override
{
45 gfx::Point
GetDialogPosition(const gfx::Size
& size
) override
{
46 gfx::Rect host_bounds
= host_window_
->GetBoundsInScreen();
47 host_bounds
.ClampToCenteredSize(size
);
48 return host_bounds
.origin();
50 void AddObserver(web_modal::ModalDialogHostObserver
* observer
) override
{
51 observer_list_
.AddObserver(observer
);
53 void RemoveObserver(web_modal::ModalDialogHostObserver
* observer
) override
{
54 observer_list_
.RemoveObserver(observer
);
57 // web_modal::WebContensModalDialogHost:
58 gfx::Size
GetMaximumDialogSize() override
{
59 return host_window_
->bounds().size();
62 // aura::WindowObserver:
63 void OnWindowDestroying(aura::Window
* window
) override
{
64 if (window
!= host_window_
)
66 host_window_
->RemoveObserver(this);
67 FOR_EACH_OBSERVER(web_modal::ModalDialogHostObserver
,
71 void OnWindowBoundsChanged(aura::Window
* window
,
72 const gfx::Rect
& old_bounds
,
73 const gfx::Rect
& new_bounds
) override
{
74 if (window
!= host_window_
)
76 FOR_EACH_OBSERVER(web_modal::ModalDialogHostObserver
,
78 OnPositionRequiresUpdate());
81 aura::Window
* host_window_
;
82 ObserverList
<web_modal::ModalDialogHostObserver
> observer_list_
;
84 DISALLOW_COPY_AND_ASSIGN(ModalDialogHostImpl
);
87 // A window property key to store the modal dialog host for
88 // dialogs created with the window as its parent.
89 DEFINE_OWNED_WINDOW_PROPERTY_KEY(web_modal::ModalDialogHost
,
94 web_modal::ModalDialogHost
* ModalDialogHostImpl::Get(
95 aura::Window
* window
) {
96 web_modal::ModalDialogHost
* host
= window
->GetProperty(kModalDialogHostKey
);
98 host
= new ModalDialogHostImpl(window
);
99 window
->SetProperty(kModalDialogHostKey
, host
);
104 class AthenaConstrainedWindowViewsClient
105 : public constrained_window::ConstrainedWindowViewsClient
{
107 AthenaConstrainedWindowViewsClient() {}
108 ~AthenaConstrainedWindowViewsClient() override
{}
111 // ConstrainedWindowViewsClient:
112 content::WebContents
* GetEmbedderWebContents(
113 content::WebContents
* initiator_web_contents
) override
{
114 extensions::GuestViewBase
* guest_view
=
115 extensions::GuestViewBase::FromWebContents(initiator_web_contents
);
116 return guest_view
&& guest_view
->embedder_web_contents() ?
117 guest_view
->embedder_web_contents() : initiator_web_contents
;
119 web_modal::ModalDialogHost
* GetModalDialogHost(
120 gfx::NativeWindow parent
) override
{
121 Activity
* activity
= ActivityManager::Get()->GetActivityForWindow(parent
);
123 return ModalDialogHostImpl::Get(parent
);
126 gfx::NativeView
GetDialogHostView(gfx::NativeWindow parent
) override
{
130 DISALLOW_COPY_AND_ASSIGN(AthenaConstrainedWindowViewsClient
);
135 void InstallConstrainedWindowViewsClient() {
136 constrained_window::SetConstrainedWindowViewsClient(
137 make_scoped_ptr(new AthenaConstrainedWindowViewsClient
));
140 void UninstallConstrainedWindowViewsClient() {
141 constrained_window::SetConstrainedWindowViewsClient(nullptr);
144 } // namespace athena