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

Side by Side Diff: ash/sticky_keys/sticky_keys_overlay.cc

Issue 754763005: Speculative fix for sticky keys overlay crash. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add more asserts Created 6 years 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "ash/sticky_keys/sticky_keys_overlay.h" 5 #include "ash/sticky_keys/sticky_keys_overlay.h"
6 6
7 #include "ash/shell.h" 7 #include "ash/shell.h"
8 #include "ash/shell_window_ids.h" 8 #include "ash/shell_window_ids.h"
9 #include "ash/sticky_keys/sticky_keys_controller.h" 9 #include "ash/sticky_keys/sticky_keys_controller.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 96
97 SetEnabledColor(label_color); 97 SetEnabledColor(label_color);
98 SetDisabledColor(label_color); 98 SetDisabledColor(label_color);
99 SetFontList(font_list().DeriveWithStyle(style)); 99 SetFontList(font_list().DeriveWithStyle(style));
100 } 100 }
101 101
102 /////////////////////////////////////////////////////////////////////////////// 102 ///////////////////////////////////////////////////////////////////////////////
103 // StickyKeysOverlayView 103 // StickyKeysOverlayView
104 class StickyKeysOverlayView : public views::WidgetDelegateView { 104 class StickyKeysOverlayView : public views::WidgetDelegateView {
105 public: 105 public:
106 // This object is not owned by the views hiearchy or by the widget. The
107 // StickyKeysOverlay is the only class that should create and destroy this
108 // view.
106 StickyKeysOverlayView(); 109 StickyKeysOverlayView();
107 110
108 ~StickyKeysOverlayView() override; 111 ~StickyKeysOverlayView() override;
109 112
110 // views::WidgetDelegateView overrides: 113 // views::WidgetDelegateView overrides:
111 void OnPaint(gfx::Canvas* canvas) override; 114 void OnPaint(gfx::Canvas* canvas) override;
115 void DeleteDelegate() override;
112 116
113 void SetKeyState(ui::EventFlags modifier, StickyKeyState state); 117 void SetKeyState(ui::EventFlags modifier, StickyKeyState state);
114 118
115 StickyKeyState GetKeyState(ui::EventFlags modifier); 119 StickyKeyState GetKeyState(ui::EventFlags modifier);
116 120
117 void SetModifierVisible(ui::EventFlags modifier, bool visible); 121 void SetModifierVisible(ui::EventFlags modifier, bool visible);
118 bool GetModifierVisible(ui::EventFlags modifier); 122 bool GetModifierVisible(ui::EventFlags modifier);
119 123
120 private: 124 private:
121 void AddKeyLabel(ui::EventFlags modifier, const std::string& key_label); 125 void AddKeyLabel(ui::EventFlags modifier, const std::string& key_label);
122 126
123 typedef std::map<ui::EventFlags, StickyKeyOverlayLabel*> ModifierLabelMap; 127 typedef std::map<ui::EventFlags, StickyKeyOverlayLabel*> ModifierLabelMap;
124 ModifierLabelMap modifier_label_map_; 128 ModifierLabelMap modifier_label_map_;
125 129
126 DISALLOW_COPY_AND_ASSIGN(StickyKeysOverlayView); 130 DISALLOW_COPY_AND_ASSIGN(StickyKeysOverlayView);
127 }; 131 };
128 132
129 StickyKeysOverlayView::StickyKeysOverlayView() { 133 StickyKeysOverlayView::StickyKeysOverlayView() {
134 // The parent StickyKeysOverlay object owns this view.
135 set_owned_by_client();
James Cook 2014/12/04 22:00:45 Doesn't the WidgetDelegateView constructor do this
Tim Song 2014/12/04 23:53:37 I just realized that there is no reason to inherit
136
130 const gfx::Font& font = 137 const gfx::Font& font =
131 ui::ResourceBundle::GetSharedInstance().GetFont(kKeyLabelFontStyle); 138 ui::ResourceBundle::GetSharedInstance().GetFont(kKeyLabelFontStyle);
132 int font_size = font.GetFontSize(); 139 int font_size = font.GetFontSize();
133 int font_padding = font.GetHeight() - font.GetBaseline(); 140 int font_padding = font.GetHeight() - font.GetBaseline();
134 141
135 // Text should have a margin of 0.5 times the font size on each side, so 142 // Text should have a margin of 0.5 times the font size on each side, so
136 // the spacing between two labels will be the same as the font size. 143 // the spacing between two labels will be the same as the font size.
137 int horizontal_spacing = font_size / 2; 144 int horizontal_spacing = font_size / 2;
138 int vertical_spacing = font_size / 2 - font_padding; 145 int vertical_spacing = font_size / 2 - font_padding;
139 int child_spacing = font_size - 2 * font_padding; 146 int child_spacing = font_size - 2 * font_padding;
(...skipping 19 matching lines...) Expand all
159 StickyKeysOverlayView::~StickyKeysOverlayView() {} 166 StickyKeysOverlayView::~StickyKeysOverlayView() {}
160 167
161 void StickyKeysOverlayView::OnPaint(gfx::Canvas* canvas) { 168 void StickyKeysOverlayView::OnPaint(gfx::Canvas* canvas) {
162 SkPaint paint; 169 SkPaint paint;
163 paint.setStyle(SkPaint::kFill_Style); 170 paint.setStyle(SkPaint::kFill_Style);
164 paint.setColor(SkColorSetARGB(0xB3, 0x55, 0x55, 0x55)); 171 paint.setColor(SkColorSetARGB(0xB3, 0x55, 0x55, 0x55));
165 canvas->DrawRoundRect(GetLocalBounds(), 2, paint); 172 canvas->DrawRoundRect(GetLocalBounds(), 2, paint);
166 views::WidgetDelegateView::OnPaint(canvas); 173 views::WidgetDelegateView::OnPaint(canvas);
167 } 174 }
168 175
176 void StickyKeysOverlayView::DeleteDelegate() {
177 // This view object is owned by StickyKeysOverlay, so no-op here.
178 }
179
169 void StickyKeysOverlayView::SetKeyState(ui::EventFlags modifier, 180 void StickyKeysOverlayView::SetKeyState(ui::EventFlags modifier,
170 StickyKeyState state) { 181 StickyKeyState state) {
171 ModifierLabelMap::iterator it = modifier_label_map_.find(modifier); 182 ModifierLabelMap::iterator it = modifier_label_map_.find(modifier);
172 DCHECK(it != modifier_label_map_.end()); 183 DCHECK(it != modifier_label_map_.end());
173 if (it != modifier_label_map_.end()) { 184 if (it != modifier_label_map_.end()) {
174 StickyKeyOverlayLabel* label = it->second; 185 StickyKeyOverlayLabel* label = it->second;
175 label->SetKeyState(state); 186 label->SetKeyState(state);
176 } 187 }
177 } 188 }
178 189
(...skipping 25 matching lines...) Expand all
204 215
205 /////////////////////////////////////////////////////////////////////////////// 216 ///////////////////////////////////////////////////////////////////////////////
206 // StickyKeysOverlay 217 // StickyKeysOverlay
207 StickyKeysOverlay::StickyKeysOverlay() 218 StickyKeysOverlay::StickyKeysOverlay()
208 : is_visible_(false), 219 : is_visible_(false),
209 overlay_view_(new StickyKeysOverlayView), 220 overlay_view_(new StickyKeysOverlayView),
210 widget_size_(overlay_view_->GetPreferredSize()) { 221 widget_size_(overlay_view_->GetPreferredSize()) {
211 views::Widget::InitParams params; 222 views::Widget::InitParams params;
212 params.type = views::Widget::InitParams::TYPE_POPUP; 223 params.type = views::Widget::InitParams::TYPE_POPUP;
213 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; 224 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
214 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 225 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
Jun Mukai 2014/12/04 21:58:54 WIDGET_OWNS_NATIVE_WIDGET is not used usually. I
Tim Song 2014/12/04 23:53:37 I'm using WIDGET_OWNS_NATIVE_WIDGET primarily so i
Jun Mukai 2014/12/05 00:10:36 I am still not so sure what's the problem. That so
Tim Song 2014/12/05 01:03:14 I guess my concern is that it's more code to do th
Jun Mukai 2014/12/05 02:23:07 The biggest benefit would be that you don't have t
Tim Song 2014/12/05 19:29:51 I feel after being burned already, keeping ownersh
215 params.accept_events = false; 226 params.accept_events = false;
216 params.keep_on_top = true; 227 params.keep_on_top = true;
217 params.remove_standard_frame = true; 228 params.remove_standard_frame = true;
218 params.delegate = overlay_view_; 229 params.delegate = overlay_view_.get();
219 params.bounds = CalculateOverlayBounds(); 230 params.bounds = CalculateOverlayBounds();
220 params.parent = Shell::GetContainer(Shell::GetTargetRootWindow(), 231 params.parent = Shell::GetContainer(Shell::GetTargetRootWindow(),
221 kShellWindowId_OverlayContainer); 232 kShellWindowId_OverlayContainer);
222 overlay_widget_.reset(new views::Widget); 233 overlay_widget_.reset(new views::Widget);
223 overlay_widget_->Init(params); 234 overlay_widget_->Init(params);
224 overlay_widget_->SetVisibilityChangedAnimationsEnabled(false); 235 overlay_widget_->SetVisibilityChangedAnimationsEnabled(false);
225 overlay_widget_->SetContentsView(overlay_view_); 236 overlay_widget_->SetContentsView(overlay_view_.get());
Jun Mukai 2014/12/04 21:58:54 Because overlay_view_ is a WidgetDelegateView, it
Tim Song 2014/12/04 23:53:37 Done. I stopped inheriting from WidgetDelegateView
226 overlay_widget_->GetNativeView()->SetName("StickyKeysOverlay"); 237 overlay_widget_->GetNativeView()->SetName("StickyKeysOverlay");
227 } 238 }
228 239
229 StickyKeysOverlay::~StickyKeysOverlay() {} 240 StickyKeysOverlay::~StickyKeysOverlay() {}
230 241
231 void StickyKeysOverlay::Show(bool visible) { 242 void StickyKeysOverlay::Show(bool visible) {
232 if (is_visible_ == visible) 243 if (is_visible_ == visible)
233 return; 244 return;
234 245
235 is_visible_ = visible; 246 is_visible_ = visible;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 void StickyKeysOverlay::SetModifierKeyState(ui::EventFlags modifier, 283 void StickyKeysOverlay::SetModifierKeyState(ui::EventFlags modifier,
273 StickyKeyState state) { 284 StickyKeyState state) {
274 overlay_view_->SetKeyState(modifier, state); 285 overlay_view_->SetKeyState(modifier, state);
275 } 286 }
276 287
277 StickyKeyState StickyKeysOverlay::GetModifierKeyState( 288 StickyKeyState StickyKeysOverlay::GetModifierKeyState(
278 ui::EventFlags modifier) { 289 ui::EventFlags modifier) {
279 return overlay_view_->GetKeyState(modifier); 290 return overlay_view_->GetKeyState(modifier);
280 } 291 }
281 292
293 views::Widget* StickyKeysOverlay::GetWidgetForTesting() {
294 return overlay_widget_.get();
295 }
296
282 gfx::Rect StickyKeysOverlay::CalculateOverlayBounds() { 297 gfx::Rect StickyKeysOverlay::CalculateOverlayBounds() {
283 int x = is_visible_ ? kHorizontalOverlayOffset : -widget_size_.width(); 298 int x = is_visible_ ? kHorizontalOverlayOffset : -widget_size_.width();
284 return gfx::Rect(gfx::Point(x, kVerticalOverlayOffset), widget_size_); 299 return gfx::Rect(gfx::Point(x, kVerticalOverlayOffset), widget_size_);
285 } 300 }
286 301
287 void StickyKeysOverlay::OnLayerAnimationEnded( 302 void StickyKeysOverlay::OnLayerAnimationEnded(
288 ui::LayerAnimationSequence* sequence) { 303 ui::LayerAnimationSequence* sequence) {
289 ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator(); 304 ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator();
290 if (animator) 305 if (animator)
291 animator->RemoveObserver(this); 306 animator->RemoveObserver(this);
292 if (!is_visible_) 307 if (!is_visible_)
293 overlay_widget_->Hide(); 308 overlay_widget_->Hide();
294 } 309 }
295 310
296 void StickyKeysOverlay::OnLayerAnimationAborted( 311 void StickyKeysOverlay::OnLayerAnimationAborted(
297 ui::LayerAnimationSequence* sequence) { 312 ui::LayerAnimationSequence* sequence) {
298 ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator(); 313 ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator();
299 if (animator) 314 if (animator)
300 animator->RemoveObserver(this); 315 animator->RemoveObserver(this);
301 } 316 }
302 317
303 void StickyKeysOverlay::OnLayerAnimationScheduled( 318 void StickyKeysOverlay::OnLayerAnimationScheduled(
304 ui::LayerAnimationSequence* sequence) { 319 ui::LayerAnimationSequence* sequence) {
305 } 320 }
306 321
307 } // namespace ash 322 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698