| OLD | NEW |
| (Empty) |
| 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 #include "athena/extensions/athena_constrained_window_views_client.h" | |
| 6 | |
| 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" | |
| 18 | |
| 19 DECLARE_WINDOW_PROPERTY_TYPE(web_modal::ModalDialogHost*); | |
| 20 | |
| 21 namespace athena { | |
| 22 namespace { | |
| 23 | |
| 24 // Provides the host environment for web modal dialogs. See | |
| 25 // web_modal::WebContentsModalDialogHost, and ModalDialogHost for more | |
| 26 // details. | |
| 27 class ModalDialogHostImpl : public web_modal::WebContentsModalDialogHost, | |
| 28 public aura::WindowObserver { | |
| 29 public: | |
| 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); | |
| 33 | |
| 34 private: | |
| 35 explicit ModalDialogHostImpl(aura::Window* host_window) | |
| 36 : host_window_(host_window) { | |
| 37 host_window_->AddObserver(this); | |
| 38 } | |
| 39 ~ModalDialogHostImpl() override {} | |
| 40 | |
| 41 // web_modal::ModalDialogHost: | |
| 42 gfx::NativeView GetHostView() const override { | |
| 43 return host_window_; | |
| 44 } | |
| 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(); | |
| 49 } | |
| 50 void AddObserver(web_modal::ModalDialogHostObserver* observer) override { | |
| 51 observer_list_.AddObserver(observer); | |
| 52 } | |
| 53 void RemoveObserver(web_modal::ModalDialogHostObserver* observer) override { | |
| 54 observer_list_.RemoveObserver(observer); | |
| 55 } | |
| 56 | |
| 57 // web_modal::WebContensModalDialogHost: | |
| 58 gfx::Size GetMaximumDialogSize() override { | |
| 59 return host_window_->bounds().size(); | |
| 60 } | |
| 61 | |
| 62 // aura::WindowObserver: | |
| 63 void OnWindowDestroying(aura::Window* window) override { | |
| 64 if (window != host_window_) | |
| 65 return; | |
| 66 host_window_->RemoveObserver(this); | |
| 67 FOR_EACH_OBSERVER(web_modal::ModalDialogHostObserver, | |
| 68 observer_list_, | |
| 69 OnHostDestroying()); | |
| 70 } | |
| 71 void OnWindowBoundsChanged(aura::Window* window, | |
| 72 const gfx::Rect& old_bounds, | |
| 73 const gfx::Rect& new_bounds) override { | |
| 74 if (window != host_window_) | |
| 75 return; | |
| 76 FOR_EACH_OBSERVER(web_modal::ModalDialogHostObserver, | |
| 77 observer_list_, | |
| 78 OnPositionRequiresUpdate()); | |
| 79 } | |
| 80 | |
| 81 aura::Window* host_window_; | |
| 82 ObserverList<web_modal::ModalDialogHostObserver> observer_list_; | |
| 83 | |
| 84 DISALLOW_COPY_AND_ASSIGN(ModalDialogHostImpl); | |
| 85 }; | |
| 86 | |
| 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, | |
| 90 kModalDialogHostKey, | |
| 91 nullptr); | |
| 92 | |
| 93 // static | |
| 94 web_modal::ModalDialogHost* ModalDialogHostImpl::Get( | |
| 95 aura::Window* window) { | |
| 96 web_modal::ModalDialogHost* host = window->GetProperty(kModalDialogHostKey); | |
| 97 if (!host) { | |
| 98 host = new ModalDialogHostImpl(window); | |
| 99 window->SetProperty(kModalDialogHostKey, host); | |
| 100 } | |
| 101 return host; | |
| 102 } | |
| 103 | |
| 104 class AthenaConstrainedWindowViewsClient | |
| 105 : public constrained_window::ConstrainedWindowViewsClient { | |
| 106 public: | |
| 107 AthenaConstrainedWindowViewsClient() {} | |
| 108 ~AthenaConstrainedWindowViewsClient() override {} | |
| 109 | |
| 110 private: | |
| 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; | |
| 118 } | |
| 119 web_modal::ModalDialogHost* GetModalDialogHost( | |
| 120 gfx::NativeWindow parent) override { | |
| 121 Activity* activity = ActivityManager::Get()->GetActivityForWindow(parent); | |
| 122 if (activity) | |
| 123 return ModalDialogHostImpl::Get(parent); | |
| 124 return nullptr; | |
| 125 } | |
| 126 gfx::NativeView GetDialogHostView(gfx::NativeWindow parent) override { | |
| 127 return parent; | |
| 128 } | |
| 129 | |
| 130 DISALLOW_COPY_AND_ASSIGN(AthenaConstrainedWindowViewsClient); | |
| 131 }; | |
| 132 | |
| 133 } // namespace | |
| 134 | |
| 135 void InstallConstrainedWindowViewsClient() { | |
| 136 constrained_window::SetConstrainedWindowViewsClient( | |
| 137 make_scoped_ptr(new AthenaConstrainedWindowViewsClient)); | |
| 138 } | |
| 139 | |
| 140 void UninstallConstrainedWindowViewsClient() { | |
| 141 constrained_window::SetConstrainedWindowViewsClient(nullptr); | |
| 142 } | |
| 143 | |
| 144 } // namespace athena | |
| OLD | NEW |