OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/automation/testing_automation_provider.h" | 5 #include "chrome/browser/automation/testing_automation_provider.h" |
6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/weak_ptr.h" |
7 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
8 #include "chrome/browser/automation/automation_browser_tracker.h" | 10 #include "chrome/browser/automation/automation_browser_tracker.h" |
9 #include "chrome/browser/automation/automation_window_tracker.h" | 11 #include "chrome/browser/automation/automation_window_tracker.h" |
10 #include "chrome/browser/ui/browser_window.h" | 12 #include "chrome/browser/ui/browser_window.h" |
11 #include "chrome/browser/ui/views/frame/browser_view.h" | 13 #include "chrome/browser/ui/views/frame/browser_view.h" |
12 #include "chrome/browser/ui/views/toolbar_view.h" | 14 #include "chrome/browser/ui/views/toolbar_view.h" |
13 #include "chrome/common/automation_messages.h" | 15 #include "chrome/common/automation_messages.h" |
14 #include "ui/gfx/point.h" | 16 #include "ui/gfx/point.h" |
15 #include "views/controls/menu/menu_listener.h" | 17 #include "views/controls/menu/menu_listener.h" |
16 #include "views/view.h" | 18 #include "views/view.h" |
17 #include "views/widget/widget.h" | 19 #include "views/widget/widget.h" |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
21 // Helper class that waits until the focus has changed to a view other | 23 // Helper class that waits until the focus has changed to a view other |
22 // than the one with the provided view id. | 24 // than the one with the provided view id. |
23 class ViewFocusChangeWaiter : public views::FocusChangeListener { | 25 class ViewFocusChangeWaiter : public views::FocusChangeListener { |
24 public: | 26 public: |
25 ViewFocusChangeWaiter(views::FocusManager* focus_manager, | 27 ViewFocusChangeWaiter(views::FocusManager* focus_manager, |
26 int previous_view_id, | 28 int previous_view_id, |
27 AutomationProvider* automation, | 29 AutomationProvider* automation, |
28 IPC::Message* reply_message) | 30 IPC::Message* reply_message) |
29 : focus_manager_(focus_manager), | 31 : focus_manager_(focus_manager), |
30 previous_view_id_(previous_view_id), | 32 previous_view_id_(previous_view_id), |
31 automation_(automation), | 33 automation_(automation), |
32 reply_message_(reply_message), | 34 reply_message_(reply_message), |
33 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 35 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
34 focus_manager_->AddFocusChangeListener(this); | 36 focus_manager_->AddFocusChangeListener(this); |
35 // Call the focus change notification once in case the focus has | 37 // Call the focus change notification once in case the focus has |
36 // already changed. | 38 // already changed. |
37 FocusWillChange(NULL, focus_manager_->GetFocusedView()); | 39 FocusWillChange(NULL, focus_manager_->GetFocusedView()); |
38 } | 40 } |
39 | 41 |
40 virtual ~ViewFocusChangeWaiter() { | 42 virtual ~ViewFocusChangeWaiter() { |
41 focus_manager_->RemoveFocusChangeListener(this); | 43 focus_manager_->RemoveFocusChangeListener(this); |
42 } | 44 } |
43 | 45 |
44 // Inherited from FocusChangeListener | 46 // Inherited from FocusChangeListener |
45 virtual void FocusWillChange(views::View* focused_before, | 47 virtual void FocusWillChange(views::View* focused_before, |
46 views::View* focused_now) { | 48 views::View* focused_now) { |
47 // This listener is called before focus actually changes. Post a task | 49 // This listener is called before focus actually changes. Post a task |
48 // that will get run after focus changes. | 50 // that will get run after focus changes. |
49 MessageLoop::current()->PostTask( | 51 MessageLoop::current()->PostTask( |
50 FROM_HERE, | 52 FROM_HERE, |
51 method_factory_.NewRunnableMethod( | 53 base::Bind(&ViewFocusChangeWaiter::FocusChanged, |
52 &ViewFocusChangeWaiter::FocusChanged, | 54 weak_factory_.GetWeakPtr(), focused_before, focused_now)); |
53 focused_before, | |
54 focused_now)); | |
55 } | 55 } |
56 | 56 |
57 private: | 57 private: |
58 void FocusChanged(views::View* focused_before, | 58 void FocusChanged(views::View* focused_before, |
59 views::View* focused_now) { | 59 views::View* focused_now) { |
60 if (focused_now && focused_now->id() != previous_view_id_) { | 60 if (focused_now && focused_now->id() != previous_view_id_) { |
61 AutomationMsg_WaitForFocusedViewIDToChange::WriteReplyParams( | 61 AutomationMsg_WaitForFocusedViewIDToChange::WriteReplyParams( |
62 reply_message_, true, focused_now->id()); | 62 reply_message_, true, focused_now->id()); |
63 | 63 |
64 automation_->Send(reply_message_); | 64 automation_->Send(reply_message_); |
65 delete this; | 65 delete this; |
66 } | 66 } |
67 } | 67 } |
68 | 68 |
69 views::FocusManager* focus_manager_; | 69 views::FocusManager* focus_manager_; |
70 int previous_view_id_; | 70 int previous_view_id_; |
71 AutomationProvider* automation_; | 71 AutomationProvider* automation_; |
72 IPC::Message* reply_message_; | 72 IPC::Message* reply_message_; |
73 ScopedRunnableMethodFactory<ViewFocusChangeWaiter> method_factory_; | 73 base::WeakPtrFactory<ViewFocusChangeWaiter> weak_factory_; |
74 | 74 |
75 DISALLOW_COPY_AND_ASSIGN(ViewFocusChangeWaiter); | 75 DISALLOW_COPY_AND_ASSIGN(ViewFocusChangeWaiter); |
76 }; | 76 }; |
77 | 77 |
78 } // namespace | 78 } // namespace |
79 | 79 |
80 class TestingAutomationProvider::PopupMenuWaiter : public views::MenuListener { | 80 class TestingAutomationProvider::PopupMenuWaiter : public views::MenuListener { |
81 public: | 81 public: |
82 PopupMenuWaiter(ToolbarView* toolbar_view, | 82 PopupMenuWaiter(ToolbarView* toolbar_view, |
83 TestingAutomationProvider* automation) | 83 TestingAutomationProvider* automation) |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 reply_message, true); | 192 reply_message, true); |
193 Send(reply_message); | 193 Send(reply_message); |
194 return; | 194 return; |
195 } | 195 } |
196 | 196 |
197 // Otherwise, register this reply message with the waiter, | 197 // Otherwise, register this reply message with the waiter, |
198 // which will handle responding to this IPC when the popup | 198 // which will handle responding to this IPC when the popup |
199 // menu opens. | 199 // menu opens. |
200 popup_menu_waiter_->set_reply_message(reply_message); | 200 popup_menu_waiter_->set_reply_message(reply_message); |
201 } | 201 } |
OLD | NEW |