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