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 Env
* Env::GetInstanceDontCreate() {
47 return lazy_tls_ptr
.Pointer()->Get();
51 void Env::DeleteInstance() {
52 delete lazy_tls_ptr
.Pointer()->Get();
55 void Env::AddObserver(EnvObserver
* observer
) {
56 observers_
.AddObserver(observer
);
59 void Env::RemoveObserver(EnvObserver
* observer
) {
60 observers_
.RemoveObserver(observer
);
63 bool Env::IsMouseButtonDown() const {
64 return input_state_lookup_
.get() ? input_state_lookup_
->IsMouseButtonDown() :
65 mouse_button_flags_
!= 0;
68 ////////////////////////////////////////////////////////////////////////////////
72 : mouse_button_flags_(0),
73 is_touch_down_(false),
74 input_state_lookup_(InputStateLookup::Create().Pass()),
75 context_factory_(NULL
) {
76 DCHECK(lazy_tls_ptr
.Pointer()->Get() == NULL
);
77 lazy_tls_ptr
.Pointer()->Set(this);
81 FOR_EACH_OBSERVER(EnvObserver
, observers_
, OnWillDestroyEnv());
82 DCHECK_EQ(this, lazy_tls_ptr
.Pointer()->Get());
83 lazy_tls_ptr
.Pointer()->Set(NULL
);
86 void Env::Init(bool create_event_source
) {
87 #if defined(USE_OZONE)
88 // The ozone platform can provide its own event source. So initialize the
89 // platform before creating the default event source.
90 ui::OzonePlatform::InitializeForUI();
92 if (create_event_source
&& !ui::PlatformEventSource::GetInstance())
93 event_source_
= ui::PlatformEventSource::CreateDefault();
96 void Env::NotifyWindowInitialized(Window
* window
) {
97 FOR_EACH_OBSERVER(EnvObserver
, observers_
, OnWindowInitialized(window
));
100 void Env::NotifyHostInitialized(WindowTreeHost
* host
) {
101 FOR_EACH_OBSERVER(EnvObserver
, observers_
, OnHostInitialized(host
));
104 void Env::NotifyHostActivated(WindowTreeHost
* host
) {
105 FOR_EACH_OBSERVER(EnvObserver
, observers_
, OnHostActivated(host
));
108 ////////////////////////////////////////////////////////////////////////////////
109 // Env, ui::EventTarget implementation:
111 bool Env::CanAcceptEvent(const ui::Event
& event
) {
115 ui::EventTarget
* Env::GetParentTarget() {
119 scoped_ptr
<ui::EventTargetIterator
> Env::GetChildIterator() const {
123 ui::EventTargeter
* Env::GetEventTargeter() {