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"
22 // Provides the host envrionment for web modal dialogs. See
23 // web_modal::WebContentsModalDialogHost, and ModalDialogHost for more
25 class ModalDialogHostImpl
: public web_modal::WebContentsModalDialogHost
,
26 public aura::WindowObserver
{
28 // Returns a modal dialog host for |window|. If it doesn't exist it creates
29 // one and stores it as owned property.
30 static ModalDialogHost
* Get(aura::Window
* window
);
33 explicit ModalDialogHostImpl(aura::Window
* host_window
)
34 : host_window_(host_window
) {
35 host_window_
->AddObserver(this);
37 ~ModalDialogHostImpl() override
{}
39 // web_modal::ModalDialogHost:
40 gfx::NativeView
GetHostView() const override
{
43 gfx::Point
GetDialogPosition(const gfx::Size
& size
) override
{
44 gfx::Rect host_bounds
= host_window_
->GetBoundsInScreen();
45 host_bounds
.ClampToCenteredSize(size
);
46 return host_bounds
.origin();
48 void AddObserver(web_modal::ModalDialogHostObserver
* observer
) override
{
49 observer_list_
.AddObserver(observer
);
51 void RemoveObserver(web_modal::ModalDialogHostObserver
* observer
) override
{
52 observer_list_
.RemoveObserver(observer
);
55 // web_modal::WebContensModalDialogHost:
56 gfx::Size
GetMaximumDialogSize() override
{
57 return host_window_
->bounds().size();
60 // aura::WindowObserver:
61 void OnWindowDestroying(aura::Window
* window
) override
{
62 if (window
!= host_window_
)
64 host_window_
->RemoveObserver(this);
65 FOR_EACH_OBSERVER(web_modal::ModalDialogHostObserver
,
69 void OnWindowBoundsChanged(aura::Window
* window
,
70 const gfx::Rect
& old_bounds
,
71 const gfx::Rect
& new_bounds
) override
{
72 if (window
!= host_window_
)
74 FOR_EACH_OBSERVER(web_modal::ModalDialogHostObserver
,
76 OnPositionRequiresUpdate());
79 aura::Window
* host_window_
;
80 ObserverList
<web_modal::ModalDialogHostObserver
> observer_list_
;
82 DISALLOW_COPY_AND_ASSIGN(ModalDialogHostImpl
);
85 // A window property key to store the modal dialog host for
86 // dialogs created with the window as its parent.
87 DEFINE_OWNED_WINDOW_PROPERTY_KEY(web_modal::ModalDialogHost
,
92 web_modal::ModalDialogHost
* ModalDialogHostImpl::Get(
93 aura::Window
* window
) {
94 web_modal::ModalDialogHost
* host
= window
->GetProperty(kModalDialogHostKey
);
96 host
= new ModalDialogHostImpl(window
);
97 window
->SetProperty(kModalDialogHostKey
, host
);
102 class AthenaConstrainedWindowViewsClient
103 : public constrained_window::ConstrainedWindowViewsClient
{
105 AthenaConstrainedWindowViewsClient() {}
106 ~AthenaConstrainedWindowViewsClient() override
{}
109 // ConstrainedWindowViewsClient:
110 content::WebContents
* GetEmbedderWebContents(
111 content::WebContents
* initiator_web_contents
) override
{
112 extensions::GuestViewBase
* guest_view
=
113 extensions::GuestViewBase::FromWebContents(initiator_web_contents
);
114 return guest_view
&& guest_view
->embedder_web_contents() ?
115 guest_view
->embedder_web_contents() : initiator_web_contents
;
117 web_modal::ModalDialogHost
* GetModalDialogHost(
118 gfx::NativeWindow parent
) override
{
119 Activity
* activity
= ActivityManager::Get()->GetActivityForWindow(parent
);
121 return ModalDialogHostImpl::Get(parent
);
125 DISALLOW_COPY_AND_ASSIGN(AthenaConstrainedWindowViewsClient
);
130 void InstallConstrainedWindowViewsClient() {
131 constrained_window::SetConstrainedWindowViewsClient(
132 make_scoped_ptr(new AthenaConstrainedWindowViewsClient
));
135 void UninstallConstrainedWindowViewsClient() {
136 constrained_window::SetConstrainedWindowViewsClient(nullptr);
139 } // namespace athena