OLD | NEW |
---|---|
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 #include "content/renderer/text_input_client_observer.h" | 5 #include "content/renderer/text_input_client_observer.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
12 #include "content/common/text_input_client_messages.h" | 12 #include "content/common/text_input_client_messages.h" |
13 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" | 13 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" |
14 #include "content/renderer/render_frame_impl.h" | |
14 #include "content/renderer/render_view_impl.h" | 15 #include "content/renderer/render_view_impl.h" |
16 #include "content/renderer/render_widget.h" | |
17 #include "ipc/ipc_message.h" | |
15 #include "third_party/WebKit/public/platform/WebPoint.h" | 18 #include "third_party/WebKit/public/platform/WebPoint.h" |
16 #include "third_party/WebKit/public/platform/WebRect.h" | 19 #include "third_party/WebKit/public/platform/WebRect.h" |
17 #include "third_party/WebKit/public/platform/WebString.h" | 20 #include "third_party/WebKit/public/platform/WebString.h" |
18 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 21 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
19 #include "third_party/WebKit/public/web/WebView.h" | 22 #include "third_party/WebKit/public/web/WebView.h" |
20 #include "third_party/WebKit/public/web/mac/WebSubstringUtil.h" | 23 #include "third_party/WebKit/public/web/mac/WebSubstringUtil.h" |
21 #include "ui/gfx/geometry/rect.h" | 24 #include "ui/gfx/geometry/rect.h" |
22 | 25 |
23 namespace content { | 26 namespace content { |
24 | 27 |
25 TextInputClientObserver::TextInputClientObserver(RenderViewImpl* render_view) | 28 TextInputClientObserver::TextInputClientObserver(RenderWidget* render_widget) |
26 #if defined(ENABLE_PLUGINS) | 29 : render_widget_(render_widget) {} |
27 : RenderViewObserver(render_view), render_view_impl_(render_view) { | |
28 #else | |
29 : RenderViewObserver(render_view) { | |
30 #endif | |
31 } | |
32 | 30 |
33 TextInputClientObserver::~TextInputClientObserver() { | 31 TextInputClientObserver::~TextInputClientObserver() { |
34 } | 32 } |
35 | 33 |
36 bool TextInputClientObserver::OnMessageReceived(const IPC::Message& message) { | 34 bool TextInputClientObserver::OnMessageReceived(const IPC::Message& message) { |
37 bool handled = true; | 35 bool handled = true; |
38 IPC_BEGIN_MESSAGE_MAP(TextInputClientObserver, message) | 36 IPC_BEGIN_MESSAGE_MAP(TextInputClientObserver, message) |
39 IPC_MESSAGE_HANDLER(TextInputClientMsg_StringAtPoint, | 37 IPC_MESSAGE_HANDLER(TextInputClientMsg_StringAtPoint, |
40 OnStringAtPoint) | 38 OnStringAtPoint) |
41 IPC_MESSAGE_HANDLER(TextInputClientMsg_CharacterIndexForPoint, | 39 IPC_MESSAGE_HANDLER(TextInputClientMsg_CharacterIndexForPoint, |
42 OnCharacterIndexForPoint) | 40 OnCharacterIndexForPoint) |
43 IPC_MESSAGE_HANDLER(TextInputClientMsg_FirstRectForCharacterRange, | 41 IPC_MESSAGE_HANDLER(TextInputClientMsg_FirstRectForCharacterRange, |
44 OnFirstRectForCharacterRange) | 42 OnFirstRectForCharacterRange) |
45 IPC_MESSAGE_HANDLER(TextInputClientMsg_StringForRange, OnStringForRange) | 43 IPC_MESSAGE_HANDLER(TextInputClientMsg_StringForRange, OnStringForRange) |
46 IPC_MESSAGE_UNHANDLED(handled = false) | 44 IPC_MESSAGE_UNHANDLED(handled = false) |
47 IPC_END_MESSAGE_MAP() | 45 IPC_END_MESSAGE_MAP() |
48 return handled; | 46 return handled; |
49 } | 47 } |
50 | 48 |
51 void TextInputClientObserver::OnDestruct() { | 49 bool TextInputClientObserver::Send(IPC::Message* message) { |
52 delete this; | 50 return render_widget_->Send(message); |
53 } | 51 } |
54 | 52 |
55 blink::WebView* TextInputClientObserver::webview() { | 53 blink::WebFrameWidget* TextInputClientObserver::GetWebFrameWidget() const { |
56 return render_view()->GetWebView(); | 54 return static_cast<blink::WebFrameWidget*>(render_widget_->GetWebWidget()); |
57 } | 55 } |
58 | 56 |
57 blink::WebLocalFrame* TextInputClientObserver::GetFocusedFrame() const { | |
58 return RenderFrameImpl::FromWebFrame(GetWebFrameWidget()->localRoot()) | |
59 ->render_view() | |
60 ->webview() | |
61 ->focusedFrame(); | |
62 } | |
63 | |
64 #if defined(ENABLE_PLUGINS) | |
65 PepperPluginInstanceImpl* TextInputClientObserver::GetFocusedPepperPlugin() | |
EhsanK
2016/09/14 22:10:32
lfg@: I think it will be more correct if we return
lfg
2016/09/15 15:51:51
Is it possible that a frame is not focused, but a
EhsanK
2016/09/19 22:34:05
I don't think it is (could not repro locally). I a
| |
66 const { | |
67 return RenderFrameImpl::FromWebFrame(GetWebFrameWidget()->localRoot()) | |
68 ->render_view() | |
69 ->GetFocusedPepperPlugin(); | |
70 } | |
71 #endif | |
72 | |
59 void TextInputClientObserver::OnStringAtPoint(gfx::Point point) { | 73 void TextInputClientObserver::OnStringAtPoint(gfx::Point point) { |
60 #if defined(OS_MACOSX) | 74 #if defined(OS_MACOSX) |
61 blink::WebPoint baselinePoint; | 75 blink::WebPoint baselinePoint; |
62 NSAttributedString* string = blink::WebSubstringUtil::attributedWordAtPoint( | 76 NSAttributedString* string = blink::WebSubstringUtil::attributedWordAtPoint( |
63 webview(), point, baselinePoint); | 77 GetWebFrameWidget(), point, baselinePoint); |
64 | 78 |
65 std::unique_ptr<const mac::AttributedStringCoder::EncodedString> encoded( | 79 std::unique_ptr<const mac::AttributedStringCoder::EncodedString> encoded( |
66 mac::AttributedStringCoder::Encode(string)); | 80 mac::AttributedStringCoder::Encode(string)); |
67 Send(new TextInputClientReplyMsg_GotStringAtPoint( | 81 Send(new TextInputClientReplyMsg_GotStringAtPoint( |
68 routing_id(), *encoded.get(), baselinePoint)); | 82 render_widget_->routing_id(), *encoded.get(), baselinePoint)); |
69 #else | 83 #else |
70 NOTIMPLEMENTED(); | 84 NOTIMPLEMENTED(); |
71 #endif | 85 #endif |
72 } | 86 } |
73 | 87 |
74 void TextInputClientObserver::OnCharacterIndexForPoint(gfx::Point point) { | 88 void TextInputClientObserver::OnCharacterIndexForPoint(gfx::Point point) { |
75 blink::WebPoint web_point(point); | 89 blink::WebPoint web_point(point); |
76 uint32_t index = static_cast<uint32_t>( | 90 uint32_t index = static_cast<uint32_t>( |
77 webview()->focusedFrame()->characterIndexForPoint(web_point)); | 91 GetFocusedFrame()->characterIndexForPoint(web_point)); |
78 Send(new TextInputClientReplyMsg_GotCharacterIndexForPoint(routing_id(), | 92 Send(new TextInputClientReplyMsg_GotCharacterIndexForPoint( |
79 index)); | 93 render_widget_->routing_id(), index)); |
80 } | 94 } |
81 | 95 |
82 void TextInputClientObserver::OnFirstRectForCharacterRange(gfx::Range range) { | 96 void TextInputClientObserver::OnFirstRectForCharacterRange(gfx::Range range) { |
83 gfx::Rect rect; | 97 gfx::Rect rect; |
84 #if defined(ENABLE_PLUGINS) | 98 #if defined(ENABLE_PLUGINS) |
85 if (render_view_impl_->GetFocusedPepperPlugin()) { | 99 PepperPluginInstanceImpl* focused_plugin = GetFocusedPepperPlugin(); |
86 rect = render_view_impl_->GetFocusedPepperPlugin()->GetCaretBounds(); | 100 if (focused_plugin) { |
101 rect = focused_plugin->GetCaretBounds(); | |
87 } else | 102 } else |
88 #endif | 103 #endif |
89 { | 104 { |
90 blink::WebLocalFrame* frame = webview()->focusedFrame(); | 105 blink::WebLocalFrame* frame = GetFocusedFrame(); |
91 // TODO(yabinh): Null check should not be necessary. | 106 // TODO(yabinh): Null check should not be necessary. |
92 // See crbug.com/304341 | 107 // See crbug.com/304341 |
93 if (frame) { | 108 if (frame) { |
94 blink::WebRect web_rect; | 109 blink::WebRect web_rect; |
95 frame->firstRectForCharacterRange(range.start(), range.length(), | 110 frame->firstRectForCharacterRange(range.start(), range.length(), |
96 web_rect); | 111 web_rect); |
97 rect = web_rect; | 112 rect = web_rect; |
98 } | 113 } |
99 } | 114 } |
100 Send(new TextInputClientReplyMsg_GotFirstRectForRange(routing_id(), rect)); | 115 Send(new TextInputClientReplyMsg_GotFirstRectForRange( |
116 render_widget_->routing_id(), rect)); | |
101 } | 117 } |
102 | 118 |
103 void TextInputClientObserver::OnStringForRange(gfx::Range range) { | 119 void TextInputClientObserver::OnStringForRange(gfx::Range range) { |
104 #if defined(OS_MACOSX) | 120 #if defined(OS_MACOSX) |
105 blink::WebPoint baselinePoint; | 121 blink::WebPoint baselinePoint; |
106 NSAttributedString* string = nil; | 122 NSAttributedString* string = nil; |
107 blink::WebLocalFrame* frame = webview()->focusedFrame(); | 123 blink::WebLocalFrame* frame = GetFocusedFrame(); |
108 // TODO(yabinh): Null check should not be necessary. | 124 // TODO(yabinh): Null check should not be necessary. |
109 // See crbug.com/304341 | 125 // See crbug.com/304341 |
110 if (frame) { | 126 if (frame) { |
111 string = blink::WebSubstringUtil::attributedSubstringInRange( | 127 string = blink::WebSubstringUtil::attributedSubstringInRange( |
112 frame, range.start(), range.length(), &baselinePoint); | 128 frame, range.start(), range.length(), &baselinePoint); |
113 } | 129 } |
114 std::unique_ptr<const mac::AttributedStringCoder::EncodedString> encoded( | 130 std::unique_ptr<const mac::AttributedStringCoder::EncodedString> encoded( |
115 mac::AttributedStringCoder::Encode(string)); | 131 mac::AttributedStringCoder::Encode(string)); |
116 Send(new TextInputClientReplyMsg_GotStringForRange(routing_id(), | 132 Send(new TextInputClientReplyMsg_GotStringForRange( |
117 *encoded.get(), baselinePoint)); | 133 render_widget_->routing_id(), *encoded.get(), baselinePoint)); |
118 #else | 134 #else |
119 NOTIMPLEMENTED(); | 135 NOTIMPLEMENTED(); |
120 #endif | 136 #endif |
121 } | 137 } |
122 | 138 |
123 } // namespace content | 139 } // namespace content |
OLD | NEW |