Home | History | Annotate | Download | only in aura
      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 "mojo/aura/window_tree_host_mojo.h"
      6 
      7 #include <vector>
      8 
      9 #include "mojo/aura/window_tree_host_mojo_delegate.h"
     10 #include "ui/aura/env.h"
     11 #include "ui/aura/window.h"
     12 #include "ui/aura/window_event_dispatcher.h"
     13 #include "ui/events/event.h"
     14 #include "ui/events/event_constants.h"
     15 
     16 namespace mojo {
     17 namespace {
     18 
     19 const char kTreeHostsKey[] = "tree_hosts";
     20 
     21 typedef std::vector<WindowTreeHostMojo*> Managers;
     22 
     23 class TreeHosts : public base::SupportsUserData::Data {
     24  public:
     25   TreeHosts() {}
     26   virtual ~TreeHosts() {}
     27 
     28   static TreeHosts* Get() {
     29     TreeHosts* hosts = static_cast<TreeHosts*>(
     30         aura::Env::GetInstance()->GetUserData(kTreeHostsKey));
     31     if (!hosts) {
     32       hosts = new TreeHosts;
     33       aura::Env::GetInstance()->SetUserData(kTreeHostsKey, hosts);
     34     }
     35     return hosts;
     36   }
     37 
     38   void Add(WindowTreeHostMojo* manager) {
     39     managers_.push_back(manager);
     40   }
     41 
     42   void Remove(WindowTreeHostMojo* manager) {
     43     Managers::iterator i = std::find(managers_.begin(), managers_.end(),
     44                                      manager);
     45     DCHECK(i != managers_.end());
     46     managers_.erase(i);
     47   }
     48 
     49   const std::vector<WindowTreeHostMojo*> managers() const {
     50     return managers_;
     51   }
     52 
     53  private:
     54   Managers managers_;
     55 
     56   DISALLOW_COPY_AND_ASSIGN(TreeHosts);
     57 };
     58 
     59 }  // namespace
     60 
     61 ////////////////////////////////////////////////////////////////////////////////
     62 // WindowTreeHostMojo, public:
     63 
     64 WindowTreeHostMojo::WindowTreeHostMojo(View* view,
     65                                        WindowTreeHostMojoDelegate* delegate)
     66     : view_(view),
     67       bounds_(view->bounds()),
     68       delegate_(delegate) {
     69   view_->AddObserver(this);
     70   CreateCompositor(GetAcceleratedWidget());
     71 
     72   TreeHosts::Get()->Add(this);
     73 }
     74 
     75 WindowTreeHostMojo::~WindowTreeHostMojo() {
     76   view_->RemoveObserver(this);
     77   TreeHosts::Get()->Remove(this);
     78   DestroyCompositor();
     79   DestroyDispatcher();
     80 }
     81 
     82 // static
     83 WindowTreeHostMojo* WindowTreeHostMojo::ForCompositor(
     84     ui::Compositor* compositor) {
     85   const Managers& managers = TreeHosts::Get()->managers();
     86   for (size_t i = 0; i < managers.size(); ++i) {
     87     if (managers[i]->compositor() == compositor)
     88       return managers[i];
     89   }
     90   return NULL;
     91 }
     92 
     93 void WindowTreeHostMojo::SetContents(const SkBitmap& contents) {
     94   delegate_->CompositorContentsChanged(contents);
     95 }
     96 
     97 ////////////////////////////////////////////////////////////////////////////////
     98 // WindowTreeHostMojo, aura::WindowTreeHost implementation:
     99 
    100 ui::EventSource* WindowTreeHostMojo::GetEventSource() {
    101   return this;
    102 }
    103 
    104 gfx::AcceleratedWidget WindowTreeHostMojo::GetAcceleratedWidget() {
    105   return gfx::kNullAcceleratedWidget;
    106 }
    107 
    108 void WindowTreeHostMojo::Show() {
    109   window()->Show();
    110 }
    111 
    112 void WindowTreeHostMojo::Hide() {
    113 }
    114 
    115 gfx::Rect WindowTreeHostMojo::GetBounds() const {
    116   return bounds_;
    117 }
    118 
    119 void WindowTreeHostMojo::SetBounds(const gfx::Rect& bounds) {
    120   window()->SetBounds(gfx::Rect(bounds.size()));
    121 }
    122 
    123 gfx::Point WindowTreeHostMojo::GetLocationOnNativeScreen() const {
    124   return gfx::Point(0, 0);
    125 }
    126 
    127 void WindowTreeHostMojo::SetCapture() {
    128   NOTIMPLEMENTED();
    129 }
    130 
    131 void WindowTreeHostMojo::ReleaseCapture() {
    132   NOTIMPLEMENTED();
    133 }
    134 
    135 void WindowTreeHostMojo::PostNativeEvent(
    136     const base::NativeEvent& native_event) {
    137   NOTIMPLEMENTED();
    138 }
    139 
    140 void WindowTreeHostMojo::SetCursorNative(gfx::NativeCursor cursor) {
    141   NOTIMPLEMENTED();
    142 }
    143 
    144 void WindowTreeHostMojo::MoveCursorToNative(const gfx::Point& location) {
    145   NOTIMPLEMENTED();
    146 }
    147 
    148 void WindowTreeHostMojo::OnCursorVisibilityChangedNative(bool show) {
    149   NOTIMPLEMENTED();
    150 }
    151 
    152 ////////////////////////////////////////////////////////////////////////////////
    153 // WindowTreeHostMojo, ui::EventSource implementation:
    154 
    155 ui::EventProcessor* WindowTreeHostMojo::GetEventProcessor() {
    156   return dispatcher();
    157 }
    158 
    159 ////////////////////////////////////////////////////////////////////////////////
    160 // WindowTreeHostMojo, ViewObserver implementation:
    161 
    162 void WindowTreeHostMojo::OnViewBoundsChanged(
    163     View* view,
    164     const gfx::Rect& old_bounds,
    165     const gfx::Rect& new_bounds) {
    166   bounds_ = new_bounds;
    167   if (old_bounds.origin() != new_bounds.origin())
    168     OnHostMoved(bounds_.origin());
    169   if (old_bounds.size() != new_bounds.size())
    170     OnHostResized(bounds_.size());
    171 }
    172 
    173 }  // namespace mojo
    174