Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(216)

Side by Side Diff: ui/aura/env.cc

Issue 1293013002: aura: Require explicit ownership of the Env instance. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/aura/env.h ('k') | ui/aura/test/aura_test_helper.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/aura/env.h" 5 #include "ui/aura/env.h"
6 6
7 #include "base/lazy_instance.h" 7 #include "base/lazy_instance.h"
8 #include "base/threading/thread_local.h" 8 #include "base/threading/thread_local.h"
9 #include "ui/aura/env_observer.h" 9 #include "ui/aura/env_observer.h"
10 #include "ui/aura/input_state_lookup.h" 10 #include "ui/aura/input_state_lookup.h"
(...skipping 10 matching lines...) Expand all
21 21
22 // Env is thread local so that aura may be used on multiple threads. 22 // Env is thread local so that aura may be used on multiple threads.
23 base::LazyInstance<base::ThreadLocalPointer<Env> >::Leaky lazy_tls_ptr = 23 base::LazyInstance<base::ThreadLocalPointer<Env> >::Leaky lazy_tls_ptr =
24 LAZY_INSTANCE_INITIALIZER; 24 LAZY_INSTANCE_INITIALIZER;
25 25
26 } // namespace 26 } // namespace
27 27
28 //////////////////////////////////////////////////////////////////////////////// 28 ////////////////////////////////////////////////////////////////////////////////
29 // Env, public: 29 // Env, public:
30 30
31 // static 31 Env::~Env() {
32 void Env::CreateInstance(bool create_event_source) { 32 FOR_EACH_OBSERVER(EnvObserver, observers_, OnWillDestroyEnv());
33 if (!lazy_tls_ptr.Pointer()->Get()) 33 DCHECK_EQ(this, lazy_tls_ptr.Pointer()->Get());
34 (new Env())->Init(create_event_source); 34 lazy_tls_ptr.Pointer()->Set(NULL);
35 } 35 }
36 36
37 // static 37 // static
38 scoped_ptr<Env> Env::CreateInstance() {
39 DCHECK(!lazy_tls_ptr.Pointer()->Get());
40 scoped_ptr<Env> env(new Env());
41 env->Init();
42 return env.Pass();
43 }
44
45 // static
38 Env* Env::GetInstance() { 46 Env* Env::GetInstance() {
39 Env* env = lazy_tls_ptr.Pointer()->Get(); 47 Env* env = lazy_tls_ptr.Pointer()->Get();
40 DCHECK(env) << "Env::CreateInstance must be called before getting the " 48 DCHECK(env) << "Env::CreateInstance must be called before getting the "
41 "instance of Env."; 49 "instance of Env.";
42 return env; 50 return env;
43 } 51 }
44 52
45 // static 53 // static
46 Env* Env::GetInstanceDontCreate() { 54 Env* Env::GetInstanceDontCreate() {
47 return lazy_tls_ptr.Pointer()->Get(); 55 return lazy_tls_ptr.Pointer()->Get();
48 } 56 }
49 57
50 // static
51 void Env::DeleteInstance() {
52 delete lazy_tls_ptr.Pointer()->Get();
53 }
54
55 void Env::AddObserver(EnvObserver* observer) { 58 void Env::AddObserver(EnvObserver* observer) {
56 observers_.AddObserver(observer); 59 observers_.AddObserver(observer);
57 } 60 }
58 61
59 void Env::RemoveObserver(EnvObserver* observer) { 62 void Env::RemoveObserver(EnvObserver* observer) {
60 observers_.RemoveObserver(observer); 63 observers_.RemoveObserver(observer);
61 } 64 }
62 65
63 bool Env::IsMouseButtonDown() const { 66 bool Env::IsMouseButtonDown() const {
64 return input_state_lookup_.get() ? input_state_lookup_->IsMouseButtonDown() : 67 return input_state_lookup_.get() ? input_state_lookup_->IsMouseButtonDown() :
65 mouse_button_flags_ != 0; 68 mouse_button_flags_ != 0;
66 } 69 }
67 70
68 //////////////////////////////////////////////////////////////////////////////// 71 ////////////////////////////////////////////////////////////////////////////////
69 // Env, private: 72 // Env, private:
70 73
71 Env::Env() 74 Env::Env()
72 : mouse_button_flags_(0), 75 : mouse_button_flags_(0),
73 is_touch_down_(false), 76 is_touch_down_(false),
74 input_state_lookup_(InputStateLookup::Create().Pass()), 77 input_state_lookup_(InputStateLookup::Create().Pass()),
75 context_factory_(NULL) { 78 context_factory_(NULL) {
76 DCHECK(lazy_tls_ptr.Pointer()->Get() == NULL); 79 DCHECK(lazy_tls_ptr.Pointer()->Get() == NULL);
77 lazy_tls_ptr.Pointer()->Set(this); 80 lazy_tls_ptr.Pointer()->Set(this);
78 } 81 }
79 82
80 Env::~Env() { 83 void Env::Init() {
81 FOR_EACH_OBSERVER(EnvObserver, observers_, OnWillDestroyEnv());
82 DCHECK_EQ(this, lazy_tls_ptr.Pointer()->Get());
83 lazy_tls_ptr.Pointer()->Set(NULL);
84 }
85
86 void Env::Init(bool create_event_source) {
87 #if defined(USE_OZONE) 84 #if defined(USE_OZONE)
88 // The ozone platform can provide its own event source. So initialize the 85 // The ozone platform can provide its own event source. So initialize the
89 // platform before creating the default event source. 86 // platform before creating the default event source.
90 ui::OzonePlatform::InitializeForUI(); 87 ui::OzonePlatform::InitializeForUI();
91 #endif 88 #endif
92 if (create_event_source && !ui::PlatformEventSource::GetInstance()) 89 if (!ui::PlatformEventSource::GetInstance())
93 event_source_ = ui::PlatformEventSource::CreateDefault(); 90 event_source_ = ui::PlatformEventSource::CreateDefault();
94 } 91 }
95 92
96 void Env::NotifyWindowInitialized(Window* window) { 93 void Env::NotifyWindowInitialized(Window* window) {
97 FOR_EACH_OBSERVER(EnvObserver, observers_, OnWindowInitialized(window)); 94 FOR_EACH_OBSERVER(EnvObserver, observers_, OnWindowInitialized(window));
98 } 95 }
99 96
100 void Env::NotifyHostInitialized(WindowTreeHost* host) { 97 void Env::NotifyHostInitialized(WindowTreeHost* host) {
101 FOR_EACH_OBSERVER(EnvObserver, observers_, OnHostInitialized(host)); 98 FOR_EACH_OBSERVER(EnvObserver, observers_, OnHostInitialized(host));
102 } 99 }
(...skipping 16 matching lines...) Expand all
119 scoped_ptr<ui::EventTargetIterator> Env::GetChildIterator() const { 116 scoped_ptr<ui::EventTargetIterator> Env::GetChildIterator() const {
120 return nullptr; 117 return nullptr;
121 } 118 }
122 119
123 ui::EventTargeter* Env::GetEventTargeter() { 120 ui::EventTargeter* Env::GetEventTargeter() {
124 NOTREACHED(); 121 NOTREACHED();
125 return NULL; 122 return NULL;
126 } 123 }
127 124
128 } // namespace aura 125 } // namespace aura
OLDNEW
« no previous file with comments | « ui/aura/env.h ('k') | ui/aura/test/aura_test_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698