Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "chrome/browser/ui/autofill/popup_controller_common.h" | 5 #include "chrome/browser/ui/autofill/popup_controller_common.h" |
| 6 | 6 |
| 7 #include "content/public/browser/render_view_host.h" | 7 #include "content/public/browser/render_frame_host.h" |
| 8 #include "content/public/browser/render_widget_host_view.h" | |
| 8 #include "content/public/browser/web_contents.h" | 9 #include "content/public/browser/web_contents.h" |
| 9 | 10 |
| 10 namespace autofill { | 11 namespace autofill { |
| 11 | 12 |
| 13 namespace { | |
| 14 | |
| 15 // Returns the RenderWidgetHost which should be currently consuming keyboard | |
| 16 // events in |web_contents|. Can return null if there is no such | |
| 17 // RenderWidgetHost. | |
| 18 content::RenderWidgetHost* KeyPressHandlingTarget( | |
| 19 content::WebContents* web_contents) { | |
| 20 content::RenderFrameHost* frame = web_contents->GetFocusedFrame(); | |
| 21 if (!frame) | |
| 22 frame = web_contents->GetMainFrame(); | |
| 23 content::RenderWidgetHostView* view = frame->GetView(); | |
| 24 if (!view) | |
| 25 return nullptr; | |
| 26 return view->GetRenderWidgetHost(); | |
| 27 } | |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 12 PopupControllerCommon::PopupControllerCommon( | 31 PopupControllerCommon::PopupControllerCommon( |
| 13 const gfx::RectF& element_bounds, | 32 const gfx::RectF& element_bounds, |
| 14 base::i18n::TextDirection text_direction, | 33 base::i18n::TextDirection text_direction, |
| 15 const gfx::NativeView container_view, | 34 const gfx::NativeView container_view, |
| 16 content::WebContents* web_contents) | 35 content::WebContents* web_contents) |
| 17 : element_bounds_(element_bounds), | 36 : element_bounds_(element_bounds), |
| 18 text_direction_(text_direction), | 37 text_direction_(text_direction), |
| 19 container_view_(container_view), | 38 container_view_(container_view), |
| 20 web_contents_(web_contents), | 39 web_contents_(web_contents), |
| 21 key_press_event_target_(NULL) { | 40 key_press_event_target_(NULL) { |
| 22 } | 41 } |
| 23 PopupControllerCommon::~PopupControllerCommon() {} | 42 PopupControllerCommon::~PopupControllerCommon() {} |
| 24 | 43 |
| 25 void PopupControllerCommon::SetKeyPressCallback( | 44 void PopupControllerCommon::SetKeyPressCallback( |
| 26 content::RenderWidgetHost::KeyPressEventCallback callback) { | 45 content::RenderWidgetHost::KeyPressEventCallback callback) { |
| 27 DCHECK(key_press_event_callback_.is_null()); | 46 DCHECK(key_press_event_callback_.is_null()); |
| 28 key_press_event_callback_ = callback; | 47 key_press_event_callback_ = callback; |
| 29 } | 48 } |
| 30 | 49 |
| 31 void PopupControllerCommon::RegisterKeyPressCallback() { | 50 void PopupControllerCommon::RegisterKeyPressCallback() { |
| 32 if (web_contents_ && !key_press_event_target_) { | 51 if (web_contents_ && !key_press_event_target_) { |
| 33 key_press_event_target_ = web_contents_->GetRenderViewHost(); | 52 key_press_event_target_ = KeyPressHandlingTarget(web_contents_); |
|
EhsanK
2017/03/27 19:35:09
I think both here and below for RemoveKeyPressCall
| |
| 34 key_press_event_target_->GetWidget()->AddKeyPressEventCallback( | 53 key_press_event_target_->AddKeyPressEventCallback( |
| 35 key_press_event_callback_); | 54 key_press_event_callback_); |
| 36 } | 55 } |
| 37 } | 56 } |
| 38 | 57 |
| 39 void PopupControllerCommon::RemoveKeyPressCallback() { | 58 void PopupControllerCommon::RemoveKeyPressCallback() { |
| 40 if (web_contents_ && (!web_contents_->IsBeingDestroyed()) && | 59 if (web_contents_ && (!web_contents_->IsBeingDestroyed()) && |
| 41 key_press_event_target_ == web_contents_->GetRenderViewHost()) { | 60 key_press_event_target_ == KeyPressHandlingTarget(web_contents_)) { |
| 42 web_contents_->GetRenderViewHost() | 61 key_press_event_target_->RemoveKeyPressEventCallback( |
|
EhsanK
2017/03/27 19:35:09
There seems to be a memory leak, i.e., callbacks w
vabr (Chromium)
2017/03/27 20:28:53
The idea to use |key_press_event_target_| seems in
| |
| 43 ->GetWidget() | 62 key_press_event_callback_); |
| 44 ->RemoveKeyPressEventCallback(key_press_event_callback_); | |
| 45 } | 63 } |
| 46 key_press_event_target_ = NULL; | 64 key_press_event_target_ = NULL; |
| 47 } | 65 } |
| 48 | 66 |
| 49 } // namespace autofill | 67 } // namespace autofill |
| OLD | NEW |