1// Copyright 2013 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#ifndef ASH_SHELF_SHELF_WINDOW_WATCHER_H_
6#define ASH_SHELF_SHELF_WINDOW_WATCHER_H_
7
8#include "ash/shelf/scoped_observer_with_duplicated_sources.h"
9#include "base/basictypes.h"
10#include "base/compiler_specific.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/scoped_observer.h"
13#include "ui/aura/window_observer.h"
14#include "ui/gfx/display_observer.h"
15#include "ui/wm/public/activation_change_observer.h"
16
17namespace aura {
18
19class Window;
20
21namespace client {
22class ActivationClient;
23}
24
25}  // namespace aura
26
27namespace ash {
28
29class ShelfModel;
30class ShelfItemDelegateManager;
31
32// ShelfWindowWatcher creates and handles a ShelfItem for windows that have
33// a ShelfItemDetails property in the default container.
34class ShelfWindowWatcher : public aura::client::ActivationChangeObserver,
35                           public aura::WindowObserver,
36                           public gfx::DisplayObserver {
37 public:
38  ShelfWindowWatcher(ShelfModel* model,
39                     ShelfItemDelegateManager* item_delegate_manager);
40  virtual ~ShelfWindowWatcher();
41
42 private:
43  class RootWindowObserver : public aura::WindowObserver {
44   public:
45    explicit RootWindowObserver(ShelfWindowWatcher* window_watcher);
46    virtual ~RootWindowObserver();
47
48   private:
49    // aura::WindowObserver overrides:
50    virtual void OnWindowDestroying(aura::Window* window) OVERRIDE;
51
52    // Owned by Shell.
53    ShelfWindowWatcher* window_watcher_;
54
55    DISALLOW_COPY_AND_ASSIGN(RootWindowObserver);
56  };
57
58  // Used to track windows that are removed. See description of
59  // ShelfWindowWatcher::StartObservingRemovedWindow() for more details.
60  class RemovedWindowObserver : public aura::WindowObserver {
61   public:
62    explicit RemovedWindowObserver(ShelfWindowWatcher* window_watcher);
63    virtual ~RemovedWindowObserver();
64
65   private:
66    // aura::WindowObserver overrides:
67    virtual void OnWindowParentChanged(aura::Window* window,
68                                       aura::Window* parent) OVERRIDE;
69    virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE;
70
71    // Owned by Shell.
72    ShelfWindowWatcher* window_watcher_;
73
74    DISALLOW_COPY_AND_ASSIGN(RemovedWindowObserver);
75  };
76
77  // Creates a ShelfItem for |window| that has ShelfItemDetails.
78  void AddShelfItem(aura::Window* window);
79
80  // Removes a ShelfItem for |window|.
81  void RemoveShelfItem(aura::Window* window);
82
83  // Adds observer to default container and ActivationClient of |root_window|.
84  void OnRootWindowAdded(aura::Window* root_window);
85
86  // Removes observer from ActivationClient of |root_window|.
87  void OnRootWindowRemoved(aura::Window* root_window);
88
89  // Updates the status of ShelfItem for |window|.
90  void UpdateShelfItemStatus(aura::Window* window, bool is_active);
91
92  // Returns the index of ShelfItem associated with |window|.
93  int GetShelfItemIndexForWindow(aura::Window* window) const;
94
95  // Used when a window is removed. During the dragging a window may be removed
96  // and when the drag completes added back. When this happens we don't want to
97  // remove the shelf item. StartObservingRemovedWindow, if necessary, attaches
98  // an observer. When done, FinishObservingRemovedWindow() is invoked.
99  void StartObservingRemovedWindow(aura::Window* window);
100
101  // Stop observing |window| by RemovedWindowObserver and remove an item
102  // associated with |window|.
103  void FinishObservingRemovedWindow(aura::Window* window);
104
105  // aura::client::ActivationChangeObserver overrides:
106  virtual void OnWindowActivated(aura::Window* gained_active,
107                                 aura::Window* lost_active) OVERRIDE;
108
109  // aura::WindowObserver overrides:
110  virtual void OnWindowAdded(aura::Window* window) OVERRIDE;
111  virtual void OnWillRemoveWindow(aura::Window* window) OVERRIDE;
112  virtual void OnWindowDestroying(aura::Window* window) OVERRIDE;
113  virtual void OnWindowPropertyChanged(aura::Window* window,
114                                       const void* key,
115                                       intptr_t old) OVERRIDE;
116
117  // gfx::DisplayObserver overrides:
118  virtual void OnDisplayAdded(const gfx::Display& display) OVERRIDE;
119  virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE;
120  virtual void OnDisplayMetricsChanged(const gfx::Display& display,
121                                       uint32_t metrics) OVERRIDE;
122
123  // Owned by Shell.
124  ShelfModel* model_;
125  ShelfItemDelegateManager* item_delegate_manager_;
126
127  RootWindowObserver root_window_observer_;
128
129  RemovedWindowObserver removed_window_observer_;
130
131  // Holds all observed windows.
132  ScopedObserver<aura::Window, aura::WindowObserver> observed_windows_;
133
134  // Holds all observed root windows.
135  ScopedObserver<aura::Window, aura::WindowObserver> observed_root_windows_;
136
137  // Holds removed windows that has an item from default container.
138  ScopedObserver<aura::Window, aura::WindowObserver> observed_removed_windows_;
139
140  // Holds all observed activation clients.
141  ScopedObserverWithDuplicatedSources<aura::client::ActivationClient,
142      aura::client::ActivationChangeObserver> observed_activation_clients_;
143
144  DISALLOW_COPY_AND_ASSIGN(ShelfWindowWatcher);
145};
146
147}  // namespace ash
148
149#endif  // ASH_SHELF_SHELF_WINDOW_WATCHER_H_
150