OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/mus/focus_synchronizer.h" | 5 #include "ui/aura/mus/focus_synchronizer.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "services/ui/public/interfaces/window_tree.mojom.h" | 8 #include "services/ui/public/interfaces/window_tree.mojom.h" |
9 #include "ui/aura/client/focus_client.h" | 9 #include "ui/aura/client/focus_client.h" |
10 #include "ui/aura/env.h" | 10 #include "ui/aura/env.h" |
11 #include "ui/aura/mus/focus_synchronizer_delegate.h" | 11 #include "ui/aura/mus/focus_synchronizer_delegate.h" |
12 #include "ui/aura/mus/window_mus.h" | 12 #include "ui/aura/mus/window_mus.h" |
13 #include "ui/aura/window.h" | 13 #include "ui/aura/window.h" |
14 | 14 |
15 namespace aura { | 15 namespace aura { |
16 | 16 |
17 FocusSynchronizer::FocusSynchronizer(FocusSynchronizerDelegate* delegate, | 17 FocusSynchronizer::FocusSynchronizer(FocusSynchronizerDelegate* delegate, |
18 ui::mojom::WindowTree* window_tree) | 18 ui::mojom::WindowTree* window_tree) |
19 : delegate_(delegate), window_tree_(window_tree) { | 19 : delegate_(delegate), window_tree_(window_tree), env_(Env::GetInstance()) { |
20 Env::GetInstance()->AddObserver(this); | 20 DCHECK(env_); |
| 21 env_->AddObserver(this); |
21 } | 22 } |
22 | 23 |
23 FocusSynchronizer::~FocusSynchronizer() { | 24 FocusSynchronizer::~FocusSynchronizer() { |
24 SetActiveFocusClient(nullptr); | 25 if (env_) { |
25 Env::GetInstance()->RemoveObserver(this); | 26 SetActiveFocusClient(nullptr); |
| 27 env_->RemoveObserver(this); |
| 28 } |
26 } | 29 } |
27 | 30 |
28 void FocusSynchronizer::SetFocusFromServer(WindowMus* window) { | 31 void FocusSynchronizer::SetFocusFromServer(WindowMus* window) { |
29 if (focused_window_ == window) | 32 if (focused_window_ == window) |
30 return; | 33 return; |
31 | 34 |
32 DCHECK(!setting_focus_); | 35 DCHECK(!setting_focus_); |
33 base::AutoReset<bool> focus_reset(&setting_focus_, true); | 36 base::AutoReset<bool> focus_reset(&setting_focus_, true); |
34 base::AutoReset<WindowMus*> window_setting_focus_to_reset( | 37 base::AutoReset<WindowMus*> window_setting_focus_to_reset( |
35 &window_setting_focus_to_, window); | 38 &window_setting_focus_to_, window); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 WindowMus* gained_focus_mus = WindowMus::Get(gained_focus); | 77 WindowMus* gained_focus_mus = WindowMus::Get(gained_focus); |
75 if (setting_focus_ && gained_focus_mus == window_setting_focus_to_) { | 78 if (setting_focus_ && gained_focus_mus == window_setting_focus_to_) { |
76 focused_window_ = gained_focus_mus; | 79 focused_window_ = gained_focus_mus; |
77 return; | 80 return; |
78 } | 81 } |
79 SetFocusedWindow(gained_focus_mus); | 82 SetFocusedWindow(gained_focus_mus); |
80 } | 83 } |
81 | 84 |
82 void FocusSynchronizer::OnWindowInitialized(Window* window) {} | 85 void FocusSynchronizer::OnWindowInitialized(Window* window) {} |
83 | 86 |
| 87 void FocusSynchronizer::OnWillDestroyEnv() { |
| 88 // TODO(sky): this override should not be necessary. http://crbug.com/674803. |
| 89 SetActiveFocusClient(nullptr); |
| 90 env_->RemoveObserver(this); |
| 91 env_ = nullptr; |
| 92 } |
| 93 |
84 void FocusSynchronizer::OnActiveFocusClientChanged( | 94 void FocusSynchronizer::OnActiveFocusClientChanged( |
85 client::FocusClient* focus_client, | 95 client::FocusClient* focus_client, |
86 Window* window) { | 96 Window* window) { |
87 SetActiveFocusClient(focus_client); | 97 SetActiveFocusClient(focus_client); |
88 if (setting_focus_) | 98 if (setting_focus_) |
89 return; | 99 return; |
90 | 100 |
91 if (focus_client) { | 101 if (focus_client) { |
92 Window* focused_window = focus_client->GetFocusedWindow(); | 102 Window* focused_window = focus_client->GetFocusedWindow(); |
93 SetFocusedWindow(focused_window ? WindowMus::Get(focused_window) | 103 SetFocusedWindow(focused_window ? WindowMus::Get(focused_window) |
94 : WindowMus::Get(window)); | 104 : WindowMus::Get(window)); |
95 } else { | 105 } else { |
96 SetFocusedWindow(nullptr); | 106 SetFocusedWindow(nullptr); |
97 } | 107 } |
98 } | 108 } |
99 | 109 |
100 } // namespace aura | 110 } // namespace aura |
OLD | NEW |