| OLD | NEW |
| (Empty) |
| 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 #include "ui/aura/env.h" | |
| 6 | |
| 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" | |
| 13 | |
| 14 #if defined(USE_OZONE) | |
| 15 #include "ui/ozone/public/ozone_platform.h" | |
| 16 #endif | |
| 17 | |
| 18 namespace aura { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 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; | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 //////////////////////////////////////////////////////////////////////////////// | |
| 29 // Env, public: | |
| 30 | |
| 31 // static | |
| 32 void Env::CreateInstance(bool create_event_source) { | |
| 33 if (!lazy_tls_ptr.Pointer()->Get()) | |
| 34 (new Env())->Init(create_event_source); | |
| 35 } | |
| 36 | |
| 37 // static | |
| 38 Env* Env::GetInstance() { | |
| 39 Env* env = lazy_tls_ptr.Pointer()->Get(); | |
| 40 DCHECK(env) << "Env::CreateInstance must be called before getting the " | |
| 41 "instance of Env."; | |
| 42 return env; | |
| 43 } | |
| 44 | |
| 45 // static | |
| 46 void Env::DeleteInstance() { | |
| 47 delete lazy_tls_ptr.Pointer()->Get(); | |
| 48 } | |
| 49 | |
| 50 void Env::AddObserver(EnvObserver* observer) { | |
| 51 observers_.AddObserver(observer); | |
| 52 } | |
| 53 | |
| 54 void Env::RemoveObserver(EnvObserver* observer) { | |
| 55 observers_.RemoveObserver(observer); | |
| 56 } | |
| 57 | |
| 58 bool Env::IsMouseButtonDown() const { | |
| 59 return input_state_lookup_.get() ? input_state_lookup_->IsMouseButtonDown() : | |
| 60 mouse_button_flags_ != 0; | |
| 61 } | |
| 62 | |
| 63 //////////////////////////////////////////////////////////////////////////////// | |
| 64 // Env, private: | |
| 65 | |
| 66 Env::Env() | |
| 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); | |
| 73 } | |
| 74 | |
| 75 Env::~Env() { | |
| 76 FOR_EACH_OBSERVER(EnvObserver, observers_, OnWillDestroyEnv()); | |
| 77 DCHECK_EQ(this, lazy_tls_ptr.Pointer()->Get()); | |
| 78 lazy_tls_ptr.Pointer()->Set(NULL); | |
| 79 } | |
| 80 | |
| 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(); | |
| 86 #endif | |
| 87 if (create_event_source && !ui::PlatformEventSource::GetInstance()) | |
| 88 event_source_ = ui::PlatformEventSource::CreateDefault(); | |
| 89 } | |
| 90 | |
| 91 void Env::NotifyWindowInitialized(Window* window) { | |
| 92 FOR_EACH_OBSERVER(EnvObserver, observers_, OnWindowInitialized(window)); | |
| 93 } | |
| 94 | |
| 95 void Env::NotifyHostInitialized(WindowTreeHost* host) { | |
| 96 FOR_EACH_OBSERVER(EnvObserver, observers_, OnHostInitialized(host)); | |
| 97 } | |
| 98 | |
| 99 void Env::NotifyHostActivated(WindowTreeHost* host) { | |
| 100 FOR_EACH_OBSERVER(EnvObserver, observers_, OnHostActivated(host)); | |
| 101 } | |
| 102 | |
| 103 //////////////////////////////////////////////////////////////////////////////// | |
| 104 // Env, ui::EventTarget implementation: | |
| 105 | |
| 106 bool Env::CanAcceptEvent(const ui::Event& event) { | |
| 107 return true; | |
| 108 } | |
| 109 | |
| 110 ui::EventTarget* Env::GetParentTarget() { | |
| 111 return NULL; | |
| 112 } | |
| 113 | |
| 114 scoped_ptr<ui::EventTargetIterator> Env::GetChildIterator() { | |
| 115 return scoped_ptr<ui::EventTargetIterator>(); | |
| 116 } | |
| 117 | |
| 118 ui::EventTargeter* Env::GetEventTargeter() { | |
| 119 NOTREACHED(); | |
| 120 return NULL; | |
| 121 } | |
| 122 | |
| 123 } // namespace aura | |
| OLD | NEW |