Chromium Code Reviews| 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 | |
|
Mr4D (OOO till 08-26)
2014/10/29 23:10:07
Could you add a comment what this class actually d
oshima
2014/10/31 00:37:50
This implements WebContentsModalDialogHost and the
| |
| 22 class ModalDialogHostImpl : public web_modal::WebContentsModalDialogHost, | |
| 23 public aura::WindowObserver { | |
| 24 public: | |
| 25 // Returns a modal dialog host for |window|. It creates one and | |
| 26 // stores as owned property if it doesn't exist. | |
|
Mr4D (OOO till 08-26)
2014/10/29 23:10:07
Maybe "if it doesn't exist it creates one and stor
oshima
2014/10/31 00:37:50
Done.
| |
| 27 static ModalDialogHost* GetModalDialogHost(aura::Window* window); | |
| 28 | |
| 29 private: | |
| 30 explicit ModalDialogHostImpl(aura::Window* host_window) | |
| 31 : host_window_(host_window) { | |
| 32 host_window_->AddObserver(this); | |
| 33 } | |
| 34 ~ModalDialogHostImpl() override {} | |
| 35 | |
| 36 // web_modal::ModalDialogHost: | |
| 37 gfx::NativeView GetHostView() const override { | |
| 38 return host_window_; | |
| 39 } | |
| 40 gfx::Point GetDialogPosition(const gfx::Size& size) override { | |
| 41 gfx::Rect host_bounds = host_window_->GetBoundsInScreen(); | |
| 42 host_bounds.ClampToCenteredSize(size); | |
| 43 return host_bounds.origin(); | |
| 44 } | |
| 45 void AddObserver(web_modal::ModalDialogHostObserver* observer) override { | |
| 46 observer_list_.AddObserver(observer); | |
| 47 } | |
| 48 void RemoveObserver(web_modal::ModalDialogHostObserver* observer) override { | |
| 49 observer_list_.RemoveObserver(observer); | |
| 50 } | |
| 51 | |
| 52 // web_modal::WebContensModalDialogHost: | |
| 53 gfx::Size GetMaximumDialogSize() override { | |
| 54 return host_window_->bounds().size(); | |
| 55 } | |
| 56 | |
| 57 // aura::WindowObserver: | |
| 58 void OnWindowDestroying(aura::Window* window) override { | |
| 59 if (window != host_window_) | |
| 60 return; | |
| 61 host_window_->RemoveObserver(this); | |
| 62 FOR_EACH_OBSERVER(web_modal::ModalDialogHostObserver, | |
| 63 observer_list_, | |
| 64 OnHostDestroying()); | |
| 65 } | |
| 66 void OnWindowBoundsChanged(aura::Window* window, | |
| 67 const gfx::Rect& old_bounds, | |
| 68 const gfx::Rect& new_bounds) override { | |
| 69 if (window != host_window_) | |
| 70 return; | |
| 71 FOR_EACH_OBSERVER(web_modal::ModalDialogHostObserver, | |
| 72 observer_list_, | |
| 73 OnPositionRequiresUpdate()); | |
| 74 } | |
| 75 | |
| 76 aura::Window* host_window_; | |
| 77 ObserverList<web_modal::ModalDialogHostObserver> observer_list_; | |
| 78 | |
| 79 DISALLOW_COPY_AND_ASSIGN(ModalDialogHostImpl); | |
| 80 }; | |
| 81 | |
| 82 DEFINE_OWNED_WINDOW_PROPERTY_KEY(web_modal::ModalDialogHost, | |
| 83 kModalDialogHostKey, | |
| 84 nullptr); | |
| 85 | |
| 86 // static | |
| 87 web_modal::ModalDialogHost* ModalDialogHostImpl::GetModalDialogHost( | |
| 88 aura::Window* window) { | |
| 89 web_modal::ModalDialogHost* host = window->GetProperty(kModalDialogHostKey); | |
| 90 if (!host) { | |
| 91 host = new ModalDialogHostImpl(window); | |
| 92 window->SetProperty(kModalDialogHostKey, host); | |
| 93 } | |
| 94 return host; | |
| 95 } | |
| 96 | |
| 97 class AthenaConstrainedWindowViewsClient | |
| 98 : public ConstrainedWindowViewsClient { | |
| 99 public: | |
| 100 AthenaConstrainedWindowViewsClient() {} | |
| 101 ~AthenaConstrainedWindowViewsClient() override {} | |
| 102 | |
| 103 private: | |
| 104 // ConstrainedWindowViewsClient: | |
| 105 content::WebContents* GetEmbedderWebContents( | |
| 106 content::WebContents* initiator_web_contents) override { | |
| 107 extensions::GuestViewBase* guest_view = | |
| 108 extensions::GuestViewBase::FromWebContents(initiator_web_contents); | |
| 109 return guest_view && guest_view->embedder_web_contents() ? | |
| 110 guest_view->embedder_web_contents() : initiator_web_contents; | |
| 111 } | |
| 112 web_modal::ModalDialogHost* GetModalDialogHost( | |
| 113 gfx::NativeWindow parent) override { | |
| 114 Activity* activity = ActivityManager::Get()->GetActivityForWindow(parent); | |
| 115 if (activity) | |
| 116 return ModalDialogHostImpl::GetModalDialogHost(parent); | |
| 117 return nullptr; | |
| 118 } | |
| 119 | |
| 120 DISALLOW_COPY_AND_ASSIGN(AthenaConstrainedWindowViewsClient); | |
| 121 }; | |
| 122 | |
| 123 } // namespace | |
| 124 | |
| 125 void InstallConstrainedWindowViewsClient() { | |
| 126 SetConstrainedWindowViewsClient( | |
| 127 make_scoped_ptr(new AthenaConstrainedWindowViewsClient)); | |
| 128 } | |
| 129 | |
| 130 void UninstallConstrainedWindowViewsClient() { | |
| 131 SetConstrainedWindowViewsClient(nullptr); | |
| 132 } | |
| 133 | |
| 134 } // namespace athena | |
| OLD | NEW |