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

Side by Side Diff: ui/views/focus/focus_manager.h

Issue 2750633004: Adds code to isolate use-after-free in Views (Closed)
Patch Set: rename to observed_view Created 3 years, 9 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 | « no previous file | ui/views/focus/focus_manager.cc » ('j') | ui/views/view_observer.h » ('J')
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 #ifndef UI_VIEWS_FOCUS_FOCUS_MANAGER_H_ 5 #ifndef UI_VIEWS_FOCUS_FOCUS_MANAGER_H_
6 #define UI_VIEWS_FOCUS_FOCUS_MANAGER_H_ 6 #define UI_VIEWS_FOCUS_FOCUS_MANAGER_H_
7 7
8 #include <memory> 8 #include <memory>
9 9
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/observer_list.h" 11 #include "base/observer_list.h"
12 #include "ui/base/accelerators/accelerator_manager.h" 12 #include "ui/base/accelerators/accelerator_manager.h"
13 #include "ui/views/view_observer.h"
13 #include "ui/views/views_export.h" 14 #include "ui/views/views_export.h"
14 15
15 // FocusManager handles focus traversal, stores and restores focused views, and 16 // FocusManager handles focus traversal, stores and restores focused views, and
16 // handles keyboard accelerators. This class is an implementation detail of 17 // handles keyboard accelerators. This class is an implementation detail of
17 // views::. Most callers should use methods of views:: classes rather than using 18 // views::. Most callers should use methods of views:: classes rather than using
18 // FocusManager directly. 19 // FocusManager directly.
19 // 20 //
20 // There are 2 types of focus: 21 // There are 2 types of focus:
21 // 22 //
22 // - The native focus, which is the focus that a gfx::NativeView has. 23 // - The native focus, which is the focus that a gfx::NativeView has.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 // the parent view that directly contains the native window. This is needed 64 // the parent view that directly contains the native window. This is needed
64 // when traversing up from the nested RootView to know which view to start 65 // when traversing up from the nested RootView to know which view to start
65 // with when going to the next/previous view. In the example: 66 // with when going to the next/previous view. In the example:
66 // 67 //
67 // hwnd_view_container_->GetWidget()->SetFocusTraversableParent( 68 // hwnd_view_container_->GetWidget()->SetFocusTraversableParent(
68 // native_control); 69 // native_control);
69 // 70 //
70 // Note that FocusTraversable views do not have to be RootViews: 71 // Note that FocusTraversable views do not have to be RootViews:
71 // AccessibleToolbarView is FocusTraversable. 72 // AccessibleToolbarView is FocusTraversable.
72 73
74 namespace base {
75 namespace debug {
76 class StackTrace;
77 }
78 }
79
73 namespace ui { 80 namespace ui {
74 class Accelerator; 81 class Accelerator;
75 class AcceleratorTarget; 82 class AcceleratorTarget;
76 class KeyEvent; 83 class KeyEvent;
77 } 84 }
78 85
79 namespace views { 86 namespace views {
80 87
81 class FocusManagerDelegate; 88 class FocusManagerDelegate;
82 class FocusSearch; 89 class FocusSearch;
(...skipping 28 matching lines...) Expand all
111 // No change to focus state has occurred yet when this function is called. 118 // No change to focus state has occurred yet when this function is called.
112 virtual void OnWillChangeFocus(View* focused_before, View* focused_now) = 0; 119 virtual void OnWillChangeFocus(View* focused_before, View* focused_now) = 0;
113 120
114 // Called after focus state has changed. 121 // Called after focus state has changed.
115 virtual void OnDidChangeFocus(View* focused_before, View* focused_now) = 0; 122 virtual void OnDidChangeFocus(View* focused_before, View* focused_now) = 0;
116 123
117 protected: 124 protected:
118 virtual ~FocusChangeListener() {} 125 virtual ~FocusChangeListener() {}
119 }; 126 };
120 127
121 class VIEWS_EXPORT FocusManager { 128 // FocusManager adds itself as a ViewObserver to the currently focused view.
129 class VIEWS_EXPORT FocusManager : public ViewObserver {
122 public: 130 public:
123 // The reason why the focus changed. 131 // The reason why the focus changed.
124 enum FocusChangeReason { 132 enum FocusChangeReason {
125 // The focus changed because the user traversed focusable views using 133 // The focus changed because the user traversed focusable views using
126 // keys like Tab or Shift+Tab. 134 // keys like Tab or Shift+Tab.
127 kReasonFocusTraversal, 135 kReasonFocusTraversal,
128 136
129 // The focus changed due to restoring the focus. 137 // The focus changed due to restoring the focus.
130 kReasonFocusRestore, 138 kReasonFocusRestore,
131 139
132 // The focus changed due to a click or a shortcut to jump directly to 140 // The focus changed due to a click or a shortcut to jump directly to
133 // a particular view. 141 // a particular view.
134 kReasonDirectFocusChange 142 kReasonDirectFocusChange
135 }; 143 };
136 144
137 // TODO: use Direction in place of bool reverse throughout. 145 // TODO: use Direction in place of bool reverse throughout.
138 enum Direction { 146 enum Direction {
139 kForward, 147 kForward,
140 kBackward 148 kBackward
141 }; 149 };
142 150
143 enum FocusCycleWrappingBehavior { 151 enum FocusCycleWrappingBehavior {
144 kWrap, 152 kWrap,
145 kNoWrap 153 kNoWrap
146 }; 154 };
147 155
148 FocusManager(Widget* widget, std::unique_ptr<FocusManagerDelegate> delegate); 156 FocusManager(Widget* widget, std::unique_ptr<FocusManagerDelegate> delegate);
149 virtual ~FocusManager(); 157 ~FocusManager() override;
150 158
151 // Processes the passed key event for accelerators and keyboard traversal. 159 // Processes the passed key event for accelerators and keyboard traversal.
152 // Returns false if the event has been consumed and should not be processed 160 // Returns false if the event has been consumed and should not be processed
153 // further. 161 // further.
154 bool OnKeyEvent(const ui::KeyEvent& event); 162 bool OnKeyEvent(const ui::KeyEvent& event);
155 163
156 // Returns true is the specified is part of the hierarchy of the window 164 // Returns true is the specified is part of the hierarchy of the window
157 // associated with this FocusManager. 165 // associated with this FocusManager.
158 bool ContainsView(View* view); 166 bool ContainsView(View* view);
159 167
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 bool reverse); 332 bool reverse);
325 333
326 // Process arrow key traversal. Returns true if the event has been consumed 334 // Process arrow key traversal. Returns true if the event has been consumed
327 // and should not be processed further. 335 // and should not be processed further.
328 bool ProcessArrowKeyTraversal(const ui::KeyEvent& event); 336 bool ProcessArrowKeyTraversal(const ui::KeyEvent& event);
329 337
330 // Whether |view| is currently focusable as per the platform's interpretation 338 // Whether |view| is currently focusable as per the platform's interpretation
331 // of |keyboard_accesible_|. 339 // of |keyboard_accesible_|.
332 bool IsFocusable(View* view) const; 340 bool IsFocusable(View* view) const;
333 341
342 // ViewObserver:
343 void OnViewIsDeleting(View* view) override;
344
334 // Whether arrow key traversal is enabled. 345 // Whether arrow key traversal is enabled.
335 static bool arrow_key_traversal_enabled_; 346 static bool arrow_key_traversal_enabled_;
336 347
337 // The top-level Widget this FocusManager is associated with. 348 // The top-level Widget this FocusManager is associated with.
338 Widget* widget_; 349 Widget* widget_;
339 350
340 // The object which handles an accelerator when |accelerator_manager_| doesn't 351 // The object which handles an accelerator when |accelerator_manager_| doesn't
341 // handle it. 352 // handle it.
342 std::unique_ptr<FocusManagerDelegate> delegate_; 353 std::unique_ptr<FocusManagerDelegate> delegate_;
343 354
344 // The view that currently is focused. 355 // The view that currently is focused.
345 View* focused_view_ = nullptr; 356 View* focused_view_ = nullptr;
346 357
358 // TODO(sky): remove, used for debugging 687232.
359 std::unique_ptr<base::debug::StackTrace> stack_when_focused_view_set_;
360
347 // The AcceleratorManager this FocusManager is associated with. 361 // The AcceleratorManager this FocusManager is associated with.
348 ui::AcceleratorManager accelerator_manager_; 362 ui::AcceleratorManager accelerator_manager_;
349 363
350 // Keeps track of whether shortcut handling is currently suspended. 364 // Keeps track of whether shortcut handling is currently suspended.
351 bool shortcut_handling_suspended_ = false; 365 bool shortcut_handling_suspended_ = false;
352 366
353 // The storage id used in the ViewStorage to store/restore the view that last 367 // The storage id used in the ViewStorage to store/restore the view that last
354 // had focus. 368 // had focus.
355 const int stored_focused_view_storage_id_; 369 const int stored_focused_view_storage_id_;
356 370
(...skipping 10 matching lines...) Expand all
367 // FocusTraversable level. Currently only used on Mac, when Full Keyboard 381 // FocusTraversable level. Currently only used on Mac, when Full Keyboard
368 // access is enabled. 382 // access is enabled.
369 bool keyboard_accessible_ = false; 383 bool keyboard_accessible_ = false;
370 384
371 DISALLOW_COPY_AND_ASSIGN(FocusManager); 385 DISALLOW_COPY_AND_ASSIGN(FocusManager);
372 }; 386 };
373 387
374 } // namespace views 388 } // namespace views
375 389
376 #endif // UI_VIEWS_FOCUS_FOCUS_MANAGER_H_ 390 #endif // UI_VIEWS_FOCUS_FOCUS_MANAGER_H_
OLDNEW
« no previous file with comments | « no previous file | ui/views/focus/focus_manager.cc » ('j') | ui/views/view_observer.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698