1// Copyright (c) 2012 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 UI_AURA_ENV_H_
6#define UI_AURA_ENV_H_
7
8#include "base/memory/scoped_ptr.h"
9#include "base/observer_list.h"
10#include "base/supports_user_data.h"
11#include "ui/aura/aura_export.h"
12#include "ui/events/event_handler.h"
13#include "ui/events/event_target.h"
14#include "ui/gfx/point.h"
15
16namespace ui {
17class ContextFactory;
18class PlatformEventSource;
19}
20namespace aura {
21
22namespace test {
23class EnvTestHelper;
24}
25
26class EnvObserver;
27class InputStateLookup;
28class Window;
29class WindowTreeHost;
30
31// A singleton object that tracks general state within Aura.
32class AURA_EXPORT Env : public ui::EventTarget, public base::SupportsUserData {
33 public:
34  // Creates the single Env instance (if it hasn't been created yet). If
35  // |create_event_source| is true a PlatformEventSource is created.
36  // TODO(sky): nuke |create_event_source|. Only necessary while mojo's
37  // nativeviewportservice lives in the same process as the viewmanager.
38  static void CreateInstance(bool create_event_source);
39  static Env* GetInstance();
40  static void DeleteInstance();
41
42  void AddObserver(EnvObserver* observer);
43  void RemoveObserver(EnvObserver* observer);
44
45  int mouse_button_flags() const { return mouse_button_flags_; }
46  void set_mouse_button_flags(int mouse_button_flags) {
47    mouse_button_flags_ = mouse_button_flags;
48  }
49  // Returns true if a mouse button is down. This may query the native OS,
50  // otherwise it uses |mouse_button_flags_|.
51  bool IsMouseButtonDown() const;
52
53  // Gets/sets the last mouse location seen in a mouse event in the screen
54  // coordinates.
55  const gfx::Point& last_mouse_location() const { return last_mouse_location_; }
56  void set_last_mouse_location(const gfx::Point& last_mouse_location) {
57    last_mouse_location_ = last_mouse_location;
58  }
59
60  // Whether any touch device is currently down.
61  bool is_touch_down() const { return is_touch_down_; }
62  void set_touch_down(bool value) { is_touch_down_ = value; }
63
64  void set_context_factory(ui::ContextFactory* context_factory) {
65    context_factory_ = context_factory;
66  }
67  ui::ContextFactory* context_factory() { return context_factory_; }
68
69 private:
70  friend class test::EnvTestHelper;
71  friend class Window;
72  friend class WindowTreeHost;
73
74  Env();
75  virtual ~Env();
76
77  // See description of CreateInstance() for deatils of |create_event_source|.
78  void Init(bool create_event_source);
79
80  // Called by the Window when it is initialized. Notifies observers.
81  void NotifyWindowInitialized(Window* window);
82
83  // Called by the WindowTreeHost when it is initialized. Notifies observers.
84  void NotifyHostInitialized(WindowTreeHost* host);
85
86  // Invoked by WindowTreeHost when it is activated. Notifies observers.
87  void NotifyHostActivated(WindowTreeHost* host);
88
89  // Overridden from ui::EventTarget:
90  virtual bool CanAcceptEvent(const ui::Event& event) OVERRIDE;
91  virtual ui::EventTarget* GetParentTarget() OVERRIDE;
92  virtual scoped_ptr<ui::EventTargetIterator> GetChildIterator() const OVERRIDE;
93  virtual ui::EventTargeter* GetEventTargeter() OVERRIDE;
94
95  ObserverList<EnvObserver> observers_;
96
97  int mouse_button_flags_;
98  // Location of last mouse event, in screen coordinates.
99  gfx::Point last_mouse_location_;
100  bool is_touch_down_;
101
102  scoped_ptr<InputStateLookup> input_state_lookup_;
103  scoped_ptr<ui::PlatformEventSource> event_source_;
104
105  ui::ContextFactory* context_factory_;
106
107  DISALLOW_COPY_AND_ASSIGN(Env);
108};
109
110}  // namespace aura
111
112#endif  // UI_AURA_ENV_H_
113