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 1871253002: aura: Require explicit ownership of the Env instance. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: tot.merge Created 4 years, 8 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/command_line.h" 7 #include "base/command_line.h"
8 #include "base/lazy_instance.h" 8 #include "base/lazy_instance.h"
9 #include "base/threading/thread_local.h" 9 #include "base/threading/thread_local.h"
10 #include "ui/aura/env_observer.h" 10 #include "ui/aura/env_observer.h"
11 #include "ui/aura/input_state_lookup.h" 11 #include "ui/aura/input_state_lookup.h"
12 #include "ui/events/event_target_iterator.h" 12 #include "ui/events/event_target_iterator.h"
13 #include "ui/events/platform/platform_event_source.h" 13 #include "ui/events/platform/platform_event_source.h"
14 14
15 #if defined(USE_OZONE) 15 #if defined(USE_OZONE)
16 #include "ui/ozone/public/ozone_platform.h" 16 #include "ui/ozone/public/ozone_platform.h"
17 #endif 17 #endif
18 18
19 namespace aura { 19 namespace aura {
20 20
21 namespace { 21 namespace {
22 22
23 // Env is thread local so that aura may be used on multiple threads. 23 // Env is thread local so that aura may be used on multiple threads.
24 base::LazyInstance<base::ThreadLocalPointer<Env> >::Leaky lazy_tls_ptr = 24 base::LazyInstance<base::ThreadLocalPointer<Env> >::Leaky lazy_tls_ptr =
25 LAZY_INSTANCE_INITIALIZER; 25 LAZY_INSTANCE_INITIALIZER;
26 26
27 #if defined(USE_OZONE)
28 // Returns true if running inside of mus. Checks for mojo specific flag. 27 // Returns true if running inside of mus. Checks for mojo specific flag.
29 bool RunningInsideMus() { 28 bool RunningInsideMus() {
30 return base::CommandLine::ForCurrentProcess()->HasSwitch( 29 return base::CommandLine::ForCurrentProcess()->HasSwitch(
31 "primordial-pipe-token"); 30 "primordial-pipe-token");
32 } 31 }
33 #endif
34 32
35 } // namespace 33 } // namespace
36 34
37 //////////////////////////////////////////////////////////////////////////////// 35 ////////////////////////////////////////////////////////////////////////////////
38 // Env, public: 36 // Env, public:
39 37
40 // static 38 Env::~Env() {
41 void Env::CreateInstance(bool create_event_source) { 39 FOR_EACH_OBSERVER(EnvObserver, observers_, OnWillDestroyEnv());
42 if (!lazy_tls_ptr.Pointer()->Get()) 40 DCHECK_EQ(this, lazy_tls_ptr.Pointer()->Get());
43 (new Env())->Init(create_event_source); 41 lazy_tls_ptr.Pointer()->Set(NULL);
44 } 42 }
45 43
46 // static 44 // static
45 std::unique_ptr<Env> Env::CreateInstance() {
46 DCHECK(!lazy_tls_ptr.Pointer()->Get());
47 std::unique_ptr<Env> env(new Env());
48 env->Init();
49 return env;
50 }
51
52 // static
47 Env* Env::GetInstance() { 53 Env* Env::GetInstance() {
48 Env* env = lazy_tls_ptr.Pointer()->Get(); 54 Env* env = lazy_tls_ptr.Pointer()->Get();
49 DCHECK(env) << "Env::CreateInstance must be called before getting the " 55 DCHECK(env) << "Env::CreateInstance must be called before getting the "
50 "instance of Env."; 56 "instance of Env.";
51 return env; 57 return env;
52 } 58 }
53 59
54 // static 60 // static
55 Env* Env::GetInstanceDontCreate() { 61 Env* Env::GetInstanceDontCreate() {
56 return lazy_tls_ptr.Pointer()->Get(); 62 return lazy_tls_ptr.Pointer()->Get();
57 } 63 }
58 64
59 // static
60 void Env::DeleteInstance() {
61 delete lazy_tls_ptr.Pointer()->Get();
62 }
63
64 void Env::AddObserver(EnvObserver* observer) { 65 void Env::AddObserver(EnvObserver* observer) {
65 observers_.AddObserver(observer); 66 observers_.AddObserver(observer);
66 } 67 }
67 68
68 void Env::RemoveObserver(EnvObserver* observer) { 69 void Env::RemoveObserver(EnvObserver* observer) {
69 observers_.RemoveObserver(observer); 70 observers_.RemoveObserver(observer);
70 } 71 }
71 72
72 bool Env::IsMouseButtonDown() const { 73 bool Env::IsMouseButtonDown() const {
73 return input_state_lookup_.get() ? input_state_lookup_->IsMouseButtonDown() : 74 return input_state_lookup_.get() ? input_state_lookup_->IsMouseButtonDown() :
74 mouse_button_flags_ != 0; 75 mouse_button_flags_ != 0;
75 } 76 }
76 77
77 //////////////////////////////////////////////////////////////////////////////// 78 ////////////////////////////////////////////////////////////////////////////////
78 // Env, private: 79 // Env, private:
79 80
80 Env::Env() 81 Env::Env()
81 : mouse_button_flags_(0), 82 : mouse_button_flags_(0),
82 is_touch_down_(false), 83 is_touch_down_(false),
83 input_state_lookup_(InputStateLookup::Create()), 84 input_state_lookup_(InputStateLookup::Create()),
84 context_factory_(NULL) { 85 context_factory_(NULL) {
85 DCHECK(lazy_tls_ptr.Pointer()->Get() == NULL); 86 DCHECK(lazy_tls_ptr.Pointer()->Get() == NULL);
86 lazy_tls_ptr.Pointer()->Set(this); 87 lazy_tls_ptr.Pointer()->Set(this);
87 } 88 }
88 89
89 Env::~Env() { 90 void Env::Init() {
90 FOR_EACH_OBSERVER(EnvObserver, observers_, OnWillDestroyEnv()); 91 if (RunningInsideMus())
91 DCHECK_EQ(this, lazy_tls_ptr.Pointer()->Get());
92 lazy_tls_ptr.Pointer()->Set(NULL);
93 }
94
95 void Env::Init(bool create_event_source) {
96 if (!create_event_source)
97 return; 92 return;
98 #if defined(USE_OZONE) 93 #if defined(USE_OZONE)
99 // The ozone platform can provide its own event source. So initialize the 94 // The ozone platform can provide its own event source. So initialize the
100 // platform before creating the default event source. If running inside mus 95 // platform before creating the default event source. If running inside mus
101 // let the mus process initialize ozone instead. 96 // let the mus process initialize ozone instead.
102 if (!RunningInsideMus()) 97 ui::OzonePlatform::InitializeForUI();
103 ui::OzonePlatform::InitializeForUI();
104 #endif 98 #endif
105 if (!ui::PlatformEventSource::GetInstance()) 99 if (!ui::PlatformEventSource::GetInstance())
106 event_source_ = ui::PlatformEventSource::CreateDefault(); 100 event_source_ = ui::PlatformEventSource::CreateDefault();
107 } 101 }
108 102
109 void Env::NotifyWindowInitialized(Window* window) { 103 void Env::NotifyWindowInitialized(Window* window) {
110 FOR_EACH_OBSERVER(EnvObserver, observers_, OnWindowInitialized(window)); 104 FOR_EACH_OBSERVER(EnvObserver, observers_, OnWindowInitialized(window));
111 } 105 }
112 106
113 void Env::NotifyHostInitialized(WindowTreeHost* host) { 107 void Env::NotifyHostInitialized(WindowTreeHost* host) {
(...skipping 18 matching lines...) Expand all
132 scoped_ptr<ui::EventTargetIterator> Env::GetChildIterator() const { 126 scoped_ptr<ui::EventTargetIterator> Env::GetChildIterator() const {
133 return nullptr; 127 return nullptr;
134 } 128 }
135 129
136 ui::EventTargeter* Env::GetEventTargeter() { 130 ui::EventTargeter* Env::GetEventTargeter() {
137 NOTREACHED(); 131 NOTREACHED();
138 return NULL; 132 return NULL;
139 } 133 }
140 134
141 } // namespace aura 135 } // 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