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.
5 #include "ui/aura/env.h"
7 #include "base/lazy_instance.h"
8 #include "base/threading/thread_local.h"
9 #include "ui/aura/env_observer.h"
10 #include "ui/aura/input_state_lookup.h"
11 #include "ui/events/event_target_iterator.h"
12 #include "ui/events/platform/platform_event_source.h"
14 #if defined(USE_OZONE)
15 #include "ui/ozone/public/ozone_platform.h"
22 // Env is thread local so that aura may be used on multiple threads.
23 base::LazyInstance
<base::ThreadLocalPointer
<Env
> >::Leaky lazy_tls_ptr
=
24 LAZY_INSTANCE_INITIALIZER
;
28 ////////////////////////////////////////////////////////////////////////////////
32 void Env::CreateInstance(bool create_event_source
) {
33 if (!lazy_tls_ptr
.Pointer()->Get())
34 (new Env())->Init(create_event_source
);
38 Env
* Env::GetInstance() {
39 Env
* env
= lazy_tls_ptr
.Pointer()->Get();
40 DCHECK(env
) << "Env::CreateInstance must be called before getting the "
46 void Env::DeleteInstance() {
47 delete lazy_tls_ptr
.Pointer()->Get();
50 void Env::AddObserver(EnvObserver
* observer
) {
51 observers_
.AddObserver(observer
);
54 void Env::RemoveObserver(EnvObserver
* observer
) {
55 observers_
.RemoveObserver(observer
);
58 bool Env::IsMouseButtonDown() const {
59 return input_state_lookup_
.get() ? input_state_lookup_
->IsMouseButtonDown() :
60 mouse_button_flags_
!= 0;
63 ////////////////////////////////////////////////////////////////////////////////
67 : mouse_button_flags_(0),
68 is_touch_down_(false),
69 input_state_lookup_(InputStateLookup::Create().Pass()),
70 context_factory_(NULL
) {
71 DCHECK(lazy_tls_ptr
.Pointer()->Get() == NULL
);
72 lazy_tls_ptr
.Pointer()->Set(this);
76 FOR_EACH_OBSERVER(EnvObserver
, observers_
, OnWillDestroyEnv());
77 DCHECK_EQ(this, lazy_tls_ptr
.Pointer()->Get());
78 lazy_tls_ptr
.Pointer()->Set(NULL
);
81 void Env::Init(bool create_event_source
) {
82 #if defined(USE_OZONE)
83 // The ozone platform can provide its own event source. So initialize the
84 // platform before creating the default event source.
85 ui::OzonePlatform::InitializeForUI();
87 if (create_event_source
&& !ui::PlatformEventSource::GetInstance())
88 event_source_
= ui::PlatformEventSource::CreateDefault();
91 void Env::NotifyWindowInitialized(Window
* window
) {
92 FOR_EACH_OBSERVER(EnvObserver
, observers_
, OnWindowInitialized(window
));
95 void Env::NotifyHostInitialized(WindowTreeHost
* host
) {
96 FOR_EACH_OBSERVER(EnvObserver
, observers_
, OnHostInitialized(host
));
99 void Env::NotifyHostActivated(WindowTreeHost
* host
) {
100 FOR_EACH_OBSERVER(EnvObserver
, observers_
, OnHostActivated(host
));
103 ////////////////////////////////////////////////////////////////////////////////
104 // Env, ui::EventTarget implementation:
106 bool Env::CanAcceptEvent(const ui::Event
& event
) {
110 ui::EventTarget
* Env::GetParentTarget() {
114 scoped_ptr
<ui::EventTargetIterator
> Env::GetChildIterator() const {
115 return scoped_ptr
<ui::EventTargetIterator
>();
118 ui::EventTargeter
* Env::GetEventTargeter() {